Skip to content

Instantly share code, notes, and snippets.

@ncdc
Created September 11, 2020 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ncdc/df100aea6dcd817b3811efae7b110bd2 to your computer and use it in GitHub Desktop.
Save ncdc/df100aea6dcd817b3811efae7b110bd2 to your computer and use it in GitHub Desktop.
diff --git a/util/patch/patch.go b/util/patch/patch.go
index e0c2b65b2..78c42ebb2 100644
--- a/util/patch/patch.go
+++ b/util/patch/patch.go
@@ -37,8 +37,8 @@ import (
type Helper struct {
client client.Client
beforeObject runtime.Object
- before *unstructured.Unstructured
- after *unstructured.Unstructured
+ before runtime.Unstructured
+ after runtime.Unstructured
changes map[string]bool
isConditionsSetter bool
@@ -94,12 +94,17 @@ func (h *Helper) Patch(ctx context.Context, obj runtime.Object, opts ...Option)
if unstructuredHasStatus(h.after) {
if options.IncludeStatusObservedGeneration {
// Set status.observedGeneration if we're asked to do so.
- if err := unstructured.SetNestedField(h.after.Object, h.after.GetGeneration(), "status", "observedGeneration"); err != nil {
+
+ // Can ignore found and err, as we'll get back 0 if !found or err != nil, which matches the behavior of
+ // unstructured.Unstructured#GetGeneration()
+ generation, _, _ := unstructured.NestedInt64(h.after.UnstructuredContent(), "metadata", "generation")
+
+ if err := unstructured.SetNestedField(h.after.UnstructuredContent(), generation, "status", "observedGeneration"); err != nil {
return err
}
// Restore the changes back to the original object.
- if err := runtime.DefaultUnstructuredConverter.FromUnstructured(h.after.Object, obj); err != nil {
+ if err := runtime.DefaultUnstructuredConverter.FromUnstructured(h.after.UnstructuredContent(), obj); err != nil {
return err
}
}
@@ -233,8 +238,8 @@ func (h *Helper) patchStatusConditions(ctx context.Context, obj runtime.Object,
// calculatePatch returns the before/after objects to be given in a controller-runtime patch, scoped down to the absolute necessary.
func (h *Helper) calculatePatch(afterObj runtime.Object, focus patchType) (runtime.Object, runtime.Object, error) {
// Make a copy of the unstructured objects first.
- before := h.before.DeepCopy()
- after := h.after.DeepCopy()
+ before := (&unstructured.Unstructured{Object: h.before.UnstructuredContent()}).DeepCopy()
+ after := (&unstructured.Unstructured{Object: h.after.UnstructuredContent()}).DeepCopy()
// Let's loop on the copies of our before/after and remove all the keys we don't need.
for _, v := range []*unstructured.Unstructured{before, after} {
@@ -260,10 +265,14 @@ func (h *Helper) calculatePatch(afterObj runtime.Object, focus patchType) (runti
// We've now applied all modifications to local unstructured objects,
// make copies of the original objects and convert them back.
+ // TODO: it would be better to instantiate a new variable using reflection whose type matches h.beforeObject, to
+ // avoid deep copying data that we'll immediately throw away when we call FromUnstructured.
beforeObj := h.beforeObject.DeepCopyObject()
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(before.Object, beforeObj); err != nil {
return nil, nil, err
}
+ // TODO: it would be better to instantiate a new variable using reflection whose type matches h.beforeObject, to
+ // avoid deep copying data that we'll immediately throw away when we call FromUnstructured.
afterObj = afterObj.DeepCopyObject()
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(after.Object, afterObj); err != nil {
return nil, nil, err
diff --git a/util/patch/utils.go b/util/patch/utils.go
index 0d8fd1d47..2557092cb 100644
--- a/util/patch/utils.go
+++ b/util/patch/utils.go
@@ -19,7 +19,7 @@ package patch
import (
"strings"
- "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
+ "k8s.io/apimachinery/pkg/runtime"
)
type patchType string
@@ -41,7 +41,7 @@ var (
}
)
-func unstructuredHasStatus(u *unstructured.Unstructured) bool {
- _, ok := u.Object["status"]
+func unstructuredHasStatus(u runtime.Unstructured) bool {
+ _, ok := u.UnstructuredContent()["status"]
return ok
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment