Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
Signed-off-by: Daishan Peng <[email protected]>
  • Loading branch information
StrongMonkey committed Nov 2, 2023
1 parent 337919f commit 4c8f739
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
4 changes: 3 additions & 1 deletion controllers/service/service_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func (r *serviceReconciler) reconcile(ctx context.Context, req ctrl.Request) err
return client.IgnoreNotFound(err)
}
if svc.Annotations[service.LoadBalancerAllocatingPortKey] == "true" {
// AllocateService has to be locked to gaurantee thread-safe since it read/writes map concurrently
r.lock.Lock()
if err := r.allocatedService(ctx, svc); err != nil {
r.lock.Unlock()
Expand All @@ -129,7 +130,8 @@ func (r *serviceReconciler) reconcile(ctx context.Context, req ctrl.Request) err
return r.reconcileLoadBalancerResources(ctx, svc, stack, lb, backendSGRequired)
}

// allocatedService allocates a stack to a service so that it can share load balancer resources with other services.
// AllocateService makes sure that each service is allocated to a virtual stack, and a stack will not have more than 50 service/listener(the limit of listener on NLB).
// It maintains an in-memory cache to be able to track the usage. If no stack is available, it will create a new stack.
func (r *serviceReconciler) allocatedService(ctx context.Context, svc *corev1.Service) error {
if !r.initialized {
var serviceList corev1.ServiceList
Expand Down
22 changes: 13 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ import (
"sigs.k8s.io/aws-load-balancer-controller/pkg/aws"
"sigs.k8s.io/aws-load-balancer-controller/pkg/aws/throttle"
"sigs.k8s.io/aws-load-balancer-controller/pkg/config"
"sigs.k8s.io/aws-load-balancer-controller/pkg/inject"
"sigs.k8s.io/aws-load-balancer-controller/pkg/k8s"
"sigs.k8s.io/aws-load-balancer-controller/pkg/networking"
"sigs.k8s.io/aws-load-balancer-controller/pkg/runtime"
"sigs.k8s.io/aws-load-balancer-controller/pkg/targetgroupbinding"
"sigs.k8s.io/aws-load-balancer-controller/pkg/version"
corewebhook "sigs.k8s.io/aws-load-balancer-controller/webhooks/core"
elbv2webhook "sigs.k8s.io/aws-load-balancer-controller/webhooks/elbv2"
networkingwebhook "sigs.k8s.io/aws-load-balancer-controller/webhooks/networking"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
Expand Down Expand Up @@ -87,7 +91,7 @@ func main() {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
//config.ConfigureWebhookServer(controllerCFG.RuntimeConfig, mgr)
config.ConfigureWebhookServer(controllerCFG.RuntimeConfig, mgr)
clientSet, err := kubernetes.NewForConfig(mgr.GetConfig())
if err != nil {
setupLog.Error(err, "unable to obtain clientSet")
Expand Down Expand Up @@ -145,14 +149,14 @@ func main() {
os.Exit(1)
}

//podReadinessGateInjector := inject.NewPodReadinessGate(controllerCFG.PodWebhookConfig,
// mgr.GetClient(), ctrl.Log.WithName("pod-readiness-gate-injector"))
//corewebhook.NewPodMutator(podReadinessGateInjector).SetupWithManager(mgr)
//corewebhook.NewServiceMutator(controllerCFG.ServiceConfig.LoadBalancerClass, ctrl.Log).SetupWithManager(mgr)
//elbv2webhook.NewIngressClassParamsValidator().SetupWithManager(mgr)
//elbv2webhook.NewTargetGroupBindingMutator(cloud.ELBV2(), ctrl.Log).SetupWithManager(mgr)
//elbv2webhook.NewTargetGroupBindingValidator(mgr.GetClient(), cloud.ELBV2(), ctrl.Log).SetupWithManager(mgr)
//networkingwebhook.NewIngressValidator(mgr.GetClient(), controllerCFG.IngressConfig, ctrl.Log).SetupWithManager(mgr)
podReadinessGateInjector := inject.NewPodReadinessGate(controllerCFG.PodWebhookConfig,
mgr.GetClient(), ctrl.Log.WithName("pod-readiness-gate-injector"))
corewebhook.NewPodMutator(podReadinessGateInjector).SetupWithManager(mgr)
corewebhook.NewServiceMutator(controllerCFG.ServiceConfig.LoadBalancerClass, ctrl.Log).SetupWithManager(mgr)
elbv2webhook.NewIngressClassParamsValidator().SetupWithManager(mgr)
elbv2webhook.NewTargetGroupBindingMutator(cloud.ELBV2(), ctrl.Log).SetupWithManager(mgr)
elbv2webhook.NewTargetGroupBindingValidator(mgr.GetClient(), cloud.ELBV2(), ctrl.Log).SetupWithManager(mgr)
networkingwebhook.NewIngressValidator(mgr.GetClient(), controllerCFG.IngressConfig, ctrl.Log).SetupWithManager(mgr)
//+kubebuilder:scaffold:builder

go func() {
Expand Down
4 changes: 1 addition & 3 deletions pkg/service/model_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,10 @@ func (b *defaultModelBuilder) Build(ctx context.Context, service *corev1.Service
Namespace: "stack",
Name: svc.Annotations[LoadBalancerStackKey],
})
b.lock.Lock()
if b.stackGlobalCache[stackID] == nil {
b.lock.Lock()
b.stackGlobalCache[stackID] = core.NewDefaultStack(stackID)
b.lock.Unlock()
}
b.lock.Lock()
b.stackGlobalCache[stackID].AddService(&svc)
b.lock.Unlock()
}
Expand Down

0 comments on commit 4c8f739

Please sign in to comment.