Skip to content

Commit d8e4b15

Browse files
address sonarqube
Signed-off-by: Andrew Brown <[email protected]>
1 parent 2a8aabd commit d8e4b15

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

rollout/replicaset.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -416,48 +416,46 @@ func getIstioDestinationRuleSpec(ro *v1alpha1.Rollout) *v1alpha1.IstioDestinatio
416416
return ro.Spec.Strategy.Canary.TrafficRouting.Istio.DestinationRule
417417
}
418418

419+
// subsetReferencesHash checks if a single subset's labels contain the given pod template hash.
420+
func subsetReferencesHash(subset map[string]interface{}, rsPodHash string) bool {
421+
labels, found, err := unstructured.NestedStringMap(subset, "labels")
422+
if err != nil || !found {
423+
return false
424+
}
425+
hash, ok := labels[v1alpha1.DefaultRolloutUniqueLabelKey]
426+
return ok && hash == rsPodHash
427+
}
428+
419429
// isReplicaSetReferencedByIstioDestinationRule checks if the given pod template hash is still
420430
// referenced by any subset in the Istio DestinationRule. This prevents scaling down a ReplicaSet
421431
// that is still receiving traffic via Istio subset-level routing.
422432
func (c *rolloutContext) isReplicaSetReferencedByIstioDestinationRule(rsPodHash string) bool {
423433
dRuleSpec := getIstioDestinationRuleSpec(c.rollout)
424-
if dRuleSpec == nil {
434+
if dRuleSpec == nil || c.IstioController == nil || c.IstioController.DestinationRuleLister == nil {
425435
return false
426436
}
427437

428-
if c.IstioController == nil || c.IstioController.DestinationRuleLister == nil {
429-
return false
430-
}
431438
dRuleUn, err := c.IstioController.DestinationRuleLister.Namespace(c.rollout.Namespace).Get(dRuleSpec.Name)
432439
if err != nil {
433440
if k8serrors.IsNotFound(err) {
434441
return false
435442
}
436-
// If we can't get the DestinationRule, err on the side of caution
443+
// For unexpected errors, err on the side of caution and assume it's still referenced.
437444
c.log.Warnf("Failed to get DestinationRule %s: %v", dRuleSpec.Name, err)
438445
return true
439446
}
440447

441-
// Extract subsets from the DestinationRule
442448
subsets, found, err := unstructured.NestedSlice(dRuleUn.UnstructuredContent(), "spec", "subsets")
443449
if err != nil || !found {
444450
return false
445451
}
446452

447453
for _, subsetObj := range subsets {
448454
subset, ok := subsetObj.(map[string]interface{})
449-
if !ok {
450-
continue
451-
}
452-
labels, found, err := unstructured.NestedStringMap(subset, "labels")
453-
if err != nil || !found {
454-
continue
455-
}
456-
if hash, ok := labels[v1alpha1.DefaultRolloutUniqueLabelKey]; ok && hash == rsPodHash {
455+
if ok && subsetReferencesHash(subset, rsPodHash) {
457456
c.log.Infof("ReplicaSet with hash %s is still referenced by DestinationRule %s", rsPodHash, dRuleSpec.Name)
458457
return true
459458
}
460459
}
461-
462460
return false
463461
}

rollout/replicaset_test.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,8 @@ func TestIsReplicaSetReferenced(t *testing.T) {
590590
}
591591

592592
func TestIsReplicaSetReferencedByIstioDestinationRule(t *testing.T) {
593+
const testDestRuleName = "test-destrule"
594+
593595
newRSWithPodTemplateHash := func(hash string) *appsv1.ReplicaSet {
594596
return &appsv1.ReplicaSet{
595597
ObjectMeta: metav1.ObjectMeta{
@@ -686,47 +688,47 @@ spec:
686688
},
687689
{
688690
name: "destination rule references hash - should return true",
689-
rollout: newRolloutWithIstioDestinationRule("test-destrule"),
690-
destinationRule: newDestinationRuleYAML("test-destrule", "abc123", "def456"),
691+
rollout: newRolloutWithIstioDestinationRule(testDestRuleName),
692+
destinationRule: newDestinationRuleYAML(testDestRuleName, "abc123", "def456"),
691693
rsHash: "abc123",
692694
expectedResult: true,
693695
},
694696
{
695697
name: "destination rule does not reference hash - should return false",
696-
rollout: newRolloutWithIstioDestinationRule("test-destrule"),
697-
destinationRule: newDestinationRuleYAML("test-destrule", "abc123", "def456"),
698+
rollout: newRolloutWithIstioDestinationRule(testDestRuleName),
699+
destinationRule: newDestinationRuleYAML(testDestRuleName, "abc123", "def456"),
698700
rsHash: "xyz789",
699701
expectedResult: false,
700702
},
701703
{
702704
name: "destination rule not found - should return false",
703705
rollout: newRolloutWithIstioDestinationRule("non-existent-destrule"),
704-
destinationRule: newDestinationRuleYAML("test-destrule", "abc123", "def456"),
706+
destinationRule: newDestinationRuleYAML(testDestRuleName, "abc123", "def456"),
705707
rsHash: "abc123",
706708
expectedResult: false,
707709
},
708710
{
709711
name: "no istio controller - should return false",
710-
rollout: newRolloutWithIstioDestinationRule("test-destrule"),
712+
rollout: newRolloutWithIstioDestinationRule(testDestRuleName),
711713
noIstioController: true,
712714
rsHash: "abc123",
713715
expectedResult: false,
714716
},
715717
{
716718
name: "canary subset references hash - should return true",
717-
rollout: newRolloutWithIstioDestinationRule("test-destrule"),
718-
destinationRule: newDestinationRuleYAML("test-destrule", "stable-hash", "canary-hash"),
719+
rollout: newRolloutWithIstioDestinationRule(testDestRuleName),
720+
destinationRule: newDestinationRuleYAML(testDestRuleName, "stable-hash", "canary-hash"),
719721
rsHash: "canary-hash",
720722
expectedResult: true,
721723
},
722724
{
723725
name: "destination rule with no subsets - should return false",
724-
rollout: newRolloutWithIstioDestinationRule("test-destrule"),
726+
rollout: newRolloutWithIstioDestinationRule(testDestRuleName),
725727
destinationRule: `
726728
apiVersion: networking.istio.io/v1alpha3
727729
kind: DestinationRule
728730
metadata:
729-
name: test-destrule
731+
name: ` + testDestRuleName + `
730732
namespace: default
731733
spec:
732734
host: test-service
@@ -736,12 +738,12 @@ spec:
736738
},
737739
{
738740
name: "destination rule with subset missing labels - should return false",
739-
rollout: newRolloutWithIstioDestinationRule("test-destrule"),
741+
rollout: newRolloutWithIstioDestinationRule(testDestRuleName),
740742
destinationRule: `
741743
apiVersion: networking.istio.io/v1alpha3
742744
kind: DestinationRule
743745
metadata:
744-
name: test-destrule
746+
name: ` + testDestRuleName + `
745747
namespace: default
746748
spec:
747749
host: test-service

0 commit comments

Comments
 (0)