Skip to content
Open
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
2 changes: 1 addition & 1 deletion docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Currently, it is not possible to use a different Kubernetes version from the ver

[Kompose CLI tests](https://github.com/kubernetes/kompose/tree/main/script/test/cmd) run `kompose convert` with compose files, and cross-check the k8s and OpenShift artifacts generated with the template files.

To generate CLI tests, please run `make gen-cmd`.


### CI

Expand Down
8 changes: 7 additions & 1 deletion pkg/transformer/kubernetes/podspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,13 @@ func SecurityContext(name string, service kobject.ServiceConfig) PodSpecOption {

// update template only if securityContext is not empty
if *securityContext != (api.SecurityContext{}) {
podSpec.Containers[0].SecurityContext = securityContext
// select the correct container to update by name
for i := range podSpec.Containers {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you able to instead add a comment above saying something like...

"Search through all the containers, find the one that matches the container name and set the security context appropriately"?

And remove the commented out code.

This implementation LGTM (sucks we have to loop through all the containers... but that's a nitpick), but otherwise this is good!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed as requested.

if podSpec.Containers[i].Name == GetContainerName(service) {
podSpec.Containers[i].SecurityContext = securityContext
}
}

}
if !reflect.DeepEqual(*podSecurityContext, api.PodSecurityContext{}) {
podSpec.SecurityContext = podSecurityContext
Expand Down
12 changes: 11 additions & 1 deletion script/test/cmd/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ convert::check_artifacts_generated "kompose -f $KOMPOSE_ROOT/script/test/fixture
convert::check_artifacts_generated "kompose -f $KOMPOSE_ROOT/script/test/fixtures/pvc-request-size/compose.yaml convert -o $TEMP_DIR/output_dir2/output-k8s.json -j --pvc-request-size=300Mi" "$TEMP_DIR/output_dir2/output-k8s.json"
convert::check_artifacts_generated "kompose --provider=openshift -f $KOMPOSE_ROOT/script/test/fixtures/pvc-request-size/compose.yaml convert -o $TEMP_DIR/output_dir2/output-os.json -j --pvc-request-size=300Mi" "$TEMP_DIR/output_dir2/output-os.json"





######
# Test the path of build image
# Test build v2 absolute compose file
Expand Down Expand Up @@ -417,4 +421,10 @@ convert::expect_success "$k8s_cmd" "$k8s_output" || exit 1
# Test deploy.labels in compose.yaml appears in the output
k8s_cmd="kompose -f $KOMPOSE_ROOT/script/test/fixtures/deploy/labels/compose.yaml convert --stdout --with-kompose-annotation=false"
k8s_output="$KOMPOSE_ROOT/script/test/fixtures/deploy/labels/output-k8s.yaml"
convert::expect_success "$k8s_cmd" "$k8s_output" || exit 1
convert::expect_success "$k8s_cmd" "$k8s_output" || exit 1


# TEST the security context conversion in service groups
k8s_cmd="kompose -f $KOMPOSE_ROOT/script/test/fixtures/security-contexts/compose.yaml convert --stdout --with-kompose-annotation=false -service-group-mode label"
k8s_output="$KOMPOSE_ROOT/script/test/fixtures/security-contexts/output-k8s.yaml"
convert::expect_success "$k8s_cmd" "$k8s_output" || exit 1
18 changes: 18 additions & 0 deletions script/test/fixtures/security-contexts/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
services:
dind:
container_name: dind
image: docker:28.1.1-dind
labels:
kompose.service.group: cup
ports:
- 2375:2375
privileged: True
cup:
container_name: cup
depends_on:
- dind
image: ghcr.io/sergi0g/cup:v3.4.0
labels:
kompose.service.group: cup
ports:
- 8000:8000
61 changes: 61 additions & 0 deletions script/test/fixtures/security-contexts/output-k8s.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
apiVersion: v1
kind: Service
metadata:
labels:
io.kompose.service: cup
name: cup
spec:
ports:
- name: "8000"
port: 8000
targetPort: 8000
selector:
io.kompose.service: cup

---
apiVersion: v1
kind: Service
metadata:
labels:
io.kompose.service: cup
name: dind
spec:
ports:
- name: "2375"
port: 2375
targetPort: 2375
selector:
io.kompose.service: cup

---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
io.kompose.service: cup
name: cup
spec:
replicas: 1
selector:
matchLabels:
io.kompose.service: cup
template:
metadata:
labels:
io.kompose.service: cup
spec:
containers:
- image: ghcr.io/sergi0g/cup:v3.4.0
name: cup
ports:
- containerPort: 8000
protocol: TCP
securityContext:
privileged: true
- image: docker:28.1.1-dind
name: dind
ports:
- containerPort: 2375
protocol: TCP
restartPolicy: Always---