Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NETOBSERV-1076 Fix CR stuck in Updating state #374

Merged
merged 2 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,8 @@ image-push: ## Push MULTIARCH_TARGETS images
.PHONY: manifest-build
manifest-build: ## Build MULTIARCH_TARGETS manifest
@echo 'building manifest $(IMAGE)'
ifeq (${OCI_BIN}, docker)
DOCKER_BUILDKIT=1 $(OCI_BIN) rmi ${IMAGE} -f
DOCKER_BUILDKIT=1 $(OCI_BIN) manifest create ${IMAGE} $(foreach target,$(MULTIARCH_TARGETS), --amend ${IMAGE}-$(target));
Copy link
Contributor

@msherif1234 msherif1234 Jun 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will break with older versions podman since it doesn't have --amend but since we default to docker at least out CI should be fine

Copy link
Member Author

@jotak jotak Jun 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since recent podman does have --amend : https://docs.podman.io/en/latest/markdown/podman-manifest-create.1.html#options I think that's ok.
idk in which version it was introduced, but the outdated version shipped with the github CI image that used once was really out of date in my understanding.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else
trap 'exit' INT; \
DOCKER_BUILDKIT=1 $(OCI_BIN) manifest create ${IMAGE} ||:
$(foreach target,$(MULTIARCH_TARGETS),$(call manifest_add_target,$(target)))
endif

.PHONY: manifest-push
manifest-push: ## Push MULTIARCH_TARGETS manifest
Expand Down
2 changes: 1 addition & 1 deletion controllers/ebpf/agent_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func NewAgentController(common *reconcilers.Common, config *operator.Config) *Ag

func (c *AgentController) Reconcile(
ctx context.Context, target *flowslatest.FlowCollector) error {
rlog := log.FromContext(ctx).WithName("ebpf.AgentController")
rlog := log.FromContext(ctx).WithName("ebpf")
ctx = log.IntoContext(ctx, rlog)
current, err := c.current(ctx)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion controllers/flowcollector_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -809,8 +809,8 @@ func GetCR(key types.NamespacedName) *flowslatest.FlowCollector {
}

func UpdateCR(key types.NamespacedName, updater func(*flowslatest.FlowCollector)) {
cr := GetCR(key)
Eventually(func() error {
cr := GetCR(key)
updater(cr)
return k8sClient.Update(ctx, cr)
}, timeout, interval).Should(Succeed())
Expand Down
11 changes: 8 additions & 3 deletions controllers/reconcilers/namespaced_objects_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ func (m *NamespacedObjectManager) FetchAll(ctx context.Context) error {
// On success, placeholder is filled with resource. Caller should keep a pointer to it.
}
}
log.Info("Fetched: " + strings.Join(fetched, ",") + ". Not found: " + strings.Join(notFound, ","))
if len(fetched) > 0 {
log.Info("FETCHED: " + strings.Join(fetched, ","))
}
if len(notFound) > 0 {
log.Info("(Items not deployed: " + strings.Join(notFound, ",") + ")")
}
return nil
}

