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

Use semantic.DeepEqual for Kubernetes Obj comparison #1668

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 @@ -10,14 +10,14 @@ 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"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"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"
)
Expand Down
8 changes: 4 additions & 4 deletions operators/endpointmetrics/pkg/collector/metrics_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,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
Expand Down Expand Up @@ -407,7 +407,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
Expand Down Expand Up @@ -493,7 +493,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
Expand Down Expand Up @@ -759,7 +759,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)
Expand Down
42 changes: 21 additions & 21 deletions operators/pkg/deploying/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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.DeepEqual(desiredServiceAccount.ImagePullSecrets, runtimeServiceAccount.ImagePullSecrets) ||
!apiequality.Semantic.DeepEqual(desiredServiceAccount.Secrets, runtimeServiceAccount.Secrets) {
logUpdateInfo(runtimeObj)
return d.client.Update(ctx, desiredServiceAccount)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
Loading