-
Notifications
You must be signed in to change notification settings - Fork 99
/
new.go
62 lines (52 loc) · 2.19 KB
/
new.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
// 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 component
import (
apicommon "github.com/DataDog/datadog-operator/apis/datadoghq/common"
"github.com/DataDog/datadog-operator/controllers/datadogagent/object"
"github.com/DataDog/datadog-operator/pkg/kubernetes"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// NewDeployment use to generate the skeleton of a new deployment based on few information
func NewDeployment(owner metav1.Object, componentKind, componentName, version string, inputSelector *metav1.LabelSelector) *appsv1.Deployment {
labels, annotations, selector := GetDefaultMetadata(owner, componentKind, componentName, version, inputSelector)
deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: componentName,
Namespace: owner.GetNamespace(),
Labels: labels,
Annotations: annotations,
},
Spec: appsv1.DeploymentSpec{
Selector: selector,
},
}
return deployment
}
func GetDefaultMetadata(owner metav1.Object, componentKind, componentName, version string, selector *metav1.LabelSelector) (map[string]string, map[string]string, *metav1.LabelSelector) {
labels := GetDefaultLabels(owner, componentKind, componentName, version)
annotations := object.GetDefaultAnnotations(owner)
if selector != nil {
for key, val := range selector.MatchLabels {
labels[key] = val
}
} else {
selector = &metav1.LabelSelector{
MatchLabels: map[string]string{
apicommon.AgentDeploymentNameLabelKey: owner.GetName(),
apicommon.AgentDeploymentComponentLabelKey: componentKind,
},
}
}
return labels, annotations, selector
}
func GetDefaultLabels(owner metav1.Object, componentKind, componentName, version string) map[string]string {
labels := object.GetDefaultLabels(owner, componentName, version)
labels[apicommon.AgentDeploymentNameLabelKey] = owner.GetName()
labels[apicommon.AgentDeploymentComponentLabelKey] = componentKind
labels[kubernetes.AppKubernetesComponentLabelKey] = componentKind
return labels
}