This is an automated email from the ASF dual-hosted git repository.
squakez 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 082bda955 fix(operator): handle deleted watched CRDs gracefully (#6981)
082bda955 is described below
commit 082bda955e1dbf6b3b3df88ab9a80c3b946eb4d3
Author: Anshul Jagota <[email protected]>
AuthorDate: Fri Nov 27 18:59:10 2026 +0530
fix(operator): handle deleted watched CRDs gracefully (#6981)
---
pkg/cmd/operator/operator.go | 42 ++++++++++++++++++++-
pkg/cmd/operator/operator_test.go | 47 ++++++++++++++++++++++++
pkg/controller/integration/camel_monitor.go | 3 +-
pkg/controller/integration/camel_monitor_test.go | 18 +++++++++
4 files changed, 108 insertions(+), 2 deletions(-)
diff --git a/pkg/cmd/operator/operator.go b/pkg/cmd/operator/operator.go
index 71324cfb8..13d02b011 100644
--- a/pkg/cmd/operator/operator.go
+++ b/pkg/cmd/operator/operator.go
@@ -27,6 +27,7 @@ import (
"runtime"
"strconv"
"strings"
+ "sync"
"time"
"k8s.io/klog/v2"
@@ -38,9 +39,12 @@ import (
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
+ k8serrors "k8s.io/apimachinery/pkg/api/errors"
+ "k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/types"
+ toolscache "k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/leaderelection/resourcelock"
"sigs.k8s.io/controller-runtime/pkg/cache"
ctrl "sigs.k8s.io/controller-runtime/pkg/client"
@@ -209,7 +213,8 @@ func Run(healthPort, monitoringPort int32, leaderElection
bool, leaderElectionID
}
options := cache.Options{
- ByObject: selectors,
+ ByObject: selectors,
+ DefaultWatchErrorHandler: newWatchErrorHandler(),
}
if !platform.IsCurrentOperatorGlobal() {
@@ -259,6 +264,41 @@ func newMetricsServerOptions(monitoringPort int32, certDir
string) metricsserver
}
}
+func isMissingResourceWatchError(err error) bool {
+ return k8serrors.IsNotFound(err) || meta.IsNoMatchError(err)
+}
+
+func newWatchErrorHandler() toolscache.WatchErrorHandlerWithContext {
+ return
newWatchErrorHandlerWithFallback(toolscache.DefaultWatchErrorHandler)
+}
+
+func newWatchErrorHandlerWithFallback(fallback
toolscache.WatchErrorHandlerWithContext)
toolscache.WatchErrorHandlerWithContext {
+ var seen sync.Map
+
+ return func(ctx context.Context, r *toolscache.Reflector, err error) {
+ if isMissingResourceWatchError(err) {
+ typeDesc := ""
+ reflectorName := ""
+ if r != nil {
+ typeDesc = r.TypeDescription()
+ reflectorName = r.Name()
+ }
+ key := reflectorName + ":" + typeDesc
+ if _, loaded := seen.LoadOrStore(key, struct{}{});
!loaded {
+ log.Infof("Resource type %q is not available on
the cluster; the watch keeps retrying with backoff (%v)", typeDesc, err)
+ } else {
+ log.Debugf("Resource type %q is still not
available on the cluster (%v)", typeDesc, err)
+ }
+
+ return
+ }
+
+ if fallback != nil {
+ fallback(ctx, r, err)
+ }
+ }
+}
+
func getNamespacesSelector(operatorNamespace string, watchNamespace string)
map[string]cache.Config {
namespacesSelector := map[string]cache.Config{
// The same operator namespace is needed while the operator
stores
diff --git a/pkg/cmd/operator/operator_test.go
b/pkg/cmd/operator/operator_test.go
index 93c42a603..195606716 100644
--- a/pkg/cmd/operator/operator_test.go
+++ b/pkg/cmd/operator/operator_test.go
@@ -19,6 +19,7 @@ package operator
import (
"context"
+ "errors"
"testing"
"github.com/apache/camel-k/v2/pkg/internal"
@@ -26,11 +27,57 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
+ k8serrors "k8s.io/apimachinery/pkg/api/errors"
+ "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
+ "k8s.io/apimachinery/pkg/runtime/schema"
+ toolscache "k8s.io/client-go/tools/cache"
"sigs.k8s.io/controller-runtime/pkg/cache"
)
+func TestWatchErrorHandler(t *testing.T) {
+ t.Run("suppresses NotFound watch error without calling fallback",
func(t *testing.T) {
+ fallbackCalled := false
+ handler :=
newWatchErrorHandlerWithFallback(func(context.Context, *toolscache.Reflector,
error) {
+ fallbackCalled = true
+ })
+
+ notFoundErr :=
k8serrors.NewNotFound(schema.GroupResource{Group: "camel.apache.org", Resource:
"camelmonitors"}, "")
+ handler(context.Background(), nil, notFoundErr)
+ handler(context.Background(), nil, notFoundErr)
+
+ assert.False(t, fallbackCalled)
+ })
+
+ t.Run("suppresses NoKindMatchError without calling fallback", func(t
*testing.T) {
+ fallbackCalled := false
+ handler :=
newWatchErrorHandlerWithFallback(func(context.Context, *toolscache.Reflector,
error) {
+ fallbackCalled = true
+ })
+
+ noMatchErr := &meta.NoKindMatchError{
+ GroupKind: schema.GroupKind{Group:
"camel.apache.org", Kind: "CamelMonitor"},
+ SearchedVersions: []string{"v1alpha1"},
+ }
+ handler(context.Background(), nil, noMatchErr)
+
+ assert.False(t, fallbackCalled)
+ })
+
+ t.Run("delegates other errors to fallback", func(t *testing.T) {
+ var capturedErr error
+ handler := newWatchErrorHandlerWithFallback(func(_
context.Context, _ *toolscache.Reflector, err error) {
+ capturedErr = err
+ })
+
+ otherErr := errors.New("connection refused")
+ handler(context.Background(), nil, otherErr)
+
+ assert.ErrorIs(t, capturedErr, otherErr)
+ })
+}
+
func TestNewMetricsServerOptions(t *testing.T) {
t.Run("defaults to HTTP when certDir is empty", func(t *testing.T) {
options := newMetricsServerOptions(8080, "")
diff --git a/pkg/controller/integration/camel_monitor.go
b/pkg/controller/integration/camel_monitor.go
index c2ef09629..b91ba5816 100644
--- a/pkg/controller/integration/camel_monitor.go
+++ b/pkg/controller/integration/camel_monitor.go
@@ -23,6 +23,7 @@ import (
monitorv1alpha1
"github.com/camel-tooling/camel-monitor-operator/pkg/apis/camel/v1alpha1"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
+ "k8s.io/apimachinery/pkg/api/meta"
ctrl "sigs.k8s.io/controller-runtime/pkg/client"
v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
@@ -31,7 +32,7 @@ import (
func addCamelMonitorCondition(ctx context.Context, c ctrl.Reader, integration
*v1.Integration) error {
monitor := &monitorv1alpha1.CamelMonitor{}
if err := c.Get(ctx, ctrl.ObjectKeyFromObject(integration), monitor);
err != nil {
- if k8serrors.IsNotFound(err) {
+ if k8serrors.IsNotFound(err) || meta.IsNoMatchError(err) {
integration.Status.RemoveCondition(v1.IntegrationConditionCamelMonitorAvailable)
return nil
diff --git a/pkg/controller/integration/camel_monitor_test.go
b/pkg/controller/integration/camel_monitor_test.go
index fd404918f..f86a8f84a 100644
--- a/pkg/controller/integration/camel_monitor_test.go
+++ b/pkg/controller/integration/camel_monitor_test.go
@@ -25,8 +25,10 @@ import (
monitorv1alpha1
"github.com/camel-tooling/camel-monitor-operator/pkg/apis/camel/v1alpha1"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
+ "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
+ "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery/fake"
k8stesting "k8s.io/client-go/testing"
"sigs.k8s.io/controller-runtime/pkg/builder"
@@ -85,6 +87,22 @@ func TestCamelMonitorLookupFailure(t *testing.T) {
require.Equal(t, previous, &integration.Status)
}
+func TestCamelMonitorNoMatchError(t *testing.T) {
+ integration := v1.NewIntegration("test", "sample")
+
integration.Status.SetCondition(v1.IntegrationConditionCamelMonitorAvailable,
corev1.ConditionTrue, "Available", "found")
+ c, err := internal.NewFakeClient()
+ require.NoError(t, err)
+ noMatchErr := &meta.NoKindMatchError{
+ GroupKind: schema.GroupKind{Group:
monitorv1alpha1.SchemeGroupVersion.Group, Kind: monitorv1alpha1.AppKind},
+ SearchedVersions:
[]string{monitorv1alpha1.SchemeGroupVersion.Version},
+ }
+ c.(*internal.FakeClient).Intercept(&interceptor.Funcs{Get:
func(context.Context, ctrl.WithWatch, ctrl.ObjectKey, ctrl.Object,
...ctrl.GetOption) error {
+ return noMatchErr
+ }})
+ require.NoError(t, addCamelMonitorCondition(context.Background(), c,
&integration))
+ require.Nil(t,
integration.Status.GetCondition(v1.IntegrationConditionCamelMonitorAvailable))
+}
+
func enableCamelMonitorDiscovery(t *testing.T, c client.Client)
*fake.FakeDiscovery {
t.Helper()
fakeClient, ok := c.(*internal.FakeClient)