Created
November 22, 2020 12:12
-
-
Save yanickxia/8a5306ab555034cb862eda5e0f12399a to your computer and use it in GitHub Desktop.
k8s-addTypeInformationToObject
This file contains 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
// 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 := scheme.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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment