Skip to content

Commit 583582a

Browse files
authored
CHAOSPLT-248: Respect annotation filters (#835)
* retry with logs * try a fix * run an e2e test surprise * Put logger in the suite_test file * remove the dontexpects * review feedback
1 parent d361e3c commit 583582a

6 files changed

+43
-9
lines changed

controllers/disruption_controller_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,23 @@ var _ = Describe("Disruption Controller", func() {
9595
Context("annotation filters should limit selected targets", func() {
9696
BeforeEach(func() {
9797
disruption.Spec.Filter = &chaosv1beta1.DisruptionFilter{
98-
Annotations: targetPod.Annotations,
98+
Annotations: map[string]string{"foo": "baz"},
9999
}
100+
disruption.Spec.Count = &intstr.IntOrString{Type: intstr.String, StrVal: "100%"}
101+
})
102+
103+
It("should only select half of all pods", func(ctx SpecContext) {
104+
ExpectChaosPods(ctx, disruption, 4)
105+
})
106+
})
107+
108+
Context("annotation filters should limit selected targets", func() {
109+
BeforeEach(func() {
110+
disruption.Spec.Filter = &chaosv1beta1.DisruptionFilter{
111+
Annotations: map[string]string{"fob": "baf"},
112+
}
113+
disruption.Spec.Selector = map[string]string{"second": "true"}
114+
disruption.Spec.Count = &intstr.IntOrString{Type: intstr.String, StrVal: "100%"}
100115
})
101116

102117
It("should only select half of all pods", func(ctx SpecContext) {

controllers/suite_toolsfor_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ func InjectPodsAndDisruption(ctx SpecContext, wantDisruption chaosv1beta1.Disrup
166166
anotherTargetPod = *targetPod.DeepCopy()
167167
// we want some small differences from the previous pod, a single container, and other annotation value for foo
168168
anotherTargetPod.Annotations["foo"] = "qux"
169+
anotherTargetPod.Labels["second"] = "true"
169170
anotherTargetPod.Spec.Containers = anotherTargetPod.Spec.Containers[0:1]
170171
anotherTargetPod.Spec.Volumes = anotherTargetPod.Spec.Volumes[0:1] // second volume is used by second container, hence not needed as we removed it
171172
anotherTargetPodCreated = CreateRunningPod(ctx, anotherTargetPod)
@@ -440,6 +441,7 @@ func uniquePod() corev1.Pod {
440441
},
441442
Annotations: map[string]string{
442443
"foo": "baz",
444+
"fob": "baf",
443445
"uid": uuid,
444446
},
445447
},

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func main() {
141141
}
142142

143143
// target selector
144-
targetSelector := targetselector.NewRunningTargetSelector(cfg.Controller.EnableSafeguards, controllerNodeName)
144+
targetSelector := targetselector.NewRunningTargetSelector(cfg.Controller.EnableSafeguards, controllerNodeName, logger)
145145

146146
var gcPtr *time.Duration
147147
if cfg.Controller.ExpiredDisruptionGCDelay >= 0 {

targetselector/running_target_selector.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
chaosv1beta1 "github.com/DataDog/chaos-controller/api/v1beta1"
1414
chaostypes "github.com/DataDog/chaos-controller/types"
15+
"go.uber.org/zap"
1516
corev1 "k8s.io/api/core/v1"
1617
"k8s.io/apimachinery/pkg/labels"
1718
"k8s.io/apimachinery/pkg/selection"
@@ -23,12 +24,14 @@ import (
2324
type runningTargetSelector struct {
2425
controllerEnableSafeguards bool
2526
controllerNodeName string
27+
logger *zap.SugaredLogger
2628
}
2729

28-
func NewRunningTargetSelector(controllerEnableSafeguards bool, controllerNodeName string) TargetSelector {
30+
func NewRunningTargetSelector(controllerEnableSafeguards bool, controllerNodeName string, logger *zap.SugaredLogger) TargetSelector {
2931
return runningTargetSelector{
3032
controllerEnableSafeguards: controllerEnableSafeguards,
3133
controllerNodeName: controllerNodeName,
34+
logger: logger,
3235
}
3336
}
3437

@@ -54,6 +57,7 @@ func (r runningTargetSelector) GetMatchingPodsOverTotalPods(c client.Client, ins
5457

5558
runningPods := &corev1.PodList{}
5659

60+
podLoop:
5761
for _, pod := range pods.Items {
5862
// check the pod is already a disruption target
5963
isAlreadyATarget := false
@@ -75,10 +79,12 @@ func (r runningTargetSelector) GetMatchingPodsOverTotalPods(c client.Client, ins
7579
}
7680

7781
if instance.Spec.Filter != nil {
82+
r.logger.Debugw("checking if pod has filtered annotations", "pod", pod.Name, "pod.Annotations", pod.Annotations, "spec.Filter", instance.Spec.Filter.Annotations)
7883
for k, v := range instance.Spec.Filter.Annotations {
7984
podAnno, ok := pod.Annotations[k]
8085
if !ok || podAnno != v {
81-
continue
86+
// This pod doesn't have the annotation specified in our filter, we don't want to include it as a target
87+
continue podLoop
8288
}
8389
}
8490
}
@@ -133,6 +139,7 @@ func (r runningTargetSelector) GetMatchingNodesOverTotalNodes(c client.Client, i
133139

134140
runningNodes := &corev1.NodeList{}
135141

142+
nodeLoop:
136143
for _, node := range nodes.Items {
137144
// apply controller safeguards if enabled
138145
if r.controllerEnableSafeguards {
@@ -153,10 +160,12 @@ func (r runningTargetSelector) GetMatchingNodesOverTotalNodes(c client.Client, i
153160
}
154161

155162
if instance.Spec.Filter != nil {
163+
r.logger.Debugw("checking if node has filtered annotations", "node", node.Name, "node.Annotations", node.Annotations, "spec.Filter", instance.Spec.Filter.Annotations)
156164
for k, v := range instance.Spec.Filter.Annotations {
157165
nodeAnno, ok := node.Annotations[k]
158166
if !ok || nodeAnno != v {
159-
continue
167+
// This node doesn't have the annotation specified in our filter, we don't want to include it as a target
168+
continue nodeLoop
160169
}
161170
}
162171
}

targetselector/running_target_selector_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ var _ = Describe("Helpers", func() {
137137
var targetSelector TargetSelector
138138

139139
BeforeEach(func() {
140-
targetSelector = NewRunningTargetSelector(false, "foo")
140+
targetSelector = NewRunningTargetSelector(false, "foo", logger)
141141

142142
c = fakeClient{}
143143

@@ -375,7 +375,7 @@ var _ = Describe("Helpers", func() {
375375

376376
Context("with controller safeguards enabled", func() {
377377
BeforeEach(func() {
378-
targetSelector = NewRunningTargetSelector(true, "runningNode")
378+
targetSelector = NewRunningTargetSelector(true, "runningNode", logger)
379379
})
380380

381381
It("should exclude the pods running on the same node as the controller from targets", func() {
@@ -440,7 +440,7 @@ var _ = Describe("Helpers", func() {
440440

441441
Context("with controller safeguards enabled", func() {
442442
BeforeEach(func() {
443-
targetSelector = NewRunningTargetSelector(true, "runningNode")
443+
targetSelector = NewRunningTargetSelector(true, "runningNode", logger)
444444
})
445445

446446
It("should exclude the controller node from targets", func() {

targetselector/targetselector_suite_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,24 @@
33
// This product includes software developed at Datadog (https://www.datadoghq.com/).
44
// Copyright 2024 Datadog, Inc.
55

6-
package targetselector_test
6+
package targetselector
77

88
import (
99
"testing"
1010

1111
. "github.com/onsi/ginkgo/v2"
1212
. "github.com/onsi/gomega"
13+
"go.uber.org/zap"
14+
"go.uber.org/zap/zaptest"
1315
)
1416

17+
var logger *zap.SugaredLogger
18+
1519
func TestTargetselector(t *testing.T) {
1620
RegisterFailHandler(Fail)
1721
RunSpecs(t, "Targetselector Suite")
1822
}
23+
24+
var _ = BeforeSuite(func(ctx SpecContext) {
25+
logger = zaptest.NewLogger(GinkgoT()).Sugar()
26+
})

0 commit comments

Comments
 (0)