Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Labels for OneAgent CR #183

Merged
merged 3 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Add OpenAPI V3 Schema to CRD objects ([#171](https://github.com/Dynatrace/dynatrace-oneagent-operator/pull/171))
* Operator log entries now use ISO-8601 timestamps (e.g., `"2019-10-30T12:59:43.717+0100"`) ([#159](https://github.com/Dynatrace/dynatrace-oneagent-operator/pull/159))
* The service account for pods can now be customized ([#182](https://github.com/Dynatrace/dynatrace-oneagent-operator/pull/182))
* Custom labels can be added to pods ([#183](https://github.com/Dynatrace/dynatrace-oneagent-operator/pull/183))

### Bug fixes

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ spec:
# DNS Policy for OneAgent pods (optional.) Empty for default (ClusterFirst), more at
# https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#pod-s-dns-policy
#dnsPolicy: ""
# Labels are customer defined labels for oneagent pods to structure workloads as desired
#labels:
# dynatrace : value1
# one-agent : value2
# tenant-id : value3
```
Save the snippet to a file or use [./deploy/cr.yaml](https://raw.githubusercontent.com/Dynatrace/dynatrace-oneagent-operator/master/deploy/cr.yaml) from this repository and adjust its values accordingly.
A secret holding tokens for authenticating to the Dynatrace cluster needs to be created upfront.
Expand Down
5 changes: 5 additions & 0 deletions deploy/cr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,8 @@ spec:
# DNS Policy for OneAgent pods (optional.) Empty for default (ClusterFirst), more at
# https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#pod-s-dns-policy
#dnsPolicy: ""
# Labels are customer defined labels for oneagent pods to structure workloads as desired
#labels:
# dynatrace : value1
# one-agent : value2
# tenant-id : value3
5 changes: 5 additions & 0 deletions deploy/crds/dynatrace.com_oneagents_crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ spec:
image:
description: Installer image Defaults to docker.io/dynatrace/oneagent:latest
type: string
labels:
additionalProperties:
type: string
description: Labels for the OneAgent pods
type: object
nodeSelector:
additionalProperties:
type: string
Expand Down
5 changes: 5 additions & 0 deletions deploy/kubernetes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,11 @@ spec:
serviceAccountName:
description: Name of the service account for the OneAgent
type: string
labels:
additionalProperties:
type: string
description: Labels for the OneAgent pods
type: object
skipCertCheck:
description:
Disable certificate validation checks for installer download
Expand Down
5 changes: 5 additions & 0 deletions deploy/openshift.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,11 @@ spec:
serviceAccountName:
description: Name of the service account for the OneAgent
type: string
labels:
additionalProperties:
type: string
description: Labels for the OneAgent pods
type: object
skipCertCheck:
description:
Disable certificate validation checks for installer download
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/dynatrace/v1alpha1/oneagent_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type OneAgentSpec struct {
DNSPolicy corev1.DNSPolicy `json:"dnsPolicy,omitempty"`
// Name of the service account for the OneAgent
ServiceAccountName string `json:"serviceAccountName,omitempty"`
// Labels for the OneAgent pods
Labels map[string]string `json:"labels,omitempty"`
}

type OneAgentConditionType string
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/dynatrace/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions pkg/apis/dynatrace/v1alpha1/zz_generated.openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,21 @@ func schema_pkg_apis_dynatrace_v1alpha1_OneAgentSpec(ref common.ReferenceCallbac
Format: "",
},
},
"labels": {
SchemaProps: spec.SchemaProps{
Description: "Labels for the OneAgent pods",
Type: []string{"object"},
AdditionalProperties: &spec.SchemaOrBool{
Allows: true,
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"string"},
Format: "",
},
},
},
},
},
},
Required: []string{"apiUrl"},
},
Expand Down
14 changes: 10 additions & 4 deletions pkg/controller/oneagent/oneagent_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ func (r *ReconcileOneAgent) Reconcile(request reconcile.Request) (reconcile.Resu
}
r.scheme.Default(instance)

if instance.Spec.Labels == nil {
instance.Spec.Labels = mergeLabels(buildLabels(instance.Name))
} else {
instance.Spec.Labels = mergeLabels(buildLabels(instance.Name), instance.Spec.Labels)
}

if err := validate(instance); err != nil {
return reconcile.Result{}, err
}
Expand Down Expand Up @@ -383,19 +389,19 @@ func (r *ReconcileOneAgent) updateCR(instance *dynatracev1alpha1.OneAgent) error
}

func newDaemonSetForCR(instance *dynatracev1alpha1.OneAgent) *appsv1.DaemonSet {
selector := buildLabels(instance.Name)

podSpec := newPodSpecForCR(instance)

return &appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Name: instance.Name,
Namespace: instance.Namespace,
Labels: selector,
namratachaudhary marked this conversation as resolved.
Show resolved Hide resolved
Labels: instance.Spec.Labels,
},
Spec: appsv1.DaemonSetSpec{
Selector: &metav1.LabelSelector{MatchLabels: selector},
Selector: &metav1.LabelSelector{MatchLabels: instance.Spec.Labels},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{Labels: selector},
ObjectMeta: metav1.ObjectMeta{Labels: instance.Spec.Labels},
Spec: podSpec,
},
},
Expand Down
3 changes: 3 additions & 0 deletions pkg/controller/oneagent/oneagent_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func TestReconcileOneAgent_ReconcileOnEmptyEnvironmentAndDNSPolicy(t *testing.T)
ApiUrl: "https://ENVIRONMENTID.live.dynatrace.com/api",
DNSPolicy: corev1.DNSClusterFirstWithHostNet,
Tokens: oaName,
Labels: map[string]string{
"label_key": "label_value",
},
}
dynatracev1alpha1.SetDefaults_OneAgentSpec(&oaSpec)

Expand Down
14 changes: 14 additions & 0 deletions pkg/controller/oneagent/oneagent_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ import (
corev1 "k8s.io/api/core/v1"
)

func mergeLabels(labels ...map[string]string) map[string]string {

res := map[string]string{}
for _, m := range labels {
for k, v := range m {
res[k] = v
}
}

return res
}

// BuildLabels returns generic labels based on the name given for a Dynatrace OneAgent
func buildLabels(name string) map[string]string {
return map[string]string{
Expand Down Expand Up @@ -68,6 +80,7 @@ func hasSpecChanged(dsSpec *appsv1.DaemonSetSpec, crSpec *dynatracev1alpha1.OneA
// value is missing in the daemonset as well.
func copyDaemonSetSpecToOneAgentSpec(dsSpec *appsv1.DaemonSetSpec, crSpec *dynatracev1alpha1.OneAgentSpec) {
crSpec.NodeSelector = nil

if dsSpec.Template.Spec.NodeSelector != nil {
in, out := &dsSpec.Template.Spec.NodeSelector, &crSpec.NodeSelector
*out = make(map[string]string, len(*in))
Expand All @@ -87,6 +100,7 @@ func copyDaemonSetSpecToOneAgentSpec(dsSpec *appsv1.DaemonSetSpec, crSpec *dynat
crSpec.ServiceAccountName = dsSpec.Template.Spec.ServiceAccountName
crSpec.PriorityClassName = dsSpec.Template.Spec.PriorityClassName
crSpec.DNSPolicy = dsSpec.Template.Spec.DNSPolicy
crSpec.Labels = dsSpec.Template.Labels
namratachaudhary marked this conversation as resolved.
Show resolved Hide resolved
crSpec.Image = ""
if len(dsSpec.Template.Spec.Containers) == 1 {
crSpec.Image = dsSpec.Template.Spec.Containers[0].Image
Expand Down