Skip to content

Instantly share code, notes, and snippets.

@lyarwood
Created August 11, 2022 16:44
Show Gist options
  • Save lyarwood/1645a40c115eb62e6da37ca1e289be9e to your computer and use it in GitHub Desktop.
Save lyarwood/1645a40c115eb62e6da37ca1e289be9e to your computer and use it in GitHub Desktop.
diff --git a/pkg/instancetype/instancetype.go b/pkg/instancetype/instancetype.go
index 641567acf..539c6bd3a 100644
--- a/pkg/instancetype/instancetype.go
+++ b/pkg/instancetype/instancetype.go
@@ -59,8 +59,31 @@ func GetRevisionName(vmName, resourceName string, resourceUID types.UID, resourc
return fmt.Sprintf("%s-%s-%s-%d", vmName, resourceName, resourceUID, resourceGeneration)
}
+// addTypeInformationToObject adds TypeMeta information to a runtime.Object based upon the loaded scheme.Scheme
+// inspired by: https://github.com/kubernetes/cli-runtime/blob/v0.19.2/pkg/printers/typesetter.go#L41
+func addTypeInformationToObject(obj runtime.Object) error {
+ gvks, _, err := generatedscheme.Scheme.ObjectKinds(obj)
+ if err != nil {
+ return fmt.Errorf("missing apiVersion or kind and cannot assign it; %w", err)
+ }
+
+ for _, gvk := range gvks {
+ if len(gvk.Kind) == 0 {
+ continue
+ }
+ if len(gvk.Version) == 0 || gvk.Version == runtime.APIVersionInternal {
+ continue
+ }
+ obj.GetObjectKind().SetGroupVersionKind(gvk)
+ break
+ }
+
+ return nil
+}
+
func CreateControllerRevision(vm *virtv1.VirtualMachine, object runtime.Object) *appsv1.ControllerRevision {
objCopy := object.DeepCopyObject()
+ addTypeInformationToObject(objCopy)
metaObj := objCopy.(metav1.Object)
revisionName := GetRevisionName(vm.Name, metaObj.GetName(), metaObj.GetUID(), metaObj.GetGeneration())
@@ -249,13 +272,8 @@ func storeRevision(revision *appsv1.ControllerRevision, clientset kubecli.Kubevi
return nil, fmt.Errorf("failed to get ControllerRevision: %w", err)
}
- decodedObj, err := runtime.Decode(generatedscheme.Codecs.UniversalDeserializer(), existingRevision.Data.Raw)
- if err != nil {
- return nil, fmt.Errorf("failed to decode object in ControllerRevision: %w", err)
- }
-
// If the data between the two differs return an error, otherwise continue and store the name below.
- if !equality.Semantic.DeepEqual(decodedObj, revision.Data.Object) {
+ if !equality.Semantic.DeepEqual(existingRevision.Data.Object, revision.Data.Object) {
return nil, fmt.Errorf("found existing ControllerRevision with unexpected data: %s", revision.Name)
}
return existingRevision, nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment