Skip to content

Commit

Permalink
Backup metadata fields before compare
Browse files Browse the repository at this point in the history
Generated object won't have them, thus the compare will always be false
but that doesn't mean that semantically they are different.
  • Loading branch information
rcarrillocruz committed May 3, 2019
1 parent 24da05a commit 9d5863c
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions pkg/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package apply

import (
"context"
"encoding/json"
"fmt"
"log"

"github.com/pkg/errors"

"k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
uns "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
// diff "k8s.io/apimachinery/pkg/util/diff"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -50,15 +51,24 @@ func ApplyObject(ctx context.Context, client k8sclient.Client, obj *uns.Unstruct
return errors.Wrapf(err, "could not retrieve existing %s", objDesc)
}

// Merge the desired object with what actually exists
if err := MergeObjectForUpdate(existing, obj); err != nil {
return errors.Wrapf(err, "could not merge object %s with existing", objDesc)
}
if !equality.Semantic.DeepEqual(existing, obj) {
marshaled_existing, _ := json.MarshalIndent(existing, "", " ")
log.Printf(string(marshaled_existing))
marshaled_merged, _ := json.MarshalIndent(obj, "", " ")
log.Printf(string(marshaled_merged))
creationTimeStamp := existing.GetCreationTimestamp()
existing.SetCreationTimestamp(metav1.Time{})
selfLink := existing.GetSelfLink()
existing.SetSelfLink("")
uid := existing.GetUID()
existing.SetUID(types.UID(""))
resourceVersion := existing.GetResourceVersion()
existing.SetResourceVersion("")

if !equality.Semantic.DeepDerivative(existing, obj) {
// Merge the desired object with what actually exists
existing.SetCreationTimestamp(creationTimeStamp)
existing.SetSelfLink(selfLink)
existing.SetUID(uid)
existing.SetResourceVersion(resourceVersion)
if err := MergeObjectForUpdate(existing, obj); err != nil {
return errors.Wrapf(err, "could not merge object %s with existing", objDesc)
}
if err := client.Update(ctx, obj); err != nil {
return errors.Wrapf(err, "could not update object %s", objDesc)
} else {
Expand Down

0 comments on commit 9d5863c

Please sign in to comment.