Merge top level non-service keys (#1187)

* merge top level non-service keys

docker-compose default behaviour is to merge these top level keys such
as `networks`
This commit is contained in:
Chander G 2019-11-28 11:48:49 +05:30 committed by Hang Yan
parent d03a6a9474
commit 5857252c3d
10 changed files with 425 additions and 0 deletions

View File

@ -635,6 +635,26 @@ func mergeComposeObject(oldCompose *types.Config, newCompose *types.Config) (*ty
oldCompose.Services[index] = tmpOldService
}
// Merge the networks information
for idx, network := range newCompose.Networks {
oldCompose.Networks[idx] = network
}
// Merge the volumes information
for idx, volume := range newCompose.Volumes {
oldCompose.Volumes[idx] = volume
}
// Merge the secrets information
for idx, secret := range newCompose.Secrets {
oldCompose.Secrets[idx] = secret
}
// Merge the configs information
for idx, config := range newCompose.Configs {
oldCompose.Configs[idx] = config
}
return oldCompose, nil
}

View File

@ -140,6 +140,17 @@ cmd="kompose --provider=openshift convert -f $KOMPOSE_ROOT/script/test/fixtures/
sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" $KOMPOSE_ROOT/script/test/fixtures/merge-multiple-compose/output-openshift-template.json > /tmp/output-os.json
convert::expect_success "$cmd" "/tmp/output-os.json"
# Test other top level keys
# In merge
cmd="kompose convert -f $KOMPOSE_ROOT/script/test/fixtures/merge-multiple-compose/other-toplevel-dev.yml -f $KOMPOSE_ROOT/script/test/fixtures/merge-multiple-compose/other-toplevel-ext.yml --stdout -j"
sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" $KOMPOSE_ROOT/script/test/fixtures/merge-multiple-compose/output-other-toplevel-merge-template.json > /tmp/output-k8s.json
convert::expect_success "$cmd" "/tmp/output-k8s.json"
# In Override
cmd="kompose convert -f $KOMPOSE_ROOT/script/test/fixtures/merge-multiple-compose/other-toplevel-base.yml -f $KOMPOSE_ROOT/script/test/fixtures/merge-multiple-compose/other-toplevel-override.yml --stdout -j"
sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" $KOMPOSE_ROOT/script/test/fixtures/merge-multiple-compose/output-other-toplevel-override-template.json > /tmp/output-k8s.json
convert::expect_success "$cmd" "/tmp/output-k8s.json"
######
# Tests related to docker-compose file in /script/test/fixtures/ports-with-proto
# kubernetes test

View File

@ -0,0 +1 @@
First config

View File

@ -0,0 +1,15 @@
version: "3.3"
services:
other-toplevel-base:
image: nginx
volumes:
- firstvolume:/var/www/nginx
configs:
- source: firstconfig
target: /etc/nginx.conf
mode: 644
configs:
firstconfig:
file: ./first_config.txt

View File

@ -0,0 +1,20 @@
version: "3.3"
services:
other-toplevel-dev:
image: nginx
volumes:
- firstvolume:/var/www/nginx
configs:
- source: firstconfig
target: /etc/nginx.conf
mode: 644
volumes:
firstvolume:
driver_opts:
type: "nfs"
configs:
firstconfig:
file: ./first_config.txt

View File

@ -0,0 +1,19 @@
version: "3.3"
services:
other-toplevel-second:
image: nginx
volumes:
- secondvolume:/var/www/nginx
configs:
- source: secondconfig
target: /etc/nginx.conf
mode: 644
volumes:
secondvolume:
external: true
configs:
secondconfig:
file: ./second_config.txt

View File

@ -0,0 +1,9 @@
version: "3.3"
volumes:
firstvolume:
external: true
configs:
firstconfig:
file: ./second_config.txt

View File

