Skip to content

Commit

Permalink
bug: fix annotation bug
Browse files Browse the repository at this point in the history
#### What type of PR is this?

<!--
Add one of the following kinds:
/kind bug
/kind cleanup
/kind documentation
/kind feature
-->

#### 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:
<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->

Fixes #1885

#### Special notes for your reviewer:

Signed-off-by: Charlie Drage <[email protected]>
  • Loading branch information
cdrage committed May 30, 2024
1 parent 50ec43d commit b3a24f5
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 14 deletions.
27 changes: 13 additions & 14 deletions pkg/transformer/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,25 +249,24 @@ 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
}

annotations["kompose.cmd"] = strings.Join(os.Args, " ")
versionCmd := exec.Command("kompose", "version")
out, err := versionCmd.Output()
if err != nil {
errors.Wrap(err, "Failed to get kompose version")
}
annotations["kompose.version"] = strings.Trim(string(out), " \n")
// Only add these to annotations if service.WithKomposeAnnotation is true
if service.WithKomposeAnnotation {
annotations["kompose.cmd"] = strings.Join(os.Args, " ")
versionCmd := exec.Command("kompose", "version")
out, err := versionCmd.Output()
if err != nil {
errors.Wrap(err, "Failed to get kompose version")
}
annotations["kompose.version"] = strings.Trim(string(out), " \n")

// If the version is blank (couldn't retrieve the kompose version for whatever reason)
if annotations["kompose.version"] == "" {
annotations["kompose.version"] = version.VERSION + " (" + version.GITCOMMIT + ")"
// If the version is blank (couldn't retrieve the kompose version for whatever reason)
if annotations["kompose.version"] == "" {
annotations["kompose.version"] = version.VERSION + " (" + version.GITCOMMIT + ")"
}
}

return annotations
Expand Down
5 changes: 5 additions & 0 deletions script/test/cmd/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions script/test/fixtures/label/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
app:
image: node:18-alpine
ports:
- 3000:3000
labels:
- "com.example.label=foo"
47 changes: 47 additions & 0 deletions script/test/fixtures/label/output-k8s.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b3a24f5

Please sign in to comment.