Skip to content

Commit 063ffd0

Browse files
harivallkishorj
authored andcommitted
remove unnecessary logs, change endpointslices flag name, clean up code
1 parent a76dc5f commit 063ffd0

File tree

7 files changed

+48
-49
lines changed

7 files changed

+48
-49
lines changed

controllers/elbv2/eventhandlers/endpointslices.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package eventhandlers
22

33
import (
44
"context"
5+
"errors"
56

67
"github.com/go-logr/logr"
78
discv1 "k8s.io/api/discovery/v1beta1"
@@ -35,7 +36,7 @@ type enqueueRequestsForEndpointSlicesEvent struct {
3536
// Create is called in response to an create event - e.g. EndpointSlice Creation.
3637
func (h *enqueueRequestsForEndpointSlicesEvent) Create(e event.CreateEvent, queue workqueue.RateLimitingInterface) {
3738
epNew := e.Object.(*discv1.EndpointSlice)
38-
h.logger.Info("Create event for EndpointSlices", "name", epNew.Name)
39+
h.logger.V(1).Info("Create event for EndpointSlices", "name", epNew.Name)
3940
h.enqueueImpactedTargetGroupBindings(queue, epNew)
4041
}
4142

@@ -45,15 +46,15 @@ func (h *enqueueRequestsForEndpointSlicesEvent) Update(e event.UpdateEvent, queu
4546
epNew := e.ObjectNew.(*discv1.EndpointSlice)
4647
h.logger.Info("Update event for EndpointSlices", "name", epNew.Name)
4748
if !equality.Semantic.DeepEqual(epOld.Ports, epNew.Ports) || !equality.Semantic.DeepEqual(epOld.Endpoints, epNew.Endpoints) {
48-
h.logger.Info("Enqueue EndpointSlice", "name", epNew.Name)
49+
h.logger.V(1).Info("Enqueue EndpointSlice", "name", epNew.Name)
4950
h.enqueueImpactedTargetGroupBindings(queue, epNew)
5051
}
5152
}
5253

5354
// Delete is called in response to a delete event - e.g. EndpointSlice Deleted.
5455
func (h *enqueueRequestsForEndpointSlicesEvent) Delete(e event.DeleteEvent, queue workqueue.RateLimitingInterface) {
5556
epOld := e.Object.(*discv1.EndpointSlice)
56-
h.logger.Info("Deletion event for EndpointSlices", "name", epOld.Name)
57+
h.logger.V(1).Info("Deletion event for EndpointSlices", "name", epOld.Name)
5758
h.enqueueImpactedTargetGroupBindings(queue, epOld)
5859
}
5960

@@ -62,24 +63,30 @@ func (h *enqueueRequestsForEndpointSlicesEvent) Delete(e event.DeleteEvent, queu
6263
func (h *enqueueRequestsForEndpointSlicesEvent) Generic(event.GenericEvent, workqueue.RateLimitingInterface) {
6364
}
6465

65-
func (h *enqueueRequestsForEndpointSlicesEvent) enqueueImpactedTargetGroupBindings(queue workqueue.RateLimitingInterface, epslice *discv1.EndpointSlice) {
66+
func (h *enqueueRequestsForEndpointSlicesEvent) enqueueImpactedTargetGroupBindings(queue workqueue.RateLimitingInterface, epSlice *discv1.EndpointSlice) {
6667
tgbList := &elbv2api.TargetGroupBindingList{}
67-
svcName := epslice.Labels["kubernetes.io/service-name"]
68+
const svcNameLabel = "kubernetes.io/service-name"
69+
svcName, present := epSlice.Labels[svcNameLabel]
70+
if !present {
71+
err := errors.New("EndpointSlice does not have a" + svcNameLabel + "label")
72+
h.logger.Error(err, "unable to find service name for endpointslice")
73+
return
74+
}
6875
if err := h.k8sClient.List(context.Background(), tgbList,
69-
client.InNamespace(epslice.Namespace),
76+
client.InNamespace(epSlice.Namespace),
7077
client.MatchingFields{targetgroupbinding.IndexKeyServiceRefName: svcName}); err != nil {
7178
h.logger.Error(err, "failed to fetch targetGroupBindings")
7279
return
7380
}
7481

75-
epsliceKey := k8s.NamespacedName(epslice)
82+
epSliceKey := k8s.NamespacedName(epSlice)
7683
for _, tgb := range tgbList.Items {
7784
if tgb.Spec.TargetType == nil || (*tgb.Spec.TargetType) != elbv2api.TargetTypeIP {
7885
continue
7986
}
8087

8188
h.logger.V(1).Info("enqueue targetGroupBinding for endpointslices event",
82-
"endpointslices", epsliceKey,
89+
"endpointslices", epSliceKey,
8390
"targetGroupBinding", k8s.NamespacedName(&tgb),
8491
)
8592
queue.Add(reconcile.Request{

controllers/elbv2/targetgroupbinding_controller.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func NewTargetGroupBindingReconciler(k8sClient client.Client, eventRecorder reco
6161

6262
maxConcurrentReconciles: config.TargetGroupBindingMaxConcurrentReconciles,
6363
maxExponentialBackoffDelay: config.TargetGroupBindingMaxExponentialBackoffDelay,
64-
useEndpointSlices: config.UseEndpointSlices,
64+
enableEndpointSlices: config.EnableEndpointSlices,
6565
}
6666
}
6767

@@ -75,7 +75,7 @@ type targetGroupBindingReconciler struct {
7575

7676
maxConcurrentReconciles int
7777
maxExponentialBackoffDelay time.Duration
78-
useEndpointSlices bool
78+
enableEndpointSlices bool
7979
}
8080

8181
// +kubebuilder:rbac:groups=elbv2.k8s.aws,resources=targetgroupbindings,verbs=get;list;watch;update;patch;create;delete
@@ -87,15 +87,16 @@ type targetGroupBindingReconciler struct {
8787
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch
8888
// +kubebuilder:rbac:groups="",resources=namespaces,verbs=get;list;watch
8989
// +kubebuilder:rbac:groups="",resources=events,verbs=create;patch
90+
// +kubebuilder:rbac:groups="discovery.k8s.io",resources=endpointslices,verbs=get;list;watch
9091

9192
func (r *targetGroupBindingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
92-
r.logger.Info("Reconcile request", "name", req.Name)
93+
r.logger.V(1).Info("Reconcile request", "name", req.Name)
9394
return runtime.HandleReconcileError(r.reconcile(ctx, req), r.logger)
9495
}
9596

9697
func (r *targetGroupBindingReconciler) reconcile(ctx context.Context, req ctrl.Request) error {
9798
tgb := &elbv2api.TargetGroupBinding{}
98-
r.logger.Info("reconcile request Get", "name", req.Name, "tgb name", tgb.ObjectMeta.Name)
99+
r.logger.V(1).Info("reconcile request Get", "name", req.Name, "tgb name", tgb.ObjectMeta.Name)
99100
if err := r.k8sClient.Get(ctx, req.NamespacedName, tgb); err != nil {
100101
return client.IgnoreNotFound(err)
101102
}
@@ -107,16 +108,15 @@ func (r *targetGroupBindingReconciler) reconcile(ctx context.Context, req ctrl.R
107108
}
108109

109110
func (r *targetGroupBindingReconciler) reconcileTargetGroupBinding(ctx context.Context, tgb *elbv2api.TargetGroupBinding) error {
110-
r.logger.Info("reconcileTargetGroupBinding AddFinalizers", "name", tgb.ObjectMeta.Name)
111111
if err := r.finalizerManager.AddFinalizers(ctx, tgb, targetGroupBindingFinalizer); err != nil {
112112
r.eventRecorder.Event(tgb, corev1.EventTypeWarning, k8s.TargetGroupBindingEventReasonFailedAddFinalizer, fmt.Sprintf("Failed add finalizer due to %v", err))
113113
return err
114114
}
115-
r.logger.Info("reconcileTargetGroupBinding Reconcile", "name", tgb.ObjectMeta.Name)
115+
116116
if err := r.tgbResourceManager.Reconcile(ctx, tgb); err != nil {
117117
return err
118118
}
119-
r.logger.Info("reconcileTargetGroupBinding updateTargetGroupBindingStatus", "name", tgb.ObjectMeta.Name)
119+
120120
if err := r.updateTargetGroupBindingStatus(ctx, tgb); err != nil {
121121
r.eventRecorder.Event(tgb, corev1.EventTypeWarning, k8s.TargetGroupBindingEventReasonFailedUpdateStatus, fmt.Sprintf("Failed update status due to %v", err))
122122
return err
@@ -163,7 +163,7 @@ func (r *targetGroupBindingReconciler) SetupWithManager(ctx context.Context, mgr
163163
r.logger.WithName("eventHandlers").WithName("node"))
164164

165165
// Use the config flag to decide whether to use and watch an Endpoints event handler or an EndpointSlices event handler
166-
if r.useEndpointSlices {
166+
if r.enableEndpointSlices {
167167
epSliceEventsHandler := eventhandlers.NewEnqueueRequestsForEndpointSlicesEvent(r.k8sClient,
168168
r.logger.WithName("eventHandlers").WithName("endpointslices"))
169169
return ctrl.NewControllerManagedBy(mgr).

docs/deploy/configurations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Currently, you can set only 1 namespace to watch in this flag. See [this Kuberne
9393
|sync-period | duration | 1h0m0s | Period at which the controller forces the repopulation of its local object stores|
9494
|targetgroupbinding-max-concurrent-reconciles | int | 3 | Maximum number of concurrently running reconcile loops for targetGroupBinding |
9595
|targetgroupbinding-max-exponential-backoff-delay | duration | 16m40s | Maximum duration of exponential backoff for targetGroupBinding reconcile failures |
96-
|use-endpoint-slices | boolean | false | Use EndpointSlices instead of Endpoints for pod endpoint and TargetGroupBinding resolution. |
96+
|enable-endpoint-slices | boolean | false | Use EndpointSlices instead of Endpoints for pod endpoint and TargetGroupBinding resolution for load balancers with IP targets. |
9797
|watch-namespace | string | | Namespace the controller watches for updates to Kubernetes objects, If empty, all namespaces are watched. |
9898
|webhook-bind-port | int | 9443 | The TCP port the Webhook server binds to |
9999
|webhook-cert-dir | string | /tmp/k8s-webhook-server/serving-certs | The directory that contains the server key and certificate |

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func main() {
106106
subnetResolver := networking.NewDefaultSubnetsResolver(azInfoProvider, cloud.EC2(), cloud.VpcID(), controllerCFG.ClusterName, ctrl.Log.WithName("subnets-resolver"))
107107
vpcResolver := networking.NewDefaultVPCResolver(cloud.EC2(), cloud.VpcID(), ctrl.Log.WithName("vpc-resolver"))
108108
tgbResManager := targetgroupbinding.NewDefaultResourceManager(mgr.GetClient(), cloud.ELBV2(), cloud.EC2(),
109-
podInfoRepo, sgManager, sgReconciler, cloud.VpcID(), controllerCFG.ClusterName, mgr.GetEventRecorderFor("targetGroupBinding"), ctrl.Log, controllerCFG.UseEndpointSlices)
109+
podInfoRepo, sgManager, sgReconciler, cloud.VpcID(), controllerCFG.ClusterName, mgr.GetEventRecorderFor("targetGroupBinding"), ctrl.Log, controllerCFG.EnableEndpointSlices)
110110
backendSGProvider := networking.NewBackendSGProvider(controllerCFG.ClusterName, controllerCFG.BackendSecurityGroup,
111111
cloud.VpcID(), cloud.EC2(), mgr.GetClient(), ctrl.Log.WithName("backend-sg-provider"))
112112
ingGroupReconciler := ingress.NewGroupReconciler(cloud, mgr.GetClient(), mgr.GetEventRecorderFor("ingress"),

pkg/backend/endpoint_resolver.go

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,11 @@ func (r *defaultEndpointResolver) ResolvePodEndpointsFromSlices(ctx context.Cont
139139
return nil, false, err
140140
}
141141

142+
const svcNameLabel = "kubernetes.io/service-name"
142143
epSlicesList := &discv1.EndpointSliceList{}
143144
if err := r.k8sClient.List(ctx, epSlicesList,
144145
client.InNamespace(svcKey.Namespace),
145-
client.MatchingLabels{"kubernetes.io/service-name": svcKey.Name}); err != nil {
146+
client.MatchingLabels{svcNameLabel: svcKey.Name}); err != nil {
146147
return nil, false, err
147148
}
148149
if len(epSlicesList.Items) == 0 {
@@ -151,7 +152,7 @@ func (r *defaultEndpointResolver) ResolvePodEndpointsFromSlices(ctx context.Cont
151152

152153
containsPotentialReadyEndpoints := false
153154
var endpoints []PodEndpoint
154-
used_addr := make(sets.String)
155+
usedAddrs := make(sets.String)
155156
for _, epSlice := range epSlicesList.Items {
156157
// process epSlice
157158
for _, epPort := range epSlice.Ports {
@@ -160,33 +161,25 @@ func (r *defaultEndpointResolver) ResolvePodEndpointsFromSlices(ctx context.Cont
160161
continue
161162
}
162163
for _, ep := range epSlice.Endpoints {
163-
if ep.Conditions.Ready == nil || *ep.Conditions.Ready {
164-
for _, epAddr := range ep.Addresses {
165-
if used_addr.Has(epAddr) {
166-
continue
167-
}
168-
if ep.TargetRef == nil || ep.TargetRef.Kind != "Pod" {
169-
continue
170-
}
164+
for _, epAddr := range ep.Addresses {
165+
if usedAddrs.Has(epAddr) {
166+
continue
167+
}
168+
if ep.TargetRef == nil || ep.TargetRef.Kind != "Pod" {
169+
continue
170+
}
171+
if ep.Conditions.Ready == nil || *ep.Conditions.Ready {
171172
pod, exists, err := r.findPodByReference(ctx, svc.Namespace, *ep.TargetRef)
172173
if err != nil {
173174
return nil, false, err
174175
}
175176
if !exists {
176177
return nil, false, errors.New("couldn't find podInfo for ready endpoint")
177178
}
178-
used_addr.Insert(epAddr)
179+
usedAddrs.Insert(epAddr)
179180
endpoints = append(endpoints, buildPodEndpointFromSlice(pod, epAddr, epPort))
180181
}
181-
}
182-
if len(resolveOpts.PodReadinessGates) != 0 {
183-
for _, epAddr := range ep.Addresses {
184-
if used_addr.Has(epAddr) {
185-
continue
186-
}
187-
if ep.TargetRef == nil || ep.TargetRef.Kind != "Pod" {
188-
continue
189-
}
182+
if len(resolveOpts.PodReadinessGates) != 0 {
190183
pod, exists, err := r.findPodByReference(ctx, svc.Namespace, *ep.TargetRef)
191184
if err != nil {
192185
return nil, false, err
@@ -202,7 +195,7 @@ func (r *defaultEndpointResolver) ResolvePodEndpointsFromSlices(ctx context.Cont
202195
containsPotentialReadyEndpoints = true
203196
continue
204197
}
205-
used_addr.Insert(epAddr)
198+
usedAddrs.Insert(epAddr)
206199
endpoints = append(endpoints, buildPodEndpointFromSlice(pod, epAddr, epPort))
207200
}
208201
}

pkg/config/controller_config.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ const (
2020
flagTargetGroupBindingMaxConcurrentReconciles = "targetgroupbinding-max-concurrent-reconciles"
2121
flagTargetGroupBindingMaxExponentialBackoffDelay = "targetgroupbinding-max-exponential-backoff-delay"
2222
flagDefaultSSLPolicy = "default-ssl-policy"
23-
flagUseEndpointSlices = "use-endpoint-slices"
2423
flagEnableBackendSG = "enable-backend-security-group"
2524
flagBackendSecurityGroup = "backend-security-group"
25+
flagEnableEndpointSlices = "enable-endpoint-slices"
2626
defaultLogLevel = "info"
2727
defaultMaxConcurrentReconciles = 3
2828
defaultMaxExponentialBackoffDelay = time.Second * 1000
2929
defaultSSLPolicy = "ELBSecurityPolicy-2016-08"
30-
defaultUseEndpointSlices = false
3130
defaultEnableBackendSG = true
31+
defaultEnableEndpointSlices = false
3232
)
3333

3434
var (
@@ -68,8 +68,8 @@ type ControllerConfig struct {
6868
// the SSL Policy annotation.
6969
DefaultSSLPolicy string
7070

71-
// Whether to use EndpointSlices resources(true) or Endpoints resources(false) in endpoint and TGB resolution.
72-
UseEndpointSlices bool
71+
// Whether to use endpointslices resources(true) or Endpoints resources(false) in endpoint and TGB resolution.
72+
EnableEndpointSlices bool
7373

7474
// Max concurrent reconcile loops for Service objects
7575
ServiceMaxConcurrentReconciles int
@@ -107,8 +107,7 @@ func (cfg *ControllerConfig) BindFlags(fs *pflag.FlagSet) {
107107
"Enable sharing of security groups for backend traffic")
108108
fs.StringVar(&cfg.BackendSecurityGroup, flagBackendSecurityGroup, "",
109109
"Backend security group id to use for the ingress rules on the worker node SG")
110-
111-
fs.BoolVar(&cfg.UseEndpointSlices, flagUseEndpointSlices, defaultUseEndpointSlices,
110+
fs.BoolVar(&cfg.EnableEndpointSlices, flagEnableEndpointSlices, defaultEnableEndpointSlices,
112111
"Use EndpointSlices(true) or Endpoints(false) resources in endpoint and TGB resolution")
113112
cfg.AWSConfig.BindFlags(fs)
114113
cfg.RuntimeConfig.BindFlags(fs)

pkg/targetgroupbinding/resource_manager.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type ResourceManager interface {
3939
// NewDefaultResourceManager constructs new defaultResourceManager.
4040
func NewDefaultResourceManager(k8sClient client.Client, elbv2Client services.ELBV2, ec2Client services.EC2,
4141
podInfoRepo k8s.PodInfoRepo, sgManager networking.SecurityGroupManager, sgReconciler networking.SecurityGroupReconciler,
42-
vpcID string, clusterName string, eventRecorder record.EventRecorder, logger logr.Logger, useEPSlices bool) *defaultResourceManager {
42+
vpcID string, clusterName string, eventRecorder record.EventRecorder, logger logr.Logger, useEndpointSlices bool) *defaultResourceManager {
4343
targetsManager := NewCachedTargetsManager(elbv2Client, logger)
4444
endpointResolver := backend.NewDefaultEndpointResolver(k8sClient, podInfoRepo, logger)
4545

@@ -57,7 +57,7 @@ func NewDefaultResourceManager(k8sClient client.Client, elbv2Client services.ELB
5757
logger: logger,
5858

5959
targetHealthRequeueDuration: defaultTargetHealthRequeueDuration,
60-
useEndpointSlices: useEPSlices,
60+
enableEndpointSlices: useEndpointSlices,
6161
}
6262
}
6363

@@ -73,7 +73,7 @@ type defaultResourceManager struct {
7373
logger logr.Logger
7474

7575
targetHealthRequeueDuration time.Duration
76-
useEndpointSlices bool
76+
enableEndpointSlices bool
7777
}
7878

7979
func (m *defaultResourceManager) Reconcile(ctx context.Context, tgb *elbv2api.TargetGroupBinding) error {
@@ -108,7 +108,7 @@ func (m *defaultResourceManager) reconcileWithIPTargetType(ctx context.Context,
108108
var err error
109109

110110
// Decide whether to use Endpoints or EndpointSlices based on config flag
111-
if m.useEndpointSlices {
111+
if m.enableEndpointSlices {
112112
endpoints, containsPotentialReadyEndpoints, err = m.endpointResolver.ResolvePodEndpointsFromSlices(ctx, svcKey, tgb.Spec.ServiceRef.Port, resolveOpts...)
113113
} else {
114114
endpoints, containsPotentialReadyEndpoints, err = m.endpointResolver.ResolvePodEndpoints(ctx, svcKey, tgb.Spec.ServiceRef.Port, resolveOpts...)

0 commit comments

Comments
 (0)