@@ -14,7 +14,7 @@ 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"
@@ -26,13 +26,13 @@ import (
2626 shardingv1alpha1 "github.com/timebertt/kubernetes-controller-sharding/pkg/apis/sharding/v1alpha1"
2727)
2828
29- // KeyFuncForResource returns the key function that maps the given resource or its controller depending on whether
29+ // FuncForResource returns the key function that maps the given resource or its controller depending on whether
3030// 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 ) {
31+ func FuncForResource (gr metav1.GroupResource , ring * shardingv1alpha1.ControllerRing ) (Func , error ) {
3232 ringResources := sets .New [metav1.GroupResource ]()
3333 controlledResources := sets .New [metav1.GroupResource ]()
3434
35- for _ , ringResource := range ring .RingResources () {
35+ for _ , ringResource := range ring .Spec . Resources {
3636 ringResources .Insert (ringResource .GroupResource )
3737
3838 for _ , controlledResource := range ringResource .ControlledResources {
@@ -42,24 +42,24 @@ func KeyFuncForResource(gr metav1.GroupResource, ring *shardingv1alpha1.Controll
4242
4343 switch {
4444 case ringResources .Has (gr ):
45- return KeyForObject , nil
45+ return ForObject , nil
4646 case controlledResources .Has (gr ):
47- return KeyForController , nil
47+ return ForController , nil
4848 }
4949
50- return nil , fmt .Errorf ("object's resource %q was not found in Ring " , gr .String ())
50+ return nil , fmt .Errorf ("object's resource %q was not found in ControllerRing " , gr .String ())
5151}
5252
53- // KeyFunc maps objects to hash keys.
54- // It returns an error if the prequisities for sharding the given object are not fulfilled.
53+ // Func maps objects to hash keys.
54+ // It returns an error if the prerequisites for sharding the given object are not fulfilled.
5555// If the returned key is empty, the object should not be assigned.
56- type KeyFunc func (client.Object ) (string , error )
56+ type Func func (client.Object ) (string , error )
5757
58- // KeyForObject returns a ring key for the given object itself.
58+ // ForObject returns a ring key for the given object itself.
5959// 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 ) {
60+ func ForObject (obj client.Object ) (string , error ) {
6161 // 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 ):
62+ // Instead, we need to calculate a unique ID ourselves. The ID has this pattern (see forMetadata ):
6363 // group/version/kind/namespace/name
6464 // With this, different object instances with the same name will use the same hash key, which sounds acceptable.
6565 // 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 +77,7 @@ func KeyForObject(obj client.Object) (string, error) {
7777 // object ID that we can also reconstruct later on for owned objects just by looking at the object itself.
7878 // We could use a cache lookup though, but this would restrict scalability of the sharding solution again.
7979 // 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
80+ // owner itself. In such case, ForController will be used instead, which doesn't care about the object's own
8181 // name but only that of the owner.
8282 // If generateName is used nevertheless, respond with a proper error.
8383 // We could assign the object after creation, however we can't use a watch cache because of the mentioned
@@ -90,12 +90,12 @@ func KeyForObject(obj client.Object) (string, error) {
9090
9191 // Namespace can be empty for cluster-scoped resources. Only check the name field as an optimistic check for
9292 // preventing wrong usage of the function.
93- return keyForMetadata (gvk .GroupVersion ().String (), gvk .Kind , obj .GetNamespace (), obj .GetName ()), nil
93+ return forMetadata (gvk .GroupVersion ().String (), gvk .Kind , obj .GetNamespace (), obj .GetName ()), nil
9494}
9595
96- // KeyForController returns a ring key for the controller of the given object.
96+ // ForController returns a ring key for the controller of the given object.
9797// It returns an empty key if the object doesn't have an ownerReference with controller=true".
98- func KeyForController (obj client.Object ) (string , error ) {
98+ func ForController (obj client.Object ) (string , error ) {
9999 ref := metav1 .GetControllerOf (obj )
100100 if ref == nil {
101101 return "" , nil
@@ -113,9 +113,9 @@ func KeyForController(obj client.Object) (string, error) {
113113
114114 // Namespace can be empty for cluster-scoped resources. Only check the other fields as an optimistic check for
115115 // preventing wrong usage of the function.
116- return keyForMetadata (ref .APIVersion , ref .Kind , obj .GetNamespace (), ref .Name ), nil
116+ return forMetadata (ref .APIVersion , ref .Kind , obj .GetNamespace (), ref .Name ), nil
117117}
118118
119- func keyForMetadata (apiVersion , kind , namespace , name string ) string {
119+ func forMetadata (apiVersion , kind , namespace , name string ) string {
120120 return apiVersion + "/" + kind + "/" + namespace + "/" + name
121121}
0 commit comments