-
Notifications
You must be signed in to change notification settings - Fork 82
/
chaos_network.go
154 lines (139 loc) · 5.16 KB
/
chaos_network.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
/*
* chaos_network.go
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2023 Apple Inc. and the FoundationDB project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package fixtures
import (
"strconv"
chaosmesh "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
)
// ensurePodPhaseSelectorIsSet this method takes a PodSelectorSpec and add the PodPhaseSelector if missing. If the
// PodPhaseSelector is not set chaos-mesh will try to inject the failure to all Pods, even to Pods that are in Pending
// or Failed. This can lead to test failures, since chaos-mesh is not able to inject the chaos to all Pods (it's impossible
// to inject chaos to non running Pods). See: https://chaos-mesh.org/docs/define-chaos-experiment-scope/#podphase-selectors
// Arguably this should be the default in chaos-mesh itself.
func ensurePodPhaseSelectorIsSet(selector *chaosmesh.PodSelectorSpec) {
if len(selector.PodPhaseSelectors) > 0 {
return
}
selector.PodPhaseSelectors = []string{string(corev1.PodRunning)}
}
// InjectNetworkLoss injects network loss between the source and the target.
func (factory *Factory) InjectNetworkLoss(lossPercentage string, source chaosmesh.PodSelectorSpec, target chaosmesh.PodSelectorSpec, direction chaosmesh.Direction) *ChaosMeshExperiment {
ensurePodPhaseSelectorIsSet(&source)
ensurePodPhaseSelectorIsSet(&target)
return factory.CreateExperiment(&chaosmesh.NetworkChaos{
ObjectMeta: metav1.ObjectMeta{
Name: RandStringRunes(32),
Namespace: factory.GetChaosNamespace(),
Labels: factory.GetDefaultLabels(),
},
Spec: chaosmesh.NetworkChaosSpec{
Action: chaosmesh.LossAction,
Duration: pointer.String(ChaosDurationForever),
PodSelector: chaosmesh.PodSelector{
Selector: source,
Mode: chaosmesh.AllMode,
},
Target: &chaosmesh.PodSelector{
Mode: chaosmesh.AllMode,
Selector: target,
},
Direction: direction,
TcParameter: chaosmesh.TcParameter{
Loss: &chaosmesh.LossSpec{
Loss: lossPercentage,
Correlation: lossPercentage,
},
},
},
})
}
// InjectNetworkLossBetweenPods Injects network loss b/w each combination of podGroups.
func (factory *Factory) InjectNetworkLossBetweenPods(pods []chaosmesh.PodSelectorSpec, loss string) {
count := len(pods)
for i := 0; i < count; i++ {
for j := i + 1; j < count; j++ {
factory.InjectNetworkLoss(loss, pods[i], pods[j], chaosmesh.Both)
}
}
}
// InjectPartition injects a partition to the provided selector.
func (factory *Factory) InjectPartition(selector chaosmesh.PodSelectorSpec) *ChaosMeshExperiment {
namespaces := make([]string, 0, len(selector.Pods))
for namespace := range selector.Pods {
namespaces = append(namespaces, namespace)
}
// Ensure we only select running Pods
target := chaosmesh.PodSelectorSpec{
GenericSelectorSpec: chaosmesh.GenericSelectorSpec{
Namespaces: namespaces,
},
}
return factory.InjectPartitionBetween(selector, target)
}
// InjectPartitionBetween injects a partition between the source and the target.
func (factory *Factory) InjectPartitionBetween(
source chaosmesh.PodSelectorSpec,
target chaosmesh.PodSelectorSpec,
) *ChaosMeshExperiment {
return factory.injectPartitionBetween(source, &chaosmesh.PodSelector{
Mode: chaosmesh.AllMode,
Selector: target,
})
}
func (factory *Factory) injectPartitionBetween(
source chaosmesh.PodSelectorSpec,
target *chaosmesh.PodSelector,
) *ChaosMeshExperiment {
ensurePodPhaseSelectorIsSet(&source)
ensurePodPhaseSelectorIsSet(&target.Selector)
return factory.CreateExperiment(&chaosmesh.NetworkChaos{
ObjectMeta: metav1.ObjectMeta{
Name: RandStringRunes(32),
Namespace: factory.GetChaosNamespace(),
Labels: factory.GetDefaultLabels(),
},
Spec: chaosmesh.NetworkChaosSpec{
Action: chaosmesh.PartitionAction,
Duration: pointer.String(ChaosDurationForever),
PodSelector: chaosmesh.PodSelector{
Selector: source,
Mode: chaosmesh.AllMode,
},
Target: target,
Direction: chaosmesh.Both,
},
})
}
// InjectPartitionOnSomeTargetPods injects a partition on some Pods instead of all Pods.
// It picks a specific number of pods randomly. If the specified "fixedNumber" is greater than the number of all targets, all targets will be chosen.
func (factory *Factory) InjectPartitionOnSomeTargetPods(
source chaosmesh.PodSelectorSpec,
target chaosmesh.PodSelectorSpec,
fixedNumber int,
) *ChaosMeshExperiment {
return factory.injectPartitionBetween(source, &chaosmesh.PodSelector{
Mode: chaosmesh.FixedMode,
Value: strconv.Itoa(fixedNumber),
Selector: target,
})
}