-
Notifications
You must be signed in to change notification settings - Fork 25
/
disruption_kind.go
113 lines (94 loc) · 3.33 KB
/
disruption_kind.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
// 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 api
import (
"fmt"
"strings"
"time"
chaostypes "github.com/DataDog/chaos-controller/types"
)
// DisruptionKind contains all methods required for a disruption sub-specification (Network, DNS, CPUPressure, etc.)
type DisruptionKind interface {
// generates CLI args for the given disruption sub-specification
GenerateArgs() []string
// validates schema for the given disruption sub-specification
Validate() error
}
type DisruptionArgs struct {
AllowedHosts []string
TargetContainers map[string]string
Level chaostypes.DisruptionLevel
Kind chaostypes.DisruptionKindName
TargetPodIP string
MetricsSink string
DisruptionName string
DisruptionNamespace string
TargetName string
TargetNodeName string
DNSServer string
KubeDNS string
ChaosNamespace string
DryRun bool
OnInit bool
PulseInitialDelay time.Duration
PulseActiveDuration time.Duration
PulseDormantDuration time.Duration
HostResolveInterval time.Duration
NotInjectedBefore time.Time
}
// CreateCmdArgs is a helper function generating common and global args and appending them to the given args array
func (d DisruptionArgs) CreateCmdArgs(args []string) []string {
formattedTargetContainers := []string{}
for name, id := range d.TargetContainers {
f := fmt.Sprintf("%s;%s", name, id)
formattedTargetContainers = append(formattedTargetContainers, f)
}
args = append(args,
// basic args
"--metrics-sink", d.MetricsSink,
"--level", string(d.Level),
"--target-containers", strings.Join(formattedTargetContainers, ","),
"--target-pod-ip", d.TargetPodIP,
"--chaos-namespace", d.ChaosNamespace,
// log context args
"--log-context-disruption-name", d.DisruptionName,
"--log-context-disruption-namespace", d.DisruptionNamespace,
"--log-context-target-name", d.TargetName,
"--log-context-target-node-name", d.TargetNodeName,
)
// enable dry-run mode
if d.DryRun {
args = append(args, "--dry-run")
}
// enable chaos handler init container notification
if d.OnInit {
args = append(args, "--on-init")
}
if d.PulseActiveDuration > 0 && d.PulseDormantDuration > 0 {
args = append(args, "--pulse-active-duration", d.PulseActiveDuration.String())
args = append(args, "--pulse-dormant-duration", d.PulseDormantDuration.String())
}
if d.PulseInitialDelay > 0 {
args = append(args, "--pulse-initial-delay", d.PulseInitialDelay.String())
}
if !d.NotInjectedBefore.IsZero() {
args = append(args, "--not-injected-before", d.NotInjectedBefore.Format(time.RFC3339))
}
// DNS disruption configs
if d.Kind == chaostypes.DisruptionKindDNSDisruption {
args = append(args, "--dns-server", d.DNSServer)
args = append(args, "--kube-dns", d.KubeDNS)
}
// append allowed hosts for network disruptions
if d.Kind == chaostypes.DisruptionKindNetworkDisruption {
for _, host := range d.AllowedHosts {
args = append(args, "--allowed-hosts", host)
}
if d.HostResolveInterval > 0 {
args = append(args, "--host-resolve-interval", d.HostResolveInterval.String())
}
}
return args
}