Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions controllers/classifier_deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,8 @@ func (r *ClassifierReconciler) getCurrentHash(ctx context.Context, classifierSco
currentHash = h.Sum(nil)
}

currentHash = addAgentSettingsToHash(currentHash)

var kubeconfig []byte
if r.ClassifierReportMode == AgentSendReportsNoGateway {
h := sha256.New()
Expand All @@ -1185,6 +1187,20 @@ func (r *ClassifierReconciler) getCurrentHash(ctx context.Context, classifierSco
return currentHash, nil
}

// addAgentSettingsToHash adds to the hash the classifier-manager settings that shape the
// sveltos-agent/sveltos-applier YAML (registry, NATS watcher). They are process flags, not part
// of the Classifier, patches or cluster annotations: without them a restart with a different value
// leaves the hash unchanged, and processClassifier skips the redeploy.
// Both are always hashed, so that going back to the default value (empty registry, NATS disabled)
// is detected as well.
func addAgentSettingsToHash(currentHash []byte) []byte {
h := sha256.New()
h.Write(currentHash)
h.Write([]byte(getSveltosAgentRegistry()))
h.Write([]byte(strconv.FormatBool(getSveltosAgentEnableNATS())))
return h.Sum(nil)
}

// processClassifier detect whether it is needed to deploy Classifier in current passed cluster.
func (r *ClassifierReconciler) processClassifier(ctx context.Context, classifierScope *scope.ClassifierScope,
cpEndpoint string, cluster *corev1.ObjectReference, f feature, logger logr.Logger,
Expand Down
30 changes: 29 additions & 1 deletion controllers/classifier_deployer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,33 @@ var _ = Describe("Classifier Deployer", func() {
Expect(reflect.DeepEqual(currentHash, hash)).To(BeTrue())
})

It("addAgentSettingsToHash changes the hash when the registry or the NATS watcher change", func() {
defer controllers.SetSveltosAgentRegistry("")
defer controllers.SetSveltosAgentEnableNATS(false)

baseHash := controllers.ClassifierHash(getClassifierInstance(randomString()))

controllers.SetSveltosAgentRegistry("")
controllers.SetSveltosAgentEnableNATS(false)
defaultHash := controllers.AddAgentSettingsToHash(baseHash)
Expect(controllers.AddAgentSettingsToHash(baseHash)).To(Equal(defaultHash))

controllers.SetSveltosAgentRegistry("registry-a.example")
registryAHash := controllers.AddAgentSettingsToHash(baseHash)
Expect(registryAHash).ToNot(Equal(defaultHash))

controllers.SetSveltosAgentRegistry("registry-b.example")
registryBHash := controllers.AddAgentSettingsToHash(baseHash)
Expect(registryBHash).ToNot(Equal(registryAHash))

// Going back to the default registry is detected as well
controllers.SetSveltosAgentRegistry("")
Expect(controllers.AddAgentSettingsToHash(baseHash)).To(Equal(defaultHash))

controllers.SetSveltosAgentEnableNATS(true)
Expect(controllers.AddAgentSettingsToHash(baseHash)).ToNot(Equal(defaultHash))
})

It("deployClassifierCRD deploys Classifier CRD", func() {
cluster := prepareCluster()

Expand Down Expand Up @@ -333,7 +360,8 @@ var _ = Describe("Classifier Deployer", func() {

Expect(waitForObject(context.TODO(), testEnv.Client, classifier)).To(Succeed())

hash := controllers.ClassifierHash(classifier)
// Same hash getCurrentHash computes when there are no patches, watch namespaces or kubeconfig
hash := controllers.AddAgentSettingsToHash(controllers.ClassifierHash(classifier))

// Create a ClassifierReport for the cluster carrying the current hash and provisioned status
// so that processClassifier detects the configuration is already deployed.
Expand Down
1 change: 1 addition & 0 deletions controllers/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var (
DeploySveltosAgentInManagedCluster = deploySveltosAgentInManagedCluster
PrepareSveltosApplierYAML = prepareSveltosApplierYAML
ClassifierHash = classifierHash
AddAgentSettingsToHash = addAgentSettingsToHash
DeployClassifierInCluster = deployClassifierInCluster
UndeployClassifierFromCluster = undeployClassifierFromCluster
RemoveClassifierInstance = removeClassifierInstance
Expand Down
Loading