Skip to content

Instantly share code, notes, and snippets.

@yanickxia
Created November 22, 2020 12:12
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 yanickxia/8a5306ab555034cb862eda5e0f12399a to your computer and use it in GitHub Desktop.
Save yanickxia/8a5306ab555034cb862eda5e0f12399a to your computer and use it in GitHub Desktop.
k8s-addTypeInformationToObject
// 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