Skip to content

Commit 6b9ee40

Browse files
authored
Merge pull request #1122 from gianlucam76/mgmt-cluster
(feat) drift-detection in management cluster
2 parents fb43d71 + deabedb commit 6b9ee40

20 files changed

+248
-131
lines changed

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ deploy-projectsveltos: $(KUSTOMIZE)
337337

338338
@echo 'Install libsveltos CRDs'
339339
$(KUBECTL) apply -f https://raw.githubusercontent.com/projectsveltos/libsveltos/$(TAG)/manifests/apiextensions.k8s.io_v1_customresourcedefinition_debuggingconfigurations.lib.projectsveltos.io.yaml
340+
$(KUBECTL) apply -f https://raw.githubusercontent.com/projectsveltos/libsveltos/$(TAG)/manifests/apiextensions.k8s.io_v1_customresourcedefinition_resourcesummaries.lib.projectsveltos.io.yaml
340341
$(KUBECTL) apply -f https://raw.githubusercontent.com/projectsveltos/libsveltos/$(TAG)/manifests/apiextensions.k8s.io_v1_customresourcedefinition_sveltosclusters.lib.projectsveltos.io.yaml
341342
$(KUBECTL) apply -f https://raw.githubusercontent.com/projectsveltos/libsveltos/$(TAG)/manifests/apiextensions.k8s.io_v1_customresourcedefinition_clustersets.lib.projectsveltos.io.yaml
342343
$(KUBECTL) apply -f https://raw.githubusercontent.com/projectsveltos/libsveltos/$(TAG)/manifests/apiextensions.k8s.io_v1_customresourcedefinition_sets.lib.projectsveltos.io.yaml

