-
Notifications
You must be signed in to change notification settings - Fork 50
Enable deep JSON merging for Kubernetes resources when updating them #1019
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f23382c
enable deep json merge for k8s resources
73450a4
fix deep merge for resource spec
65e5ee8
merge for all resrources type
3a4a1a1
each changed object updated once
9705c95
compare whole resrouces file instead of spec section
b59fd58
set ownerreferences
2e86588
ensure ann, label and set owner on updated resrouce
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -771,12 +771,12 @@ func (r *Reconciler) updateCustomResource(ctx context.Context, existingCR unstru | |
| // Merge spec from update existing CR and OperandConfig spec | ||
| updatedCRSpec := util.MergeCR(updatedExistingCRRaw, crConfig) | ||
|
|
||
| CRgeneration := existingCR.GetGeneration() | ||
|
|
||
| if reflect.DeepEqual(existingCR.Object["spec"], updatedCRSpec) && !forceUpdate { | ||
| return true, nil | ||
| } | ||
|
|
||
| CRgeneration := existingCR.GetGeneration() | ||
|
|
||
| klog.V(2).Infof("updating custom resource with apiversion: %s, kind: %s, %s/%s", apiversion, kind, namespace, name) | ||
|
|
||
| existingCR.Object["spec"] = updatedCRSpec | ||
|
|
@@ -1057,61 +1057,66 @@ func (r *Reconciler) updateK8sResource(ctx context.Context, existingK8sRes unstr | |
| return true, nil | ||
| } | ||
|
|
||
| // isEqual := r.CheckAnnotation(existingK8sRes, newAnnotations) && r.CheckLabel(existingK8sRes, newLabels) | ||
| if k8sResConfig != nil { | ||
|
|
||
| k8sResConfigDecoded := make(map[string]interface{}) | ||
| k8sResConfigUnmarshalErr := json.Unmarshal(k8sResConfig.Raw, &k8sResConfigDecoded) | ||
| if k8sResConfigUnmarshalErr != nil { | ||
| klog.Errorf("failed to unmarshal k8s Resource Config: %v", k8sResConfigUnmarshalErr) | ||
| } | ||
|
|
||
| for k, v := range k8sResConfigDecoded { | ||
| // isEqual = isEqual && reflect.DeepEqual(existingK8sRes.Object[k], v) | ||
| existingK8sRes.Object[k] = v | ||
| // Convert existing k8s resource spec to string | ||
| existingK8sResRaw, err := json.Marshal(existingK8sRes.Object) | ||
| if err != nil { | ||
| klog.Error(err) | ||
| return false, err | ||
| } | ||
| } | ||
|
|
||
| CRgeneration := existingK8sRes.GetGeneration() | ||
| // Merge spec from existing CR and OperandConfig spec | ||
| updatedExistingK8sRes := util.MergeCR(existingK8sResRaw, k8sResConfig.Raw) | ||
|
|
||
| // if isEqual { | ||
| // return true, nil | ||
| // } | ||
| r.EnsureAnnotation(existingK8sRes, newAnnotations) | ||
| r.EnsureLabel(existingK8sRes, newLabels) | ||
| if err := r.setOwnerReferences(ctx, &existingK8sRes, ownerReferences); err != nil { | ||
| return false, errors.Wrapf(err, "failed to set ownerReferences for k8s resource -- Kind: %s, NamespacedName: %s/%s", kind, namespace, name) | ||
| } | ||
|
|
||
| r.EnsureAnnotation(existingK8sRes, newAnnotations) | ||
| r.EnsureLabel(existingK8sRes, newLabels) | ||
| if err := r.setOwnerReferences(ctx, &existingK8sRes, ownerReferences); err != nil { | ||
| return false, errors.Wrapf(err, "failed to set ownerReferences for k8s resource -- Kind: %s, NamespacedName: %s/%s", kind, namespace, name) | ||
| } | ||
| if reflect.DeepEqual(existingK8sRes.Object, updatedExistingK8sRes) { | ||
| return true, nil | ||
| } | ||
| klog.Infof("updating k8s resource with apiversion: %s, kind: %s, %s/%s", apiversion, kind, namespace, name) | ||
|
|
||
| klog.V(2).Infof("updating k8s resource with apiversion: %s, kind: %s, %s/%s", apiversion, kind, namespace, name) | ||
| existingK8sRes.Object = updatedExistingK8sRes | ||
|
||
|
|
||
| err = r.Update(ctx, &existingK8sRes) | ||
| CRgeneration := existingK8sRes.GetGeneration() | ||
|
|
||
| if err != nil { | ||
| return false, errors.Wrapf(err, "failed to update k8s resource -- Kind: %s, NamespacedName: %s/%s", kind, namespace, name) | ||
| } | ||
| err = r.Update(ctx, &existingK8sRes) | ||
|
|
||
| UpdatedK8sRes := unstructured.Unstructured{ | ||
| Object: map[string]interface{}{ | ||
| "apiVersion": apiversion, | ||
| "kind": kind, | ||
| }, | ||
| } | ||
| if err != nil { | ||
| return false, errors.Wrapf(err, "failed to update k8s resource -- Kind: %s, NamespacedName: %s/%s", kind, namespace, name) | ||
| } | ||
|
|
||
| err = r.Client.Get(ctx, types.NamespacedName{ | ||
| Name: name, | ||
| Namespace: namespace, | ||
| }, &UpdatedK8sRes) | ||
| UpdatedK8sRes := unstructured.Unstructured{ | ||
| Object: map[string]interface{}{ | ||
| "apiVersion": apiversion, | ||
| "kind": kind, | ||
| }, | ||
| } | ||
|
|
||
| if err != nil { | ||
| return false, errors.Wrapf(err, "failed to get k8s resource -- Kind: %s, NamespacedName: %s/%s", kind, namespace, name) | ||
| err = r.Client.Get(ctx, types.NamespacedName{ | ||
| Name: name, | ||
| Namespace: namespace, | ||
| }, &UpdatedK8sRes) | ||
|
|
||
| } | ||
| if err != nil { | ||
| return false, errors.Wrapf(err, "failed to get k8s resource -- Kind: %s, NamespacedName: %s/%s", kind, namespace, name) | ||
|
|
||
| if UpdatedK8sRes.GetGeneration() != CRgeneration { | ||
| klog.V(2).Infof("Finish updating the k8s Resource: -- Kind: %s, NamespacedName: %s/%s", kind, namespace, name) | ||
| } | ||
| } | ||
|
|
||
| if UpdatedK8sRes.GetGeneration() != CRgeneration { | ||
| klog.Infof("Finish updating the k8s Resource: -- Kind: %s, NamespacedName: %s/%s", kind, namespace, name) | ||
| } | ||
| } | ||
| return true, nil | ||
| }) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we could remove this section.