Skip to content

Commit

Permalink
refactor: Add the IsObjectLabelsEqual function to check the object …
Browse files Browse the repository at this point in the history
…labels are equal (#224)

* refactor: Add the IsObjectLabelsEqual function to check if the object labels are equal

* chore: change logs
  • Loading branch information
daviderli614 authored Nov 27, 2024
1 parent 90a4b73 commit 6c905bf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
8 changes: 5 additions & 3 deletions pkg/deployer/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,15 @@ func (d *DefaultDeployer) Apply(ctx context.Context, _ client.Object, objects []
}

if oldObject != nil {
equal, err := k8sutils.IsObjectSpecEqual(oldObject, newObject, LastAppliedResourceSpec)
specEqual, err := k8sutils.IsObjectSpecEqual(oldObject, newObject, LastAppliedResourceSpec)
if err != nil {
return err
}

// If the spec is not equal, update the object.
if !equal {
labelsEqual := k8sutils.IsObjectLabelsEqual(oldObject.GetLabels(), newObject.GetLabels())

// If the spec or labels is not equal, update the object.
if !specEqual || !labelsEqual {
newObject.SetResourceVersion(oldObject.GetResourceVersion())
if err := d.Client.Patch(ctx, newObject, client.MergeFrom(oldObject)); err != nil {
return err
Expand Down
27 changes: 16 additions & 11 deletions pkg/util/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,35 @@ func CreateObjectIfNotExist(ctx context.Context, c client.Client, source, newObj
}

// IsObjectSpecEqual checks if the spec of the object is equal to other one.
func IsObjectSpecEqual(objectA, objectB client.Object, annotationKey string) (bool, error) {
objectASpecStr, ok := objectA.GetAnnotations()[annotationKey]
func IsObjectSpecEqual(oldObject, newObject client.Object, annotationKey string) (bool, error) {
oldObjectSpecStr, ok := oldObject.GetAnnotations()[annotationKey]
if !ok {
return false, fmt.Errorf("the objectA object '%s' does not have annotation '%s'",
client.ObjectKeyFromObject(objectA), annotationKey)
return false, fmt.Errorf("the old object '%s' does not have annotation '%s'",
client.ObjectKeyFromObject(oldObject), annotationKey)
}

objectBSpecStr, ok := objectB.GetAnnotations()[annotationKey]
newObjectSpecStr, ok := newObject.GetAnnotations()[annotationKey]
if !ok {
return false, fmt.Errorf("the objectB object '%s' does not have annotation '%s'",
client.ObjectKeyFromObject(objectB), annotationKey)
return false, fmt.Errorf("the new object '%s' does not have annotation '%s'",
client.ObjectKeyFromObject(newObject), annotationKey)
}

var objectASpec, objectBSpec interface{}
var oldObjectSpec, newObjectSpec interface{}

if err := json.Unmarshal([]byte(objectASpecStr), &objectASpec); err != nil {
if err := json.Unmarshal([]byte(oldObjectSpecStr), &oldObjectSpec); err != nil {
return false, err
}

if err := json.Unmarshal([]byte(objectBSpecStr), &objectBSpec); err != nil {
if err := json.Unmarshal([]byte(newObjectSpecStr), &newObjectSpec); err != nil {
return false, err
}

return reflect.DeepEqual(objectASpec, objectBSpec), nil
return reflect.DeepEqual(oldObjectSpec, newObjectSpec), nil
}

// IsObjectLabelsEqual checks if the labels of the object is equal to other one.
func IsObjectLabelsEqual(oldObjectLabels, newObjectLabels map[string]string) bool {
return reflect.DeepEqual(oldObjectLabels, newObjectLabels)
}

// IsDeploymentReady checks if the deployment is ready.
Expand Down

0 comments on commit 6c905bf

Please sign in to comment.