@@ -14,25 +14,26 @@ See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
1616
17- package sharding
17+ package key
1818
1919import (
2020 "fmt"
2121
2222 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+ "k8s.io/apimachinery/pkg/runtime/schema"
2324 "k8s.io/apimachinery/pkg/util/sets"
2425 "sigs.k8s.io/controller-runtime/pkg/client"
2526
2627 shardingv1alpha1 "github.com/timebertt/kubernetes-controller-sharding/pkg/apis/sharding/v1alpha1"
2728)
2829
29- // KeyFuncForResource returns the key function that maps the given resource or its controller depending on whether
30+ // FuncForResource returns the key function that maps the given resource or its controller depending on whether
3031// the resource is listed as a resource or controlled resource in the given ring.
31- func KeyFuncForResource (gr metav1.GroupResource , ring * shardingv1alpha1.ControllerRing ) (KeyFunc , error ) {
32+ func FuncForResource (gr metav1.GroupResource , ring * shardingv1alpha1.ControllerRing ) (Func , error ) {
3233 ringResources := sets .New [metav1.GroupResource ]()
3334 controlledResources := sets .New [metav1.GroupResource ]()
3435
35- for _ , ringResource := range ring .RingResources () {
36+ for _ , ringResource := range ring .Spec . Resources {
3637 ringResources .Insert (ringResource .GroupResource )
3738
3839 for _ , controlledResource := range ringResource .ControlledResources {
@@ -42,24 +43,24 @@ func KeyFuncForResource(gr metav1.GroupResource, ring *shardingv1alpha1.Controll
4243
4344 switch {
4445 case ringResources .Has (gr ):
45- return KeyForObject , nil
46+ return ForObject , nil
4647 case controlledResources .Has (gr ):
47- return KeyForController , nil
48+ return ForController , nil
4849 }
4950
50- return nil , fmt .Errorf ("object's resource %q was not found in Ring " , gr .String ())
51+ return nil , fmt .Errorf ("object's resource %q was not found in ControllerRing " , gr .String ())
5152}
5253
53- // KeyFunc maps objects to hash keys.
54- // It returns an error if the prequisities for sharding the given object are not fulfilled.
54+ // Func maps objects to hash keys.
55+ // It returns an error if the prerequisites for sharding the given object are not fulfilled.
5556// If the returned key is empty, the object should not be assigned.
56- type KeyFunc func (client.Object ) (string , error )
57+ type Func func (client.Object ) (string , error )
5758
58- // KeyForObject returns a ring key for the given object itself.
59+ // ForObject returns a ring key for the given object itself.
5960// It needs the TypeMeta (GVK) to be set, which is not set on objects after decoding by default.
60- func KeyForObject (obj client.Object ) (string , error ) {
61+ func ForObject (obj client.Object ) (string , error ) {
6162 // We can't use the object's UID, as it is unset during admission for CREATE requests.
62- // Instead, we need to calculate a unique ID ourselves. The ID has this pattern (see keyForMetadata ):
63+ // Instead, we need to calculate a unique ID ourselves. The ID has this pattern (see forMetadata ):
6364 // group/version/kind/namespace/name
6465 // With this, different object instances with the same name will use the same hash key, which sounds acceptable.
6566 // We can only use fields that are also present in owner references as we need to assign owners and ownees to the same
@@ -77,7 +78,7 @@ func KeyForObject(obj client.Object) (string, error) {
7778 // object ID that we can also reconstruct later on for owned objects just by looking at the object itself.
7879 // We could use a cache lookup though, but this would restrict scalability of the sharding solution again.
7980 // Generally, this tradeoff seems acceptable, as generateName is mostly used on owned objects, but rarely the
80- // owner itself. In such case, KeyForController will be used instead, which doesn't care about the object's own
81+ // owner itself. In such case, ForController will be used instead, which doesn't care about the object's own
8182 // name but only that of the owner.
8283 // If generateName is used nevertheless, respond with a proper error.
8384 // We could assign the object after creation, however we can't use a watch cache because of the mentioned
@@ -90,12 +91,12 @@ func KeyForObject(obj client.Object) (string, error) {
9091
9192 // Namespace can be empty for cluster-scoped resources. Only check the name field as an optimistic check for
9293 // preventing wrong usage of the function.
93- return keyForMetadata (gvk .GroupVersion (). String () , gvk .Kind , obj .GetNamespace (), obj .GetName ()), nil
94+ return forMetadata (gvk .Group , gvk .Kind , obj .GetNamespace (), obj .GetName ()), nil
9495}
9596
96- // KeyForController returns a ring key for the controller of the given object.
97+ // ForController returns a ring key for the controller of the given object.
9798// It returns an empty key if the object doesn't have an ownerReference with controller=true".
98- func KeyForController (obj client.Object ) (string , error ) {
99+ func ForController (obj client.Object ) (string , error ) {
99100 ref := metav1 .GetControllerOf (obj )
100101 if ref == nil {
101102 return "" , nil
@@ -111,11 +112,16 @@ func KeyForController(obj client.Object) (string, error) {
111112 return "" , fmt .Errorf ("name of controller reference must not be empty" )
112113 }
113114
115+ gv , err := schema .ParseGroupVersion (ref .APIVersion )
116+ if err != nil {
117+ return "" , fmt .Errorf ("invalid apiVersion of controller reference: %w" , err )
118+ }
119+
114120 // Namespace can be empty for cluster-scoped resources. Only check the other fields as an optimistic check for
115121 // preventing wrong usage of the function.
116- return keyForMetadata ( ref . APIVersion , ref .Kind , obj .GetNamespace (), ref .Name ), nil
122+ return forMetadata ( gv . Group , ref .Kind , obj .GetNamespace (), ref .Name ), nil
117123}
118124
119- func keyForMetadata ( apiVersion , kind , namespace , name string ) string {
120- return apiVersion + "/" + kind + "/" + namespace + "/" + name
125+ func forMetadata ( group , kind , namespace , name string ) string {
126+ return group + "/" + kind + "/" + namespace + "/" + name
121127}
0 commit comments