Skip to content

Commit 4b69768

Browse files
authored
Follow-up PR to disable local agent (#5381)
* Follow-up PR to disable local agent There was a problem when testing the code merged in in Rancher, as the implementation was using the bootstrap controller + a label in the Cluster to disable the local agent. Rancher does not enable the boostrap controller, so that approach is only valid for Fleet standalone. This PR is a follow-up to that approach and tracks the value using a config-driven approach this time. Refers to: #4807 --------- Signed-off-by: Xavi Garcia <xavi.garcia@suse.com>
1 parent 8b304fc commit 4b69768

7 files changed

Lines changed: 192 additions & 221 deletions

File tree

internal/cmd/controller/agentmanagement/controllers/bootstrap/bootstrap.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,6 @@ func (h *handler) OnConfig(config *fleetconfig.Config) error {
7777
maps.Copy(localClusterLabels, config.Bootstrap.ClusterLabels)
7878
}
7979

80-
if config.Bootstrap.LocalAgentDisabled {
81-
localClusterLabels[fleet.LocalAgentDisabledLabel] = "true"
82-
}
83-
8480
if config.Bootstrap.Namespace == "" || config.Bootstrap.Namespace == "-" {
8581
return nil
8682
}

internal/cmd/controller/agentmanagement/controllers/bootstrap/bootstrap_test.go

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ func TestOnConfig_AppliesClusterLabels(t *testing.T) {
2222
cases := []struct {
2323
name string
2424
configLabels map[string]string
25-
agentDisabled bool
2625
expectedLabels map[string]string
2726
}{
2827
{
@@ -43,43 +42,15 @@ func TestOnConfig_AppliesClusterLabels(t *testing.T) {
4342
"random-hash-12345": "enabled",
4443
},
4544
},
46-
{
47-
name: "agentDisabled stamps the local-agent-disabled label",
48-
agentDisabled: true,
49-
expectedLabels: map[string]string{
50-
"name": "local",
51-
fleet.LocalAgentDisabledLabel: "true",
52-
},
53-
},
54-
{
55-
name: "agentDisabled false leaves the label off",
56-
agentDisabled: false,
57-
expectedLabels: map[string]string{
58-
"name": "local",
59-
},
60-
},
61-
{
62-
name: "agentDisabled coexists with user-supplied clusterLabels",
63-
agentDisabled: true,
64-
configLabels: map[string]string{
65-
"region": "eu-west-1",
66-
},
67-
expectedLabels: map[string]string{
68-
"name": "local",
69-
"region": "eu-west-1",
70-
fleet.LocalAgentDisabledLabel: "true",
71-
},
72-
},
7345
}
7446

7547
for _, c := range cases {
7648
t.Run(c.name, func(t *testing.T) {
7749

7850
inputConfig := &fleetconfig.Config{
7951
Bootstrap: fleetconfig.Bootstrap{
80-
Namespace: "bootstrap-ns", // not empty
81-
ClusterLabels: c.configLabels,
82-
LocalAgentDisabled: c.agentDisabled,
52+
Namespace: "bootstrap-ns", // not empty
53+
ClusterLabels: c.configLabels,
8354
},
8455
}
8556

internal/cmd/controller/agentmanagement/controllers/cluster/import.go

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ func RegisterImport(
8989
clusters.OnChange(ctx, "import-cluster", h.OnChange)
9090
fleetcontrollers.RegisterClusterStatusHandler(ctx, clusters, "Imported", "import-cluster", h.importCluster)
9191
config.OnChange(ctx, h.onConfig)
92+
config.OnChange(ctx, h.enqueueLocalClusterOnConfig)
9293

9394
clustersCache := clusters.Cache()
9495
clustersCache.AddIndexer(clusterForKubeconfigSecretIndexer, func(cluster *fleet.Cluster) ([]string, error) {
@@ -160,6 +161,18 @@ func (i *importHandler) onConfig(cfg *config.Config) error {
160161
return nil
161162
}
162163

164+
// enqueueLocalClusterOnConfig re-evaluates the local cluster whenever the global
165+
// config changes, so that flipping Bootstrap.LocalAgentDisabled triggers
166+
// teardown (or redeploy) of its agent through importCluster. The config is
167+
// the single source of truth; see manageagent.LocalAgentDisabled.
168+
func (i *importHandler) enqueueLocalClusterOnConfig(cfg *config.Config) error {
169+
if cfg == nil || cfg.Bootstrap.Namespace == "" || cfg.Bootstrap.Namespace == "-" {
170+
return nil
171+
}
172+
i.clusters.Enqueue(cfg.Bootstrap.Namespace, fleet.LocalClusterName)
173+
return nil
174+
}
175+
163176
// hasAPIServerConfigChanged checks for changes in API server URL or CA configuration, comparing the current state of
164177
// the cluster with cfg. However, if the cluster references a secret through its `KubeConfigSecret` field, then API
165178
// server URL and CA are understood to be sourced from there, hence config changes for those fields will be skipped.
@@ -196,20 +209,11 @@ func hashStatusField(field any) string {
196209
}
197210

198211
// localAgentDisabled reports whether the agent must not be deployed for this
199-
// cluster. It only ever applies to the management (local) cluster: the
200-
// disabling label is honored exclusively there, so that labeling a downstream
201-
// cluster cannot prevent its agent from being deployed or tear it down.
212+
// cluster. It only ever applies to the management (local) cluster and is driven
213+
// by the global config, so it is honored even when the bootstrap controller is
214+
// disabled (e.g. under Rancher). See manageagent.LocalAgentDisabled.
202215
func localAgentDisabled(cluster *fleet.Cluster) bool {
203-
return isLocalCluster(cluster) && cluster.Labels[fleet.LocalAgentDisabledLabel] == "true"
204-
}
205-
206-
// isLocalCluster is a fast, metadata-only check that a Cluster object is the
207-
// management (local) cluster: it must carry the well-known name in the
208-
// configured bootstrap namespace, matching what the bootstrap controller
209-
// creates.
210-
func isLocalCluster(cluster *fleet.Cluster) bool {
211-
return cluster.Name == fleet.LocalClusterName &&
212-
cluster.Namespace == config.Get().Bootstrap.Namespace
216+
return manageagent.LocalAgentDisabled(cluster)
213217
}
214218

215219
// isManagementCluster confirms, with certainty, that the cluster reachable
@@ -568,17 +572,17 @@ func (i *importHandler) teardownLocalAgent(cluster *fleet.Cluster) error {
568572
}
569573

570574
// Before deleting anything, make absolutely sure the kubeconfig points at
571-
// the management cluster itself. isLocalCluster already gated us here based
572-
// on metadata, but tearing down an agent is destructive, so we confirm
573-
// cluster identity for real.
575+
// the management cluster itself. manageagent.IsLocalCluster already gated us
576+
// here based on metadata, but tearing down an agent is destructive, so we
577+
// confirm cluster identity for real.
574578
isLocal, err := i.isManagementCluster(kc)
575579
if err != nil {
576580
return err
577581
}
578582
if !isLocal {
579583
logrus.Warnf(
580-
"Cluster import for '%s/%s'. Refusing to remove agent: label %s=true is set but the cluster reached through its kubeconfig is not the management cluster",
581-
cluster.Namespace, cluster.Name, fleet.LocalAgentDisabledLabel,
584+
"Cluster import for '%s/%s'. Refusing to remove agent: localAgentDisabled is set but the cluster reached through its kubeconfig is not the management cluster",
585+
cluster.Namespace, cluster.Name,
582586
)
583587
return nil
584588
}
@@ -606,7 +610,7 @@ func (i *importHandler) teardownLocalAgent(cluster *fleet.Cluster) error {
606610
return err
607611
}
608612

609-
logrus.Infof("Cluster import for '%s/%s'. Removed local agent (%s=true)", cluster.Namespace, cluster.Name, fleet.LocalAgentDisabledLabel)
613+
logrus.Infof("Cluster import for '%s/%s'. Removed local agent (localAgentDisabled=true)", cluster.Namespace, cluster.Name)
610614
return nil
611615
}
612616

0 commit comments

Comments
 (0)