From 1f7441845e642aed23ba6d5d282a54c8440e203e Mon Sep 17 00:00:00 2001 From: Charlie Drage Date: Sun, 2 Jun 2024 14:46:39 -0400 Subject: [PATCH] bug: fix annotation bug (#1887) #### What type of PR is this? #### What this PR does / why we need it: When supplying a label in the compose file, it should stay in the output too. #### Which issue(s) this PR fixes: Fixes https://github.com/kubernetes/kompose/issues/1885 #### Special notes for your reviewer: Signed-off-by: Charlie Drage --- pkg/transformer/utils.go | 13 +++-- script/test/cmd/tests.sh | 5 ++ .../configmap-file-configs/compose-3.yaml | 4 +- .../configmap-file-configs/output-k8s-3.yaml | 1 + .../configmap-file-configs/output-os-3.yaml | 1 + script/test/fixtures/label/compose.yaml | 7 +++ script/test/fixtures/label/output-k8s.yaml | 47 +++++++++++++++++++ 7 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 script/test/fixtures/label/compose.yaml create mode 100644 script/test/fixtures/label/output-k8s.yaml diff --git a/pkg/transformer/utils.go b/pkg/transformer/utils.go index fe2e1aea..c29c4be9 100644 --- a/pkg/transformer/utils.go +++ b/pkg/transformer/utils.go @@ -249,10 +249,6 @@ func ConfigAllLabels(name string, service *kobject.ServiceConfig) map[string]str func ConfigAnnotations(service kobject.ServiceConfig) map[string]string { annotations := map[string]string{} - if !service.WithKomposeAnnotation { - return annotations - } - for key, value := range service.Annotations { annotations[key] = value } @@ -270,6 +266,15 @@ func ConfigAnnotations(service kobject.ServiceConfig) map[string]string { annotations["kompose.version"] = version.VERSION + " (" + version.GITCOMMIT + ")" } + // if service.WithKomposeAnnotation = false, we remove **all** kompose annotations (io.kompose.*) + if !service.WithKomposeAnnotation { + for key := range annotations { + if strings.HasPrefix(key, "kompose.") { + delete(annotations, key) + } + } + } + return annotations } diff --git a/script/test/cmd/tests.sh b/script/test/cmd/tests.sh index c683b6ca..9220fbc1 100755 --- a/script/test/cmd/tests.sh +++ b/script/test/cmd/tests.sh @@ -386,3 +386,8 @@ os_cmd="kompose -f $KOMPOSE_ROOT/script/test/fixtures/envvars-with-status/compos os_output="$KOMPOSE_ROOT/script/test/fixtures/envvars-with-status/output-os.yaml" convert::expect_success "$k8s_cmd" "$k8s_output" || exit 1 convert::expect_success "$os_cmd" "$os_output" || exit 1 + +# Test label in compose.yaml appears in the output annotation +k8s_cmd="kompose -f $KOMPOSE_ROOT/script/test/fixtures/label/compose.yaml convert --stdout --with-kompose-annotation=false" +k8s_output="$KOMPOSE_ROOT/script/test/fixtures/label/output-k8s.yaml" +convert::expect_success "$k8s_cmd" "$k8s_output" || exit 1 \ No newline at end of file diff --git a/script/test/fixtures/configmap-file-configs/compose-3.yaml b/script/test/fixtures/configmap-file-configs/compose-3.yaml index 9fbc4e97..58b9c2ff 100644 --- a/script/test/fixtures/configmap-file-configs/compose-3.yaml +++ b/script/test/fixtures/configmap-file-configs/compose-3.yaml @@ -5,6 +5,4 @@ services: - "8081:8080" - "8026:8025" volumes: - - /var/run/docker.sock:/var/run/docker.sock:ro - - /sys:/sys:ro - - /var/lib/docker/:/var/lib/docker:ro + - /tmp:/tmp:ro \ No newline at end of file diff --git a/script/test/fixtures/configmap-file-configs/output-k8s-3.yaml b/script/test/fixtures/configmap-file-configs/output-k8s-3.yaml index b6e85eba..79e2db5a 100644 --- a/script/test/fixtures/configmap-file-configs/output-k8s-3.yaml +++ b/script/test/fixtures/configmap-file-configs/output-k8s-3.yaml @@ -45,3 +45,4 @@ spec: - containerPort: 8025 protocol: TCP restartPolicy: Always + diff --git a/script/test/fixtures/configmap-file-configs/output-os-3.yaml b/script/test/fixtures/configmap-file-configs/output-os-3.yaml index ace73f23..4e116d28 100644 --- a/script/test/fixtures/configmap-file-configs/output-os-3.yaml +++ b/script/test/fixtures/configmap-file-configs/output-os-3.yaml @@ -73,3 +73,4 @@ spec: name: latest referencePolicy: type: "" + diff --git a/script/test/fixtures/label/compose.yaml b/script/test/fixtures/label/compose.yaml new file mode 100644 index 00000000..3f52a090 --- /dev/null +++ b/script/test/fixtures/label/compose.yaml @@ -0,0 +1,7 @@ +services: + app: + image: node:18-alpine + ports: + - 3000:3000 + labels: + - "com.example.label=foo" diff --git a/script/test/fixtures/label/output-k8s.yaml b/script/test/fixtures/label/output-k8s.yaml new file mode 100644 index 00000000..b82ca6b7 --- /dev/null +++ b/script/test/fixtures/label/output-k8s.yaml @@ -0,0 +1,47 @@ +--- +apiVersion: v1 +kind: Service +metadata: + annotations: + com.example.label: foo + labels: + io.kompose.service: app + name: app +spec: + ports: + - name: "3000" + port: 3000 + targetPort: 3000 + selector: + io.kompose.service: app + +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + annotations: + com.example.label: foo + labels: + io.kompose.service: app + name: app +spec: + replicas: 1 + selector: + matchLabels: + io.kompose.service: app + template: + metadata: + annotations: + com.example.label: foo + labels: + io.kompose.network/label-default: "true" + io.kompose.service: app + spec: + containers: + - image: node:18-alpine + name: app + ports: + - containerPort: 3000 + protocol: TCP + restartPolicy: Always +