@@ -26,6 +26,33 @@ import (
26
26
"k8s.io/client-go/tools/cache"
27
27
)
28
28
29
+ // Key identifies an object of some implict kind.
30
+ // Put another way, Key identifies an instance of some implicit resource.
31
+ // Put another way, Key is what SplitMetaClusterNamespaceKey wants to return.
32
+ type Key struct {
33
+ Cluster logicalcluster.Name
34
+ Namespace string
35
+ Name string
36
+ }
37
+
38
+ // String returns the standard string representation of the given Key
39
+ func (key Key ) String () string {
40
+ var ans string
41
+ if key .Cluster != "" {
42
+ ans += key .Cluster .String () + "|"
43
+ }
44
+ if key .Namespace != "" {
45
+ ans += key .Namespace + "/"
46
+ }
47
+ ans += key .Name
48
+ return ans
49
+
50
+ }
51
+
52
+ func (key Key ) Parts () (cluster logicalcluster.Name , namespace , name string ) {
53
+ return key .Cluster , key .Namespace , key .Name
54
+ }
55
+
29
56
// DeletionHandlingMetaClusterNamespaceKeyFunc checks for
30
57
// DeletedFinalStateUnknown objects before calling
31
58
// MetaClusterNamespaceKeyFunc.
@@ -36,38 +63,38 @@ func DeletionHandlingMetaClusterNamespaceKeyFunc(obj interface{}) (string, error
36
63
return MetaClusterNamespaceKeyFunc (obj )
37
64
}
38
65
66
+ // ObjMetaClusterNamespaceKey is a convenient default KeyFunc which knows how to make
67
+ // structured keys for API objects which implement meta.Interface.
68
+ // This is the structured alternative to MetaClusterNamespaceKeyFunc;
69
+ // putting such an object reference in a queue means that no parsing errors are possible downstream.
70
+ func ObjMetaClusterNamespaceKey (obj interface {}) (Key , error ) {
71
+ if key , ok := obj .(cache.ExplicitKey ); ok {
72
+ return ParseKey (string (key ))
73
+ }
74
+ meta , err := meta .Accessor (obj )
75
+ if err != nil {
76
+ return Key {}, fmt .Errorf ("object has no meta: %v" , err )
77
+ }
78
+ return Key {Cluster : logicalcluster .From (meta ), Namespace : meta .GetNamespace (), Name : meta .GetName ()}, nil
79
+ }
80
+
39
81
// MetaClusterNamespaceKeyFunc is a convenient default KeyFunc which knows how to make
40
82
// keys for API objects which implement meta.Interface.
41
83
// The key uses the format <clusterName>|<namespace>/<name> unless <namespace> is empty, then
42
84
// it's just <clusterName>|<name>, and if running in a single-cluster context where no explicit
43
85
// cluster name is given, it's just <name>.
44
86
func MetaClusterNamespaceKeyFunc (obj interface {}) (string , error ) {
45
- if key , ok := obj .(cache.ExplicitKey ); ok {
46
- return string (key ), nil
47
- }
48
- meta , err := meta .Accessor (obj )
49
- if err != nil {
50
- return "" , fmt .Errorf ("object has no meta: %v" , err )
51
- }
52
- return ToClusterAwareKey (logicalcluster .From (meta ).String (), meta .GetNamespace (), meta .GetName ()), nil
87
+ key , err := ObjMetaClusterNamespaceKey (obj )
88
+ return key .String (), err
53
89
}
54
90
55
91
// ToClusterAwareKey formats a cluster, namespace, and name as a key.
56
92
func ToClusterAwareKey (cluster , namespace , name string ) string {
57
- var key string
58
- if cluster != "" {
59
- key += cluster + "|"
60
- }
61
- if namespace != "" {
62
- key += namespace + "/"
63
- }
64
- key += name
65
- return key
93
+ return Key {Cluster : logicalcluster .Name (cluster ), Namespace : namespace , Name : name }.String ()
66
94
}
67
95
68
- // SplitMetaClusterNamespaceKey returns the namespace and name that
69
- // MetaClusterNamespaceKeyFunc encoded into key.
70
- func SplitMetaClusterNamespaceKey (key string ) (clusterName logicalcluster.Name , namespace , name string , err error ) {
96
+ // ParseKey is the structured form of SplitMetaClusterNamespaceKey
97
+ func ParseKey (key string ) (Key , error ) {
71
98
invalidKey := fmt .Errorf ("unexpected key format: %q" , key )
72
99
outerParts := strings .Split (key , "|" )
73
100
switch len (outerParts ) {
@@ -76,14 +103,22 @@ func SplitMetaClusterNamespaceKey(key string) (clusterName logicalcluster.Name,
76
103
if err != nil {
77
104
err = invalidKey
78
105
}
79
- return "" , namespace , name , err
106
+ return Key { Cluster : "" , Namespace : namespace , Name : name } , err
80
107
case 2 :
81
108
namespace , name , err := cache .SplitMetaNamespaceKey (outerParts [1 ])
82
109
if err != nil {
83
110
err = invalidKey
84
111
}
85
- return logicalcluster .Name (outerParts [0 ]), namespace , name , err
112
+ return Key { Cluster : logicalcluster .Name (outerParts [0 ]), Namespace : namespace , Name : name } , err
86
113
default :
87
- return "" , "" , "" , invalidKey
114
+ return Key { Cluster : "" , Namespace : "" , Name : "" } , invalidKey
88
115
}
89
116
}
117
+
118
+ // SplitMetaClusterNamespaceKey returns the cluster, namespace, and name that
119
+ // MetaClusterNamespaceKeyFunc encoded into key.
120
+ func SplitMetaClusterNamespaceKey (key string ) (clusterName logicalcluster.Name , namespace , name string , err error ) {
121
+ parsed , err := ParseKey (key )
122
+ clusterName , namespace , name = parsed .Parts ()
123
+ return
124
+ }
0 commit comments