Skip to content

Commit

Permalink
add infra resource update check logic
Browse files Browse the repository at this point in the history
Signed-off-by: qicz <qiczzhu@gmail.com>
  • Loading branch information
qicz committed Apr 24, 2023
1 parent ab36e76 commit 5fca0b3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
18 changes: 10 additions & 8 deletions internal/infrastructure/kubernetes/infra_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package kubernetes

import (
"context"

kerrors "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand All @@ -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.
Expand All @@ -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
}

Expand All @@ -50,6 +52,6 @@ func (cli *InfraClient) Delete(ctx context.Context, object client.Object) error
}
return err
}

return nil
}
17 changes: 13 additions & 4 deletions internal/infrastructure/kubernetes/infra_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package kubernetes

import (
"context"
"reflect"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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.
Expand Down

0 comments on commit 5fca0b3

Please sign in to comment.