This repository has been archived by the owner on Jun 7, 2022. It is now read-only.
forked from atlassian/smith
/
crd.go
58 lines (51 loc) · 1.6 KB
/
crd.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
53
54
55
56
57
58
package store
import (
"github.com/pkg/errors"
apiext_v1b1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/tools/cache"
)
const (
byGroupKindIndexName = "ByGroupKind"
)
type Crd struct {
byIndex func(indexName, indexKey string) ([]interface{}, error)
}
func NewCrd(crdInf cache.SharedIndexInformer) (*Crd, error) {
err := crdInf.AddIndexers(cache.Indexers{
byGroupKindIndexName: byGroupKindIndex,
})
if err != nil {
return nil, errors.WithStack(err)
}
return &Crd{
byIndex: crdInf.GetIndexer().ByIndex,
}, nil
}
// Get returns the CRD that defines the resource of provided group and kind.
func (s *Crd) Get(resource schema.GroupKind) (*apiext_v1b1.CustomResourceDefinition, error) {
objs, err := s.byIndex(byGroupKindIndexName, byGroupKindIndexKey(resource.Group, resource.Kind))
if err != nil {
return nil, err
}
switch len(objs) {
case 0:
return nil, nil
case 1:
crd := objs[0].(*apiext_v1b1.CustomResourceDefinition).DeepCopy()
// Objects from type-specific informers don't have GVK set
crd.Kind = "CustomResourceDefinition"
crd.APIVersion = apiext_v1b1.SchemeGroupVersion.String()
return crd, nil
default:
// Must never happen
panic(errors.Errorf("multiple CRDs by group %q and kind %q: %s", resource.Group, resource.Kind, objs))
}
}
func byGroupKindIndex(obj interface{}) ([]string, error) {
crd := obj.(*apiext_v1b1.CustomResourceDefinition)
return []string{byGroupKindIndexKey(crd.Spec.Group, crd.Spec.Names.Kind)}, nil
}
func byGroupKindIndexKey(group, kind string) string {
return group + "/" + kind
}