Expand All @@ -83,7 +88,7 @@ func (m *NamespacedObjectManager) cleanup(ctx context.Context, namespace string)
ref := obj.placeholder.DeepCopyObject().(client.Object)
ref.SetName(obj.name)
ref.SetNamespace(namespace)
log.Info("Deleting old "+obj.kind, "Namespace", namespace, "Name", obj.name)
log.Info("DELETING "+obj.kind, "Namespace", namespace, "Name", obj.name)
err := m.client.Delete(ctx, ref)
if client.IgnoreNotFound(err) != nil {
log.Error(err, "Failed to delete old "+obj.kind, "Namespace", namespace, "Name", obj.name)
Expand All @@ -103,7 +108,7 @@ func (m *NamespacedObjectManager) TryDelete(ctx context.Context, obj client.Obje
if m.Exists(obj) {
log := log.FromContext(ctx)
kind := reflect.TypeOf(obj).String()
log.Info("Deleting old "+kind, "Namespace", obj.GetNamespace(), "Name", obj.GetName())
log.Info("DELETING "+kind, "Namespace", obj.GetNamespace(), "Name", obj.GetName())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I don't know if there is a need to have all caps here I don't think this align with go coding style ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intent is to make some important information more visible - I found it very useful when debugging, whereas with the normal sentence case it's sometimes hard to find relevant logs at a glance. That's really for pragmatism.

err := m.client.Delete(ctx, obj)
if err != nil {
log.Error(err, "Failed to delete old "+kind, "Namespace", obj.GetNamespace(), "Name", obj.GetName())
Expand Down
15 changes: 12 additions & 3 deletions pkg/helper/client_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (c *Client) CreateOwned(ctx context.Context, obj client.Object) error {
return err
}
kind := reflect.TypeOf(obj).String()
log.Info("Creating a new "+kind, "Namespace", obj.GetNamespace(), "Name", obj.GetName())
log.Info("CREATING a new "+kind, "Namespace", obj.GetNamespace(), "Name", obj.GetName())
err = c.Create(ctx, obj)
if err != nil {
log.Error(err, "Failed to create new "+kind, "Namespace", obj.GetNamespace(), "Name", obj.GetName())
Expand All @@ -48,7 +48,6 @@ func (c *Client) CreateOwned(ctx context.Context, obj client.Object) error {
// UpdateOwned is an helper function that updates an object, sets owner reference and writes info & errors logs
func (c *Client) UpdateOwned(ctx context.Context, old, obj client.Object) error {
log := log.FromContext(ctx)
c.SetChanged(true)
if old != nil {
obj.SetResourceVersion(old.GetResourceVersion())
}
Expand All @@ -58,12 +57,22 @@ func (c *Client) UpdateOwned(ctx context.Context, old, obj client.Object) error
return err
}
kind := reflect.TypeOf(obj).String()
log.Info("Updating "+kind, "Namespace", obj.GetNamespace(), "Name", obj.GetName())
log.Info("UPDATING "+kind, "Namespace", obj.GetNamespace(), "Name", obj.GetName())
err = c.Update(ctx, obj)
if err != nil {
log.Error(err, "Failed to update "+kind, "Namespace", obj.GetNamespace(), "Name", obj.GetName())
return err
}
err = c.Get(ctx, client.ObjectKeyFromObject(obj), obj)
if err != nil {
log.Error(err, "Failed to get updated resource "+kind, "Namespace", obj.GetNamespace(), "Name", obj.GetName())
return err
}
if obj.GetResourceVersion() != old.GetResourceVersion() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about using deep.Equal(old, obj) to know if it was updated or not ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that would work also, but is there a benefit? A simple GetResourceVersion() comparison is for sure more efficient, and I would expect that it does exactly what we need here ... unless if it's possible that the object is actually updated without a version change?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

u never know what k8s do that is why I suggested to deepEqual to be 100% certain

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think given the official definition of resource version, we should be fine: https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions

Resource versions are strings that identify the server's internal version of an object. Resource versions can be used by clients to determine when objects have changed [etc.]

That's exactly what's done here

c.SetChanged(true)
} else {
log.Info(kind+" not updated", "Namespace", obj.GetNamespace(), "Name", obj.GetName())
}
return nil
}

Expand Down
17 changes: 13 additions & 4 deletions pkg/watchers/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ func (w *Watcher) ProcessCACert(ctx context.Context, cl helper.Client, tls *flow

func (w *Watcher) reconcile(ctx context.Context, cl helper.Client, ref objectRef, destNamespace string) (string, error) {
rlog := log.FromContext(ctx, "Name", ref.name, "Source namespace", ref.namespace, "Target namespace", destNamespace)
report := helper.NewChangeReport("Watcher for " + string(ref.kind) + " " + ref.name)
defer report.LogIfNeeded(ctx)

w.watch(ref.kind, ref.name, ref.namespace)
var watchable Watchable
Expand Down Expand Up @@ -141,12 +143,19 @@ func (w *Watcher) reconcile(ctx context.Context, cl helper.Client, ref objectRef
return "", err
}
} else {
// Update existing
rlog.Info(fmt.Sprintf("updating %s %s in namespace %s", ref.kind, ref.name, destNamespace))
watchable.PrepareForUpdate(obj, target)
if err := cl.UpdateOwned(ctx, target, target); err != nil {
// Check for update
targetDigest, err := watchable.GetDigest(target, ref.keys)
if err != nil {
return "", err
}
if report.Check("Digest changed", targetDigest != digest) {
// Update existing
rlog.Info(fmt.Sprintf("updating %s %s in namespace %s", ref.kind, ref.name, destNamespace))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this log spam the log file or its not expected to change too often ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no it's not expected to change often, hardly never, in fact: only during certificate rotations or if the certificate owner (kafka/loki/...) is reinstalled

watchable.PrepareForUpdate(obj, target)
if err := cl.UpdateOwned(ctx, target, target); err != nil {
return "", err
}
}
}
}
return digest, nil
Expand Down
32 changes: 32 additions & 0 deletions pkg/watchers/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,13 @@ func TestUpdateCertificate(t *testing.T) {
assert := assert.New(t)
clientMock := test.ClientMock{}
clientMock.MockConfigMap(&otherLokiCA)
// Copy cert changing content => should be updated
copied := otherLokiCA
copied.Namespace = baseNamespace
copied.Data = map[string]string{
"tls.crt": " -- MODIFIED LOKI OTHER CA --",
}

clientMock.MockConfigMap(&copied)
clientMock.MockCreateUpdate()

Expand All @@ -207,3 +212,30 @@ func TestUpdateCertificate(t *testing.T) {
clientMock.AssertNotCalled(t, "Create")
clientMock.AssertCalled(t, "Update", mock.Anything, mock.Anything, mock.Anything)
}

func TestNoUpdateCertificate(t *testing.T) {
assert := assert.New(t)
clientMock := test.ClientMock{}
clientMock.MockConfigMap(&otherLokiCA)
// Copy cert keeping same content => should not be updated
copied := otherLokiCA
copied.Namespace = baseNamespace
copied.Data = map[string]string{
"tls.crt": otherLokiCA.Data["tls.crt"],
}
clientMock.MockConfigMap(&copied)
clientMock.MockCreateUpdate()

builder := builder.Builder{}
watcher := RegisterWatcher(&builder)
assert.NotNil(watcher)
watcher.Reset(baseNamespace)
cl := helper.UnmanagedClient(&clientMock)

_, _, err := watcher.ProcessMTLSCerts(context.Background(), cl, &otherLokiTLS, baseNamespace)
assert.NoError(err)
clientMock.AssertCalled(t, "Get", mock.Anything, types.NamespacedName{Name: otherLokiCA.Name, Namespace: otherLokiCA.Namespace}, mock.Anything)
clientMock.AssertCalled(t, "Get", mock.Anything, types.NamespacedName{Name: otherLokiCA.Name, Namespace: baseNamespace}, mock.Anything)
clientMock.AssertNotCalled(t, "Create")
clientMock.AssertNotCalled(t, "Update")
}