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

Only run update logic if objects are actually different #160

Merged
merged 1 commit into from
May 24, 2019
Merged
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
15 changes: 7 additions & 8 deletions pkg/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/pkg/errors"

"k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
uns "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -52,14 +53,12 @@ func ApplyObject(ctx context.Context, client k8sclient.Client, obj *uns.Unstruct
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)
}
if existing.GetResourceVersion() == obj.GetResourceVersion() {
log.Printf("update was noop")
} else {
log.Printf("update was successful")
if !equality.Semantic.DeepEqual(existing, obj) {
if err := client.Update(ctx, obj); err != nil {
return errors.Wrapf(err, "could not update object %s", objDesc)
} else {
log.Printf("update was successful")
}
}

return nil
Expand Down
23 changes: 18 additions & 5 deletions pkg/apply/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,26 @@ import (
uns "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

// MergeMetadataForUpdate merges the read-only fields of metadata.
// This is to be able to do a a meaningful comparison in apply,
// since objects created on runtime do not have these fields populated.
func MergeMetadataForUpdate(current, updated *uns.Unstructured) error {
squeed marked this conversation as resolved.
Show resolved Hide resolved
updated.SetCreationTimestamp(current.GetCreationTimestamp())
updated.SetSelfLink(current.GetSelfLink())
updated.SetGeneration(current.GetGeneration())
updated.SetUID(current.GetUID())
updated.SetResourceVersion(current.GetResourceVersion())

mergeAnnotations(current, updated)
mergeLabels(current, updated)

return nil
}

// MergeObjectForUpdate prepares a "desired" object to be updated.
// Some objects, such as Deployments and Services require
// some semantic-aware updates
func MergeObjectForUpdate(current, updated *uns.Unstructured) error {
updated.SetResourceVersion(current.GetResourceVersion())

if err := MergeDeploymentForUpdate(current, updated); err != nil {
return err
}
Expand All @@ -24,11 +38,10 @@ func MergeObjectForUpdate(current, updated *uns.Unstructured) error {
return err
}

// For all object types, merge annotations.
// For all object types, merge metadata.
// Run this last, in case any of the more specific merge logic has
// changed "updated"
mergeAnnotations(current, updated)
mergeLabels(current, updated)
MergeMetadataForUpdate(current, updated)

return nil
}
Expand Down