k3s/vendor/github.com/rancher/wrangler/pkg/gvk/get.go

48 lines
878 B
Go
Raw Normal View History

2019-07-12 17:13:20 +00:00
package gvk
import (
"fmt"
"github.com/rancher/wrangler/pkg/schemes"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
func Get(obj runtime.Object) (schema.GroupVersionKind, error) {
gvk := obj.GetObjectKind().GroupVersionKind()
if gvk.Kind != "" {
return gvk, nil
}
gvks, _, err := schemes.All.ObjectKinds(obj)
if err != nil {
return schema.GroupVersionKind{}, err
}
if len(gvks) == 0 {
return schema.GroupVersionKind{}, fmt.Errorf("failed to find gvk for %v", obj.GetObjectKind())
}
return gvks[0], nil
}
2019-09-05 18:55:53 +00:00
func Set(obj runtime.Object) error {
gvk := obj.GetObjectKind().GroupVersionKind()
if gvk.Kind != "" {
return nil
}
2019-12-12 01:27:03 +00:00
2019-09-05 18:55:53 +00:00
gvks, _, err := schemes.All.ObjectKinds(obj)
if err != nil {
return err
}
if len(gvks) == 0 {
return nil
}
kind := obj.GetObjectKind()
kind.SetGroupVersionKind(gvks[0])
return nil
}