-
Notifications
You must be signed in to change notification settings - Fork 99
/
default.go
345 lines (321 loc) · 9.62 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// 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"
rbacv1 "k8s.io/api/rbac/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"
"github.com/DataDog/datadog-operator/controllers/datadogagent/component"
componentdca "github.com/DataDog/datadog-operator/controllers/datadogagent/component/clusteragent"
"github.com/DataDog/datadog-operator/pkg/controller/utils"
"github.com/DataDog/datadog-operator/pkg/defaulting"
"github.com/DataDog/datadog-operator/pkg/kubernetes/rbac"
)
// NewDefaultClusterChecksRunnerDeployment return a new default cluster-checks-runner deployment
func NewDefaultClusterChecksRunnerDeployment(dda metav1.Object) *appsv1.Deployment {
deployment := component.NewDeployment(dda, apicommon.DefaultClusterChecksRunnerResourceSuffix, component.GetClusterChecksRunnerName(dda), component.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{
component.GetVolumeInstallInfo(dda),
component.GetVolumeForConfig(),
component.GetVolumeForRmCorechecks(),
component.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.
component.GetVolumeForTmp(),
}
volumeMounts := []corev1.VolumeMount{
component.GetVolumeMountForInstallInfo(),
component.GetVolumeMountForConfig(),
component.GetVolumeMountForLogs(),
component.GetVolumeMountForTmp(),
component.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
}
// GetDefaultClusterChecksRunnerClusterRolePolicyRules returns the default Cluster Role Policy Rules for the Cluster Checks Runner
func GetDefaultClusterChecksRunnerClusterRolePolicyRules(dda metav1.Object, excludeNonResourceRules bool) []rbacv1.PolicyRule {
policyRule := []rbacv1.PolicyRule{
{
APIGroups: []string{rbac.CoreAPIGroup},
Resources: []string{
rbac.ServicesResource,
rbac.EventsResource,
rbac.EndpointsResource,
rbac.PodsResource,
rbac.NodesResource,
rbac.ComponentStatusesResource,
rbac.ConfigMapsResource,
rbac.NamespaceResource,
},
Verbs: []string{
rbac.GetVerb,
rbac.ListVerb,
rbac.WatchVerb,
},
},
{
APIGroups: []string{rbac.CoreAPIGroup},
Resources: []string{
rbac.ConfigMapsResource,
},
Verbs: []string{
rbac.CreateVerb,
},
},
{
APIGroups: []string{rbac.CoreAPIGroup},
Resources: []string{
rbac.ConfigMapsResource,
},
ResourceNames: []string{
utils.GetDatadogLeaderElectionResourceName(dda),
},
Verbs: []string{
rbac.GetVerb,
rbac.UpdateVerb,
},
},
{
APIGroups: []string{rbac.OpenShiftQuotaAPIGroup},
Resources: []string{
rbac.ClusterResourceQuotasResource,
},
Verbs: []string{
rbac.GetVerb,
rbac.ListVerb,
},
},
{
NonResourceURLs: []string{
rbac.VersionURL,
rbac.HealthzURL,
},
Verbs: []string{
rbac.GetVerb,
},
},
// Leader election that uses Leases, such as kube-controller-manager
{
APIGroups: []string{rbac.CoordinationAPIGroup},
Resources: []string{
rbac.LeasesResource,
},
Verbs: []string{
rbac.GetVerb,
rbac.ListVerb,
rbac.WatchVerb,
},
},
// Horizontal Pod Autoscaling
{
APIGroups: []string{rbac.AutoscalingAPIGroup},
Resources: []string{
rbac.HorizontalPodAutoscalersRecource,
},
Verbs: []string{
rbac.ListVerb,
rbac.WatchVerb,
},
},
{
APIGroups: []string{rbac.CoreAPIGroup},
Resources: []string{
rbac.NamespaceResource,
},
ResourceNames: []string{
common.KubeSystemResourceName,
},
Verbs: []string{
rbac.GetVerb,
},
},
}
if !excludeNonResourceRules {
policyRule = append(policyRule, rbacv1.PolicyRule{
NonResourceURLs: []string{rbac.MetricsURL},
Verbs: []string{rbac.GetVerb},
})
}
return policyRule
}
// GetDefaultServiceAccountName return the default Cluster-Agent ServiceAccountName
func GetDefaultServiceAccountName(dda metav1.Object) string {
return fmt.Sprintf("%s-%s", dda.GetName(), apicommon.DefaultClusterChecksRunnerResourceSuffix)
}
// GetCCRRbacResourcesName returns the Cluster Checks Runner RBAC resource name
func GetCCRRbacResourcesName(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.GetDefaultLivenessProbe(),
ReadinessProbe: apicommon.GetDefaultReadinessProbe(),
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",
},
},
},
},
}
}