Skip to content

Commit cc573a4

Browse files
committed
Extract pkg/sharding/key package
1 parent 6888026 commit cc573a4

File tree

5 files changed

+30
-41
lines changed

5 files changed

+30
-41
lines changed

pkg/apis/sharding/v1alpha1/types_controllerring.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,3 @@ func (c *ControllerRing) LabelShard() string {
130130
func (c *ControllerRing) LabelDrain() string {
131131
return LabelDrain(c.Name)
132132
}
133-
134-
// RingResources returns the list of resources that are distributed across shards in this ControllerRing.
135-
func (c *ControllerRing) RingResources() []RingResource {
136-
return c.Spec.Resources
137-
}

pkg/apis/sharding/v1alpha1/types_controllerring_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,4 @@ var _ = Describe("ControllerRing", func() {
6464
Expect(ring.LabelDrain()).To(Equal("drain.alpha.sharding.timebertt.dev/operator"))
6565
})
6666
})
67-
68-
Describe("#RingResources", func() {
69-
It("should return the specified resources", func() {
70-
Expect(ring.RingResources()).To(Equal(ring.Spec.Resources))
71-
})
72-
})
7367
})

pkg/controller/sharder/reconciler.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ import (
3535

3636
configv1alpha1 "github.com/timebertt/kubernetes-controller-sharding/pkg/apis/config/v1alpha1"
3737
shardingv1alpha1 "github.com/timebertt/kubernetes-controller-sharding/pkg/apis/sharding/v1alpha1"
38-
"github.com/timebertt/kubernetes-controller-sharding/pkg/sharding"
3938
"github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/consistenthash"
39+
"github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/key"
4040
"github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/leases"
4141
shardingmetrics "github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/metrics"
4242
"github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/ring"
@@ -201,22 +201,22 @@ func (r *Reconciler) resyncObject(
201201
) error {
202202
log = log.WithValues("object", client.ObjectKeyFromObject(obj))
203203

204-
keyFunc := sharding.KeyForObject
204+
keyFunc := key.ForObject
205205
if controlled {
206-
keyFunc = sharding.KeyForController
206+
keyFunc = key.ForController
207207
}
208208

209-
key, err := keyFunc(obj)
209+
hashKey, err := keyFunc(obj)
210210
if err != nil {
211211
return err
212212
}
213-
if key == "" {
213+
if hashKey == "" {
214214
// object should not be assigned
215215
return nil
216216
}
217217

218218
var (
219-
desiredShard = hashRing.Hash(key)
219+
desiredShard = hashRing.Hash(hashKey)
220220
currentShard = obj.Labels[ring.LabelShard()]
221221
)
222222

pkg/sharding/key.go renamed to pkg/sharding/key/key.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package sharding
17+
package key
1818

1919
import (
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
}

pkg/webhook/sharder/handler.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
3434

3535
shardingv1alpha1 "github.com/timebertt/kubernetes-controller-sharding/pkg/apis/sharding/v1alpha1"
36-
"github.com/timebertt/kubernetes-controller-sharding/pkg/sharding"
36+
"github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/key"
3737
shardingmetrics "github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/metrics"
3838
"github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/ring"
3939
)
@@ -67,19 +67,19 @@ func (h *Handler) Handle(ctx context.Context, req admission.Request) admission.R
6767
return admission.Allowed("object is already assigned")
6868
}
6969

70-
keyFunc, err := sharding.KeyFuncForResource(metav1.GroupResource{
70+
keyFunc, err := key.FuncForResource(metav1.GroupResource{
7171
Group: req.Resource.Group,
7272
Resource: req.Resource.Resource,
7373
}, controllerRing)
7474
if err != nil {
7575
return admission.Errored(http.StatusBadRequest, fmt.Errorf("error deteriming hash key func for object: %w", err))
7676
}
7777

78-
key, err := keyFunc(obj)
78+
hashKey, err := keyFunc(obj)
7979
if err != nil {
8080
return admission.Errored(http.StatusBadRequest, fmt.Errorf("error calculating hash key for object: %w", err))
8181
}
82-
if key == "" {
82+
if hashKey == "" {
8383
return admission.Allowed("object should not be assigned")
8484
}
8585

@@ -91,7 +91,7 @@ func (h *Handler) Handle(ctx context.Context, req admission.Request) admission.R
9191

9292
// get ring from cache and hash the object onto the ring
9393
hashRing, _ := ring.FromLeases(controllerRing, leaseList, h.Clock.Now())
94-
shard := hashRing.Hash(key)
94+
shard := hashRing.Hash(hashKey)
9595

9696
log.V(1).Info("Assigning object for ControllerRing", "controllerRing", client.ObjectKeyFromObject(controllerRing), "shard", shard)
9797

0 commit comments

Comments
 (0)