cmd/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ func main() {
181181
controllers.SetLuaConfigMap(luaConfigMap)
182182
controllers.SetCAPIOnboardAnnotation(capiOnboardAnnotation)
183183
controllers.SetDriftDetectionRegistry(registry)
184+
controllers.SetAgentInMgmtCluster(agentInMgmtCluster)
185+
184186
// Start dependency manager
185187
dependencymanager.InitializeManagerInstance(ctx, mgr.GetClient(), autoDeployDependencies, ctrl.Log.WithName("dependency_manager"))
186188

@@ -495,7 +497,6 @@ func getClusterSummaryReconciler(ctx context.Context, mgr manager.Manager) *cont
495497
ShardKey: shardKey,
496498
Version: version,
497499
ReportMode: reportMode,
498-
AgentInMgmtCluster: agentInMgmtCluster,
499500
Deployer: d,
500501
ClusterMap: make(map[corev1.ObjectReference]*libsveltosset.Set),
501502
ReferenceMap: make(map[corev1.ObjectReference]*libsveltosset.Set),

config/rbac/role.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ rules:
145145
- lib.projectsveltos.io
146146
resources:
147147
- clustersets
148+
- resourcesummaries
148149
- sets
149150
verbs:
150151
- create
@@ -180,6 +181,14 @@ rules:
180181
- get
181182
- list
182183
- watch
184+
- apiGroups:
185+
- lib.projectsveltos.io
186+
resources:
187+
- resourcesummaries/status
188+
verbs:
189+
- get
190+
- list
191+
- update
183192
- apiGroups:
184193
- source.toolkit.fluxcd.io
185194
resources:

controllers/clustersummary_controller.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ type ClusterSummaryReconciler struct {
9090
Scheme *runtime.Scheme
9191
Logger logr.Logger
9292
ReportMode ReportMode
93-
AgentInMgmtCluster bool // if true, indicates drift-detection-manager needs to be started in the management cluster
9493
ShardKey string // when set, only clusters matching the ShardKey will be reconciled
9594
Version string
9695
Deployer deployer.DeployerInterface
@@ -103,13 +102,18 @@ type ClusterSummaryReconciler struct {
103102
ctrl controller.Controller
104103
}
105104

105+
// If the drift-detection component is deployed in the management cluster, the addon-controller will deploy ResourceSummaries within the same cluster,
106+
// thus requiring the necessary permissions.
107+
106108
//+kubebuilder:rbac:groups=config.projectsveltos.io,resources=clustersummaries,verbs=get;list;watch;create;update;patch;delete
107109
//+kubebuilder:rbac:groups=config.projectsveltos.io,resources=clustersummaries/status,verbs=get;update;patch
108110
//+kubebuilder:rbac:groups=config.projectsveltos.io,resources=clustersummaries/finalizers,verbs=update;patch
109111
//+kubebuilder:rbac:groups=config.projectsveltos.io,resources=clusterconfigurations,verbs=get;list;watch
110112
//+kubebuilder:rbac:groups=config.projectsveltos.io,resources=clusterconfigurations/status,verbs=get;list;update
111113
//+kubebuilder:rbac:groups=config.projectsveltos.io,resources=clusterreports,verbs=get;list;watch
112114
//+kubebuilder:rbac:groups=config.projectsveltos.io,resources=clusterreports/status,verbs=get;list;update
115+
//+kubebuilder:rbac:groups=lib.projectsveltos.io,resources=resourcesummaries,verbs=get;list;watch;create;update;patch;delete
116+
//+kubebuilder:rbac:groups=lib.projectsveltos.io,resources=resourcesummaries/status,verbs=get;list;update
113117
//+kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch
114118
//+kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch
115119
//+kubebuilder:rbac:groups=controlplane.cluster.x-k8s.io,resources=kubeadmcontrolplanes,verbs=get;watch;list
@@ -431,10 +435,11 @@ func (r *ClusterSummaryReconciler) SetupWithManager(ctx context.Context, mgr ctr
431435
// Later on, in main, we detect that and if CAPI is present WatchForCAPI will be invoked.
432436

433437
if r.ReportMode == CollectFromManagementCluster {
434-
go collectAndProcessResourceSummaries(ctx, mgr.GetClient(), r.ShardKey, r.Version, mgr.GetLogger())
438+
go collectAndProcessResourceSummaries(ctx, mgr.GetClient(), getAgentInMgmtCluster(), r.ShardKey,
439+
r.Version, mgr.GetLogger())
435440
}
436441

437-
if r.AgentInMgmtCluster {
442+
if getAgentInMgmtCluster() {
438443
go removeStaleDriftDetectionManager(ctx, r.Logger)
439444
}
440445

@@ -1135,14 +1140,8 @@ func (r *ClusterSummaryReconciler) removeResourceSummary(ctx context.Context,
11351140
// ResourceSummary is a Sveltos resource deployed in managed clusters.
11361141
// Such resources are always created, removed using cluster-admin roles.
11371142
cs := clusterSummaryScope.ClusterSummary
1138-
remoteClient, err := clusterproxy.GetKubernetesClient(ctx, r.Client, cs.Spec.ClusterNamespace,
1139-
cs.Spec.ClusterName, "", "", cs.Spec.ClusterType, logger)
1140-
if err != nil {
1141-
return err
1142-
}
1143-
1144-
err = unDeployResourceSummaryInstance(ctx, remoteClient, cs.Spec.ClusterNamespace,
1145-
cs.Name, logger)
1143+
err := unDeployResourceSummaryInstance(ctx, cs.Spec.ClusterNamespace, cs.Spec.ClusterName,
1144+
cs.Name, cs.Spec.ClusterType, logger)
11461145
if err != nil {
11471146
if apierrors.IsNotFound(err) {
11481147
return nil

controllers/clustersummary_deployer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func (r *ClusterSummaryReconciler) proceedDeployingFeature(ctx context.Context,
164164
// Getting here means either feature failed to be deployed or configuration has changed.
165165
// Feature must be (re)deployed.
166166
options := deployer.Options{HandlerOptions: map[string]string{}}
167-
if r.AgentInMgmtCluster {
167+
if getAgentInMgmtCluster() {
168168
options.HandlerOptions[driftDetectionInMgtmCluster] = "management"
169169
}
170170

controllers/export_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ var (
153153
var (
154154
DeployDebuggingConfigurationCRD = deployDebuggingConfigurationCRD
155155
DeployResourceSummaryCRD = deployResourceSummaryCRD
156+
DeployDriftDetectionManagerInCluster = deployDriftDetectionManagerInCluster
156157
DeployResourceSummaryInCluster = deployResourceSummaryInCluster
157158
DeployResourceSummaryInstance = deployResourceSummaryInstance
158159
UpdateDeployedGroupVersionKind = updateDeployedGroupVersionKind
@@ -162,8 +163,8 @@ var (
162163
GetDriftDetectionNamespaceInMgmtCluster = getDriftDetectionNamespaceInMgmtCluster
163164
TransformDriftExclusionsToPatches = transformDriftExclusionsToPatches
164165

165-
GetResourceSummaryNamespace = getResourceSummaryNamespace
166-
GetResourceSummaryName = getResourceSummaryName
166+
GetResourceSummaryNamespaceInManagedCluster = getResourceSummaryNamespaceInManagedCluster
167+
GetResourceSummaryNameInManagedCluster = getResourceSummaryNameInManagedCluster
167168
)
168169

169170
var (

controllers/handlers_utils.go

+6
Original file line numberDiff line numberDiff line change
@@ -1977,6 +1977,12 @@ func getClusterProfileSpecHash(ctx context.Context, clusterSummary *configv1beta
19771977
// or viceversa) reconcile.
19781978
config += fmt.Sprintf("%v", clusterProfileSpec.SyncMode)
19791979

1980+
// When using ContinuousWithDriftDetection in agentless mode, ResourceSummary instances are now managed in the management cluster.
1981+
// This addition ensures the ClusterSummary is redeployed due to the change in deployment location.
1982+
if clusterProfileSpec.SyncMode == configv1beta1.SyncModeContinuousWithDriftDetection && getAgentInMgmtCluster() {
1983+
config += ("agentless")
1984+
}
1985+
19801986
// If Reloader changes, Reloader needs to be deployed or undeployed
19811987
// So consider it in the hash
19821988
config += fmt.Sprintf("%v", clusterProfileSpec.Reloader)

controllers/management_cluster.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ var (
3232
luaConfigMap string
3333
capiOnboardAnnotation string
3434
driftDetectionRegistry string
35+
agentInMgmtCluster bool
3536
)
3637

3738
func SetManagementClusterAccess(c client.Client, config *rest.Config) {
@@ -51,6 +52,14 @@ func SetCAPIOnboardAnnotation(key string) {
5152
capiOnboardAnnotation = key
5253
}
5354

55+
func SetDriftDetectionRegistry(reg string) {
56+
driftDetectionRegistry = reg
57+
}
58+
59+
func SetAgentInMgmtCluster(isInMgmtCluster bool) {
60+
agentInMgmtCluster = isInMgmtCluster
61+
}
62+
5463
func getManagementClusterConfig() *rest.Config {
5564
return managementClusterConfig
5665
}
@@ -71,8 +80,12 @@ func getCAPIOnboardAnnotation() string {
7180
return capiOnboardAnnotation
7281
}
7382

74-
func SetDriftDetectionRegistry(reg string) {
75-
driftDetectionRegistry = reg
83+
func getDriftDetectionRegistry() string {
84+
return driftDetectionRegistry
85+
}
86+
87+
func getAgentInMgmtCluster() bool {
88+
return agentInMgmtCluster
7689
}
7790

7891
func collectDriftDetectionConfigMap(ctx context.Context) (*corev1.ConfigMap, error) {
@@ -100,7 +113,3 @@ func collectLuaConfigMap(ctx context.Context) (*corev1.ConfigMap, error) {
100113

101114
return configMap, nil
102115
}
103-
104-
func getDriftDetectionRegistry() string {
105-
return driftDetectionRegistry
106-
}

0 commit comments

Comments
 (0)