-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathdefault.go
233 lines (212 loc) · 7.13 KB
/
default.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
// 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 2016-present Datadog, Inc.
package clusterchecksrunner
import (
"fmt"
"strconv"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apicommon "github.com/DataDog/datadog-operator/apis/datadoghq/common"
apicommonv1 "github.com/DataDog/datadog-operator/apis/datadoghq/common/v1"
apiutils "github.com/DataDog/datadog-operator/apis/utils"
"github.com/DataDog/datadog-operator/controllers/datadogagent/common"
componentdca "github.com/DataDog/datadog-operator/controllers/datadogagent/component/clusteragent"
"github.com/DataDog/datadog-operator/pkg/defaulting"
)
// GetClusterChecksRunnerName return the Cluster-Checks-Runner name based on the DatadogAgent name
func GetClusterChecksRunnerName(dda metav1.Object) string {
return fmt.Sprintf("%s-%s", dda.GetName(), apicommon.DefaultClusterChecksRunnerResourceSuffix)
}
// NewDefaultClusterChecksRunnerDeployment return a new default cluster-checks-runner deployment
func NewDefaultClusterChecksRunnerDeployment(dda metav1.Object) *appsv1.Deployment {
deployment := common.NewDeployment(dda, apicommon.DefaultClusterChecksRunnerResourceSuffix, GetClusterChecksRunnerName(dda), common.GetAgentVersion(dda), nil)
podTemplate := NewDefaultClusterChecksRunnerPodTemplateSpec(dda)
for key, val := range deployment.GetLabels() {
podTemplate.Labels[key] = val
}
for key, val := range deployment.GetAnnotations() {
podTemplate.Annotations[key] = val
}
deployment.Spec.Template = *podTemplate
deployment.Spec.Replicas = apiutils.NewInt32Pointer(apicommon.DefaultClusterChecksRunnerReplicas)
return deployment
}
// NewDefaultClusterChecksRunnerPodTemplateSpec returns a default cluster-checks-runner for the cluster-agent deployment
func NewDefaultClusterChecksRunnerPodTemplateSpec(dda metav1.Object) *corev1.PodTemplateSpec {
volumes := []corev1.Volume{
common.GetVolumeInstallInfo(dda),
common.GetVolumeForConfig(),
common.GetVolumeForRmCorechecks(),
common.GetVolumeForLogs(),
// /tmp is needed because some versions of the DCA (at least until
// 1.19.0) write to it.
// In some code paths, the klog lib writes to /tmp instead of using the
// standard datadog logs path.
// In some envs like Openshift, when running as non-root, the pod will
// not have permissions to write on /tmp, that's why we need to mount
// it with write perms.
common.GetVolumeForTmp(),
}
volumeMounts := []corev1.VolumeMount{
common.GetVolumeMountForInstallInfo(),
common.GetVolumeMountForConfig(),
common.GetVolumeMountForLogs(),
common.GetVolumeMountForTmp(),
common.GetVolumeMountForRmCorechecks(),
}
template := &corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: make(map[string]string),
Annotations: make(map[string]string),
},
Spec: defaultPodSpec(dda, volumes, volumeMounts, defaultEnvVars(dda)),
}
return template
}
// getDefaultServiceAccountName return the default Cluster-Agent ServiceAccountName
func getDefaultServiceAccountName(dda metav1.Object) string {
return fmt.Sprintf("%s-%s", dda.GetName(), apicommon.DefaultClusterChecksRunnerResourceSuffix)
}
func clusterChecksRunnerImage() string {
return fmt.Sprintf("%s/%s:%s", apicommon.DefaultImageRegistry, apicommon.DefaultAgentImageName, defaulting.AgentLatestVersion)
}
func defaultPodSpec(dda metav1.Object, volumes []corev1.Volume, volumeMounts []corev1.VolumeMount, envVars []corev1.EnvVar) corev1.PodSpec {
podSpec := corev1.PodSpec{
ServiceAccountName: getDefaultServiceAccountName(dda),
InitContainers: []corev1.Container{
{
Name: "init-config",
Image: clusterChecksRunnerImage(),
Command: []string{"bash", "-c"},
Args: []string{
"for script in $(find /etc/cont-init.d/ -type f -name '*.sh' | sort) ; do bash $script ; done",
},
VolumeMounts: volumeMounts,
},
},
Containers: []corev1.Container{
{
Name: string(apicommonv1.ClusterChecksRunnersContainerName),
Image: clusterChecksRunnerImage(),
Env: envVars,
VolumeMounts: volumeMounts,
Command: []string{"bash", "-c"},
Args: []string{
"agent run",
},
LivenessProbe: apicommon.GetAgentLivenessProbe(),
ReadinessProbe: apicommon.GetAgentReadinessProbe(),
StartupProbe: apicommon.GetAgentStartupProbe(),
SecurityContext: &corev1.SecurityContext{
ReadOnlyRootFilesystem: apiutils.NewBoolPointer(true),
AllowPrivilegeEscalation: apiutils.NewBoolPointer(false),
},
},
},
Affinity: DefaultAffinity(),
Volumes: volumes,
// To be uncommented when the agent Dockerfile will be updated to use a non-root user by default
// SecurityContext: &corev1.PodSecurityContext{
// RunAsNonRoot: apiutils.NewBoolPointer(true),
// },
}
return podSpec
}
func defaultEnvVars(dda metav1.Object) []corev1.EnvVar {
envVars := []corev1.EnvVar{
{
Name: apicommon.DDClusterAgentKubeServiceName,
Value: componentdca.GetClusterAgentServiceName(dda),
},
{
Name: apicommon.DDClusterAgentEnabled,
Value: "true",
},
{
Name: apicommon.DDHealthPort,
Value: strconv.Itoa(int(apicommon.DefaultAgentHealthPort)),
},
{
Name: apicommon.KubernetesEnvVar,
Value: "yes",
},
{
Name: apicommon.DDEnableMetadataCollection,
Value: "false",
},
{
Name: apicommon.DDClcRunnerEnabled,
Value: "true",
},
{
Name: apicommon.DDClcRunnerHost,
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: apicommon.FieldPathStatusPodIP,
},
},
},
{
Name: apicommon.DDClcRunnerID,
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: apicommon.FieldPathMetaName,
},
},
},
{
Name: apicommon.DDDogstatsdEnabled,
Value: "false",
},
{
Name: apicommon.DDProcessCollectionEnabled,
Value: "false",
},
{
Name: apicommon.DDContainerCollectionEnabled,
Value: "true",
},
{
Name: apicommon.DDLogsEnabled,
Value: "false",
},
{
Name: apicommon.DDAPMEnabled,
Value: "false",
},
{
Name: apicommon.DDHostname,
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: apicommon.FieldPathSpecNodeName,
},
},
},
}
return envVars
}
// DefaultAffinity returns the pod anti affinity of the cluster checks runners
// The default anti affinity prefers scheduling the runners on different nodes if possible
// for better checks stability in case of node failure.
func DefaultAffinity() *corev1.Affinity {
return &corev1.Affinity{
PodAntiAffinity: &corev1.PodAntiAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{
{
Weight: 50,
PodAffinityTerm: corev1.PodAffinityTerm{
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
apicommon.AgentDeploymentComponentLabelKey: apicommon.DefaultClusterChecksRunnerResourceSuffix,
},
},
TopologyKey: "kubernetes.io/hostname",
},
},
},
},
}
}