Skip to content

Commit 531079c

Browse files
chore(controllers): Don't run controlplane specific controllers on workers
Signed-off-by: Homayoon (Hue) Alimohammadi <[email protected]>
1 parent c8f91e8 commit 531079c

File tree

5 files changed

+76
-47
lines changed

5 files changed

+76
-47
lines changed

src/k8s/pkg/k8sd/app/app.go

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -189,29 +189,31 @@ func New(cfg Config) (*App, error) {
189189
app.controllerCoordinator = controllers.NewCoordinator(
190190
cfg.Snap,
191191
app.readyWg.Wait,
192-
cfg.DisableUpgradeController,
193-
upgrade.ControllerOptions{
194-
FeatureControllerReadyCh: app.featureController.ReadyCh(),
195-
NotifyNetworkFeature: app.NotifyNetwork,
196-
NotifyGatewayFeature: app.NotifyGateway,
197-
NotifyIngressFeature: app.NotifyIngress,
198-
NotifyLoadBalancerFeature: app.NotifyLoadBalancer,
199-
NotifyLocalStorageFeature: app.NotifyLocalStorage,
200-
NotifyMetricsServerFeature: app.NotifyMetricsServer,
201-
NotifyDNSFeature: app.NotifyDNS,
202-
FeatureToReconciledCh: map[types.FeatureName]<-chan struct{}{
203-
features.Network: app.featureController.ReconciledNetworkCh(),
204-
features.Gateway: app.featureController.ReconciledGatewayCh(),
205-
features.Ingress: app.featureController.ReconciledIngressCh(),
206-
features.DNS: app.featureController.ReconciledDNSCh(),
207-
features.LoadBalancer: app.featureController.ReconciledLoadBalancerCh(),
208-
features.LocalStorage: app.featureController.ReconciledLocalStorageCh(),
209-
features.MetricsServer: app.featureController.ReconciledMetricsServerCh(),
192+
controllers.UpgradeControllerOptions{
193+
Disable: cfg.DisableUpgradeController,
194+
ControllerOptions: upgrade.ControllerOptions{
195+
FeatureControllerReadyCh: app.featureController.ReadyCh(),
196+
NotifyNetworkFeature: app.NotifyNetwork,
197+
NotifyGatewayFeature: app.NotifyGateway,
198+
NotifyIngressFeature: app.NotifyIngress,
199+
NotifyLoadBalancerFeature: app.NotifyLoadBalancer,
200+
NotifyLocalStorageFeature: app.NotifyLocalStorage,
201+
NotifyMetricsServerFeature: app.NotifyMetricsServer,
202+
NotifyDNSFeature: app.NotifyDNS,
203+
FeatureToReconciledCh: map[types.FeatureName]<-chan struct{}{
204+
features.Network: app.featureController.ReconciledNetworkCh(),
205+
features.Gateway: app.featureController.ReconciledGatewayCh(),
206+
features.Ingress: app.featureController.ReconciledIngressCh(),
207+
features.DNS: app.featureController.ReconciledDNSCh(),
208+
features.LoadBalancer: app.featureController.ReconciledLoadBalancerCh(),
209+
features.LocalStorage: app.featureController.ReconciledLocalStorageCh(),
210+
features.MetricsServer: app.featureController.ReconciledMetricsServerCh(),
211+
},
210212
},
211-
FeatureControllerReadyTimeout: 10 * time.Minute,
212-
FeatureControllerReconcileTimeout: 2 * time.Minute,
213213
},
214-
cfg.DisableCSRSigningController,
214+
controllers.CSRSigningControllerOptions{
215+
Disable: cfg.DisableCSRSigningController,
216+
},
215217
)
216218

217219
return app, nil

src/k8s/pkg/k8sd/app/hooks_start.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,18 @@ func (a *App) onStart(ctx context.Context, s state.State) error {
124124
}
125125

126126
// start controller coordinator
127-
go func() {
128-
if err := a.controllerCoordinator.Run(
129-
ctx,
130-
func(ctx context.Context) (types.ClusterConfig, error) {
131-
return databaseutil.GetClusterConfig(ctx, s)
132-
},
133-
); err != nil {
134-
log.FromContext(ctx).Error(err, "Failed to start controller coordinator")
135-
}
136-
}()
127+
if a.controllerCoordinator != nil {
128+
go func() {
129+
if err := a.controllerCoordinator.Run(
130+
ctx,
131+
func(ctx context.Context) (types.ClusterConfig, error) {
132+
return databaseutil.GetClusterConfig(ctx, s)
133+
},
134+
); err != nil {
135+
log.FromContext(ctx).Error(err, "Failed to start controller coordinator")
136+
}
137+
}()
138+
}
137139

138140
return nil
139141
}

src/k8s/pkg/k8sd/controllers/coordinator.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/canonical/k8s/pkg/k8sd/types"
1313
"github.com/canonical/k8s/pkg/log"
1414
"github.com/canonical/k8s/pkg/snap"
15+
snaputil "github.com/canonical/k8s/pkg/snap/util"
1516
"github.com/canonical/k8s/pkg/utils"
1617
"k8s.io/client-go/rest"
1718
"sigs.k8s.io/controller-runtime/pkg/cache"
@@ -25,28 +26,31 @@ type Coordinator struct {
2526
snap snap.Snap
2627
waitReady func()
2728

28-
// Upgrade controller
29-
disableUpgradeController bool
30-
upgradeControllerOpts upgrade.ControllerOptions
29+
upgradeControllerOptions UpgradeControllerOptions
30+
csrSigningControllerOptions CSRSigningControllerOptions
31+
}
32+
33+
type UpgradeControllerOptions struct {
34+
upgrade.ControllerOptions
35+
Disable bool
36+
}
3137

32-
// CSR signing controller
33-
disableCSRSigningController bool
38+
type CSRSigningControllerOptions struct {
39+
Disable bool
3440
}
3541

3642
// NewCoordinator creates a new Coordinator instance.
3743
func NewCoordinator(
3844
snap snap.Snap,
3945
waitReady func(),
40-
disableUpgradeController bool,
41-
upgradeControllerOpts upgrade.ControllerOptions,
42-
disableCSRSiningController bool,
46+
upgradeControllerOptions UpgradeControllerOptions,
47+
csrSigningControllerOptions CSRSigningControllerOptions,
4348
) *Coordinator {
4449
return &Coordinator{
4550
snap: snap,
4651
waitReady: waitReady,
47-
disableUpgradeController: disableUpgradeController,
48-
upgradeControllerOpts: upgradeControllerOpts,
49-
disableCSRSigningController: disableCSRSiningController,
52+
upgradeControllerOptions: upgradeControllerOptions,
53+
csrSigningControllerOptions: csrSigningControllerOptions,
5054
}
5155
}
5256

@@ -56,6 +60,16 @@ func (c *Coordinator) Run(
5660
getClusterConfig func(context.Context) (types.ClusterConfig, error),
5761
) error {
5862
logger := log.FromContext(ctx).WithName("controller-coordinator")
63+
64+
isWorker, err := snaputil.IsWorker(c.snap)
65+
if err != nil {
66+
return fmt.Errorf("failed to determine if snap is running as worker: %w", err)
67+
}
68+
if isWorker {
69+
logger.Info("Skipping controller coordinator on worker node")
70+
return nil
71+
}
72+
5973
ctrllog.SetLogger(logger)
6074
ctx = log.NewContext(ctx, logger)
6175

@@ -132,7 +146,7 @@ func (c *Coordinator) setupUpgradeController(
132146
) error {
133147
logger := mgr.GetLogger()
134148

135-
if c.disableUpgradeController {
149+
if c.upgradeControllerOptions.Disable {
136150
logger.Info("Upgrade controller is disabled. Skipping setup.")
137151
return nil
138152
}
@@ -150,7 +164,7 @@ func (c *Coordinator) setupUpgradeController(
150164
upgradeController := upgrade.NewController(
151165
logger,
152166
mgr.GetClient(),
153-
c.upgradeControllerOpts,
167+
c.upgradeControllerOptions.ControllerOptions,
154168
)
155169

156170
if err := upgradeController.SetupWithManager(mgr); err != nil {
@@ -166,7 +180,7 @@ func (c *Coordinator) setupCSRSigningController(
166180
) error {
167181
logger := mgr.GetLogger()
168182

169-
if c.disableCSRSigningController {
183+
if c.csrSigningControllerOptions.Disable {
170184
logger.Info("CSR signing controller is disabled. Skipping setup.")
171185
return nil
172186
}

src/k8s/pkg/k8sd/controllers/feature.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/canonical/k8s/pkg/k8sd/types"
1313
"github.com/canonical/k8s/pkg/log"
1414
"github.com/canonical/k8s/pkg/snap"
15+
snaputil "github.com/canonical/k8s/pkg/snap/util"
1516
"github.com/canonical/k8s/pkg/utils"
1617
timeutils "github.com/canonical/k8s/pkg/utils/time"
1718
"github.com/canonical/microcluster/v2/state"
@@ -142,9 +143,19 @@ func (c *FeatureController) Run(
142143
) {
143144
ctx = log.NewContext(ctx, log.FromContext(ctx).WithValues("controller", "feature"))
144145
log := log.FromContext(ctx)
145-
log.Info("Starting feature controller")
146146
c.waitReady()
147147

148+
isWorker, err := snaputil.IsWorker(c.snap)
149+
if err != nil {
150+
log.Error(err, "Failed to determine if snap is running as worker")
151+
}
152+
if isWorker {
153+
log.Info("Skipping feature controller on worker node")
154+
return
155+
}
156+
157+
log.Info("Starting feature controller")
158+
148159
s := getState()
149160

150161
go c.reconcileLoop(ctx, getClusterConfig, setFeatureStatus, features.Network, c.triggerNetworkCh, c.reconciledNetworkCh, func(cfg types.ClusterConfig) (types.FeatureStatus, error) {

src/k8s/pkg/k8sd/controllers/node_label.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ func (c *NodeLabelController) reconcile(ctx context.Context, client *kubernetes.
172172

173173
var errs []error
174174

175-
log.FromContext(ctx).Info("Reconciling node failure domain", "nodeName", node.Name)
176175
if !isWorker {
176+
log.FromContext(ctx).Info("Reconciling node failure domain", "nodeName", node.Name)
177177
if err := c.reconcileFailureDomain(ctx, node, getDatastoreType); err != nil {
178178
errs = append(errs, fmt.Errorf("failed to reconcile failure domain: %w", err))
179179
}

0 commit comments

Comments
 (0)