From f3ecbf6d14fe7cb94001ec5d57a80d388d48cb7c Mon Sep 17 00:00:00 2001 From: Coleen Iona Quadros Date: Tue, 12 Nov 2024 13:11:19 +0100 Subject: [PATCH 1/3] use deep equal Signed-off-by: Coleen Iona Quadros --- .../ocp_monitoring_config.go | 3 +- .../pkg/collector/metrics_collector.go | 10 ++--- operators/pkg/deploying/deployer.go | 42 +++++++++---------- 3 files changed, 26 insertions(+), 29 deletions(-) diff --git a/operators/endpointmetrics/controllers/observabilityendpoint/ocp_monitoring_config.go b/operators/endpointmetrics/controllers/observabilityendpoint/ocp_monitoring_config.go index 622c08b30..0980dcebb 100644 --- a/operators/endpointmetrics/controllers/observabilityendpoint/ocp_monitoring_config.go +++ b/operators/endpointmetrics/controllers/observabilityendpoint/ocp_monitoring_config.go @@ -17,7 +17,6 @@ import ( "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" - "github.com/ghodss/yaml" cmomanifests "github.com/openshift/cluster-monitoring-operator/pkg/manifests" operatorconfig "github.com/stolostron/multicluster-observability-operator/operators/pkg/config" ) @@ -423,7 +422,7 @@ func createOrUpdateClusterMonitoringConfig( newCMOCfg.PrometheusK8sConfig = newPmK8sConfig } - if equality.Semantic.DeepEqual(*foundClusterMonitoringConfiguration, *newCMOCfg) { + if equality.Semantic.Deep(*foundClusterMonitoringConfiguration, *newCMOCfg) { return nil } diff --git a/operators/endpointmetrics/pkg/collector/metrics_collector.go b/operators/endpointmetrics/pkg/collector/metrics_collector.go index d9ec1f75d..d028e0c5f 100644 --- a/operators/endpointmetrics/pkg/collector/metrics_collector.go +++ b/operators/endpointmetrics/pkg/collector/metrics_collector.go @@ -13,9 +13,7 @@ import ( "strings" "time" - "github.com/go-logr/logr" monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" - "gopkg.in/yaml.v2" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/equality" @@ -325,7 +323,7 @@ func (m *MetricsCollector) ensureService(ctx context.Context, isUWL bool) error return fmt.Errorf("failed to get service %s/%s: %w", m.Namespace, name, err) } - if !equality.Semantic.DeepDerivative(desiredService.Spec, foundService.Spec) { + if !equality.Semantic.DeepEqual(desiredService.Spec, foundService.Spec) { m.Log.Info("Updating Service", "name", name, "namespace", m.Namespace) foundService.Spec = desiredService.Spec @@ -407,7 +405,7 @@ func (m *MetricsCollector) ensureServiceMonitor(ctx context.Context, isUWL bool) return fmt.Errorf("failed to get ServiceMonitor %s/%s: %w", m.Namespace, name, err) } - if !equality.Semantic.DeepDerivative(desiredSm.Spec, foundSm.Spec) { + if !equality.Semantic.DeepEqual(desiredSm.Spec, foundSm.Spec) { m.Log.Info("Updating ServiceMonitor", "name", name, "namespace", m.Namespace) foundSm.Spec = desiredSm.Spec @@ -493,7 +491,7 @@ func (m *MetricsCollector) ensureAlertingRule(ctx context.Context, isUWL bool) e return fmt.Errorf("failed to get PrometheusRule %s/%s: %w", m.Namespace, name, err) } - if !equality.Semantic.DeepDerivative(desiredPromRule.Spec, foundPromRule.Spec) { + if !equality.Semantic.DeepEqual(desiredPromRule.Spec, foundPromRule.Spec) { m.Log.Info("Updating PrometheusRule", "name", name, "namespace", m.Namespace) foundPromRule.Spec = desiredPromRule.Spec @@ -759,7 +757,7 @@ func (m *MetricsCollector) ensureDeployment(ctx context.Context, isUWL bool, dep return fmt.Errorf("failed to get Deployment %s/%s: %w", m.Namespace, name, err) } - isDifferentSpec := !equality.Semantic.DeepDerivative(desiredMetricsCollectorDep.Spec.Template.Spec, foundMetricsCollectorDep.Spec.Template.Spec) + isDifferentSpec := !equality.Semantic.DeepEqual(desiredMetricsCollectorDep.Spec.Template.Spec, foundMetricsCollectorDep.Spec.Template.Spec) isDifferentReplicas := !equality.Semantic.DeepEqual(desiredMetricsCollectorDep.Spec.Replicas, foundMetricsCollectorDep.Spec.Replicas) if isDifferentSpec || isDifferentReplicas || deployParams.forceRestart { m.Log.Info("Updating Deployment", "name", name, "namespace", m.Namespace, "isDifferentSpec", isDifferentSpec, "isDifferentReplicas", isDifferentReplicas, "forceRestart", deployParams.forceRestart) diff --git a/operators/pkg/deploying/deployer.go b/operators/pkg/deploying/deployer.go index 8595649f1..496467912 100644 --- a/operators/pkg/deploying/deployer.go +++ b/operators/pkg/deploying/deployer.go @@ -109,7 +109,7 @@ func (d *Deployer) updateDeployment(ctx context.Context, desiredObj, runtimeObj return err } - if !apiequality.Semantic.DeepDerivative(desiredDeploy.Spec, runtimeDepoly.Spec) { + if !apiequality.Semantic.DeepEqual(desiredDeploy.Spec, runtimeDepoly.Spec) { logUpdateInfo(runtimeObj) return d.client.Update(ctx, desiredDeploy) } @@ -123,8 +123,8 @@ func (d *Deployer) updateStatefulSet(ctx context.Context, desiredObj, runtimeObj return err } - if !apiequality.Semantic.DeepDerivative(desiredDepoly.Spec.Template, runtimeDepoly.Spec.Template) || - !apiequality.Semantic.DeepDerivative(desiredDepoly.Spec.Replicas, runtimeDepoly.Spec.Replicas) { + if !apiequality.Semantic.DeepEqual(desiredDepoly.Spec.Template, runtimeDepoly.Spec.Template) || + !apiequality.Semantic.DeepEqual(desiredDepoly.Spec.Replicas, runtimeDepoly.Spec.Replicas) { logUpdateInfo(runtimeObj) runtimeDepoly.Spec.Replicas = desiredDepoly.Spec.Replicas runtimeDepoly.Spec.Template = desiredDepoly.Spec.Template @@ -140,7 +140,7 @@ func (d *Deployer) updateService(ctx context.Context, desiredObj, runtimeObj *un return err } - if !apiequality.Semantic.DeepDerivative(desiredService.Spec, runtimeService.Spec) { + if !apiequality.Semantic.DeepEqual(desiredService.Spec, runtimeService.Spec) { desiredService.ObjectMeta.ResourceVersion = runtimeService.ObjectMeta.ResourceVersion desiredService.Spec.ClusterIP = runtimeService.Spec.ClusterIP logUpdateInfo(runtimeObj) @@ -156,7 +156,7 @@ func (d *Deployer) updateConfigMap(ctx context.Context, desiredObj, runtimeObj * return err } - if !apiequality.Semantic.DeepDerivative(desiredConfigMap.Data, runtimeConfigMap.Data) { + if !apiequality.Semantic.DeepEqual(desiredConfigMap.Data, runtimeConfigMap.Data) { logUpdateInfo(runtimeObj) return d.client.Update(ctx, desiredConfigMap) } @@ -171,7 +171,7 @@ func (d *Deployer) updateSecret(ctx context.Context, desiredObj, runtimeObj *uns } if desiredSecret.Data == nil || - !apiequality.Semantic.DeepDerivative(desiredSecret.Data, runtimeSecret.Data) { + !apiequality.Semantic.DeepEqual(desiredSecret.Data, runtimeSecret.Data) { logUpdateInfo(desiredObj) return d.client.Update(ctx, desiredSecret) } @@ -184,8 +184,8 @@ func (d *Deployer) updateClusterRole(ctx context.Context, desiredObj, runtimeObj return err } - if !apiequality.Semantic.DeepDerivative(desiredClusterRole.Rules, runtimeClusterRole.Rules) || - !apiequality.Semantic.DeepDerivative(desiredClusterRole.AggregationRule, runtimeClusterRole.AggregationRule) { + if !apiequality.Semantic.DeepEqual(desiredClusterRole.Rules, runtimeClusterRole.Rules) || + !apiequality.Semantic.DeepEqual(desiredClusterRole.AggregationRule, runtimeClusterRole.AggregationRule) { logUpdateInfo(desiredObj) return d.client.Update(ctx, desiredClusterRole) } @@ -198,8 +198,8 @@ func (d *Deployer) updateClusterRoleBinding(ctx context.Context, desiredObj, run return err } - if !apiequality.Semantic.DeepDerivative(desiredClusterRoleBinding.Subjects, runtimeClusterRoleBinding.Subjects) || - !apiequality.Semantic.DeepDerivative(desiredClusterRoleBinding.RoleRef, runtimeClusterRoleBinding.RoleRef) { + if !apiequality.Semantic.DeepEqual(desiredClusterRoleBinding.Subjects, runtimeClusterRoleBinding.Subjects) || + !apiequality.Semantic.DeepEqual(desiredClusterRoleBinding.RoleRef, runtimeClusterRoleBinding.RoleRef) { logUpdateInfo(desiredObj) return d.client.Update(ctx, desiredClusterRoleBinding) } @@ -214,7 +214,7 @@ func (d *Deployer) updateCRD(ctx context.Context, desiredObj, runtimeObj *unstru desiredCRD.ObjectMeta.ResourceVersion = runtimeCRD.ObjectMeta.ResourceVersion - if !apiequality.Semantic.DeepDerivative(desiredCRD.Spec, runtimeCRD.Spec) { + if !apiequality.Semantic.DeepEqual(desiredCRD.Spec, runtimeCRD.Spec) { logUpdateInfo(runtimeObj) return d.client.Update(ctx, desiredCRD) } @@ -254,7 +254,7 @@ func (d *Deployer) updatePrometheus(ctx context.Context, desiredObj, runtimeObj log.Info("Desired Prometheus: AdditionalAlertManagerConfig is null") } - if !apiequality.Semantic.DeepDerivative(desiredPrometheus.Spec, runtimePrometheus.Spec) { + if !apiequality.Semantic.DeepEqual(desiredPrometheus.Spec, runtimePrometheus.Spec) { logUpdateInfo(runtimeObj) return d.client.Update(ctx, desiredPrometheus) } else { @@ -269,7 +269,7 @@ func (d *Deployer) updatePrometheusRule(ctx context.Context, desiredObj, runtime return err } - if !apiequality.Semantic.DeepDerivative(desiredPrometheusRule.Spec, runtimePrometheusRule.Spec) { + if !apiequality.Semantic.DeepEqual(desiredPrometheusRule.Spec, runtimePrometheusRule.Spec) { logUpdateInfo(runtimeObj) if desiredPrometheusRule.ResourceVersion != runtimePrometheusRule.ResourceVersion { desiredPrometheusRule.ResourceVersion = runtimePrometheusRule.ResourceVersion @@ -286,7 +286,7 @@ func (d *Deployer) updateIngress(ctx context.Context, desiredObj, runtimeObj *un return err } - if !apiequality.Semantic.DeepDerivative(desiredIngress.Spec, runtimeIngress.Spec) { + if !apiequality.Semantic.DeepEqual(desiredIngress.Spec, runtimeIngress.Spec) { logUpdateInfo(runtimeObj) return d.client.Update(ctx, desiredIngress) } @@ -300,7 +300,7 @@ func (d *Deployer) updateRole(ctx context.Context, desiredObj, runtimeObj *unstr return err } - if !apiequality.Semantic.DeepDerivative(desiredRole.Rules, runtimeRole.Rules) { + if !apiequality.Semantic.DeepEqual(desiredRole.Rules, runtimeRole.Rules) { logUpdateInfo(runtimeObj) return d.client.Update(ctx, desiredRole) } @@ -314,8 +314,8 @@ func (d *Deployer) updateRoleBinding(ctx context.Context, desiredObj, runtimeObj return err } - if !apiequality.Semantic.DeepDerivative(desiredRoleBinding.Subjects, runtimeRoleBinding.Subjects) || - !apiequality.Semantic.DeepDerivative(desiredRoleBinding.RoleRef, runtimeRoleBinding.RoleRef) { + if !apiequality.Semantic.DeepEqual(desiredRoleBinding.Subjects, runtimeRoleBinding.Subjects) || + !apiequality.Semantic.DeepEqual(desiredRoleBinding.RoleRef, runtimeRoleBinding.RoleRef) { logUpdateInfo(runtimeObj) return d.client.Update(ctx, desiredRoleBinding) } @@ -329,8 +329,8 @@ func (d *Deployer) updateServiceAccount(ctx context.Context, desiredObj, runtime return err } - if !apiequality.Semantic.DeepDerivative(desiredServiceAccount.ImagePullSecrets, runtimeServiceAccount.ImagePullSecrets) || - !apiequality.Semantic.DeepDerivative(desiredServiceAccount.Secrets, runtimeServiceAccount.Secrets) { + if !apiequality.Semantic.DeepEquale(desiredServiceAccount.ImagePullSecrets, runtimeServiceAccount.ImagePullSecrets) || + !apiequality.Semantic.DeepEqual(desiredServiceAccount.Secrets, runtimeServiceAccount.Secrets) { logUpdateInfo(runtimeObj) return d.client.Update(ctx, desiredServiceAccount) } @@ -344,7 +344,7 @@ func (d *Deployer) updateDaemonSet(ctx context.Context, desiredObj, runtimeObj * return err } - if !apiequality.Semantic.DeepDerivative(desiredDaemonSet.Spec, runtimeDaemonSet.Spec) { + if !apiequality.Semantic.DeepEqual(desiredDaemonSet.Spec, runtimeDaemonSet.Spec) { logUpdateInfo(runtimeObj) return d.client.Update(ctx, desiredDaemonSet) } @@ -358,7 +358,7 @@ func (d *Deployer) updateServiceMonitor(ctx context.Context, desiredObj, runtime return err } - if !apiequality.Semantic.DeepDerivative(desiredServiceMonitor.Spec, runtimeServiceMonitor.Spec) { + if !apiequality.Semantic.DeepEqual(desiredServiceMonitor.Spec, runtimeServiceMonitor.Spec) { logUpdateInfo(runtimeObj) return d.client.Update(ctx, desiredServiceMonitor) } From 771f87df77c9ab8daebbd23c161528ea30efadc4 Mon Sep 17 00:00:00 2001 From: Coleen Iona Quadros Date: Tue, 12 Nov 2024 13:44:20 +0100 Subject: [PATCH 2/3] use deep equal Signed-off-by: Coleen Iona Quadros --- .../controllers/observabilityendpoint/ocp_monitoring_config.go | 2 +- operators/pkg/deploying/deployer.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/operators/endpointmetrics/controllers/observabilityendpoint/ocp_monitoring_config.go b/operators/endpointmetrics/controllers/observabilityendpoint/ocp_monitoring_config.go index 0980dcebb..06d4e857b 100644 --- a/operators/endpointmetrics/controllers/observabilityendpoint/ocp_monitoring_config.go +++ b/operators/endpointmetrics/controllers/observabilityendpoint/ocp_monitoring_config.go @@ -422,7 +422,7 @@ func createOrUpdateClusterMonitoringConfig( newCMOCfg.PrometheusK8sConfig = newPmK8sConfig } - if equality.Semantic.Deep(*foundClusterMonitoringConfiguration, *newCMOCfg) { + if equality.Semantic.DeepEqual(*foundClusterMonitoringConfiguration, *newCMOCfg) { return nil } diff --git a/operators/pkg/deploying/deployer.go b/operators/pkg/deploying/deployer.go index 496467912..c8040445f 100644 --- a/operators/pkg/deploying/deployer.go +++ b/operators/pkg/deploying/deployer.go @@ -329,7 +329,7 @@ func (d *Deployer) updateServiceAccount(ctx context.Context, desiredObj, runtime return err } - if !apiequality.Semantic.DeepEquale(desiredServiceAccount.ImagePullSecrets, runtimeServiceAccount.ImagePullSecrets) || + if !apiequality.Semantic.DeepEqual(desiredServiceAccount.ImagePullSecrets, runtimeServiceAccount.ImagePullSecrets) || !apiequality.Semantic.DeepEqual(desiredServiceAccount.Secrets, runtimeServiceAccount.Secrets) { logUpdateInfo(runtimeObj) return d.client.Update(ctx, desiredServiceAccount) From 5b5ae020868194603ef6422a13bf4bfd1816de9e Mon Sep 17 00:00:00 2001 From: Coleen Iona Quadros Date: Wed, 13 Nov 2024 10:54:56 +0100 Subject: [PATCH 3/3] update Signed-off-by: Coleen Iona Quadros --- .../controllers/observabilityendpoint/ocp_monitoring_config.go | 1 + operators/endpointmetrics/pkg/collector/metrics_collector.go | 2 ++ 2 files changed, 3 insertions(+) diff --git a/operators/endpointmetrics/controllers/observabilityendpoint/ocp_monitoring_config.go b/operators/endpointmetrics/controllers/observabilityendpoint/ocp_monitoring_config.go index 06d4e857b..8218e044c 100644 --- a/operators/endpointmetrics/controllers/observabilityendpoint/ocp_monitoring_config.go +++ b/operators/endpointmetrics/controllers/observabilityendpoint/ocp_monitoring_config.go @@ -10,6 +10,7 @@ import ( "net/url" "reflect" + "gopkg.in/yaml.v2" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/equality" "k8s.io/apimachinery/pkg/api/errors" diff --git a/operators/endpointmetrics/pkg/collector/metrics_collector.go b/operators/endpointmetrics/pkg/collector/metrics_collector.go index d028e0c5f..bc820984f 100644 --- a/operators/endpointmetrics/pkg/collector/metrics_collector.go +++ b/operators/endpointmetrics/pkg/collector/metrics_collector.go @@ -13,7 +13,9 @@ import ( "strings" "time" + "github.com/go-logr/logr" monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" + "gopkg.in/yaml.v2" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/equality"