Skip to content

Commit

Permalink
Fix crash on kube manager's service-lb-controller after v1.31.0. (#12…
Browse files Browse the repository at this point in the history
…8182)

* Fix crash on kube manager's service-lb-controller after v1.31.0.

* Update cmd/kube-controller-manager/app/controllermanager_test.go

Co-authored-by: Antonio Ojea <antonio.ojea.garcia@gmail.com>

---------

Co-authored-by: Antonio Ojea <antonio.ojea.garcia@gmail.com>
  • Loading branch information
carlory and aojea authored Oct 21, 2024
1 parent f1e447b commit 442183a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
26 changes: 26 additions & 0 deletions cmd/kube-controller-manager/app/controllermanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,29 @@ func TestTaintEvictionControllerGating(t *testing.T) {
})
}
}

func TestNoCloudProviderControllerStarted(t *testing.T) {
_, ctx := ktesting.NewTestContext(t)
ctx, cancel := context.WithCancel(ctx)
defer cancel()

controllerCtx := ControllerContext{
Cloud: nil,
LoopMode: IncludeCloudLoops,
}
controllerCtx.ComponentConfig.Generic.Controllers = []string{"*"}
for _, controller := range NewControllerDescriptors() {
if !controller.IsCloudProviderController() {
continue
}

controllerName := controller.Name()
checker, err := StartController(ctx, controllerCtx, controller, nil)
if err != nil {
t.Errorf("Error starting controller %q: %v", controllerName, err)
}
if checker != nil {
t.Errorf("Controller %q should not be started", controllerName)
}
}
}
13 changes: 12 additions & 1 deletion cmd/kube-controller-manager/app/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ func newServiceLBControllerDescriptor() *ControllerDescriptor {
}

func startServiceLBController(ctx context.Context, controllerContext ControllerContext, controllerName string) (controller.Interface, bool, error) {
logger := klog.FromContext(ctx)
if controllerContext.Cloud == nil {
logger.Info("Warning: service-controller is set, but no cloud provider specified. Will not configure service controller.")
return nil, false, nil
}

serviceController, err := servicecontroller.New(
controllerContext.Cloud,
controllerContext.ClientBuilder.ClientOrDie("service-controller"),
Expand All @@ -102,7 +108,7 @@ func startServiceLBController(ctx context.Context, controllerContext ControllerC
)
if err != nil {
// This error shouldn't fail. It lives like this as a legacy.
klog.FromContext(ctx).Error(err, "Failed to start service controller")
logger.Error(err, "Failed to start service controller.")
return nil, false, nil
}
go serviceController.Run(ctx, int(controllerContext.ComponentConfig.ServiceController.ConcurrentServiceSyncs), controllerContext.ControllerManagerMetrics)
Expand Down Expand Up @@ -261,6 +267,11 @@ func newCloudNodeLifecycleControllerDescriptor() *ControllerDescriptor {

func startCloudNodeLifecycleController(ctx context.Context, controllerContext ControllerContext, controllerName string) (controller.Interface, bool, error) {
logger := klog.FromContext(ctx)
if controllerContext.Cloud == nil {
logger.Info("Warning: node-controller is set, but no cloud provider specified. Will not configure node lifecyle controller.")
return nil, false, nil
}

cloudNodeLifecycleController, err := cloudnodelifecyclecontroller.NewCloudNodeLifecycleController(
controllerContext.InformerFactory.Core().V1().Nodes(),
// cloud node lifecycle controller uses existing cluster role from node-controller
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func New(
featureGate featuregate.FeatureGate,
) (*Controller, error) {
registerMetrics()

s := &Controller{
cloud: cloud,
kubeClient: kubeClient,
Expand All @@ -126,6 +127,10 @@ func New(
lastSyncedNodes: make(map[string][]*v1.Node),
}

if err := s.init(); err != nil {
return nil, err
}

serviceInformer.Informer().AddEventHandlerWithResyncPeriod(
cache.ResourceEventHandlerFuncs{
AddFunc: func(cur interface{}) {
Expand Down Expand Up @@ -180,10 +185,6 @@ func New(
nodeSyncPeriod,
)

if err := s.init(); err != nil {
return nil, err
}

return s, nil
}

Expand Down

0 comments on commit 442183a

Please sign in to comment.