/
apiobject.go
39 lines (29 loc) 路 1.01 KB
/
apiobject.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
package cluster
import (
"fmt"
"k8s.io/apimachinery/pkg/runtime"
anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
)
// APIObject represents a kubernetes API object.
type APIObject interface {
runtime.Object
GetName() string
}
type ObjectLookup map[string]APIObject
// GetFromRef searches in a ObjectLookup for an APIObject referenced by a anywherev1.Ref.
func (o ObjectLookup) GetFromRef(apiVersion string, ref anywherev1.Ref) APIObject {
return o[keyForRef(apiVersion, ref)]
}
func (o ObjectLookup) add(obj APIObject) {
o[keyForObject(obj)] = obj
}
func keyForRef(apiVersion string, ref anywherev1.Ref) string {
return key(apiVersion, ref.Kind, ref.Name)
}
func key(apiVersion, kind, name string) string {
// this assumes we don't allow to have objects in multiple namespaces
return fmt.Sprintf("%s%s%s", apiVersion, kind, name)
}
func keyForObject(o APIObject) string {
return key(o.GetObjectKind().GroupVersionKind().GroupVersion().String(), o.GetObjectKind().GroupVersionKind().Kind, o.GetName())
}