Skip to content

Commit da0e58c

Browse files
authored
Merge pull request #1397 from gianlucam76/dependency-sort
(bug) dependencies: sort before evaluating hash
2 parents 48b9521 + 06ff64e commit da0e58c

File tree

6 files changed

+59
-16
lines changed

6 files changed

+59
-16
lines changed

controllers/clusterprofile_controller_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,17 @@ var _ = Describe("Profile: Reconciler", func() {
248248
err = testEnv.Get(context.TODO(), clusterProfileName, currentClusterProfile)
249249
Expect(err).ToNot(HaveOccurred())
250250

251+
Eventually(func() bool {
252+
currentClusterSummary := &configv1beta1.ClusterSummary{}
253+
err = testEnv.Get(context.TODO(),
254+
types.NamespacedName{Namespace: clusterSummary.Namespace, Name: clusterSummary.Name},
255+
currentClusterSummary)
256+
if err != nil {
257+
return false
258+
}
259+
return !currentClusterSummary.DeletionTimestamp.IsZero()
260+
}, timeout, pollingInterval).Should(BeTrue())
261+
251262
// Remove ClusterSummary finalizer
252263
currentClusterSummary := &configv1beta1.ClusterSummary{}
253264
Expect(testEnv.Get(context.TODO(),

controllers/dependencymanager/manager.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"crypto/sha256"
2222
"fmt"
23+
"sort"
2324
"sync"
2425
"sync/atomic"
2526
"time"
@@ -409,7 +410,15 @@ func calculateHash(data map[corev1.ObjectReference]RequestingProfiles) []byte {
409410
h := sha256.New()
410411
var config string
411412

413+
// 1. Extract all keys into a slice.
414+
keys := make([]corev1.ObjectReference, 0, len(data))
412415
for k := range data {
416+
keys = append(keys, k)
417+
}
418+
419+
sort.Sort(SortedCorev1ObjectReference(keys))
420+
421+
for k := range keys {
413422
config += render.AsCode(k)
414423
}
415424

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Copyright 2026. projectsveltos.io. All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package dependencymanager
18+
19+
import (
20+
corev1 "k8s.io/api/core/v1"
21+
)
22+
23+
type SortedCorev1ObjectReference []corev1.ObjectReference
24+
25+
func (a SortedCorev1ObjectReference) Len() int { return len(a) }
26+
func (a SortedCorev1ObjectReference) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
27+
func (a SortedCorev1ObjectReference) Less(i, j int) bool {
28+
if a[i].Kind != a[j].Kind {
29+
return a[i].Kind < a[j].Kind
30+
}
31+
if a[i].Namespace != a[j].Namespace {
32+
return a[i].Namespace < a[j].Namespace
33+
}
34+
return a[i].Name < a[j].Name
35+
}

controllers/handlers_resources.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535

3636
configv1beta1 "github.com/projectsveltos/addon-controller/api/v1beta1"
3737
"github.com/projectsveltos/addon-controller/controllers/clustercache"
38+
"github.com/projectsveltos/addon-controller/controllers/dependencymanager"
3839
"github.com/projectsveltos/addon-controller/lib/clusterops"
3940
libsveltosv1beta1 "github.com/projectsveltos/libsveltos/api/v1beta1"
4041
"github.com/projectsveltos/libsveltos/lib/clusterproxy"
@@ -525,7 +526,7 @@ func resourcesHash(ctx context.Context, c client.Client, clusterSummary *configv
525526
}
526527
}
527528

528-
sort.Sort(SortedCorev1ObjectReference(referencedObjects))
529+
sort.Sort(dependencymanager.SortedCorev1ObjectReference(referencedObjects))
529530

530531
for i := range referencedObjects {
531532
reference := &referencedObjects[i]

controllers/handlers_resources_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939

4040
configv1beta1 "github.com/projectsveltos/addon-controller/api/v1beta1"
4141
"github.com/projectsveltos/addon-controller/controllers"
42+
"github.com/projectsveltos/addon-controller/controllers/dependencymanager"
4243
"github.com/projectsveltos/addon-controller/lib/clusterops"
4344
"github.com/projectsveltos/addon-controller/pkg/scope"
4445
libsveltosv1beta1 "github.com/projectsveltos/libsveltos/api/v1beta1"
@@ -419,7 +420,7 @@ var _ = Describe("Hash methods", func() {
419420
Name: clusterSummary.Spec.ClusterProfileSpec.PolicyRefs[i].Name,
420421
}
421422
}
422-
sort.Sort(controllers.SortedCorev1ObjectReference(referencedObjects))
423+
sort.Sort(dependencymanager.SortedCorev1ObjectReference(referencedObjects))
423424
for i := range referencedObjects {
424425
switch referencedObjects[i].Name {
425426
case configMap1.Name:

controllers/utils.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -349,20 +349,6 @@ func getFeatureDeploymentInfoForFeatureID(clusterSummay *configv1beta1.ClusterSu
349349
return nil
350350
}
351351

352-
type SortedCorev1ObjectReference []corev1.ObjectReference
353-
354-
func (a SortedCorev1ObjectReference) Len() int { return len(a) }
355-
func (a SortedCorev1ObjectReference) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
356-
func (a SortedCorev1ObjectReference) Less(i, j int) bool {
357-
if a[i].Kind != a[j].Kind {
358-
return a[i].Kind < a[j].Kind
359-
}
360-
if a[i].Namespace != a[j].Namespace {
361-
return a[i].Namespace < a[j].Namespace
362-
}
363-
return a[i].Name < a[j].Name
364-
}
365-
366352
type SortedHelmCharts []configv1beta1.HelmChart
367353

368354
func (a SortedHelmCharts) Len() int { return len(a) }

0 commit comments

Comments
 (0)