Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
realgam3 committed Jan 6, 2024
1 parent 8974dcf commit ac533e3
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ services:
- "bash"
- "-c"
- "echo hello from cron"
restart: "always"
restart: "no"
21 changes: 13 additions & 8 deletions pkg/transformer/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1295,10 +1295,6 @@ func (k *Kubernetes) CreateWorkloadAndConfigMapObjects(name string, service kobj
objects = append(objects, k.InitSS(name, service, replica))
}

if opt.Controller == CronJobController {
objects = append(objects, k.InitCJ(name, service, service.CronJobSchedule, service.CronJobConcurrencyPolicy))
}

if len(service.EnvFile) > 0 {
for _, envFile := range service.EnvFile {
configMap := k.InitConfigMapForEnv(name, opt, envFile)
Expand Down Expand Up @@ -1628,11 +1624,20 @@ func (k *Kubernetes) Transform(komposeObject kobject.KomposeObject, opt kobject.
}
}

if service.Restart == "always" && isCronJobController {
log.Infof("cronjob restart policy will be converted from '%s' to 'on-failure'", service.Restart)
service.Restart = "on-failure"
}

// Generate pod and configmap objects
if (service.Restart == "no" || service.Restart == "on-failure") && !opt.IsPodController() && !isCronJobController {
log.Infof("Create kubernetes pod instead of pod controller due to restart policy: %s", service.Restart)
pod := k.InitPod(name, service)
objects = append(objects, pod)
if (service.Restart == "no" || service.Restart == "on-failure") && !opt.IsPodController() {
if !isCronJobController {
log.Infof("Create kubernetes pod instead of pod controller due to restart policy: %s", service.Restart)
pod := k.InitPod(name, service)
objects = append(objects, pod)
} else {
objects = append(objects, k.InitCJ(name, service, service.CronJobSchedule, service.CronJobConcurrencyPolicy))
}

if len(service.EnvFile) > 0 {
for _, envFile := range service.EnvFile {
Expand Down
27 changes: 25 additions & 2 deletions pkg/transformer/openshift/openshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package openshift

import (
"fmt"
"github.com/kubernetes/kompose/pkg/loader/compose"
"os"
"sort"

Expand All @@ -44,6 +45,10 @@ type OpenShift struct {
kubernetes.Kubernetes
}

const (
CronJobController = "cronjob"
)

// list of all unsupported keys for this transformer
// Keys are names of variables in kobject struct.
// this is map to make searching for keys easier
Expand Down Expand Up @@ -325,14 +330,32 @@ func (o *OpenShift) Transform(komposeObject kobject.KomposeObject, opt kobject.C
}
}

// check if controller type is set to cronjob
isCronJobController := false
if val, ok := service.Labels[compose.LabelControllerType]; ok {
if val == CronJobController {
isCronJobController = true
}
}

if service.Restart == "always" && isCronJobController {
log.Infof("cronjob restart policy will be converted from '%s' to 'on-failure'", service.Restart)
service.Restart = "on-failure"
}

// Generate pod and configmap objects
if service.Restart == "no" || service.Restart == "on-failure" {
// Error out if Controller Object is specified with restart: 'on-failure'
if opt.IsDeploymentConfigFlag {
return nil, errors.New("Controller object cannot be specified with restart: 'on-failure'")
}
pod := o.InitPod(name, service)
objects = append(objects, pod)

if !isCronJobController {
pod := o.InitPod(name, service)
objects = append(objects, pod)
} else {
objects = append(objects, o.InitCJ(name, service, service.CronJobSchedule, service.CronJobConcurrencyPolicy))
}

if len(service.EnvFile) > 0 {
for _, envFile := range service.EnvFile {
Expand Down
8 changes: 8 additions & 0 deletions script/test/cmd/tests_new.sh
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ ocp_output="$KOMPOSE_ROOT/script/test/fixtures/statefulset/output-os.yaml"
convert::expect_success "$k8s_cmd" "$k8s_output" || exit 1
convert::expect_success "$ocp_cmd" "$ocp_output" || exit 1

# test cronjob
k8s_cmd="kompose -f $KOMPOSE_ROOT/script/test/fixtures/cronjob/docker-compose.yaml convert --stdout --with-kompose-annotation=false"
ocp_cmd="kompose --provider=openshift -f $KOMPOSE_ROOT/script/test/fixtures/cronjob/docker-compose.yaml convert --stdout --with-kompose-annotation=false"
k8s_output="$KOMPOSE_ROOT/script/test/fixtures/cronjob/output-k8s.yaml"
ocp_output="$KOMPOSE_ROOT/script/test/fixtures/cronjob/output-os.yaml"
convert::expect_success "$k8s_cmd" "$k8s_output" || exit 1
convert::expect_success "$ocp_cmd" "$ocp_output" || exit 1

# test specifying volume type using service label
k8s_cmd="kompose -f $KOMPOSE_ROOT/script/test/fixtures/multiple-type-volumes/docker-compose.yaml convert --stdout --with-kompose-annotation=false"
os_cmd="kompose --provider=openshift -f $KOMPOSE_ROOT/script/test/fixtures/multiple-type-volumes/docker-compose.yaml convert --stdout --with-kompose-annotation=false"
Expand Down
6 changes: 0 additions & 6 deletions script/test/fixtures/configmap-pod/output-k8s.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
io.kompose.service: redis
name: redis
Expand All @@ -19,7 +18,6 @@ spec:
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
io.kompose.network/configmap-pod-default: "true"
io.kompose.service: redis
Expand Down Expand Up @@ -48,7 +46,6 @@ spec:
- containerPort: 6379
hostPort: 6379
protocol: TCP
resources: {}
restartPolicy: Never

---
Expand All @@ -57,7 +54,6 @@ data:
ALLOW_EMPTY_PASSWORD: "yes"
kind: ConfigMap
metadata:
creationTimestamp: null
labels:
io.kompose.service: redis-foo-env
name: foo-env
Expand All @@ -69,8 +65,6 @@ data:
FOO: BAR
kind: ConfigMap
metadata:
creationTimestamp: null
labels:
io.kompose.service: redis-bar-env
name: bar-env

6 changes: 0 additions & 6 deletions script/test/fixtures/configmap-pod/output-os.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
io.kompose.service: redis
name: redis
Expand All @@ -19,7 +18,6 @@ spec:
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
io.kompose.network/configmap-pod-default: "true"
io.kompose.service: redis
Expand Down Expand Up @@ -48,7 +46,6 @@ spec:
- containerPort: 6379
hostPort: 6379
protocol: TCP
resources: {}
restartPolicy: Never

---
Expand All @@ -57,7 +54,6 @@ data:
ALLOW_EMPTY_PASSWORD: "yes"
kind: ConfigMap
metadata:
creationTimestamp: null
labels:
io.kompose.service: redis-foo-env
name: foo-env
Expand All @@ -69,8 +65,6 @@ data:
FOO: BAR
kind: ConfigMap
metadata:
creationTimestamp: null
labels:
io.kompose.service: redis-bar-env
name: bar-env

14 changes: 14 additions & 0 deletions script/test/fixtures/cronjob/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: '3'

services:
challenge:
image: busybox:stable-glibc
labels:
kompose.controller.type: cronjob
kompose.cronjob.schedule: "*/1 * * * *"
kompose.cronjob.concurrency_policy: "Forbid"
command:
- "bash"
- "-c"
- "echo hello from cron"
restart: "no"
26 changes: 26 additions & 0 deletions script/test/fixtures/cronjob/output-k8s.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
apiVersion: batch/v1
kind: CronJob
metadata:
labels:
io.kompose.service: challenge
name: challenge
spec:
concurrencyPolicy: Forbid
jobTemplate:
spec:
template:
metadata:
labels:
io.kompose.network/cronjob-default: "true"
io.kompose.service: challenge
spec:
containers:
- args:
- bash
- -c
- echo hello from cron
image: busybox:stable-glibc
name: challenge
restartPolicy: Never
schedule: '*/1 * * * *'
26 changes: 26 additions & 0 deletions script/test/fixtures/cronjob/output-os.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
apiVersion: batch/v1
kind: CronJob
metadata:
labels:
io.kompose.service: challenge
name: challenge
spec:
concurrencyPolicy: Forbid
jobTemplate:
spec:
template:
metadata:
labels:
io.kompose.network/cronjob-default: "true"
io.kompose.service: challenge
spec:
containers:
- args:
- bash
- -c
- echo hello from cron
image: busybox:stable-glibc
name: challenge
restartPolicy: Never
schedule: '*/1 * * * *'

0 comments on commit ac533e3

Please sign in to comment.