This is an automated email from the ASF dual-hosted git repository.
pcongiusti pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git
The following commit(s) were added to refs/heads/main by this push:
new 3c2e3e113 fix(ctrl): adding notification action parameter
3c2e3e113 is described below
commit 3c2e3e113a2d58bcacf0b23ce3a54b4ed55f8a14
Author: Pasquale Congiusti <[email protected]>
AuthorDate: Sat Mar 28 09:34:22 2026 +0100
fix(ctrl): adding notification action parameter
Closes #6546
---
pkg/controller/build/build_controller.go | 4 +-
pkg/controller/build/monitor_routine.go | 4 +-
pkg/controller/catalog/catalog_controller.go | 4 +-
.../integration/integration_controller.go | 4 +-
.../integrationkit/integrationkit_controller.go | 4 +-
.../integrationplatform_controller.go | 4 +-
pkg/controller/pipe/pipe_controller.go | 6 +-
pkg/event/manager.go | 96 +++-------------
pkg/event/manager_test.go | 123 +++++++++++++++++++++
9 files changed, 153 insertions(+), 96 deletions(-)
diff --git a/pkg/controller/build/build_controller.go
b/pkg/controller/build/build_controller.go
index ca0b4bc46..57fac5b2c 100644
--- a/pkg/controller/build/build_controller.go
+++ b/pkg/controller/build/build_controller.go
@@ -193,7 +193,7 @@ func (r *reconcileBuild) Reconcile(ctx context.Context,
request reconcile.Reques
newTarget, err := a.Handle(ctx, target)
if err != nil {
- camelevent.NotifyBuildError(ctx, r.client, r.recorder,
&instance, newTarget, err)
+ camelevent.NotifyError(r.recorder, &instance,
newTarget, instance.Name, instance.Kind, err)
return reconcile.Result{}, err
}
@@ -232,7 +232,7 @@ func (r *reconcileBuild) update(ctx context.Context, l
log.Logger, base *v1.Buil
target.Status.ObservedGeneration = base.Generation
if err := r.client.Status().Patch(ctx, target, ctrl.MergeFrom(base));
err != nil {
- camelevent.NotifyBuildError(ctx, r.client, r.recorder, base,
target, err)
+ camelevent.NotifyError(r.recorder, base, target, target.Name,
target.Kind, err)
return err
}
diff --git a/pkg/controller/build/monitor_routine.go
b/pkg/controller/build/monitor_routine.go
index 663eefbe4..59c505560 100644
--- a/pkg/controller/build/monitor_routine.go
+++ b/pkg/controller/build/monitor_routine.go
@@ -210,7 +210,7 @@ func (action *monitorRoutineAction) updateBuildStatus(ctx
context.Context, build
p, err := patch.MergePatch(build, target)
if err != nil {
action.L.Errorf(err, "Cannot patch build status: %s",
build.Name)
- event.NotifyBuildError(ctx, action.client, action.recorder,
build, target, err)
+ event.NotifyError(action.recorder, build, target, target.Name,
target.Kind, err)
return err
}
@@ -225,7 +225,7 @@ func (action *monitorRoutineAction) updateBuildStatus(ctx
context.Context, build
err = action.client.Status().Patch(ctx, target,
ctrl.RawPatch(types.MergePatchType, p))
if err != nil {
action.L.Errorf(err, "Cannot update build status: %s",
build.Name)
- event.NotifyBuildError(ctx, action.client,
action.recorder, build, target, err)
+ event.NotifyError(action.recorder, build, target,
target.Name, target.Kind, err)
return err
}
diff --git a/pkg/controller/catalog/catalog_controller.go
b/pkg/controller/catalog/catalog_controller.go
index 3d2477b9c..58aa4eacb 100644
--- a/pkg/controller/catalog/catalog_controller.go
+++ b/pkg/controller/catalog/catalog_controller.go
@@ -175,7 +175,7 @@ func (r *reconcileCatalog) Reconcile(ctx context.Context,
request reconcile.Requ
target, err = a.Handle(ctx, target)
if err != nil {
- camelevent.NotifyCamelCatalogError(ctx, r.client,
r.recorder, &instance, target, err)
+ camelevent.NotifyError(r.recorder, &instance, target,
instance.Name, instance.Kind, err)
return reconcile.Result{}, err
}
@@ -184,7 +184,7 @@ func (r *reconcileCatalog) Reconcile(ctx context.Context,
request reconcile.Requ
target.Status.ObservedGeneration =
instance.GetGeneration()
if err := r.client.Status().Patch(ctx, target,
ctrl.MergeFrom(&instance)); err != nil {
- camelevent.NotifyCamelCatalogError(ctx,
r.client, r.recorder, &instance, target, err)
+ camelevent.NotifyError(r.recorder, &instance,
target, target.Name, target.Kind, err)
return reconcile.Result{}, err
}
diff --git a/pkg/controller/integration/integration_controller.go
b/pkg/controller/integration/integration_controller.go
index 14f7a6751..e9de81d93 100644
--- a/pkg/controller/integration/integration_controller.go
+++ b/pkg/controller/integration/integration_controller.go
@@ -526,7 +526,7 @@ func (r *reconcileIntegration) Reconcile(ctx
context.Context, request reconcile.
newTarget, err := a.Handle(ctx, target)
if err != nil {
- camelevent.NotifyIntegrationError(ctx, r.client,
r.recorder, &instance, newTarget, err)
+ camelevent.NotifyError(r.recorder, &instance, target,
instance.Name, instance.Kind, err)
// Update the integration (mostly just to update its
phase) if the new instance is returned
if newTarget != nil {
_ = r.update(ctx, &instance, newTarget,
&targetLog)
@@ -537,7 +537,7 @@ func (r *reconcileIntegration) Reconcile(ctx
context.Context, request reconcile.
if newTarget != nil {
if err := r.update(ctx, &instance, newTarget,
&targetLog); err != nil {
- camelevent.NotifyIntegrationError(ctx,
r.client, r.recorder, &instance, newTarget, err)
+ camelevent.NotifyError(r.recorder, &instance,
target, target.Name, target.Kind, err)
return reconcile.Result{}, err
}
diff --git a/pkg/controller/integrationkit/integrationkit_controller.go
b/pkg/controller/integrationkit/integrationkit_controller.go
index 2d603c12b..8456db539 100644
--- a/pkg/controller/integrationkit/integrationkit_controller.go
+++ b/pkg/controller/integrationkit/integrationkit_controller.go
@@ -285,14 +285,14 @@ func (r *reconcileIntegrationKit) Reconcile(ctx
context.Context, request reconci
newTarget, err := a.Handle(ctx, target)
if err != nil {
- camelevent.NotifyIntegrationKitError(ctx, r.client,
r.recorder, &instance, newTarget, err)
+ camelevent.NotifyError(r.recorder, &instance, target,
instance.Name, instance.Kind, err)
return reconcile.Result{}, err
}
if newTarget != nil {
if res, err := r.update(ctx, &instance, newTarget); err
!= nil {
- camelevent.NotifyIntegrationKitError(ctx,
r.client, r.recorder, &instance, newTarget, err)
+ camelevent.NotifyError(r.recorder, &instance,
target, target.Name, target.Kind, err)
return res, err
}
diff --git
a/pkg/controller/integrationplatform/integrationplatform_controller.go
b/pkg/controller/integrationplatform/integrationplatform_controller.go
index 27fef840e..1efbce909 100644
--- a/pkg/controller/integrationplatform/integrationplatform_controller.go
+++ b/pkg/controller/integrationplatform/integrationplatform_controller.go
@@ -176,7 +176,7 @@ func (r *reconcileIntegrationPlatform) Reconcile(ctx
context.Context, request re
target, err = a.Handle(ctx, target)
if err != nil {
- camelevent.NotifyIntegrationPlatformError(ctx,
r.client, r.recorder, &instance, target, err)
+ camelevent.NotifyError(r.recorder, &instance, target,
instance.Name, instance.Kind, err)
return reconcile.Result{}, err
}
@@ -185,7 +185,7 @@ func (r *reconcileIntegrationPlatform) Reconcile(ctx
context.Context, request re
target.Status.ObservedGeneration = instance.Generation
if err := r.client.Status().Patch(ctx, target,
ctrl.MergeFrom(&instance)); err != nil {
- camelevent.NotifyIntegrationPlatformError(ctx,
r.client, r.recorder, &instance, target, err)
+ camelevent.NotifyError(r.recorder, &instance,
target, target.Name, target.Kind, err)
return reconcile.Result{}, err
}
diff --git a/pkg/controller/pipe/pipe_controller.go
b/pkg/controller/pipe/pipe_controller.go
index 731a3cfd1..ca8762ed6 100644
--- a/pkg/controller/pipe/pipe_controller.go
+++ b/pkg/controller/pipe/pipe_controller.go
@@ -200,7 +200,7 @@ func (r *ReconcilePipe) Reconcile(ctx context.Context,
request reconcile.Request
target, err = a.Handle(ctx, target)
if err != nil {
- camelevent.NotifyPipeError(ctx, r.client, r.recorder,
&instance, target, err)
+ camelevent.NotifyError(r.recorder, &instance, target,
instance.Name, instance.Kind, err)
// Update the binding (mostly just to update its phase)
if the new instance is returned
if target != nil {
_ = r.update(ctx, &instance, target, &targetLog)
@@ -211,7 +211,7 @@ func (r *ReconcilePipe) Reconcile(ctx context.Context,
request reconcile.Request
if target != nil {
if err := r.update(ctx, &instance, target, &targetLog);
err != nil {
- camelevent.NotifyPipeError(ctx, r.client,
r.recorder, &instance, target, err)
+ camelevent.NotifyError(r.recorder, &instance,
target, target.Name, target.Kind, err)
return reconcile.Result{}, err
}
@@ -231,7 +231,7 @@ func (r *ReconcilePipe) update(ctx context.Context, base
*v1.Pipe, target *v1.Pi
target.Status.ObservedGeneration = base.Generation
if err := r.client.Status().Patch(ctx, target, ctrl.MergeFrom(base));
err != nil {
- camelevent.NotifyPipeError(ctx, r.client, r.recorder, base,
target, err)
+ camelevent.NotifyError(r.recorder, base, target, target.Name,
target.Kind, err)
return err
}
diff --git a/pkg/event/manager.go b/pkg/event/manager.go
index 7cb048b37..c4b44e406 100644
--- a/pkg/event/manager.go
+++ b/pkg/event/manager.go
@@ -38,22 +38,16 @@ const (
ReasonIntegrationPhaseUpdated = "IntegrationPhaseUpdated"
// ReasonIntegrationConditionChanged --.
ReasonIntegrationConditionChanged = "IntegrationConditionChanged"
- // ReasonIntegrationError --.
- ReasonIntegrationError = "IntegrationError"
// ReasonIntegrationKitPhaseUpdated --.
ReasonIntegrationKitPhaseUpdated = "IntegrationKitPhaseUpdated"
// ReasonIntegrationKitConditionChanged --.
ReasonIntegrationKitConditionChanged = "IntegrationKitConditionChanged"
- // ReasonIntegrationKitError --.
- ReasonIntegrationKitError = "IntegrationKitError"
// ReasonIntegrationPlatformPhaseUpdated --.
ReasonIntegrationPlatformPhaseUpdated =
"IntegrationPlatformPhaseUpdated"
// ReasonIntegrationPlatformConditionChanged --.
ReasonIntegrationPlatformConditionChanged =
"IntegrationPlatformConditionChanged"
- // ReasonIntegrationPlatformError --.
- ReasonIntegrationPlatformError = "IntegrationPlatformError"
// ReasonIntegrationProfilePhaseUpdated --.
ReasonIntegrationProfilePhaseUpdated = "IntegrationProfilePhaseUpdated"
@@ -66,11 +60,7 @@ const (
ReasonBuildPhaseUpdated = "BuildPhaseUpdated"
// ReasonBuildConditionChanged --.
ReasonBuildConditionChanged = "BuildConditionChanged"
- // ReasonBuildError --.
- ReasonBuildError = "BuildError"
- // ReasonKameletError --.
- ReasonKameletError = "KameletError"
// ReasonKameletConditionChanged --.
ReasonKameletConditionChanged = "KameletConditionChanged"
// ReasonKameletPhaseUpdated --.
@@ -82,23 +72,12 @@ const (
ReasonPipeConditionChanged = "PipeConditionChanged"
// ReasonPipePhaseUpdated --.
ReasonPipePhaseUpdated = "PipePhaseUpdated"
-
// ReasonRelatedObjectChanged --.
ReasonRelatedObjectChanged = "ReasonRelatedObjectChanged"
+ // ActionPhaseUpdated --.
+ ActionPhaseUpdated = "PhaseUpdated"
)
-// NotifyIntegrationError automatically generates error events when the
integration reconcile cycle phase has an error.
-func NotifyIntegrationError(ctx context.Context, c client.Client, recorder
events.EventRecorder, old, newResource *v1.Integration, err error) {
- it := old
- if newResource != nil {
- it = newResource
- }
- if it == nil {
- return
- }
- recorder.Eventf(it, nil, corev1.EventTypeWarning,
ReasonIntegrationError, "Cannot reconcile Integration %s: %v", it.Name, err)
-}
-
// NotifyIntegrationUpdated automatically generates events when the
integration changes.
func NotifyIntegrationUpdated(ctx context.Context, c client.Client, recorder
events.EventRecorder, old, newResource *v1.Integration) {
if newResource == nil {
@@ -137,18 +116,6 @@ func NotifyIntegrationKitUpdated(ctx context.Context, c
client.Client, recorder
"Integration Kit", newResource.Name,
ReasonIntegrationKitPhaseUpdated, "")
}
-// NotifyIntegrationKitError automatically generates error events when the
integration kit reconcile cycle phase has an error.
-func NotifyIntegrationKitError(ctx context.Context, c client.Client, recorder
events.EventRecorder, old, newResource *v1.IntegrationKit, err error) {
- kit := old
- if newResource != nil {
- kit = newResource
- }
- if kit == nil {
- return
- }
- recorder.Eventf(kit, nil, corev1.EventTypeWarning,
ReasonIntegrationKitError, "Cannot reconcile Integration Kit %s: %v", kit.Name,
err)
-}
-
// NotifyIntegrationPlatformUpdated automatically generates events when an
integration platform changes.
func NotifyIntegrationPlatformUpdated(ctx context.Context, c client.Client,
recorder events.EventRecorder, old, newResource *v1.IntegrationPlatform) {
if newResource == nil {
@@ -168,18 +135,6 @@ func NotifyIntegrationPlatformUpdated(ctx context.Context,
c client.Client, reco
"Integration Platform", newResource.Name,
ReasonIntegrationPlatformPhaseUpdated, "")
}
-// NotifyIntegrationPlatformError automatically generates error events when
the integration Platform reconcile cycle phase has an error.
-func NotifyIntegrationPlatformError(ctx context.Context, c client.Client,
recorder events.EventRecorder, old, newResource *v1.IntegrationPlatform, err
error) {
- p := old
- if newResource != nil {
- p = newResource
- }
- if p == nil {
- return
- }
- recorder.Eventf(p, nil, corev1.EventTypeWarning,
ReasonIntegrationPlatformError, "Cannot reconcile Integration Platform %s: %v",
p.Name, err)
-}
-
// NotifyCamelCatalogUpdated automatically generates events when a
CamelCatalog changes.
func NotifyCamelCatalogUpdated(ctx context.Context, c client.Client, recorder
events.EventRecorder, old, newResource *v1.CamelCatalog) {
if newResource == nil {
@@ -199,18 +154,6 @@ func NotifyCamelCatalogUpdated(ctx context.Context, c
client.Client, recorder ev
"CamelCatalog", newResource.Name, ReasonKameletPhaseUpdated, "")
}
-// NotifyCamelCatalogError automatically generates error events when the
CamelCatalog reconcile cycle phase has an error.
-func NotifyCamelCatalogError(ctx context.Context, c client.Client, recorder
events.EventRecorder, old, newResource *v1.CamelCatalog, err error) {
- k := old
- if newResource != nil {
- k = newResource
- }
- if k == nil {
- return
- }
- recorder.Eventf(k, nil, corev1.EventTypeWarning, ReasonKameletError,
"Cannot reconcile CamelCatalog %s: %v", k.Name, err)
-}
-
// NotifyPipeUpdated automatically generates events when a Pipe changes.
func NotifyPipeUpdated(ctx context.Context, c client.Client, recorder
events.EventRecorder, old, newResource *v1.Pipe) {
if newResource == nil {
@@ -230,18 +173,6 @@ func NotifyPipeUpdated(ctx context.Context, c
client.Client, recorder events.Eve
"Pipe", newResource.Name, ReasonPipePhaseUpdated, "")
}
-// NotifyPipeError automatically generates error events when the binding
reconcile cycle phase has an error.
-func NotifyPipeError(ctx context.Context, c client.Client, recorder
events.EventRecorder, old, newResource *v1.Pipe, err error) {
- k := old
- if newResource != nil {
- k = newResource
- }
- if k == nil {
- return
- }
- recorder.Eventf(k, nil, corev1.EventTypeWarning, ReasonKameletError,
"Cannot reconcile Pipe %s: %v", k.Name, err)
-}
-
// NotifyBuildUpdated automatically generates events when a build changes.
func NotifyBuildUpdated(ctx context.Context, c client.Client, recorder
events.EventRecorder, old, newResource *v1.Build) {
if newResource == nil {
@@ -265,16 +196,19 @@ func NotifyBuildUpdated(ctx context.Context, c
client.Client, recorder events.Ev
notifyIfPhaseUpdated(ctx, c, recorder, newResource, oldPhase,
string(newResource.Status.Phase), "Build", newResource.Name,
ReasonBuildPhaseUpdated, info)
}
-// NotifyBuildError automatically generates error events when the build
reconcile cycle phase has an error.
-func NotifyBuildError(ctx context.Context, c client.Client, recorder
events.EventRecorder, old, newResource *v1.Build, err error) {
- p := old
+// NotifyError is the generic event error recorder.
+func NotifyError(recorder events.EventRecorder, old, newResource
runtime.Object, name, kind string, err error) {
+ reason := kind + "Error"
+ action := kind + "Reconciliation"
+ res := old
if newResource != nil {
- p = newResource
+ res = newResource
}
- if p == nil {
+ if res == nil {
return
}
- recorder.Eventf(p, nil, corev1.EventTypeWarning, ReasonBuildError,
"Cannot reconcile Build %s: %v", p.Name, err)
+ recorder.Eventf(res, nil, corev1.EventTypeWarning, reason, action,
"Cannot reconcile %s %s: %v",
+ kind, name, err)
}
//nolint:lll
@@ -289,14 +223,14 @@ func notifyIfPhaseUpdated(ctx context.Context, c
client.Client, recorder events.
if phase == "" {
phase = "[none]"
}
- recorder.Eventf(newResource, nil, corev1.EventTypeNormal, reason, "%s
%q in phase %q%s", resourceType, name, phase, info)
+ recorder.Eventf(newResource, nil, corev1.EventTypeNormal, reason,
ActionPhaseUpdated, "%s %q in phase %q%s", resourceType, name, phase, info)
if creatorRef, creator := getCreatorObject(ctx, c, newResource);
creatorRef != nil && creator != nil {
if namespace := newResource.GetNamespace(); namespace ==
creatorRef.Namespace {
- recorder.Eventf(creator, nil, corev1.EventTypeNormal,
ReasonRelatedObjectChanged,
+ recorder.Eventf(creator, nil, corev1.EventTypeNormal,
ReasonRelatedObjectChanged, ActionPhaseUpdated,
"%s %q, created by %s %q, changed phase to
%q%s", resourceType, name, creatorRef.Kind, creatorRef.Name, phase, info)
} else {
- recorder.Eventf(creator, nil, corev1.EventTypeNormal,
ReasonRelatedObjectChanged,
+ recorder.Eventf(creator, nil, corev1.EventTypeNormal,
ReasonRelatedObjectChanged, ActionPhaseUpdated,
"%s \"%s/%s\", created by %s %q, changed phase
to %q%s", resourceType, namespace, name, creatorRef.Kind, creatorRef.Name,
phase, info)
}
}
@@ -314,7 +248,7 @@ func notifyIfConditionUpdated(recorder
events.EventRecorder, newResource runtime
tail = tail[:800] + "..."
}
}
- recorder.Eventf(newResource, nil, corev1.EventTypeNormal,
reason, "Condition %q is %q for %s %s%s",
+ recorder.Eventf(newResource, nil, corev1.EventTypeNormal,
reason, "ConditionUpdated", "Condition %q is %q for %s %s%s",
cond.GetType(), cond.GetStatus(), resourceType, name,
tail)
}
}
diff --git a/pkg/event/manager_test.go b/pkg/event/manager_test.go
new file mode 100644
index 000000000..7d05f9e7f
--- /dev/null
+++ b/pkg/event/manager_test.go
@@ -0,0 +1,123 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements. See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package event
+
+import (
+ "fmt"
+ "testing"
+
+ corev1 "k8s.io/api/core/v1"
+ "k8s.io/apimachinery/pkg/runtime"
+)
+
+// fakeRecorder implements events.EventRecorder and captures calls.
+type fakeRecorder struct {
+ called bool
+ obj runtime.Object
+ eventtype string
+ reason string
+ action string
+ message string
+}
+
+func (f *fakeRecorder) Eventf(obj runtime.Object, old runtime.Object,
eventtype, reason, action, note string, args ...interface{}) {
+ f.called = true
+ f.obj = obj
+ f.eventtype = eventtype
+ f.reason = reason
+ f.action = action
+ f.message = fmt.Sprintf(note, args...)
+}
+
+// Unused but required to satisfy interface
+func (f *fakeRecorder) Event(obj runtime.Object, old runtime.Object,
eventtype, reason, action, note string) {
+}
+
+func TestNotifyError(t *testing.T) {
+ err := fmt.Errorf("boom")
+
+ oldObj := &runtime.Unknown{}
+ newObj := &runtime.Unknown{}
+
+ tests := []struct {
+ name string
+ old runtime.Object
+ new runtime.Object
+ expectCalled bool
+ expectedObj runtime.Object
+ }{
+ {
+ name: "uses newResource when not nil",
+ old: oldObj,
+ new: newObj,
+ expectCalled: true,
+ expectedObj: newObj,
+ },
+ {
+ name: "falls back to old when newResource is
nil",
+ old: oldObj,
+ new: nil,
+ expectCalled: true,
+ expectedObj: oldObj,
+ },
+ {
+ name: "does nothing when both are nil",
+ old: nil,
+ new: nil,
+ expectCalled: false,
+ expectedObj: nil,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ rec := &fakeRecorder{}
+
+ NotifyError(rec, tt.old, tt.new, "my-name", "MyKind",
err)
+
+ if tt.expectCalled != rec.called {
+ t.Fatalf("expected called=%v, got %v",
tt.expectCalled, rec.called)
+ }
+
+ if !tt.expectCalled {
+ return
+ }
+
+ if rec.obj != tt.expectedObj {
+ t.Errorf("expected object %v, got %v",
tt.expectedObj, rec.obj)
+ }
+
+ if rec.eventtype != corev1.EventTypeWarning {
+ t.Errorf("expected event type %s, got %s",
corev1.EventTypeWarning, rec.eventtype)
+ }
+
+ if rec.reason != "MyKindError" {
+ t.Errorf("unexpected reason: %s", rec.reason)
+ }
+
+ if rec.action != "MyKindReconciliation" {
+ t.Errorf("unexpected action: %s", rec.action)
+ }
+
+ expectedMsg := "Cannot reconcile MyKind my-name: boom"
+ if rec.message != expectedMsg {
+ t.Errorf("expected message %q, got %q",
expectedMsg, rec.message)
+ }
+ })
+ }
+}