-
Notifications
You must be signed in to change notification settings - Fork 6
/
object.go
52 lines (44 loc) · 1.26 KB
/
object.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package object
import (
"fmt"
"reflect"
"github.com/samber/lo"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/yaml"
)
// GroupVersionKindNamespacedName uniquely identifies an object
type GroupVersionKindNamespacedName struct {
schema.GroupVersionKind
types.NamespacedName
}
// GVKNN returns a GroupVersionKindNamespacedName that uniquely identifies the object
func GVKNN(o client.Object) GroupVersionKindNamespacedName {
return GroupVersionKindNamespacedName{
GroupVersionKind: GVK(o),
NamespacedName: client.ObjectKeyFromObject(o),
}
}
func (gvknn GroupVersionKindNamespacedName) String() string {
str := fmt.Sprintf("%s/%s", gvknn.Group, gvknn.Kind)
if gvknn.Namespace != "" {
str += "/" + gvknn.Namespace
}
str += "/" + gvknn.Name
return str
}
func GVK(o runtime.Object) schema.GroupVersionKind {
return lo.Must(apiutil.GVKForObject(o, scheme.Scheme))
}
func New[T any]() T {
return reflect.New(reflect.TypeOf(*new(T)).Elem()).Interface().(T)
}
func Unmarshal[T any](raw []byte) *T {
t := *new(T)
lo.Must0(yaml.Unmarshal(raw, &t))
return &t
}