@ -0,0 +1,217 @@
{
"kind": "List",
"apiVersion": "v1",
"metadata": {},
"items": [
{
"kind": "ConfigMap",
"apiVersion": "v1",
"metadata": {
"name": "firstconfig",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "other-toplevel-dev"
}
},
"data": {
"first-config.txt": "First config\n"
}
},
{
"kind": "Deployment",
"apiVersion": "extensions/v1beta1",
"metadata": {
"name": "other-toplevel-dev",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "other-toplevel-dev"
},
"annotations": {
"kompose.cmd": "%CMD%",
"kompose.version": "%VERSION%"
}
},
"spec": {
"replicas": 1,
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"io.kompose.service": "other-toplevel-dev"
},
"annotations": {
"kompose.cmd": "%CMD%",
"kompose.version": "%VERSION%"
}
},
"spec": {
"volumes": [
{
"name": "firstconfig",
"configMap": {
"name": "firstconfig",
"defaultMode": 644
}
},
{
"name": "firstvolume",
"persistentVolumeClaim": {
"claimName": "firstvolume"
}
}
],
"containers": [
{
"name": "other-toplevel-dev",
"image": "nginx",
"resources": {},
"volumeMounts": [
{
"name": "firstconfig",
"mountPath": "/nginx.conf"
},
{
"name": "firstvolume",
"mountPath": "/var/www/nginx"
}
]
}
],
"restartPolicy": "Always"
}
},
"strategy": {
"type": "Recreate"
}
},
"status": {}
},
{
"kind": "PersistentVolumeClaim",
"apiVersion": "v1",
"metadata": {
"name": "firstvolume",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "firstvolume"
}
},
"spec": {
"accessModes": [
"ReadWriteOnce"
],
"resources": {
"requests": {
"storage": "100Mi"
}
}
},
"status": {}
},
{
"kind": "ConfigMap",
"apiVersion": "v1",
"metadata": {
"name": "secondconfig",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "other-toplevel-second"
}
},
"data": {
"second-config.txt": "Second config\n"
}
},
{
"kind": "Deployment",
"apiVersion": "extensions/v1beta1",
"metadata": {
"name": "other-toplevel-second",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "other-toplevel-second"
},
"annotations": {
"kompose.cmd": "%CMD%",
"kompose.version": "%VERSION%"
}
},
"spec": {
"replicas": 1,
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"io.kompose.service": "other-toplevel-second"
},
"annotations": {
"kompose.cmd": "%CMD%",
"kompose.version": "%VERSION%"
}
},
"spec": {
"volumes": [
{
"name": "secondconfig",
"configMap": {
"name": "secondconfig",
"defaultMode": 644
}
},
{
"name": "secondvolume",
"persistentVolumeClaim": {
"claimName": "secondvolume"
}
}
],
"containers": [
{
"name": "other-toplevel-second",
"image": "nginx",
"resources": {},
"volumeMounts": [
{
"name": "secondconfig",
"mountPath": "/nginx.conf"
},
{
"name": "secondvolume",
"mountPath": "/var/www/nginx"
}
]
}
],
"restartPolicy": "Always"
}
},
"strategy": {
"type": "Recreate"
}
},
"status": {}
},
{
"kind": "PersistentVolumeClaim",
"apiVersion": "v1",
"metadata": {
"name": "secondvolume",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "secondvolume"
}
},
"spec": {
"accessModes": [
"ReadWriteOnce"
],
"resources": {
"requests": {
"storage": "100Mi"
}
}
},
"status": {}
}
]
}

View File

@ -0,0 +1,112 @@
{
"kind": "List",
"apiVersion": "v1",
"metadata": {},
"items": [
{
"kind": "ConfigMap",
"apiVersion": "v1",
"metadata": {
"name": "firstconfig",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "other-toplevel-base"
}
},
"data": {
"second-config.txt": "Second config\n"
}
},
{
"kind": "Deployment",
"apiVersion": "extensions/v1beta1",
"metadata": {
"name": "other-toplevel-base",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "other-toplevel-base"
},
"annotations": {
"kompose.cmd": "%CMD%",
"kompose.version": "%VERSION%"
}
},
"spec": {
"replicas": 1,
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"io.kompose.service": "other-toplevel-base"
},
"annotations": {
"kompose.cmd": "%CMD%",
"kompose.version": "%VERSION%"
}
},
"spec": {
"volumes": [
{
"name": "firstconfig",
"configMap": {
"name": "firstconfig",
"defaultMode": 644
}
},
{
"name": "firstvolume",
"persistentVolumeClaim": {
"claimName": "firstvolume"
}
}
],
"containers": [
{
"name": "other-toplevel-base",
"image": "nginx",
"resources": {},
"volumeMounts": [
{
"name": "firstconfig",
"mountPath": "/nginx.conf"
},
{
"name": "firstvolume",
"mountPath": "/var/www/nginx"
}
]
}
],
"restartPolicy": "Always"
}
},
"strategy": {
"type": "Recreate"
}
},
"status": {}
},
{
"kind": "PersistentVolumeClaim",
"apiVersion": "v1",
"metadata": {
"name": "firstvolume",
"creationTimestamp": null,
"labels": {
"io.kompose.service": "firstvolume"
}
},
"spec": {
"accessModes": [
"ReadWriteOnce"
],
"resources": {
"requests": {
"storage": "100Mi"
}
}
},
"status": {}
}
]
}

View File

@ -0,0 +1 @@
Second config