Skip to content

Commit

Permalink
User Request: Add support for tolerations on injector pods (#874)
Browse files Browse the repository at this point in the history
* User Request: Add support for tolerations on injector pods
  • Loading branch information
JoshuaBell9181 authored Jun 27, 2024
1 parent 3c8b208 commit 2e96cbe
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 7 deletions.
11 changes: 11 additions & 0 deletions builderstest/chaospod.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ func (b *ChaosPodBuilder) WithChaosPodLabels(name, namespace, target, kind strin
return b
}

// WithTolerations sets tolerations to the spec.
func (b *ChaosPodBuilder) WithTolerations(tolerations []v1.Toleration) *ChaosPodBuilder {
b.modifiers = append(
b.modifiers,
func() {
b.Spec.Tolerations = tolerations
})

return b
}

// WithLabels sets custom labels.
func (b *ChaosPodBuilder) WithLabels(labels map[string]string) *ChaosPodBuilder {
b.modifiers = append(
Expand Down
18 changes: 18 additions & 0 deletions chart/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ data:
{{ $key }}: {{ $val | quote }}
{{- end }}
{{- end }}
{{- if .Values.injector.tolerations }}
tolerations:
{{- range .Values.injector.tolerations }}
- key: {{ .key | quote }}
{{- if .operator }}
operator: {{ .operator | default "Equal" | quote }}
{{- end }}
{{- if .value }}
value: {{ .value | quote }}
{{- end }}
{{- if .effect }}
effect: {{ .effect | quote }}
{{- end }}
{{- if .tolerationSeconds }}
tolerationSeconds: {{ .tolerationSeconds }}
{{- end }}
{{- end }}
{{- end }}
serviceAccount: {{ .Values.injector.serviceAccount | quote }}
chaosNamespace: {{ .Values.chaosNamespace | quote }}
dnsDisruption:
Expand Down
12 changes: 12 additions & 0 deletions chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ injector:
repo: chaos-injector
annotations: {} # extra annotations passed to the chaos injector pods
labels: {} # extra labels passed to the chaos injector pods
tolerations: [] # extra tolerations passed to the chaos injector pods
# (here's the expected format, key/effect are required, all other fields are optional)
# tolerations:
# - key: "key1"
# operator: "Equal"
# value: "value1"
# effect: "NoExecute"
# tolerationSeconds: 3600
# - key: "key1"
# operator: "Equal"
# value: "value1"
# effect: "NoExecute"
serviceAccount: chaos-injector # service account to use for the chaos injector pods
dnsDisruption: # dns disruption configuration
dnsServer: "" # IP address of the upstream dns server
Expand Down
9 changes: 9 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ type injectorConfig struct {
DNSDisruption injectorDNSDisruptionConfig `json:"dnsDisruption"`
NetworkDisruption injectorNetworkDisruptionConfig `json:"networkDisruption"`
ImagePullSecrets string `json:"imagePullSecrets"`
Tolerations []Toleration `json:"tolerations"`
}

type Toleration struct {
Key string `json:"key"`
Operator string `json:"operator"`
Value string `json:"value"`
Effect string `json:"effect"`
TolerationSeconds *int64 `json:"tolerationSeconds,omitempty"`
}

type injectorDNSDisruptionConfig struct {
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func main() {
Image: cfg.Injector.Image,
Annotations: cfg.Injector.Annotations,
Labels: cfg.Injector.Labels,
Tolerations: cfg.Injector.Tolerations,
NetworkDisruptionAllowedHosts: cfg.Injector.NetworkDisruption.AllowedHosts,
DNSDisruptionDNSServer: cfg.Injector.DNSDisruption.DNSServer,
DNSDisruptionKubeDNS: cfg.Injector.DNSDisruption.KubeDNS,
Expand Down
32 changes: 25 additions & 7 deletions services/chaospod.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

chaosapi "github.com/DataDog/chaos-controller/api"
chaosv1beta1 "github.com/DataDog/chaos-controller/api/v1beta1"
"github.com/DataDog/chaos-controller/config"
"github.com/DataDog/chaos-controller/env"
"github.com/DataDog/chaos-controller/o11y/metrics"
"github.com/DataDog/chaos-controller/targetselector"
Expand Down Expand Up @@ -63,13 +64,14 @@ type ChaosPodService interface {

// ChaosPodServiceInjectorConfig contains configuration options for the injector.
type ChaosPodServiceInjectorConfig struct {
ServiceAccount string // Service account to be used by the injector.
Image string // Image to be used for the injector.
Annotations, Labels map[string]string // Annotations and labels to be applied to injected pods.
NetworkDisruptionAllowedHosts []string // List of hosts allowed during network disruption.
DNSDisruptionDNSServer string // DNS server to be used for DNS disruption.
DNSDisruptionKubeDNS string // KubeDNS server to be used for DNS disruption.
ImagePullSecrets string // Image pull secrets for the injector.
ServiceAccount string // Service account to be used by the injector.
Image string // Image to be used for the injector.
Annotations, Labels map[string]string // Annotations and labels to be applied to injected pods.
NetworkDisruptionAllowedHosts []string // List of hosts allowed during network disruption.
DNSDisruptionDNSServer string // DNS server to be used for DNS disruption.
DNSDisruptionKubeDNS string // KubeDNS server to be used for DNS disruption.
ImagePullSecrets string // Image pull secrets for the injector.
Tolerations []config.Toleration // Tolerations to be applied to injected pods.
}

// ChaosPodServiceConfig contains configuration options for the chaosPodService.
Expand Down Expand Up @@ -403,6 +405,21 @@ func (m *chaosPodService) HandleOrphanedChaosPods(ctx context.Context, req ctrl.
return nil
}

func (m *chaosPodService) convertTolerations(tolerations []config.Toleration) []corev1.Toleration {
var coreTolerations []corev1.Toleration //nolint:prealloc
for _, t := range tolerations {
coreTolerations = append(coreTolerations, corev1.Toleration{
Key: t.Key,
Operator: corev1.TolerationOperator(t.Operator),
Value: t.Value,
Effect: corev1.TaintEffect(t.Effect),
TolerationSeconds: t.TolerationSeconds,
})
}

return coreTolerations
}

func (m *chaosPodService) generateLabels(disruption *chaosv1beta1.Disruption, targetName string, kind chaostypes.DisruptionKindName) map[string]string {
podLabels := make(map[string]string)

Expand All @@ -426,6 +443,7 @@ func (m *chaosPodService) generateChaosPodSpec(targetNodeName string, terminatio
ServiceAccountName: m.config.Injector.ServiceAccount, // service account to use
TerminationGracePeriodSeconds: &terminationGracePeriod,
ActiveDeadlineSeconds: &activeDeadlineSeconds,
Tolerations: m.convertTolerations(m.config.Injector.Tolerations),
Containers: []corev1.Container{
{
Name: "injector", // container name
Expand Down
36 changes: 36 additions & 0 deletions services/chaospod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/mock"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -1007,6 +1008,41 @@ var _ = Describe("Chaos Pod Service", func() {
})
})

Describe("CreatePod with tolerations", func() {
var tolerations []corev1.Toleration

BeforeEach(func() {
// Arrange
tolerations = []corev1.Toleration{
{
Key: "key",
Operator: corev1.TolerationOpEqual,
Value: "value",
Effect: corev1.TaintEffectNoSchedule,
},
}
chaosPod = builderstest.NewPodBuilder("test-1", DefaultNamespace).WithTolerations(tolerations).Build()
})

JustBeforeEach(func() {
// Action
err = chaosPodService.CreatePod(context.Background(), &chaosPod)
})

Describe("success case", func() {
BeforeEach(func() {
// Arrange
By("create the chaos pod with the ks8 client")
k8sClientMock.EXPECT().Create(mock.Anything, &chaosPod).Return(nil)
})

It("should not return an error", func() {
// Assert
Expect(err).ShouldNot(HaveOccurred())
})
})
})

Describe("CreatePod", func() {
BeforeEach(func() {
// Arrange
Expand Down

0 comments on commit 2e96cbe

Please sign in to comment.