Skip to content

Commit

Permalink
[CPS-427] fix(upgrade): skip update if manifest is not changed (#176)
Browse files Browse the repository at this point in the history
* fix(upgrade): skip update if manifest is not changed

* refactor(*): simplify code
  • Loading branch information
Lei Qian authored Nov 8, 2020
1 parent 36673b8 commit 5932bf0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
28 changes: 24 additions & 4 deletions pkg/kube/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Client interface {
// Get gets the current object by resources.
Get(namespace string, resources []string, options GetOptions) ([]runtime.Object, error)
// Apply creates/updates all these resources.
Apply(namespace string, resources []string, options ApplyOptions) error
Apply(namespace string, newResources, oldResources []string, options ApplyOptions) error
// Create creates all these resources.
Create(namespace string, resources []string, options CreateOptions) error
// Update updates all resources.
Expand Down Expand Up @@ -127,17 +127,37 @@ func (c *client) Get(namespace string, resources []string, options GetOptions) (
}

// Apply creates/updates all these resources.
func (c *client) Apply(namespace string, resources []string, options ApplyOptions) error {
objs, err := c.objectsByOrder(resources, InstallOrder)
func (c *client) Apply(namespace string, newResources, oldResources []string, options ApplyOptions) error {
newObjects, err := c.objectsByOrder(newResources, InstallOrder)
if err != nil {
return err
}
for _, obj := range objs {
oldObjects, err := c.objectsByOrder(oldResources, InstallOrder)
if err != nil {
return err
}
for _, obj := range newObjects {

gvk := obj.GetObjectKind().GroupVersionKind()
accessor, err := c.codec.AccessorForObject(obj)
if err != nil {
return err
}

if namespace != "default" && namespace != "kube-system" {
skipped := false
for _, each := range oldObjects {
if reflect.DeepEqual(obj, each) {
skipped = true
break
}
}
if skipped {
glog.Infoln(gvk.Kind, namespace, accessor.GetName(), "same manifest, skip update")
continue
}
}

if options.OwnerReferences != nil &&
// options.Checker is used to check if the object is belong to current owner.
// If not, add owner references to obj.
Expand Down
3 changes: 2 additions & 1 deletion pkg/release/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
func (rc *releaseContext) applyRelease(backend storage.ReleaseStorage, release *releaseapi.Release) error {
// Deep copy release. Avoid modifying original release.
release = release.DeepCopy()
oldManifests := render.SplitManifest(release.Status.Manifest)

var manifests []string
var postUpdate bool
Expand Down Expand Up @@ -115,7 +116,7 @@ func (rc *releaseContext) applyRelease(backend storage.ReleaseStorage, release *
// FIXME: when the number of failure larger than 3 which set int function handler, the resource will apply failed and the
// resource can not be consistent with the Spec.Config
// Apply resources.
if err := rc.client.Apply(release.Namespace, manifests, kube.ApplyOptions{
if err := rc.client.Apply(release.Namespace, manifests, oldManifests, kube.ApplyOptions{
OwnerReferences: referencesForRelease(release),
Checker: rc.ignore,
}); err != nil {
Expand Down

0 comments on commit 5932bf0

Please sign in to comment.