Skip to content

Commit

Permalink
Merge pull request k0sproject#5490 from twz123/single-controller
Browse files Browse the repository at this point in the history
Disable leader election in single-controller clusters
  • Loading branch information
twz123 authored Feb 3, 2025
2 parents f68a783 + adf0968 commit ea288c3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
21 changes: 12 additions & 9 deletions cmd/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ func (c *command) start(ctx context.Context) error {
logrus.Infof("using storage backend %s", nodeConfig.Spec.Storage.Type)
nodeComponents.Add(ctx, storageBackend)

// Will the cluster support multiple controllers, or just a single one?
singleController := c.SingleNode || !nodeConfig.Spec.Storage.IsJoinable()

// Assume a single active controller during startup
numActiveControllers := value.NewLatest[uint](1)

Expand Down Expand Up @@ -300,7 +303,7 @@ func (c *command) start(ctx context.Context) error {
return fmt.Errorf("failed to determine node name: %w", err)
}

if !c.SingleNode {
if !singleController {
nodeComponents.Add(ctx, &controller.K0sControllersLeaseCounter{
NodeName: nodeName,
InvocationID: c.K0sVars.InvocationID,
Expand All @@ -316,12 +319,12 @@ func (c *command) start(ctx context.Context) error {
}

// One leader elector per controller
if !c.SingleNode {
if singleController {
leaderElector = &leaderelector.Dummy{Leader: true}
} else {
// The name used to be hardcoded in the component itself
// At some point we need to rename this.
leaderElector = leaderelector.NewLeasePool(c.K0sVars.InvocationID, adminClientFactory, "k0s-endpoint-reconciler")
} else {
leaderElector = &leaderelector.Dummy{Leader: true}
}
nodeComponents.Add(ctx, leaderElector)

Expand Down Expand Up @@ -561,17 +564,17 @@ func (c *command) start(ctx context.Context) error {

if !slices.Contains(c.DisableComponents, constant.KubeSchedulerComponentName) {
clusterComponents.Add(ctx, &controller.Scheduler{
LogLevel: c.LogLevels.KubeScheduler,
K0sVars: c.K0sVars,
SingleNode: c.SingleNode,
LogLevel: c.LogLevels.KubeScheduler,
K0sVars: c.K0sVars,
DisableLeaderElection: singleController,
})
}

if !slices.Contains(c.DisableComponents, constant.KubeControllerManagerComponentName) {
clusterComponents.Add(ctx, &controller.Manager{
LogLevel: c.LogLevels.KubeControllerManager,
K0sVars: c.K0sVars,
SingleNode: c.SingleNode,
DisableLeaderElection: singleController,
ServiceClusterIPRange: nodeConfig.Spec.Network.BuildServiceCIDR(nodeConfig.Spec.API.Address),
ExtraArgs: c.KubeControllerManagerExtraArgs,
})
Expand All @@ -591,7 +594,7 @@ func (c *command) start(ctx context.Context) error {
K0sVars: c.K0sVars,
KubeletExtraArgs: c.KubeletExtraArgs,
AdminClientFactory: adminClientFactory,
EnableWorker: c.EnableWorker,
Workloads: c.EnableWorker,
})

restConfig, err := adminClientFactory.GetRESTConfig()
Expand Down
4 changes: 2 additions & 2 deletions pkg/component/controller/autopilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Autopilot struct {
K0sVars *config.CfgVars
KubeletExtraArgs string
AdminClientFactory kubernetes.ClientFactoryInterface
EnableWorker bool
Workloads bool
}

func (a *Autopilot) Init(ctx context.Context) error {
Expand Down Expand Up @@ -66,7 +66,7 @@ func (a *Autopilot) Start(ctx context.Context) error {
ManagerPort: 8899,
MetricsBindAddr: "0",
HealthProbeBindAddr: "0",
}, logrus.WithFields(logrus.Fields{"component": "autopilot"}), a.EnableWorker, a.AdminClientFactory, autopilotClientFactory)
}, logrus.WithFields(logrus.Fields{"component": "autopilot"}), a.Workloads, a.AdminClientFactory, autopilotClientFactory)
if err != nil {
return fmt.Errorf("failed to create autopilot controller: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/component/controller/controllermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
type Manager struct {
K0sVars *config.CfgVars
LogLevel string
SingleNode bool
DisableLeaderElection bool
ServiceClusterIPRange string
ExtraArgs string

Expand Down Expand Up @@ -132,7 +132,7 @@ func (a *Manager) Reconcile(_ context.Context, clusterConfig *v1beta1.ClusterCon
args[name] = value
}
}
if a.SingleNode {
if a.DisableLeaderElection {
args["leader-elect"] = "false"
}

Expand Down
16 changes: 8 additions & 8 deletions pkg/component/controller/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ import (

// Scheduler implement the component interface to run kube scheduler
type Scheduler struct {
gid int
K0sVars *config.CfgVars
LogLevel string
SingleNode bool
supervisor *supervisor.Supervisor
uid int
previousConfig stringmap.StringMap
gid int
K0sVars *config.CfgVars
LogLevel string
DisableLeaderElection bool
supervisor *supervisor.Supervisor
uid int
previousConfig stringmap.StringMap
}

var _ manager.Component = (*Scheduler)(nil)
Expand Down Expand Up @@ -95,7 +95,7 @@ func (a *Scheduler) Reconcile(_ context.Context, clusterConfig *v1beta1.ClusterC
}
args[name] = value
}
if a.SingleNode {
if a.DisableLeaderElection {
args["leader-elect"] = "false"
}
args = clusterConfig.Spec.FeatureGates.BuildArgs(args, kubeSchedulerComponentName)
Expand Down

0 comments on commit ea288c3

Please sign in to comment.