-
Notifications
You must be signed in to change notification settings - Fork 102
/
feature.go
270 lines (228 loc) · 10.6 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
270
// 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 orchestratorexplorer
import (
"fmt"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/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/pkg/controller/utils/comparison"
"github.com/DataDog/datadog-operator/pkg/kubernetes"
"github.com/DataDog/datadog-operator/pkg/utils"
"github.com/go-logr/logr"
apicommon "github.com/DataDog/datadog-operator/apis/datadoghq/common"
apicommonv1 "github.com/DataDog/datadog-operator/apis/datadoghq/common/v1"
common "github.com/DataDog/datadog-operator/controllers/datadogagent/common"
"github.com/DataDog/datadog-operator/controllers/datadogagent/component"
"github.com/DataDog/datadog-operator/controllers/datadogagent/feature"
"github.com/DataDog/datadog-operator/controllers/datadogagent/object"
"github.com/DataDog/datadog-operator/controllers/datadogagent/object/volume"
)
func init() {
err := feature.Register(feature.OrchestratorExplorerIDType, buildOrchestratorExplorerFeature)
if err != nil {
panic(err)
}
}
func buildOrchestratorExplorerFeature(options *feature.Options) feature.Feature {
orchestratorExplorerFeat := &orchestratorExplorerFeature{
rbacSuffix: common.ClusterAgentSuffix,
}
if options != nil {
orchestratorExplorerFeat.logger = options.Logger
}
return orchestratorExplorerFeat
}
type orchestratorExplorerFeature struct {
runInClusterChecksRunner bool
scrubContainers bool
extraTags []string
ddURL string
rbacSuffix string
serviceAccountName string
owner metav1.Object
customConfig *apicommonv1.CustomConfig
customResources []string
configConfigMapName string
logger logr.Logger
customConfigAnnotationKey string
customConfigAnnotationValue string
processAgentRequired bool
}
const NoProcessAgentMinVersion = "7.51.0-0"
// ID returns the ID of the Feature
func (f *orchestratorExplorerFeature) ID() feature.IDType {
return feature.OrchestratorExplorerIDType
}
// Configure is used to configure the feature from a v2alpha1.DatadogAgent instance.
func (f *orchestratorExplorerFeature) Configure(dda *v2alpha1.DatadogAgent) (reqComp feature.RequiredComponents) {
f.owner = dda
orchestratorExplorer := dda.Spec.Features.OrchestratorExplorer
if orchestratorExplorer != nil && apiutils.BoolValue(orchestratorExplorer.Enabled) {
reqComp.ClusterAgent.IsRequired = apiutils.NewBoolPointer(true)
reqContainers := []apicommonv1.AgentContainerName{apicommonv1.CoreAgentContainerName}
// Process Agent is not required as of agent version 7.51.0
if nodeAgent, ok := dda.Spec.Override[v2alpha1.NodeAgentComponentName]; ok {
if nodeAgent.Image != nil && !utils.IsAboveMinVersion(component.GetAgentVersionFromImage(*nodeAgent.Image), NoProcessAgentMinVersion) {
f.processAgentRequired = true
reqContainers = append(reqContainers, apicommonv1.ProcessAgentContainerName)
}
}
reqComp.Agent = feature.RequiredComponent{
IsRequired: apiutils.NewBoolPointer(true),
Containers: reqContainers,
}
if orchestratorExplorer.Conf != nil {
f.customConfig = v2alpha1.ConvertCustomConfig(orchestratorExplorer.Conf)
hash, err := comparison.GenerateMD5ForSpec(f.customConfig)
if err != nil {
f.logger.Error(err, "couldn't generate hash for orchestrator explorer custom config")
} else {
f.logger.V(2).Info("built orchestrator explorer from custom config", "hash", hash)
}
f.customConfigAnnotationValue = hash
f.customConfigAnnotationKey = object.GetChecksumAnnotationKey(feature.OrchestratorExplorerIDType)
}
f.customResources = dda.Spec.Features.OrchestratorExplorer.CustomResources
f.configConfigMapName = apicommonv1.GetConfName(dda, f.customConfig, apicommon.DefaultOrchestratorExplorerConf)
f.scrubContainers = apiutils.BoolValue(orchestratorExplorer.ScrubContainers)
f.extraTags = orchestratorExplorer.ExtraTags
if orchestratorExplorer.DDUrl != nil {
f.ddURL = *orchestratorExplorer.DDUrl
}
f.serviceAccountName = v2alpha1.GetClusterAgentServiceAccount(dda)
if v2alpha1.IsClusterChecksEnabled(dda) {
if v2alpha1.IsCCREnabled(dda) {
f.runInClusterChecksRunner = true
f.rbacSuffix = common.ChecksRunnerSuffix
f.serviceAccountName = v2alpha1.GetClusterChecksRunnerServiceAccount(dda)
reqComp.ClusterChecksRunner.IsRequired = apiutils.NewBoolPointer(true)
}
}
}
return reqComp
}
// ConfigureV1 use to configure the feature from a v1alpha1.DatadogAgent instance.
func (f *orchestratorExplorerFeature) ConfigureV1(dda *v1alpha1.DatadogAgent) (reqComp feature.RequiredComponents) {
f.owner = dda
orchestratorExplorer := dda.Spec.Features.OrchestratorExplorer
f.processAgentRequired = true
if orchestratorExplorer != nil && apiutils.BoolValue(orchestratorExplorer.Enabled) {
reqComp.ClusterAgent.IsRequired = apiutils.NewBoolPointer(true)
reqComp.Agent = feature.RequiredComponent{
IsRequired: apiutils.NewBoolPointer(true),
Containers: []apicommonv1.AgentContainerName{apicommonv1.CoreAgentContainerName, apicommonv1.ProcessAgentContainerName},
}
if orchestratorExplorer.Conf != nil {
f.customConfig = v1alpha1.ConvertCustomConfig(orchestratorExplorer.Conf)
}
f.configConfigMapName = apicommonv1.GetConfName(dda, f.customConfig, apicommon.DefaultOrchestratorExplorerConf)
if orchestratorExplorer.Scrubbing != nil {
f.scrubContainers = apiutils.BoolValue(orchestratorExplorer.Scrubbing.Containers)
}
f.extraTags = orchestratorExplorer.ExtraTags
if orchestratorExplorer.DDUrl != nil {
f.ddURL = *orchestratorExplorer.DDUrl
}
f.serviceAccountName = v1alpha1.GetClusterAgentServiceAccount(dda)
if v1alpha1.IsClusterChecksEnabled(dda) && apiutils.BoolValue(orchestratorExplorer.ClusterCheck) {
if v1alpha1.IsCCREnabled(dda) {
f.runInClusterChecksRunner = true
reqComp.ClusterChecksRunner.IsRequired = apiutils.NewBoolPointer(true)
f.rbacSuffix = common.ChecksRunnerSuffix
f.serviceAccountName = v1alpha1.GetClusterChecksRunnerServiceAccount(dda)
}
}
}
return reqComp
}
// ManageDependencies allows a feature to manage its dependencies.
// Feature's dependencies should be added in the store.
func (f *orchestratorExplorerFeature) ManageDependencies(managers feature.ResourceManagers, components feature.RequiredComponents) error {
// Create a configMap if CustomConfig.ConfigData is provided and CustomConfig.ConfigMap == nil,
// OR if the default configMap is needed.
configCM, err := f.buildOrchestratorExplorerConfigMap()
if err != nil {
return err
}
if configCM != nil {
// Add md5 hash annotation for custom config
if f.customConfigAnnotationKey != "" && f.customConfigAnnotationValue != "" {
annotations := object.MergeAnnotationsLabels(f.logger, configCM.GetAnnotations(), map[string]string{f.customConfigAnnotationKey: f.customConfigAnnotationValue}, "*")
configCM.SetAnnotations(annotations)
}
if err := managers.Store().AddOrUpdate(kubernetes.ConfigMapKind, configCM); err != nil {
return err
}
}
// Manage RBAC permission
rbacName := GetOrchestratorExplorerRBACResourceName(f.owner, f.rbacSuffix)
return managers.RBACManager().AddClusterPolicyRules(f.owner.GetNamespace(), rbacName, f.serviceAccountName, getRBACPolicyRules(f.logger, f.customResources))
}
// ManageClusterAgent allows a feature to configure the ClusterAgent's corev1.PodTemplateSpec
// It should do nothing if the feature doesn't need to configure it.
func (f *orchestratorExplorerFeature) ManageClusterAgent(managers feature.PodTemplateManagers) error {
// Manage orchestrator config in configmap
var vol corev1.Volume
var volMount corev1.VolumeMount
if f.customConfig != nil && f.customConfig.ConfigMap != nil {
// Custom config is referenced via ConfigMap
vol, volMount = volume.GetVolumesFromConfigMap(
f.customConfig.ConfigMap,
apicommon.OrchestratorExplorerVolumeName,
f.configConfigMapName,
orchestratorExplorerFolderName,
)
} else {
// Otherwise, configMap was created in ManageDependencies (whether from CustomConfig.ConfigData or using defaults, so mount default volume)
vol = volume.GetBasicVolume(f.configConfigMapName, apicommon.OrchestratorExplorerVolumeName)
volMount = corev1.VolumeMount{
Name: apicommon.OrchestratorExplorerVolumeName,
MountPath: fmt.Sprintf("%s%s/%s", apicommon.ConfigVolumePath, apicommon.ConfdVolumePath, orchestratorExplorerFolderName),
ReadOnly: true,
}
}
managers.VolumeMount().AddVolumeMountToContainer(&volMount, apicommonv1.ClusterAgentContainerName)
managers.Volume().AddVolume(&vol)
if f.customConfigAnnotationKey != "" && f.customConfigAnnotationValue != "" {
managers.Annotation().AddAnnotation(f.customConfigAnnotationKey, f.customConfigAnnotationValue)
}
for _, env := range f.getEnvVars() {
managers.EnvVar().AddEnvVar(env)
}
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 *orchestratorExplorerFeature) ManageSingleContainerNodeAgent(managers feature.PodTemplateManagers, provider string) error {
for _, env := range f.getEnvVars() {
managers.EnvVar().AddEnvVarToContainer(apicommonv1.UnprivilegedSingleAgentContainerName, env)
}
return nil
}
// ManageNodeAgent allows a feature to configure the Node Agent's corev1.PodTemplateSpec
// It should do nothing if the feature doesn't need to configure it.
func (f *orchestratorExplorerFeature) ManageNodeAgent(managers feature.PodTemplateManagers, provider string) error {
for _, env := range f.getEnvVars() {
if f.processAgentRequired {
managers.EnvVar().AddEnvVarToContainer(apicommonv1.ProcessAgentContainerName, env)
}
managers.EnvVar().AddEnvVarToContainer(apicommonv1.CoreAgentContainerName, env)
}
return nil
}
// ManageClusterChecksRunner allows a feature to configure the ClusterChecksRunner's corev1.PodTemplateSpec
// It should do nothing if the feature doesn't need to configure it.
func (f *orchestratorExplorerFeature) ManageClusterChecksRunner(managers feature.PodTemplateManagers) error {
if f.runInClusterChecksRunner {
for _, env := range f.getEnvVars() {
managers.EnvVar().AddEnvVarToContainer(apicommonv1.ClusterChecksRunnersContainerName, env)
}
}
return nil
}