diff --git a/internal/infrastructure/kubernetes/infra_client.go b/internal/infrastructure/kubernetes/infra_client.go index 7c6fd2353f3..dfab1d22151 100644 --- a/internal/infrastructure/kubernetes/infra_client.go +++ b/internal/infrastructure/kubernetes/infra_client.go @@ -7,7 +7,7 @@ package kubernetes import ( "context" - + kerrors "k8s.io/apimachinery/pkg/api/errors" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -22,7 +22,7 @@ func New(cli client.Client) *InfraClient { } } -func (cli *InfraClient) Create(ctx context.Context, key client.ObjectKey, current client.Object, specific client.Object) error { +func (cli *InfraClient) Create(ctx context.Context, key client.ObjectKey, current client.Object, specific client.Object, updateChecker func() bool) error { if err := cli.Client.Get(ctx, key, current); err != nil { if kerrors.IsNotFound(err) { // Create if it does not exist. @@ -33,13 +33,15 @@ func (cli *InfraClient) Create(ctx context.Context, key client.ObjectKey, curren } else { // Since the ServiceAccount does not have a specific Spec field to compare // just perform an update for now. - specific.SetResourceVersion(current.GetResourceVersion()) - specific.SetUID(current.GetUID()) - if err := cli.Client.Update(ctx, specific); err != nil { - return err + if updateChecker() { + specific.SetResourceVersion(current.GetResourceVersion()) + specific.SetUID(current.GetUID()) + if err := cli.Client.Update(ctx, specific); err != nil { + return err + } } } - + return nil } @@ -50,6 +52,6 @@ func (cli *InfraClient) Delete(ctx context.Context, object client.Object) error } return err } - + return nil } diff --git a/internal/infrastructure/kubernetes/infra_resource.go b/internal/infrastructure/kubernetes/infra_resource.go index 876db105756..798d5650bd6 100644 --- a/internal/infrastructure/kubernetes/infra_resource.go +++ b/internal/infrastructure/kubernetes/infra_resource.go @@ -7,6 +7,7 @@ package kubernetes import ( "context" + "reflect" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" @@ -28,7 +29,9 @@ func (i *Infra) createOrUpdateServiceAccount(ctx context.Context, r ResourceRend Name: sa.Name, } - return i.Client.Create(ctx, key, current, sa) + return i.Client.Create(ctx, key, current, sa, func() bool { + return true + }) } // createOrUpdateConfigMap creates a ConfigMap in the Kube api server based on the provided @@ -45,7 +48,9 @@ func (i *Infra) createOrUpdateConfigMap(ctx context.Context, r ResourceRender) e Name: cm.Name, } - return i.Client.Create(ctx, key, current, cm) + return i.Client.Create(ctx, key, current, cm, func() bool { + return !reflect.DeepEqual(cm.Data, current.Data) + }) } // createOrUpdateDeployment creates a Deployment in the kube api server based on the provided @@ -62,7 +67,9 @@ func (i *Infra) createOrUpdateDeployment(ctx context.Context, r ResourceRender) Name: deployment.Name, } - return i.Client.Create(ctx, key, current, deployment) + return i.Client.Create(ctx, key, current, deployment, func() bool { + return !reflect.DeepEqual(deployment.Spec, current.Spec) + }) } // createOrUpdateRateLimitService creates a Service in the kube api server based on the provided ResourceRender, @@ -79,7 +86,9 @@ func (i *Infra) createOrUpdateService(ctx context.Context, r ResourceRender) err Name: svc.Name, } - return i.Client.Create(ctx, key, current, svc) + return i.Client.Create(ctx, key, current, svc, func() bool { + return !reflect.DeepEqual(svc.Spec, current.Spec) + }) } // deleteServiceAccount deletes the ServiceAccount in the kube api server, if it exists.