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

58 lines
1.1 KiB
Go
Raw Normal View History

2019-07-12 17:13:20 +00:00
package gvk
import (
"fmt"
2020-03-26 21:07:15 +00:00
"github.com/pkg/errors"
2019-07-12 17:13:20 +00:00
"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 {
2020-03-26 21:07:15 +00:00
return schema.GroupVersionKind{}, errors.Wrapf(err, "failed to find gvk for %T, you may need to import the wrangler generated controller package", obj)
2019-07-12 17:13:20 +00:00
}
if len(gvks) == 0 {
2020-03-26 21:07:15 +00:00
return schema.GroupVersionKind{}, fmt.Errorf("failed to find gvk for %T", obj)
2019-07-12 17:13:20 +00:00
}
return gvks[0], nil
}
2019-09-05 18:55:53 +00:00
2020-04-22 22:34:19 +00:00
func Set(objs ...runtime.Object) error {
for _, obj := range objs {
if err := setObject(obj); err != nil {
return err
}
}
return nil
}
func setObject(obj runtime.Object) error {
2019-09-05 18:55:53 +00:00
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
}