@@ -32,6 +32,7 @@ import (
3232 logf "sigs.k8s.io/controller-runtime/pkg/log"
3333 "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
3434
35+ shardingv1alpha1 "github.com/timebertt/kubernetes-controller-sharding/pkg/apis/sharding/v1alpha1"
3536 "github.com/timebertt/kubernetes-controller-sharding/pkg/sharding"
3637 shardingmetrics "github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/metrics"
3738 "github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/ring"
@@ -46,9 +47,9 @@ type Handler struct {
4647func (h * Handler ) Handle (ctx context.Context , req admission.Request ) admission.Response {
4748 log := logf .FromContext (ctx )
4849
49- ringObj , err := RingForRequest (ctx , h .Reader )
50+ controllerRing , err := ControllerRingForRequest (ctx , h .Reader )
5051 if err != nil {
51- return admission .Errored (http .StatusInternalServerError , fmt .Errorf ("error determining ring for request: %w" , err ))
52+ return admission .Errored (http .StatusInternalServerError , fmt .Errorf ("error determining ControllerRing for request: %w" , err ))
5253 }
5354
5455 // Unfortunately, admission.Decoder / runtime.Decoder can't handle decoding into PartialObjectMetadata.
@@ -58,7 +59,7 @@ func (h *Handler) Handle(ctx context.Context, req admission.Request) admission.R
5859 return admission .Errored (http .StatusInternalServerError , fmt .Errorf ("error decoding object: %w" , err ))
5960 }
6061
61- labelShard := ringObj .LabelShard ()
62+ labelShard := controllerRing .LabelShard ()
6263
6364 // Don't touch labels that the object already has, we can't simply reassign it because the active shard might still
6465 // be working on it.
@@ -69,7 +70,7 @@ func (h *Handler) Handle(ctx context.Context, req admission.Request) admission.R
6970 keyFunc , err := sharding .KeyFuncForResource (metav1.GroupResource {
7071 Group : req .Resource .Group ,
7172 Resource : req .Resource .Resource ,
72- }, ringObj )
73+ }, controllerRing )
7374 if err != nil {
7475 return admission .Errored (http .StatusBadRequest , fmt .Errorf ("error deteriming hash key func for object: %w" , err ))
7576 }
@@ -84,15 +85,15 @@ func (h *Handler) Handle(ctx context.Context, req admission.Request) admission.R
8485
8586 // collect list of shards in the ring
8687 leaseList := & coordinationv1.LeaseList {}
87- if err := h .Reader .List (ctx , leaseList , client.MatchingLabelsSelector {Selector : ringObj .LeaseSelector ()}); err != nil {
88+ if err := h .Reader .List (ctx , leaseList , client.MatchingLabelsSelector {Selector : controllerRing .LeaseSelector ()}); err != nil {
8889 return admission .Errored (http .StatusInternalServerError , fmt .Errorf ("error listing Leases for ControllerRing: %w" , err ))
8990 }
9091
9192 // get ring from cache and hash the object onto the ring
92- hashRing , _ := ring .FromLeases (ringObj , leaseList , h .Clock .Now ())
93+ hashRing , _ := ring .FromLeases (controllerRing , leaseList , h .Clock .Now ())
9394 shard := hashRing .Hash (key )
9495
95- log .V (1 ).Info ("Assigning object for ring " , "ring " , client .ObjectKeyFromObject (ringObj ), "shard" , shard )
96+ log .V (1 ).Info ("Assigning object for ControllerRing " , "controllerRing " , client .ObjectKeyFromObject (controllerRing ), "shard" , shard )
9697
9798 patches := make ([]jsonpatch.JsonPatchOperation , 0 , 2 )
9899 if obj .Labels == nil {
@@ -103,30 +104,30 @@ func (h *Handler) Handle(ctx context.Context, req admission.Request) admission.R
103104
104105 if ! ptr .Deref (req .DryRun , false ) {
105106 shardingmetrics .AssignmentsTotal .WithLabelValues (
106- ringObj . GetName () , req .Resource .Group , req .Resource .Resource ,
107+ controllerRing . Name , req .Resource .Group , req .Resource .Resource ,
107108 ).Inc ()
108109 }
109110
110111 return admission .Patched ("assigning object" , patches ... )
111112}
112113
113- // RingForRequest returns the Ring object matching the requests' path.
114- func RingForRequest (ctx context.Context , c client.Reader ) (sharding. Ring , error ) {
114+ // ControllerRingForRequest returns the Ring object matching the requests' path.
115+ func ControllerRingForRequest (ctx context.Context , c client.Reader ) (* shardingv1alpha1. ControllerRing , error ) {
115116 requestPath , err := RequestPathFromContext (ctx )
116117 if err != nil {
117118 return nil , err
118119 }
119120
120- ring , err := RingForWebhookPath (requestPath )
121+ controllerRing , err := ControllerRingForWebhookPath (requestPath )
121122 if err != nil {
122123 return nil , err
123124 }
124125
125- if err := c .Get (ctx , client .ObjectKeyFromObject (ring ), ring ); err != nil {
126- return nil , fmt .Errorf ("error getting ring : %w" , err )
126+ if err := c .Get (ctx , client .ObjectKeyFromObject (controllerRing ), controllerRing ); err != nil {
127+ return nil , fmt .Errorf ("error getting ControllerRing : %w" , err )
127128 }
128129
129- return ring , nil
130+ return controllerRing , nil
130131}
131132
132133// rfc6901Encoder can escape / characters in label keys for inclusion in JSON patch paths.
0 commit comments