forked from LaconicNetwork/kompose
* fix issue https://github.com/kubernetes/kompose/issues/1683 * add tests, die if inputfiles not specified * fix GetComposeFileDir function and use it * use fixed GetComposeFileDir instead of filepath.Dir * return error instead of fatal * add pod configmap creation to openshift
This commit is contained in:
parent
63f60e66fe
commit
92ca12ae5b
@ -18,6 +18,7 @@ package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
@ -224,6 +225,31 @@ func Convert(opt kobject.ConvertOptions) ([]runtime.Object, error) {
|
||||
|
||||
komposeObject.Namespace = opt.Namespace
|
||||
|
||||
// Get the directory of the compose file
|
||||
workDir, err := transformer.GetComposeFileDir(opt.InputFiles)
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to get compose file directory: %s", err)
|
||||
}
|
||||
|
||||
// convert env_file from absolute to relative path
|
||||
for _, service := range komposeObject.ServiceConfigs {
|
||||
if len(service.EnvFile) <= 0 {
|
||||
continue
|
||||
}
|
||||
for i, envFile := range service.EnvFile {
|
||||
if !filepath.IsAbs(envFile) {
|
||||
continue
|
||||
}
|
||||
|
||||
relPath, err := filepath.Rel(workDir, envFile)
|
||||
if err != nil {
|
||||
log.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
service.EnvFile[i] = filepath.ToSlash(relPath)
|
||||
}
|
||||
}
|
||||
|
||||
// Get a transformer that maps komposeObject to provider's primitives
|
||||
t := getTransformer(opt)
|
||||
|
||||
|
||||
@ -220,7 +220,11 @@ func (k *Kubernetes) InitSvc(name string, service kobject.ServiceConfig) *api.Se
|
||||
|
||||
// InitConfigMapForEnv initializes a ConfigMap object
|
||||
func (k *Kubernetes) InitConfigMapForEnv(name string, opt kobject.ConvertOptions, envFile string) *api.ConfigMap {
|
||||
envs, err := GetEnvsFromFile(envFile)
|
||||
workDir, err := transformer.GetComposeFileDir(opt.InputFiles)
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to get compose file directory: %s", err)
|
||||
}
|
||||
envs, err := GetEnvsFromFile(filepath.Join(workDir, envFile))
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to retrieve env file: %s", err)
|
||||
}
|
||||
@ -1103,7 +1107,11 @@ func ConfigEnvs(service kobject.ServiceConfig, opt kobject.ConvertOptions) ([]ap
|
||||
envName := FormatEnvName(file)
|
||||
|
||||
// Load environment variables from file
|
||||
envLoad, err := GetEnvsFromFile(file)
|
||||
workDir, err := transformer.GetComposeFileDir(opt.InputFiles)
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to get compose file directory: %s", err)
|
||||
}
|
||||
envLoad, err := GetEnvsFromFile(filepath.Join(workDir, file))
|
||||
if err != nil {
|
||||
return envs, errors.Wrap(err, "Unable to read env_file")
|
||||
}
|
||||
@ -1579,11 +1587,18 @@ func (k *Kubernetes) Transform(komposeObject kobject.KomposeObject, opt kobject.
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Generate pod only and nothing more
|
||||
// Generate pod and configmap objects
|
||||
if (service.Restart == "no" || service.Restart == "on-failure") && !opt.IsPodController() {
|
||||
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 len(service.EnvFile) > 0 {
|
||||
for _, envFile := range service.EnvFile {
|
||||
configMap := k.InitConfigMapForEnv(name, opt, envFile)
|
||||
objects = append(objects, configMap)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
objects = k.CreateWorkloadAndConfigMapObjects(name, service, opt)
|
||||
}
|
||||
|
||||
@ -325,7 +325,7 @@ func (o *OpenShift) Transform(komposeObject kobject.KomposeObject, opt kobject.C
|
||||
}
|
||||
}
|
||||
|
||||
// Generate pod only and nothing more
|
||||
// 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 {
|
||||
@ -333,6 +333,13 @@ func (o *OpenShift) Transform(komposeObject kobject.KomposeObject, opt kobject.C
|
||||
}
|
||||
pod := o.InitPod(name, service)
|
||||
objects = append(objects, pod)
|
||||
|
||||
if len(service.EnvFile) > 0 {
|
||||
for _, envFile := range service.EnvFile {
|
||||
configMap := o.InitConfigMapForEnv(name, opt, envFile)
|
||||
objects = append(objects, configMap)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
objects = o.CreateWorkloadAndConfigMapObjects(name, service, opt)
|
||||
|
||||
|
||||
@ -336,14 +336,15 @@ func (env EnvSort) Swap(i, j int) {
|
||||
|
||||
// GetComposeFileDir returns compose file directory
|
||||
func GetComposeFileDir(inputFiles []string) (string, error) {
|
||||
// Check if input files are specified
|
||||
if len(inputFiles) <= 0 {
|
||||
return "", errors.New("No input files specified")
|
||||
}
|
||||
|
||||
// Lets assume all the docker-compose files are in the same directory
|
||||
inputFile := inputFiles[0]
|
||||
if strings.Index(inputFile, "/") != 0 {
|
||||
workDir, err := os.Getwd()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
inputFile = filepath.Join(workDir, inputFile)
|
||||
inputFile, err := filepath.Abs(inputFiles[0])
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
log.Debugf("Compose file dir: %s", filepath.Dir(inputFile))
|
||||
return filepath.Dir(inputFile), nil
|
||||
|
||||
@ -142,6 +142,14 @@ os_output="$KOMPOSE_ROOT/script/test/fixtures/configmap-volume/output-os-withlab
|
||||
convert::expect_success_and_warning "$k8s_cmd" "$k8s_output" || exit 1
|
||||
convert::expect_success "$os_cmd" "$os_output" || exit 1
|
||||
|
||||
# test configmap pod generation
|
||||
k8s_cmd="kompose -f $KOMPOSE_ROOT/script/test/fixtures/configmap-pod/docker-compose.yml convert --stdout --with-kompose-annotation=false"
|
||||
k8s_output="$KOMPOSE_ROOT/script/test/fixtures/configmap-pod/output-k8s.yaml"
|
||||
os_output="$KOMPOSE_ROOT/script/test/fixtures/configmap-pod/output-os.yaml"
|
||||
os_cmd="kompose --provider=openshift -f $KOMPOSE_ROOT/script/test/fixtures/configmap-pod/docker-compose.yml convert --stdout --with-kompose-annotation=false"
|
||||
convert::expect_success "$k8s_cmd" "$k8s_output" || exit 1
|
||||
convert::expect_success "$os_cmd" "$os_output" || exit 1
|
||||
|
||||
# Test that emptyDir works
|
||||
k8s_cmd="kompose -f $KOMPOSE_ROOT/script/test/fixtures/change-in-volume/docker-compose.yml convert --with-kompose-annotation=false --stdout --volumes emptyDir"
|
||||
k8s_output="$KOMPOSE_ROOT/script/test/fixtures/change-in-volume/output-k8s-empty-vols-template.yaml"
|
||||
|
||||
3
script/test/fixtures/configmap-pod/bar.env
vendored
Normal file
3
script/test/fixtures/configmap-pod/bar.env
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Multi-line test
|
||||
FOO=BAR
|
||||
BAR=FOO
|
||||
16
script/test/fixtures/configmap-pod/docker-compose.yml
vendored
Normal file
16
script/test/fixtures/configmap-pod/docker-compose.yml
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
version: '3'
|
||||
|
||||
services:
|
||||
redis:
|
||||
image: 'bitnami/redis:latest'
|
||||
environment:
|
||||
- ALLOW_EMPTY_PASSWORD=no
|
||||
# Env file will override environment / warn!
|
||||
env_file:
|
||||
- "foo.env"
|
||||
- bar.env
|
||||
labels:
|
||||
kompose.service.type: nodeport
|
||||
ports:
|
||||
- '6379:6379'
|
||||
restart: "no"
|
||||
2
script/test/fixtures/configmap-pod/foo.env
vendored
Normal file
2
script/test/fixtures/configmap-pod/foo.env
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
# Test comment!
|
||||
ALLOW_EMPTY_PASSWORD=yes
|
||||
76
script/test/fixtures/configmap-pod/output-k8s.yaml
vendored
Normal file
76
script/test/fixtures/configmap-pod/output-k8s.yaml
vendored
Normal file
@ -0,0 +1,76 @@
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
creationTimestamp: null
|
||||
labels:
|
||||
io.kompose.service: redis
|
||||
name: redis
|
||||
spec:
|
||||
ports:
|
||||
- name: "6379"
|
||||
port: 6379
|
||||
targetPort: 6379
|
||||
selector:
|
||||
io.kompose.service: redis
|
||||
type: NodePort
|
||||
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
creationTimestamp: null
|
||||
labels:
|
||||
io.kompose.network/configmap-pod-default: "true"
|
||||
io.kompose.service: redis
|
||||
name: redis
|
||||
spec:
|
||||
containers:
|
||||
- env:
|
||||
- name: ALLOW_EMPTY_PASSWORD
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
key: ALLOW_EMPTY_PASSWORD
|
||||
name: foo-env
|
||||
- name: BAR
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
key: BAR
|
||||
name: bar-env
|
||||
- name: FOO
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
key: FOO
|
||||
name: bar-env
|
||||
image: bitnami/redis:latest
|
||||
name: redis
|
||||
ports:
|
||||
- containerPort: 6379
|
||||
hostPort: 6379
|
||||
protocol: TCP
|
||||
resources: {}
|
||||
restartPolicy: Never
|
||||
|
||||
---
|
||||
apiVersion: v1
|
||||
data:
|
||||
ALLOW_EMPTY_PASSWORD: "yes"
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
creationTimestamp: null
|
||||
labels:
|
||||
io.kompose.service: redis-foo-env
|
||||
name: foo-env
|
||||
|
||||
---
|
||||
apiVersion: v1
|
||||
data:
|
||||
BAR: FOO
|
||||
FOO: BAR
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
creationTimestamp: null
|
||||
labels:
|
||||
io.kompose.service: redis-bar-env
|
||||
name: bar-env
|
||||
|
||||
76
script/test/fixtures/configmap-pod/output-os.yaml
vendored
Normal file
76
script/test/fixtures/configmap-pod/output-os.yaml
vendored
Normal file
@ -0,0 +1,76 @@
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
creationTimestamp: null
|
||||
labels:
|
||||
io.kompose.service: redis
|
||||
name: redis
|
||||
spec:
|
||||
ports:
|
||||
- name: "6379"
|
||||
port: 6379
|
||||
targetPort: 6379
|
||||
selector:
|
||||
io.kompose.service: redis
|
||||
type: NodePort
|
||||
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
creationTimestamp: null
|
||||
labels:
|
||||
io.kompose.network/configmap-pod-default: "true"
|
||||
io.kompose.service: redis
|
||||
name: redis
|
||||
spec:
|
||||
containers:
|
||||
- env:
|
||||
- name: ALLOW_EMPTY_PASSWORD
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
key: ALLOW_EMPTY_PASSWORD
|
||||
name: foo-env
|
||||
- name: BAR
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
key: BAR
|
||||
name: bar-env
|
||||
- name: FOO
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
key: FOO
|
||||
name: bar-env
|
||||
image: bitnami/redis:latest
|
||||
name: redis
|
||||
ports:
|
||||
- containerPort: 6379
|
||||
hostPort: 6379
|
||||
protocol: TCP
|
||||
resources: {}
|
||||
restartPolicy: Never
|
||||
|
||||
---
|
||||
apiVersion: v1
|
||||
data:
|
||||
ALLOW_EMPTY_PASSWORD: "yes"
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
creationTimestamp: null
|
||||
labels:
|
||||
io.kompose.service: redis-foo-env
|
||||
name: foo-env
|
||||
|
||||
---
|
||||
apiVersion: v1
|
||||
data:
|
||||
BAR: FOO
|
||||
FOO: BAR
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
creationTimestamp: null
|
||||
labels:
|
||||
io.kompose.service: redis-bar-env
|
||||
name: bar-env
|
||||
|
||||
Loading…
Reference in New Issue
Block a user