diff --git a/pkg/transformer/kubernetes/k8sutils.go b/pkg/transformer/kubernetes/k8sutils.go index 03967e49..6241dea0 100644 --- a/pkg/transformer/kubernetes/k8sutils.go +++ b/pkg/transformer/kubernetes/k8sutils.go @@ -853,8 +853,15 @@ func GetContentFromFile(file string) (string, error) { // FormatEnvName format env name func FormatEnvName(name string) string { envName := strings.Trim(name, "./") + // only take string after the last slash only if the string contains a slash + if strings.Contains(envName, "/") { + envName = envName[strings.LastIndex(envName, "/")+1:] + } + // take only last 63 chars + if len(envName) > 63 { + envName = envName[len(envName)-63:] + } envName = strings.Replace(envName, ".", "-", -1) - envName = strings.Replace(envName, "/", "-", -1) return envName } diff --git a/script/test/cmd/tests_new.sh b/script/test/cmd/tests_new.sh index 054cdb6b..25107cf9 100755 --- a/script/test/cmd/tests_new.sh +++ b/script/test/cmd/tests_new.sh @@ -296,3 +296,12 @@ os_cmd="kompose -f $KOMPOSE_ROOT/script/test/fixtures/env/docker-compose.yml con os_output="$KOMPOSE_ROOT/script/test/fixtures/env/output-os.yaml" convert::expect_success "$k8s_cmd" "$k8s_output" || exit 1 convert::expect_success "$os_cmd" "$os_output" || exit 1 + +# disabled until FormatEnvName can take into account conflicting/duplicate file names +# Test env_file support for multiple env_file with the same name from different dirs +# k8s_cmd="kompose -f $KOMPOSE_ROOT/script/test/fixtures/env-multiple/docker-compose.yml convert --stdout --with-kompose-annotation=false" +# k8s_output="$KOMPOSE_ROOT/script/test/fixtures/env-multiple/output-k8s.yaml" +# os_cmd="kompose -f $KOMPOSE_ROOT/script/test/fixtures/env-multiple/docker-compose.yml convert --provider openshift --stdout --with-kompose-annotation=false" +# os_output="$KOMPOSE_ROOT/script/test/fixtures/env-multiple/output-os.yaml" +# convert::expect_success "$k8s_cmd" "$k8s_output" || exit 1 +# convert::expect_success "$os_cmd" "$os_output" || exit 1 diff --git a/script/test/fixtures/env-multiple/docker-compose.yml b/script/test/fixtures/env-multiple/docker-compose.yml new file mode 100644 index 00000000..62d808af --- /dev/null +++ b/script/test/fixtures/env-multiple/docker-compose.yml @@ -0,0 +1,18 @@ +version: '3' +services: + namenode: + image: bde2020/hadoop-namenode:2.0.0-hadoop2.7.4-java8 + environment: + - CLUSTER_NAME=test + env_file: + - env1/hadoop-hive-namenode.env + ports: + - "50070:50070" + - "8020:8020" + another-namenode: + image: bde2020/hadoop-namenode:2.0.0-hadoop2.7.4-java8 + ports: + - "50070:50070" + - "8020:8020" + env_file: + - env2/hadoop-hive-namenode.env diff --git a/script/test/fixtures/env-multiple/env1/hadoop-hive-namenode.env b/script/test/fixtures/env-multiple/env1/hadoop-hive-namenode.env new file mode 100644 index 00000000..6ac867af --- /dev/null +++ b/script/test/fixtures/env-multiple/env1/hadoop-hive-namenode.env @@ -0,0 +1 @@ +FOO=BAR diff --git a/script/test/fixtures/env-multiple/env2/hadoop-hive-namenode.env b/script/test/fixtures/env-multiple/env2/hadoop-hive-namenode.env new file mode 100644 index 00000000..9279e277 --- /dev/null +++ b/script/test/fixtures/env-multiple/env2/hadoop-hive-namenode.env @@ -0,0 +1 @@ +BAR=FOO diff --git a/script/test/fixtures/env-multiple/output-k8s.yaml b/script/test/fixtures/env-multiple/output-k8s.yaml new file mode 100644 index 00000000..d61ada19 --- /dev/null +++ b/script/test/fixtures/env-multiple/output-k8s.yaml @@ -0,0 +1,148 @@ +--- +apiVersion: v1 +kind: Service +metadata: + creationTimestamp: null + labels: + io.kompose.service: another-namenode + name: another-namenode +spec: + ports: + - name: "50070" + port: 50070 + targetPort: 50070 + - name: "8020" + port: 8020 + targetPort: 8020 + selector: + io.kompose.service: another-namenode +status: + loadBalancer: {} + +--- +apiVersion: v1 +kind: Service +metadata: + creationTimestamp: null + labels: + io.kompose.service: namenode + name: namenode +spec: + ports: + - name: "50070" + port: 50070 + targetPort: 50070 + - name: "8020" + port: 8020 + targetPort: 8020 + selector: + io.kompose.service: namenode +status: + loadBalancer: {} + +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + creationTimestamp: null + labels: + io.kompose.service: another-namenode + name: another-namenode +spec: + replicas: 1 + selector: + matchLabels: + io.kompose.service: another-namenode + strategy: {} + template: + metadata: + creationTimestamp: null + labels: + io.kompose.network/env-default: "true" + io.kompose.service: another-namenode + spec: + containers: + - env: + - name: BAR + valueFrom: + configMapKeyRef: + key: BAR + name: hadoop-hive-namenode-env + image: bde2020/hadoop-namenode:2.0.0-hadoop2.7.4-java8 + name: another-namenode + ports: + - containerPort: 50070 + hostPort: 50070 + protocol: TCP + - containerPort: 8020 + hostPort: 8020 + protocol: TCP + resources: {} + restartPolicy: Always +status: {} + +--- +apiVersion: v1 +data: + FOO: BAR +kind: ConfigMap +metadata: + creationTimestamp: null + labels: + io.kompose.service: hadoop-hive-namenode-env + name: hadoop-hive-namenode-env + +--- +apiVersion: v1 +data: + BAR: FOO +kind: ConfigMap +metadata: + creationTimestamp: null + labels: + io.kompose.service: another-namenode-hadoop-hive-namenode-env + name: hadoop-hive-namenode-env1 + +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + creationTimestamp: null + labels: + io.kompose.service: namenode + name: namenode +spec: + replicas: 1 + selector: + matchLabels: + io.kompose.service: namenode + strategy: {} + template: + metadata: + creationTimestamp: null + labels: + io.kompose.network/env-default: "true" + io.kompose.service: namenode + spec: + containers: + - env: + - name: CLUSTER_NAME + value: test + - name: FOO + valueFrom: + configMapKeyRef: + key: FOO + name: hadoop-hive-namenode-env + image: bde2020/hadoop-namenode:2.0.0-hadoop2.7.4-java8 + name: namenode + ports: + - containerPort: 50070 + hostPort: 50070 + protocol: TCP + - containerPort: 8020 + hostPort: 8020 + protocol: TCP + resources: {} + restartPolicy: Always +status: {} + diff --git a/script/test/fixtures/env-multiple/output-os.yaml b/script/test/fixtures/env-multiple/output-os.yaml new file mode 100644 index 00000000..57c42676 --- /dev/null +++ b/script/test/fixtures/env-multiple/output-os.yaml @@ -0,0 +1,230 @@ +--- +apiVersion: v1 +kind: Service +metadata: + creationTimestamp: null + labels: + io.kompose.service: another-namenode + name: another-namenode +spec: + ports: + - name: "50070" + port: 50070 + targetPort: 50070 + - name: "8020" + port: 8020 + targetPort: 8020 + selector: + io.kompose.service: another-namenode +status: + loadBalancer: {} + +--- +apiVersion: v1 +kind: Service +metadata: + creationTimestamp: null + labels: + io.kompose.service: namenode + name: namenode +spec: + ports: + - name: "50070" + port: 50070 + targetPort: 50070 + - name: "8020" + port: 8020 + targetPort: 8020 + selector: + io.kompose.service: namenode +status: + loadBalancer: {} + +--- +apiVersion: v1 +data: + FOO: BAR +kind: ConfigMap +metadata: + creationTimestamp: null + labels: + io.kompose.service: hadoop-hive-namenode-env + name: hadoop-hive-namenode-env + +--- +apiVersion: v1 +data: + BAR: FOO +kind: ConfigMap +metadata: + creationTimestamp: null + labels: + io.kompose.service: another-namenode-hadoop-hive-namenode-env + name: hadoop-hive-namenode-env1 + +--- +apiVersion: apps.openshift.io/v1 +kind: DeploymentConfig +metadata: + creationTimestamp: null + labels: + io.kompose.service: another-namenode + name: another-namenode +spec: + replicas: 1 + selector: + io.kompose.service: another-namenode + strategy: + resources: {} + template: + metadata: + creationTimestamp: null + labels: + io.kompose.network/env-default: "true" + io.kompose.service: another-namenode + spec: + containers: + - env: + - name: BAR + valueFrom: + configMapKeyRef: + key: BAR + name: hadoop-hive-namenode-env1 + image: ' ' + name: another-namenode + ports: + - containerPort: 50070 + hostPort: 50070 + protocol: TCP + - containerPort: 8020 + hostPort: 8020 + protocol: TCP + resources: {} + restartPolicy: Always + test: false + triggers: + - type: ConfigChange + - imageChangeParams: + automatic: true + containerNames: + - another-namenode + from: + kind: ImageStreamTag + name: another-namenode:2.0.0-hadoop2.7.4-java8 + type: ImageChange +status: + availableReplicas: 0 + latestVersion: 0 + observedGeneration: 0 + replicas: 0 + unavailableReplicas: 0 + updatedReplicas: 0 + +--- +apiVersion: image.openshift.io/v1 +kind: ImageStream +metadata: + creationTimestamp: null + labels: + io.kompose.service: another-namenode + name: another-namenode +spec: + lookupPolicy: + local: false + tags: + - annotations: null + from: + kind: DockerImage + name: bde2020/hadoop-namenode:2.0.0-hadoop2.7.4-java8 + generation: null + importPolicy: {} + name: 2.0.0-hadoop2.7.4-java8 + referencePolicy: + type: "" +status: + dockerImageRepository: "" + +--- +apiVersion: apps.openshift.io/v1 +kind: DeploymentConfig +metadata: + creationTimestamp: null + labels: + io.kompose.service: namenode + name: namenode +spec: + replicas: 1 + selector: + io.kompose.service: namenode + strategy: + resources: {} + template: + metadata: + creationTimestamp: null + labels: + io.kompose.network/env-default: "true" + io.kompose.service: namenode + spec: + containers: + - env: + - name: CLUSTER_NAME + value: test + - name: FOO + valueFrom: + configMapKeyRef: + key: FOO + name: hadoop-hive-namenode-env + image: ' ' + name: namenode + ports: + - containerPort: 50070 + hostPort: 50070 + protocol: TCP + - containerPort: 8020 + hostPort: 8020 + protocol: TCP + resources: {} + restartPolicy: Always + test: false + triggers: + - type: ConfigChange + - imageChangeParams: + automatic: true + containerNames: + - namenode + from: + kind: ImageStreamTag + name: namenode:2.0.0-hadoop2.7.4-java8 + type: ImageChange +status: + availableReplicas: 0 + latestVersion: 0 + observedGeneration: 0 + replicas: 0 + unavailableReplicas: 0 + updatedReplicas: 0 + +--- +apiVersion: image.openshift.io/v1 +kind: ImageStream +metadata: + creationTimestamp: null + labels: + io.kompose.service: namenode + name: namenode +spec: + lookupPolicy: + local: false + tags: + - annotations: null + from: + kind: DockerImage + name: bde2020/hadoop-namenode:2.0.0-hadoop2.7.4-java8 + generation: null + importPolicy: {} + name: 2.0.0-hadoop2.7.4-java8 + referencePolicy: + type: "" +status: + dockerImageRepository: "" + diff --git a/script/test/fixtures/env/output-k8s.yaml b/script/test/fixtures/env/output-k8s.yaml index 61bf3efe..f53119da 100644 --- a/script/test/fixtures/env/output-k8s.yaml +++ b/script/test/fixtures/env/output-k8s.yaml @@ -67,12 +67,12 @@ spec: valueFrom: configMapKeyRef: key: BAR - name: home-runner-work-kompose-kompose-script-test-fixtures-env-hadoop-hive-namenode-env + name: hadoop-hive-namenode-env - name: FOO valueFrom: configMapKeyRef: key: FOO - name: home-runner-work-kompose-kompose-script-test-fixtures-env-hadoop-hive-namenode-env + name: hadoop-hive-namenode-env image: bde2020/hadoop-namenode:2.0.0-hadoop2.7.4-java8 name: another-namenode ports: @@ -95,8 +95,8 @@ kind: ConfigMap metadata: creationTimestamp: null labels: - io.kompose.service: another-namenode-home-runner-work-kompose-kompose-script-test-fixtures-env-hadoop-hive-namenode-env - name: home-runner-work-kompose-kompose-script-test-fixtures-env-hadoop-hive-namenode-env + io.kompose.service: another-namenode-hadoop-hive-namenode-env + name: hadoop-hive-namenode-env --- apiVersion: apps/v1 @@ -125,14 +125,14 @@ spec: valueFrom: configMapKeyRef: key: BAR - name: home-runner-work-kompose-kompose-script-test-fixtures-env-hadoop-hive-namenode-env + name: hadoop-hive-namenode-env - name: CLUSTER_NAME value: test - name: FOO valueFrom: configMapKeyRef: key: FOO - name: home-runner-work-kompose-kompose-script-test-fixtures-env-hadoop-hive-namenode-env + name: hadoop-hive-namenode-env image: bde2020/hadoop-namenode:2.0.0-hadoop2.7.4-java8 name: namenode ports: diff --git a/script/test/fixtures/env/output-os.yaml b/script/test/fixtures/env/output-os.yaml index f4283959..487b5710 100644 --- a/script/test/fixtures/env/output-os.yaml +++ b/script/test/fixtures/env/output-os.yaml @@ -49,8 +49,8 @@ kind: ConfigMap metadata: creationTimestamp: null labels: - io.kompose.service: another-namenode-home-runner-work-kompose-kompose-script-test-fixtures-env-hadoop-hive-namenode-env - name: home-runner-work-kompose-kompose-script-test-fixtures-env-hadoop-hive-namenode-env + io.kompose.service: another-namenode-hadoop-hive-namenode-env + name: hadoop-hive-namenode-env --- apiVersion: apps.openshift.io/v1 @@ -79,12 +79,12 @@ spec: valueFrom: configMapKeyRef: key: BAR - name: home-runner-work-kompose-kompose-script-test-fixtures-env-hadoop-hive-namenode-env + name: hadoop-hive-namenode-env - name: FOO valueFrom: configMapKeyRef: key: FOO - name: home-runner-work-kompose-kompose-script-test-fixtures-env-hadoop-hive-namenode-env + name: hadoop-hive-namenode-env image: ' ' name: another-namenode ports: @@ -166,14 +166,14 @@ spec: valueFrom: configMapKeyRef: key: BAR - name: home-runner-work-kompose-kompose-script-test-fixtures-env-hadoop-hive-namenode-env + name: hadoop-hive-namenode-env - name: CLUSTER_NAME value: test - name: FOO valueFrom: configMapKeyRef: key: FOO - name: home-runner-work-kompose-kompose-script-test-fixtures-env-hadoop-hive-namenode-env + name: hadoop-hive-namenode-env image: ' ' name: namenode ports: