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

[2.12 backport]: Fix flapping status #1665

Merged
merged 1 commit into from
Nov 8, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ func (r *ObservabilityAddonReconciler) SetupWithManager(mgr ctrl.Manager) error
Watches(
&corev1.ConfigMap{},
&handler.EnqueueRequestForObject{},
builder.WithPredicates(getPred(clusterMonitoringConfigName, promNamespace, true, true, true)),
builder.WithPredicates(configMapDataChangedPredicate(clusterMonitoringConfigName, promNamespace)),
).
Watches(
&appsv1.Deployment{},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

v1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"

Expand Down Expand Up @@ -74,3 +75,29 @@ func getPred(name string, namespace string,
DeleteFunc: deleteFunc,
}
}

func configMapDataChangedPredicate(name, namespace string) predicate.Funcs {
return predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
oldCM, okOld := e.ObjectOld.(*corev1.ConfigMap)
newCM, okNew := e.ObjectNew.(*corev1.ConfigMap)
if !okOld || !okNew {
return false
}

if newCM.Name != name || newCM.Namespace != namespace {
return false
}

return !reflect.DeepEqual(oldCM.Data, newCM.Data)
},
CreateFunc: func(e event.CreateEvent) bool {
cm, ok := e.Object.(*corev1.ConfigMap)
return ok && cm.Name == name && cm.Namespace == namespace
},
DeleteFunc: func(e event.DeleteEvent) bool {
cm, ok := e.Object.(*corev1.ConfigMap)
return ok && cm.Name == name && cm.Namespace == namespace
},
}
}
30 changes: 21 additions & 9 deletions operators/endpointmetrics/pkg/collector/metrics_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@ type ClusterInfo struct {
}

type MetricsCollector struct {
Client client.Client
ClusterInfo ClusterInfo
HubInfo *operatorconfig.HubInfo
Log logr.Logger
Namespace string
ObsAddon *oav1beta1.ObservabilityAddon
ServiceAccountName string
Client client.Client
ClusterInfo ClusterInfo
HubInfo *operatorconfig.HubInfo
Log logr.Logger
Namespace string
ObsAddon *oav1beta1.ObservabilityAddon
ServiceAccountName string
platformCollectorWasUpdated bool
userWorkloadCollectorWasUpdated bool
}

type proxyConfig struct {
Expand Down Expand Up @@ -110,7 +112,7 @@ func (m *MetricsCollector) Update(ctx context.Context, req ctrl.Request) error {
m.reportStatus(ctx, status.MetricsCollector, status.UpdateFailed, "Failed to update metrics collector")
return err
} else {
if m.ObsAddon.Spec.EnableMetrics {
if m.ObsAddon.Spec.EnableMetrics && m.platformCollectorWasUpdated {
m.reportStatus(ctx, status.MetricsCollector, status.UpdateSuccessful, "Metrics collector updated")
} else {
m.reportStatus(ctx, status.MetricsCollector, status.Disabled, "Metrics collector disabled")
Expand All @@ -129,7 +131,7 @@ func (m *MetricsCollector) Update(ctx context.Context, req ctrl.Request) error {
m.reportStatus(ctx, status.UwlMetricsCollector, status.UpdateFailed, "Failed to update UWL Metrics collector")
return err
} else {
if m.ObsAddon.Spec.EnableMetrics {
if m.ObsAddon.Spec.EnableMetrics && m.userWorkloadCollectorWasUpdated {
m.reportStatus(ctx, status.UwlMetricsCollector, status.UpdateSuccessful, "UWL Metrics collector updated")
} else {
m.reportStatus(ctx, status.UwlMetricsCollector, status.Disabled, "UWL Metrics collector disabled")
Expand Down Expand Up @@ -744,6 +746,14 @@ func (m *MetricsCollector) ensureDeployment(ctx context.Context, isUWL bool, dep
desiredMetricsCollectorDep.Spec.Template.Spec.Containers[0].Resources = *m.ObsAddon.Spec.Resources
}

wasUpdated := func() {
if isUWL {
m.userWorkloadCollectorWasUpdated = true
} else {
m.platformCollectorWasUpdated = true
}
}

retryErr := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
foundMetricsCollectorDep := &appsv1.Deployment{}
err := m.Client.Get(ctx, types.NamespacedName{Name: name, Namespace: m.Namespace}, foundMetricsCollectorDep)
Expand All @@ -753,6 +763,7 @@ func (m *MetricsCollector) ensureDeployment(ctx context.Context, isUWL bool, dep
return fmt.Errorf("failed to create Deployment %s/%s: %w", m.Namespace, name, err)
}

wasUpdated()
return nil
}
if err != nil {
Expand All @@ -773,6 +784,7 @@ func (m *MetricsCollector) ensureDeployment(ctx context.Context, isUWL bool, dep
return fmt.Errorf("failed to update Deployment %s/%s: %w", m.Namespace, name, err)
}

wasUpdated()
return nil
}

Expand Down
Loading