-
Notifications
You must be signed in to change notification settings - Fork 104
/
feature.go
269 lines (234 loc) · 8.29 KB
/
feature.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
// 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 clusterchecks
import (
apicommon "github.com/DataDog/datadog-operator/apis/datadoghq/common"
"github.com/DataDog/datadog-operator/apis/datadoghq/common/v1"
"github.com/DataDog/datadog-operator/apis/datadoghq/v1alpha1"
"github.com/DataDog/datadog-operator/apis/datadoghq/v2alpha1"
apiutils "github.com/DataDog/datadog-operator/apis/utils"
"github.com/DataDog/datadog-operator/controllers/datadogagent/component"
"github.com/DataDog/datadog-operator/controllers/datadogagent/feature"
"github.com/DataDog/datadog-operator/controllers/datadogagent/object"
cilium "github.com/DataDog/datadog-operator/pkg/cilium/v1"
"github.com/DataDog/datadog-operator/pkg/controller/utils/comparison"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
netv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
func init() {
err := feature.Register(feature.ClusterChecksIDType, buildClusterChecksFeature)
if err != nil {
panic(err)
}
}
type clusterChecksFeature struct {
useClusterCheckRunners bool
owner metav1.Object
createKubernetesNetworkPolicy bool
createCiliumNetworkPolicy bool
customConfigAnnotationKey string
customConfigAnnotationValue string
logger logr.Logger
}
func buildClusterChecksFeature(options *feature.Options) feature.Feature {
feature := &clusterChecksFeature{}
if options != nil {
feature.logger = options.Logger
}
return feature
}
// ID returns the ID of the Feature
func (f *clusterChecksFeature) ID() feature.IDType {
return feature.ClusterChecksIDType
}
func (f *clusterChecksFeature) Configure(dda *v2alpha1.DatadogAgent) (reqComp feature.RequiredComponents) {
if apiutils.BoolValue(dda.Spec.Features.ClusterChecks.Enabled) {
f.updateConfigHash(dda)
f.owner = dda
if enabled, flavor := v2alpha1.IsNetworkPolicyEnabled(dda); enabled {
if flavor == v2alpha1.NetworkPolicyFlavorCilium {
f.createCiliumNetworkPolicy = true
} else {
f.createKubernetesNetworkPolicy = true
}
}
f.useClusterCheckRunners = apiutils.BoolValue(dda.Spec.Features.ClusterChecks.UseClusterChecksRunners)
reqComp = feature.RequiredComponents{
Agent: feature.RequiredComponent{IsRequired: apiutils.NewBoolPointer(true)},
ClusterAgent: feature.RequiredComponent{IsRequired: apiutils.NewBoolPointer(true)},
ClusterChecksRunner: feature.RequiredComponent{IsRequired: &f.useClusterCheckRunners},
}
}
return reqComp
}
func (f *clusterChecksFeature) ConfigureV1(dda *v1alpha1.DatadogAgent) feature.RequiredComponents {
clusterChecksEnabled := false
if dda != nil && dda.Spec.ClusterAgent.Config != nil {
clusterChecksEnabled = apiutils.BoolValue(dda.Spec.ClusterAgent.Config.ClusterChecksEnabled)
f.useClusterCheckRunners = clusterChecksEnabled && apiutils.BoolValue(dda.Spec.ClusterChecksRunner.Enabled)
}
if clusterChecksEnabled {
f.owner = dda
if enabled, flavor := v1alpha1.IsAgentNetworkPolicyEnabled(dda); enabled {
if flavor == v1alpha1.NetworkPolicyFlavorCilium {
f.createCiliumNetworkPolicy = true
} else {
f.createKubernetesNetworkPolicy = true
}
}
return feature.RequiredComponents{
ClusterAgent: feature.RequiredComponent{IsRequired: apiutils.NewBoolPointer(true)},
ClusterChecksRunner: feature.RequiredComponent{IsRequired: &f.useClusterCheckRunners},
}
}
return feature.RequiredComponents{
ClusterChecksRunner: feature.RequiredComponent{IsRequired: apiutils.NewBoolPointer(false)},
}
}
func (f *clusterChecksFeature) ManageDependencies(managers feature.ResourceManagers, components feature.RequiredComponents) error {
policyName, podSelector := component.GetNetworkPolicyMetadata(f.owner, v2alpha1.ClusterAgentComponentName)
_, ccrPodSelector := component.GetNetworkPolicyMetadata(f.owner, v2alpha1.ClusterChecksRunnerComponentName)
if f.createKubernetesNetworkPolicy {
ingressRules := []netv1.NetworkPolicyIngressRule{
{
Ports: []netv1.NetworkPolicyPort{
{
Port: &intstr.IntOrString{
Type: intstr.Int,
IntVal: apicommon.DefaultClusterAgentServicePort,
},
},
},
From: []netv1.NetworkPolicyPeer{
{
PodSelector: &ccrPodSelector,
},
},
},
}
return managers.NetworkPolicyManager().AddKubernetesNetworkPolicy(
policyName,
f.owner.GetNamespace(),
podSelector,
nil,
ingressRules,
nil,
)
} else if f.createCiliumNetworkPolicy {
policySpecs := []cilium.NetworkPolicySpec{
{
Description: "Ingress from cluster workers",
EndpointSelector: podSelector,
Ingress: []cilium.IngressRule{
{
FromEndpoints: []metav1.LabelSelector{ccrPodSelector},
ToPorts: []cilium.PortRule{
{
Ports: []cilium.PortProtocol{
{
Port: "5005",
Protocol: cilium.ProtocolTCP,
},
},
},
},
},
},
},
}
return managers.CiliumPolicyManager().AddCiliumPolicy(policyName, f.owner.GetNamespace(), policySpecs)
}
return nil
}
func (f *clusterChecksFeature) ManageClusterAgent(managers feature.PodTemplateManagers) error {
managers.EnvVar().AddEnvVarToContainer(
common.ClusterAgentContainerName,
&corev1.EnvVar{
Name: apicommon.DDClusterChecksEnabled,
Value: "true",
},
)
managers.EnvVar().AddEnvVarToContainer(
common.ClusterAgentContainerName,
&corev1.EnvVar{
Name: apicommon.DDExtraConfigProviders,
Value: apicommon.KubeServicesAndEndpointsConfigProviders,
},
)
managers.EnvVar().AddEnvVarToContainer(
common.ClusterAgentContainerName,
&corev1.EnvVar{
Name: apicommon.DDExtraListeners,
Value: apicommon.KubeServicesAndEndpointsListeners,
},
)
if f.customConfigAnnotationKey != "" && f.customConfigAnnotationValue != "" {
managers.Annotation().AddAnnotation(f.customConfigAnnotationKey, f.customConfigAnnotationValue)
}
return nil
}
// ManageSingleContainerNodeAgent allows a feature to configure the Agent container for the Node Agent's corev1.PodTemplateSpec
// if SingleContainerStrategy is enabled and can be used with the configured feature set.
// It should do nothing if the feature doesn't need to configure it.
func (f *clusterChecksFeature) ManageSingleContainerNodeAgent(managers feature.PodTemplateManagers, provider string) error {
f.manageNodeAgent(common.UnprivilegedSingleAgentContainerName, managers, provider)
return nil
}
func (f *clusterChecksFeature) ManageNodeAgent(managers feature.PodTemplateManagers, provider string) error {
f.manageNodeAgent(common.CoreAgentContainerName, managers, provider)
return nil
}
func (f *clusterChecksFeature) manageNodeAgent(agentContainerName common.AgentContainerName, managers feature.PodTemplateManagers, provider string) error {
if f.useClusterCheckRunners {
managers.EnvVar().AddEnvVarToContainer(
agentContainerName,
&corev1.EnvVar{
Name: apicommon.DDExtraConfigProviders,
Value: apicommon.EndpointsChecksConfigProvider,
},
)
} else {
managers.EnvVar().AddEnvVarToContainer(
agentContainerName,
&corev1.EnvVar{
Name: apicommon.DDExtraConfigProviders,
Value: apicommon.ClusterAndEndpointsConfigProviders,
},
)
}
return nil
}
func (f *clusterChecksFeature) ManageClusterChecksRunner(managers feature.PodTemplateManagers) error {
if f.useClusterCheckRunners {
managers.EnvVar().AddEnvVarToContainer(
common.ClusterChecksRunnersContainerName,
&corev1.EnvVar{
Name: apicommon.DDClusterChecksEnabled,
Value: "true",
},
)
managers.EnvVar().AddEnvVarToContainer(
common.ClusterChecksRunnersContainerName,
&corev1.EnvVar{
Name: apicommon.DDExtraConfigProviders,
Value: apicommon.ClusterChecksConfigProvider,
},
)
}
return nil
}
func (f *clusterChecksFeature) updateConfigHash(dda *v2alpha1.DatadogAgent) {
hash, err := comparison.GenerateMD5ForSpec(dda.Spec.Features.ClusterChecks)
if err != nil {
f.logger.Error(err, "couldn't generate hash for cluster checks config")
} else {
f.logger.V(2).Info("created cluster checks", "hash", hash)
}
f.customConfigAnnotationValue = hash
f.customConfigAnnotationKey = object.GetChecksumAnnotationKey(feature.ClusterChecksIDType)
}