/
disruption.go
236 lines (198 loc) · 6.21 KB
/
disruption.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2024 Datadog, Inc.
package builderstest_test
import (
"time"
"github.com/DataDog/chaos-controller/api/v1beta1"
"github.com/DataDog/chaos-controller/types"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
// DisruptionBuilder is a struct used to build a disruption instance.
type DisruptionBuilder struct {
*v1beta1.Disruption
// we store action we want to perform instead of performing them right away because they are time sensitive
// this enables us to ensure time.Now is as late as it can be without faking it (that we should do at some point)
modifiers []func()
}
// NewDisruptionBuilder creates a new DisruptionBuilder instance with an initial disruption spec and a creation timestamp modifier.
func NewDisruptionBuilder() *DisruptionBuilder {
return (&DisruptionBuilder{
Disruption: &v1beta1.Disruption{
Spec: v1beta1.DisruptionSpec{
Duration: "1m", // per spec definition a valid disruption going to the reconcile loop MUST have a duration, let's not test wrong test cases
},
},
}).WithCreationDuration(30 * time.Second)
}
// WithDisruptionTriggers sets the specified triggers of disruption.
func (b *DisruptionBuilder) WithDisruptionTriggers(triggers v1beta1.DisruptionTriggers) *DisruptionBuilder {
b.modifiers = append(
b.modifiers,
func() {
b.Spec.Triggers = triggers
})
return b
}
// WithCount sets the specified count.
func (b *DisruptionBuilder) WithCount(count *intstr.IntOrString) *DisruptionBuilder {
b.modifiers = append(
b.modifiers,
func() {
b.Spec.Count = count
})
return b
}
// WithDisruptionKind sets the specified kind of disruption in the DisruptionBuilder's spec.
func (b *DisruptionBuilder) WithDisruptionKind(kind types.DisruptionKindName) *DisruptionBuilder {
b.modifiers = append(
b.modifiers,
func() {
switch kind {
case types.DisruptionKindNodeFailure:
if b.Spec.NodeFailure == nil {
b.Spec.NodeFailure = &v1beta1.NodeFailureSpec{}
}
case types.DisruptionKindContainerFailure:
if b.Spec.ContainerFailure == nil {
b.Spec.ContainerFailure = &v1beta1.ContainerFailureSpec{}
}
case types.DisruptionKindNetworkDisruption:
if b.Spec.Network == nil {
b.Spec.Network = &v1beta1.NetworkDisruptionSpec{}
}
case types.DisruptionKindCPUPressure:
if b.Spec.CPUPressure == nil {
b.Spec.CPUPressure = &v1beta1.CPUPressureSpec{}
}
case types.DisruptionKindDiskPressure:
if b.Spec.DiskPressure == nil {
b.Spec.DiskPressure = &v1beta1.DiskPressureSpec{}
}
case types.DisruptionKindDNSDisruption:
if b.Spec.DNS == nil {
b.Spec.DNS = v1beta1.DNSDisruptionSpec{}
}
case types.DisruptionKindGRPCDisruption:
if b.Spec.GRPC == nil {
b.Spec.GRPC = &v1beta1.GRPCDisruptionSpec{}
}
case types.DisruptionKindDiskFailure:
if b.Spec.DiskFailure == nil {
b.Spec.DiskFailure = &v1beta1.DiskFailureSpec{}
}
}
})
return b
}
// Build generates a v1.Disruption instance based on the configuration.
func (b *DisruptionBuilder) Build() v1beta1.Disruption {
for _, modifier := range b.modifiers {
modifier()
}
return *b.Disruption
}
// Reset resets the DisruptionBuilder by clearing all modifiers.
func (b *DisruptionBuilder) Reset() *DisruptionBuilder {
b.modifiers = nil
return b
}
// WithCreationDuration adjusts the creation timestamp.
func (b *DisruptionBuilder) WithCreationDuration(past time.Duration) *DisruptionBuilder {
b.modifiers = append(
b.modifiers,
func() {
b.CreationTimestamp = v1.NewTime(time.Now().Add(-past))
})
return b
}
// WithCreationTime adjusts the creation timestamp.
func (b *DisruptionBuilder) WithCreationTime(creationTimestamp time.Time) *DisruptionBuilder {
b.modifiers = append(
b.modifiers,
func() {
b.CreationTimestamp = v1.NewTime(creationTimestamp)
})
return b
}
// WithDuration sets the duration timestamp.
func (b *DisruptionBuilder) WithDuration(duration v1beta1.DisruptionDuration) *DisruptionBuilder {
b.modifiers = append(
b.modifiers,
func() {
b.Spec.Duration = duration
})
return b
}
// WithDeletion sets the deletion timestamp to the current time.
func (b *DisruptionBuilder) WithDeletion() *DisruptionBuilder {
b.modifiers = append(
b.modifiers,
func() {
v1t := v1.NewTime(time.Now())
b.DeletionTimestamp = &v1t
})
return b
}
// WithNamespace sets the namespace.
func (b *DisruptionBuilder) WithNamespace(namespace string) *DisruptionBuilder {
b.modifiers = append(
b.modifiers,
func() {
b.Namespace = namespace
})
return b
}
// WithName sets the name.
func (b *DisruptionBuilder) WithName(name string) *DisruptionBuilder {
b.modifiers = append(
b.modifiers,
func() {
b.Name = name
})
return b
}
// WithNetworkDisruptionCloudSpec sets the NetworkDisruptionCloudSpecs to the Network spec.
func (b *DisruptionBuilder) WithNetworkDisruptionCloudSpec(spec *v1beta1.NetworkDisruptionCloudSpec) *DisruptionBuilder {
b.modifiers = append(
b.modifiers,
func() {
if b.Spec.Network == nil {
b.Spec.Network = &v1beta1.NetworkDisruptionSpec{}
}
b.Spec.Network.Cloud = spec
})
return b
}
// WithSpecPulse sets the DisruptionPulse to the Pulse spec.
func (b *DisruptionBuilder) WithSpecPulse(specPulse *v1beta1.DisruptionPulse) *DisruptionBuilder {
b.modifiers = append(
b.modifiers,
func() {
b.Spec.Pulse = specPulse
})
return b
}
// WithNetworkDisableDefaultAllowedHosts set the NetworkDisruptionSpec to the network spec.
func (b *DisruptionBuilder) WithNetworkDisableDefaultAllowedHosts(enable bool) *DisruptionBuilder {
b.modifiers = append(
b.modifiers,
func() {
if b.Spec.Network == nil {
b.Spec.Network = &v1beta1.NetworkDisruptionSpec{}
}
b.Spec.Network.DisableDefaultAllowedHosts = enable
})
return b
}
// WithInjectionStatus sets the specified injection status in the DisruptionBuilder's status.
func (b *DisruptionBuilder) WithInjectionStatus(status types.DisruptionInjectionStatus) *DisruptionBuilder {
b.modifiers = append(
b.modifiers,
func() {
b.Status.InjectionStatus = status
})
return b
}