From 69e05dfb48d35f9600b3ff546e2adcd53915ebaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Svantesson?= Date: Tue, 19 Sep 2023 09:44:17 +0200 Subject: [PATCH] fix: handle alias when adding annotation or label fixing wrong Node Kind for ... expected: MappingNode was AliasNode --- .../annotate/testpodspec/deploy/expected.yaml | 3 +- .../annotate/testpodspec/deploy/source.yaml | 10 +- pkg/cmd/label/testdata/deploy/expected.yaml | 104 ++++++++++++++++++ .../testdata/deploy/expectednotoverride.yaml | 104 ++++++++++++++++++ pkg/cmd/label/testdata/deploy/source.yaml | 98 +++++++++++++++++ pkg/tagging/base.go | 5 + 6 files changed, 314 insertions(+), 10 deletions(-) create mode 100644 pkg/cmd/label/testdata/deploy/expected.yaml create mode 100644 pkg/cmd/label/testdata/deploy/expectednotoverride.yaml create mode 100644 pkg/cmd/label/testdata/deploy/source.yaml diff --git a/pkg/cmd/annotate/testpodspec/deploy/expected.yaml b/pkg/cmd/annotate/testpodspec/deploy/expected.yaml index 781b5f86..a32368da 100644 --- a/pkg/cmd/annotate/testpodspec/deploy/expected.yaml +++ b/pkg/cmd/annotate/testpodspec/deploy/expected.yaml @@ -5,7 +5,6 @@ metadata: app.kubernetes.io/instance: external-dns app.kubernetes.io/managed-by: Helm app.kubernetes.io/name: external-dns - gitops.jenkins-x.io/pipeline: namespaces helm.sh/chart: external-dns-6.8.2 name: external-dns namespace: jx @@ -27,8 +26,8 @@ spec: annotations: prometheus.io/port: "7979" prometheus.io/scrape: "true" - wine: 'merlot' beer: 'stella' + wine: 'merlot' labels: app.kubernetes.io/instance: external-dns app.kubernetes.io/managed-by: Helm diff --git a/pkg/cmd/annotate/testpodspec/deploy/source.yaml b/pkg/cmd/annotate/testpodspec/deploy/source.yaml index 156eece6..9ac0428c 100644 --- a/pkg/cmd/annotate/testpodspec/deploy/source.yaml +++ b/pkg/cmd/annotate/testpodspec/deploy/source.yaml @@ -1,11 +1,10 @@ apiVersion: apps/v1 kind: Deployment metadata: - labels: + labels: &test_labels app.kubernetes.io/instance: external-dns app.kubernetes.io/managed-by: Helm app.kubernetes.io/name: external-dns - gitops.jenkins-x.io/pipeline: namespaces helm.sh/chart: external-dns-6.8.2 name: external-dns namespace: jx @@ -27,12 +26,7 @@ spec: annotations: prometheus.io/port: "7979" prometheus.io/scrape: "true" - wine: "riesling" - labels: - app.kubernetes.io/instance: external-dns - app.kubernetes.io/managed-by: Helm - app.kubernetes.io/name: external-dns - helm.sh/chart: external-dns-6.8.2 + labels: *test_labels spec: affinity: podAntiAffinity: diff --git a/pkg/cmd/label/testdata/deploy/expected.yaml b/pkg/cmd/label/testdata/deploy/expected.yaml new file mode 100644 index 00000000..7f234c61 --- /dev/null +++ b/pkg/cmd/label/testdata/deploy/expected.yaml @@ -0,0 +1,104 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app.kubernetes.io/instance: external-dns + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/name: external-dns + helm.sh/chart: external-dns-6.8.2 + beer: 'stella' + wine: 'merlot' + name: external-dns + namespace: jx +spec: + progressDeadlineSeconds: 600 + replicas: 1 + revisionHistoryLimit: 10 + selector: + matchLabels: + app.kubernetes.io/instance: external-dns + app.kubernetes.io/name: external-dns + strategy: + rollingUpdate: + maxSurge: 25% + maxUnavailable: 25% + type: RollingUpdate + template: + metadata: + annotations: + prometheus.io/port: "7979" + prometheus.io/scrape: "true" + labels: + app.kubernetes.io/instance: external-dns + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/name: external-dns + helm.sh/chart: external-dns-6.8.2 + spec: + affinity: + podAntiAffinity: + preferredDuringSchedulingIgnoredDuringExecution: + - podAffinityTerm: + labelSelector: + matchLabels: + app.kubernetes.io/instance: external-dns + app.kubernetes.io/name: external-dns + namespaces: + - jx + topologyKey: kubernetes.io/hostname + weight: 1 + containers: + - args: + - --metrics-address=:7979 + - --log-level=debug + - --log-format=text + - --domain-filter=aws.ticket.se + - --policy=sync + - --provider=aws + - --registry=txt + - --interval=1m + - --source=ingress + - --source=service + - --aws-api-retries=3 + - --aws-zone-type= + - --aws-batch-change-size=1000 + env: + - name: AWS_DEFAULT_REGION + value: eu-north-1 + image: docker.io/bitnami/external-dns:0.12.2-debian-11-r14 + imagePullPolicy: IfNotPresent + livenessProbe: + failureThreshold: 2 + httpGet: + path: /healthz + port: http + scheme: HTTP + initialDelaySeconds: 10 + periodSeconds: 10 + successThreshold: 1 + timeoutSeconds: 5 + name: external-dns + ports: + - containerPort: 7979 + name: http + protocol: TCP + readinessProbe: + failureThreshold: 6 + httpGet: + path: /healthz + port: http + scheme: HTTP + initialDelaySeconds: 5 + periodSeconds: 10 + successThreshold: 1 + timeoutSeconds: 5 + terminationMessagePath: /dev/termination-log + terminationMessagePolicy: File + dnsPolicy: ClusterFirst + restartPolicy: Always + schedulerName: default-scheduler + securityContext: + fsGroup: 65534 + runAsUser: 1001 + serviceAccount: external-dns + serviceAccountName: external-dns + terminationGracePeriodSeconds: 30 diff --git a/pkg/cmd/label/testdata/deploy/expectednotoverride.yaml b/pkg/cmd/label/testdata/deploy/expectednotoverride.yaml new file mode 100644 index 00000000..7f234c61 --- /dev/null +++ b/pkg/cmd/label/testdata/deploy/expectednotoverride.yaml @@ -0,0 +1,104 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app.kubernetes.io/instance: external-dns + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/name: external-dns + helm.sh/chart: external-dns-6.8.2 + beer: 'stella' + wine: 'merlot' + name: external-dns + namespace: jx +spec: + progressDeadlineSeconds: 600 + replicas: 1 + revisionHistoryLimit: 10 + selector: + matchLabels: + app.kubernetes.io/instance: external-dns + app.kubernetes.io/name: external-dns + strategy: + rollingUpdate: + maxSurge: 25% + maxUnavailable: 25% + type: RollingUpdate + template: + metadata: + annotations: + prometheus.io/port: "7979" + prometheus.io/scrape: "true" + labels: + app.kubernetes.io/instance: external-dns + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/name: external-dns + helm.sh/chart: external-dns-6.8.2 + spec: + affinity: + podAntiAffinity: + preferredDuringSchedulingIgnoredDuringExecution: + - podAffinityTerm: + labelSelector: + matchLabels: + app.kubernetes.io/instance: external-dns + app.kubernetes.io/name: external-dns + namespaces: + - jx + topologyKey: kubernetes.io/hostname + weight: 1 + containers: + - args: + - --metrics-address=:7979 + - --log-level=debug + - --log-format=text + - --domain-filter=aws.ticket.se + - --policy=sync + - --provider=aws + - --registry=txt + - --interval=1m + - --source=ingress + - --source=service + - --aws-api-retries=3 + - --aws-zone-type= + - --aws-batch-change-size=1000 + env: + - name: AWS_DEFAULT_REGION + value: eu-north-1 + image: docker.io/bitnami/external-dns:0.12.2-debian-11-r14 + imagePullPolicy: IfNotPresent + livenessProbe: + failureThreshold: 2 + httpGet: + path: /healthz + port: http + scheme: HTTP + initialDelaySeconds: 10 + periodSeconds: 10 + successThreshold: 1 + timeoutSeconds: 5 + name: external-dns + ports: + - containerPort: 7979 + name: http + protocol: TCP + readinessProbe: + failureThreshold: 6 + httpGet: + path: /healthz + port: http + scheme: HTTP + initialDelaySeconds: 5 + periodSeconds: 10 + successThreshold: 1 + timeoutSeconds: 5 + terminationMessagePath: /dev/termination-log + terminationMessagePolicy: File + dnsPolicy: ClusterFirst + restartPolicy: Always + schedulerName: default-scheduler + securityContext: + fsGroup: 65534 + runAsUser: 1001 + serviceAccount: external-dns + serviceAccountName: external-dns + terminationGracePeriodSeconds: 30 diff --git a/pkg/cmd/label/testdata/deploy/source.yaml b/pkg/cmd/label/testdata/deploy/source.yaml new file mode 100644 index 00000000..9ac0428c --- /dev/null +++ b/pkg/cmd/label/testdata/deploy/source.yaml @@ -0,0 +1,98 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: &test_labels + app.kubernetes.io/instance: external-dns + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/name: external-dns + helm.sh/chart: external-dns-6.8.2 + name: external-dns + namespace: jx +spec: + progressDeadlineSeconds: 600 + replicas: 1 + revisionHistoryLimit: 10 + selector: + matchLabels: + app.kubernetes.io/instance: external-dns + app.kubernetes.io/name: external-dns + strategy: + rollingUpdate: + maxSurge: 25% + maxUnavailable: 25% + type: RollingUpdate + template: + metadata: + annotations: + prometheus.io/port: "7979" + prometheus.io/scrape: "true" + labels: *test_labels + spec: + affinity: + podAntiAffinity: + preferredDuringSchedulingIgnoredDuringExecution: + - podAffinityTerm: + labelSelector: + matchLabels: + app.kubernetes.io/instance: external-dns + app.kubernetes.io/name: external-dns + namespaces: + - jx + topologyKey: kubernetes.io/hostname + weight: 1 + containers: + - args: + - --metrics-address=:7979 + - --log-level=debug + - --log-format=text + - --domain-filter=aws.ticket.se + - --policy=sync + - --provider=aws + - --registry=txt + - --interval=1m + - --source=ingress + - --source=service + - --aws-api-retries=3 + - --aws-zone-type= + - --aws-batch-change-size=1000 + env: + - name: AWS_DEFAULT_REGION + value: eu-north-1 + image: docker.io/bitnami/external-dns:0.12.2-debian-11-r14 + imagePullPolicy: IfNotPresent + livenessProbe: + failureThreshold: 2 + httpGet: + path: /healthz + port: http + scheme: HTTP + initialDelaySeconds: 10 + periodSeconds: 10 + successThreshold: 1 + timeoutSeconds: 5 + name: external-dns + ports: + - containerPort: 7979 + name: http + protocol: TCP + readinessProbe: + failureThreshold: 6 + httpGet: + path: /healthz + port: http + scheme: HTTP + initialDelaySeconds: 5 + periodSeconds: 10 + successThreshold: 1 + timeoutSeconds: 5 + terminationMessagePath: /dev/termination-log + terminationMessagePolicy: File + dnsPolicy: ClusterFirst + restartPolicy: Always + schedulerName: default-scheduler + securityContext: + fsGroup: 65534 + runAsUser: 1001 + serviceAccount: external-dns + serviceAccountName: external-dns + terminationGracePeriodSeconds: 30 diff --git a/pkg/tagging/base.go b/pkg/tagging/base.go index 31e0765c..af7da520 100644 --- a/pkg/tagging/base.go +++ b/pkg/tagging/base.go @@ -65,6 +65,11 @@ func NewCmdUpdateTag(tagVerb, tagType string) (*cobra.Command, *Options) { // UpdateTagInYamlFiles updates the annotations in yaml files func (o *Options) UpdateTagInYamlFiles(tagType string, tags []string) error { modifyFn := func(node *yaml.RNode, path string) (bool, error) { + // The FieldMatcher assumes MappingNode notAliasNode, so need to DeAnchor before going on + err := node.DeAnchor() + if err != nil { + return false, err + } sort.Strings(tags) tagNode, err := getTagNode(node, path, tagType, o) if err != nil {