Support config short syntax and fix old bugs (#1230)

This commit is contained in:
Hang Yan 2020-01-05 10:19:22 +08:00 committed by GitHub
parent fe4301192a
commit 05e9bf4fc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 209 additions and 77 deletions

View File

@ -23,7 +23,7 @@ __Glossary:__
| cap_add, cap_drop | ✓ | ✓ | ✓ | Pod.Spec.Container.SecurityContext.Capabilities.Add/Drop | |
| command | ✓ | ✓ | ✓ | Pod.Spec.Container.Command | |
| configs | n | n | ✓ | | |
| configs: short-syntax | n | n | n | | Only create configMap |
| configs: short-syntax | n | n | | | Only create configMap |
| configs: long-syntax | n | n | ✓ | | If target path is /, ignore this and only create configMap |
| cgroup_parent | x | x | x | | Not supported within Kubernetes. See issue https://github.com/kubernetes/kubernetes/issues/11986 |
| container_name | ✓ | ✓ | ✓ | Metadata.Name + Deployment.Spec.Containers.Name | |

View File

@ -19,7 +19,9 @@ package kobject
import (
dockerCliTypes "github.com/docker/cli/cli/compose/types"
"github.com/docker/libcompose/yaml"
"github.com/pkg/errors"
"k8s.io/kubernetes/pkg/api"
"path/filepath"
)
// KomposeObject holds the generic struct of Kompose transformation
@ -164,3 +166,23 @@ type Volumes struct {
PVCSize string // PVC size
SelectorValue string // Value of the label selector
}
// GetConfigMapKeyFromMeta...
// given a source name ,find the file and extract the filename which will be act as ConfigMap key
// return "" if not found
func (s *ServiceConfig) GetConfigMapKeyFromMeta(name string) (string, error) {
if s.ConfigsMetaData == nil {
return "", errors.Errorf("config %s not found", name)
}
if _, ok := s.ConfigsMetaData[name]; !ok {
return "", errors.Errorf("config %s not found", name)
}
config := s.ConfigsMetaData[name]
if config.External.External {
return "", errors.Errorf("config %s is external", name)
}
return filepath.Base(config.File), nil
}

View File

@ -798,12 +798,7 @@ func checkUnsupportedKeyForV3(composeObject *types.Config) []string {
var keysFound []string
for _, service := range composeObject.Services {
//For short syntax, volume mount path must be /, but this will cause pod create fail in kubernetes
//So we ignore this attribute
for _, tmpConfig := range service.Configs {
if tmpConfig.Mode == nil {
keysFound = append(keysFound, "short syntax config")
} else {
if tmpConfig.GID != "" {
keysFound = append(keysFound, "long syntax config gid")
}
@ -811,7 +806,6 @@ func checkUnsupportedKeyForV3(composeObject *types.Config) []string {
keysFound = append(keysFound, "long syntax config uid")
}
}
}
if service.CredentialSpec.Registry != "" || service.CredentialSpec.File != "" {
keysFound = append(keysFound, "credential_spec")

View File

@ -155,32 +155,48 @@ func (k *Kubernetes) InitPodSpecWithConfigMap(name string, image string, service
var volumeMounts []api.VolumeMount
var volumes []api.Volume
if len(service.Configs) > 0 && service.Configs[0].Mode != nil {
//This is for LONG SYNTAX
log.Debugf("fuck config: %+v", service.Configs)
for _, value := range service.Configs {
if value.Target == "/" {
log.Warnf("Long syntax config, target path can not be /")
cmVolName := FormatFileName(value.Source)
target := value.Target
if target == "" {
// short syntax, = /<source>
target = "/" + value.Source
}
subPath := filepath.Base(target)
volSource := api.ConfigMapVolumeSource{}
volSource.Name = cmVolName
key, err := service.GetConfigMapKeyFromMeta(value.Source)
if err != nil {
log.Warnf("cannot parse config %s , %s", value.Source, err.Error())
// mostly it's external
continue
}
tmpKey := FormatFileName(value.Source)
volSource.Items = []api.KeyToPath{{
Key: key,
Path: subPath,
}}
if value.Mode != nil {
tmpMode := int32(*value.Mode)
volSource.DefaultMode = &tmpMode
}
cmVol := api.Volume{
Name: cmVolName,
VolumeSource: api.VolumeSource{ConfigMap: &volSource},
}
volumeMounts = append(volumeMounts,
api.VolumeMount{
Name: tmpKey,
MountPath: "/" + FormatFileName(value.Target),
Name: cmVolName,
MountPath: target,
SubPath: subPath,
})
volumes = append(volumes, cmVol)
tmpVolume := api.Volume{
Name: tmpKey,
}
tmpVolume.ConfigMap = &api.ConfigMapVolumeSource{}
tmpVolume.ConfigMap.Name = tmpKey
var tmpMode int32
tmpMode = int32(*value.Mode)
tmpVolume.ConfigMap.DefaultMode = &tmpMode
volumes = append(volumes, tmpVolume)
}
} else {
//This is for SHORT SYNTAX, unsupported
}
pod := api.PodSpec{
@ -337,11 +353,8 @@ func (k *Kubernetes) InitConfigMapFromFile(name string, service kobject.ServiceC
log.Fatalf("Unable to retrieve file: %s", err)
}
originFileName := FormatFileName(fileName)
dataMap := make(map[string]string)
dataMap[originFileName] = content
dataMap[filepath.Base(fileName)] = content
configMapName := ""
for key, tmpConfig := range service.ConfigsMetaData {

View File

@ -882,7 +882,7 @@ convert::expect_success "$cmd" "/tmp/output-os.json"
cmd="kompose convert --stdout -j -f $KOMPOSE_ROOT/script/test/fixtures/compose-v3.3-test/compose-config-short.yaml"
sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" $KOMPOSE_ROOT/script/test/fixtures/compose-v3.3-test/output-k8s-config-short.json > /tmp/output-k8s.json
convert::expect_success_and_warning "$cmd" "/tmp/output-k8s.json"
convert::expect_success "$cmd" "/tmp/output-k8s.json"
cmd="kompose convert --stdout -j -f $KOMPOSE_ROOT/script/test/fixtures/compose-v3.3-test/compose-config-short-warning.yaml"
sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" $KOMPOSE_ROOT/script/test/fixtures/compose-v3.3-test/output-k8s-config-short-warning.json > /tmp/output-k8s.json
@ -917,7 +917,7 @@ convert::expect_success_and_warning "$cmd" "/tmp/output-os.json"
cmd="kompose --provider openshift convert --stdout -j -f $KOMPOSE_ROOT/script/test/fixtures/compose-v3.3-test/compose-config-short.yaml"
sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" $KOMPOSE_ROOT/script/test/fixtures/compose-v3.3-test/output-os-config-short.json > /tmp/output-os.json
convert::expect_success_and_warning "$cmd" "/tmp/output-os.json"
convert::expect_success "$cmd" "/tmp/output-os.json"
cmd="kompose --provider openshift convert --stdout -j -f $KOMPOSE_ROOT/script/test/fixtures/compose-v3.3-test/compose-endpoint-mode-1.yaml"
sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" $KOMPOSE_ROOT/script/test/fixtures/compose-v3.3-test/output-os-mode-1.json > /tmp/output-os.json

View File

@ -14,7 +14,7 @@
}
},
"data": {
"my-config.txt": "aaaa"
"my_config.txt": "aaaa"
}
},
{
@ -59,8 +59,9 @@
"resources": {},
"volumeMounts": [
{
"mountPath": "/redis-config",
"name": "my-config"
"mountPath": "/redis_config",
"name": "my-config",
"subPath": "redis_config"
}
]
}
@ -70,8 +71,14 @@
"volumes": [
{
"configMap": {
"name": "my-config",
"defaultMode": 288
"defaultMode": 288,
"items": [
{
"key": "my_config.txt",
"path": "redis_config"
}
],
"name": "my-config"
},
"name": "my-config"
}

View File

@ -14,7 +14,7 @@
}
},
"data": {
"my-config.txt": "aaaa"
"my_config.txt": "aaaa"
}
},
{
@ -59,8 +59,9 @@
"resources": {},
"volumeMounts": [
{
"mountPath": "/redis-config",
"name": "my-config"
"mountPath": "/redis_config",
"name": "my-config",
"subPath": "redis_config"
}
]
}
@ -70,8 +71,14 @@
"volumes": [
{
"configMap": {
"name": "my-config",
"defaultMode": 288
"defaultMode": 288,
"items": [
{
"key": "my_config.txt",
"path": "redis_config"
}
],
"name": "my-config"
},
"name": "my-config"
}

View File

@ -14,7 +14,7 @@
}
},
"data": {
"my-config.txt": "aaaa"
"my_config.txt": "aaaa"
}
},
{
@ -56,12 +56,32 @@
"image": "redis:latest",
"imagePullPolicy": "",
"name": "redis",
"resources": {}
"resources": {},
"volumeMounts": [
{
"mountPath": "/my_config",
"name": "my-config",
"subPath": "my_config"
}
]
}
],
"restartPolicy": "Always",
"serviceAccountName": "",
"volumes": null
"volumes": [
{
"configMap": {
"items": [
{
"key": "my_config.txt",
"path": "my_config"
}
],
"name": "my-config"
},
"name": "my-config"
}
]
}
}
},

View File

@ -14,7 +14,7 @@
}
},
"data": {
"my-config.txt": "aaaa"
"my_config.txt": "aaaa"
}
},
{
@ -56,12 +56,32 @@
"image": "redis:latest",
"imagePullPolicy": "",
"name": "redis",
"resources": {}
"resources": {},
"volumeMounts": [
{
"mountPath": "/my_config",
"name": "my-config",
"subPath": "my_config"
}
]
}
],
"restartPolicy": "Always",
"serviceAccountName": "",
"volumes": null
"volumes": [
{
"configMap": {
"items": [
{
"key": "my_config.txt",
"path": "my_config"
}
],
"name": "my-config"
},
"name": "my-config"
}
]
}
}
},

View File

@ -14,7 +14,7 @@
}
},
"data": {
"my-config.txt": "aaaa"
"my_config.txt": "aaaa"
}
},
{
@ -71,6 +71,12 @@
"name": "my-config",
"configMap": {
"name": "my-config",
"items": [
{
"key": "my_config.txt",
"path": "redis_config"
}
],
"defaultMode": 288
}
}
@ -83,7 +89,8 @@
"volumeMounts": [
{
"name": "my-config",
"mountPath": "/redis-config"
"mountPath": "/redis_config",
"subPath": "redis_config"
}
]
}

View File

@ -14,7 +14,7 @@
}
},
"data": {
"my-config.txt": "aaaa"
"my_config.txt": "aaaa"
}
},
{
@ -66,11 +66,32 @@
}
},
"spec": {
"volumes": [
{
"name": "my-config",
"configMap": {
"name": "my-config",
"items": [
{
"key": "my_config.txt",
"path": "my_config"
}
]
}
}
],
"containers": [
{
"name": "redis",
"image": " ",
"resources": {}
"resources": {},
"volumeMounts": [
{
"name": "my-config",
"mountPath": "/my_config",
"subPath": "my_config"
}
]
}
],
"restartPolicy": "Always"

View File

@ -14,7 +14,7 @@
}
},
"data": {
"first-config.txt": "First config\n"
"first_config.txt": "First config\n"
}
},
{
@ -61,8 +61,9 @@
"resources": {},
"volumeMounts": [
{
"mountPath": "/nginx.conf",
"name": "firstconfig"
"mountPath": "/etc/nginx.conf",
"name": "firstconfig",
"subPath": "nginx.conf"
},
{
"mountPath": "/var/www/nginx",
@ -76,8 +77,14 @@
"volumes": [
{
"configMap": {
"name": "firstconfig",
"defaultMode": 644
"defaultMode": 644,
"items": [
{
"key": "first_config.txt",
"path": "nginx.conf"
}
],
"name": "firstconfig"
},
"name": "firstconfig"
},
@ -126,7 +133,7 @@
}
},
"data": {
"second-config.txt": "Second config\n"
"second_config.txt": "Second config\n"
}
},
{
@ -173,8 +180,9 @@
"resources": {},
"volumeMounts": [
{
"mountPath": "/nginx.conf",
"name": "secondconfig"
"mountPath": "/etc/nginx.conf",
"name": "secondconfig",
"subPath": "nginx.conf"
},
{
"mountPath": "/var/www/nginx",
@ -188,8 +196,14 @@
"volumes": [
{
"configMap": {
"name": "secondconfig",
"defaultMode": 644
"defaultMode": 644,
"items": [
{
"key": "second_config.txt",
"path": "nginx.conf"
}
],
"name": "secondconfig"
},
"name": "secondconfig"
},

View File

@ -14,7 +14,7 @@
}
},
"data": {
"second-config.txt": "Second config\n"
"second_config.txt": "Second config\n"
}
},
{
@ -61,8 +61,9 @@
"resources": {},
"volumeMounts": [
{
"mountPath": "/nginx.conf",
"name": "firstconfig"
"mountPath": "/etc/nginx.conf",
"name": "firstconfig",
"subPath": "nginx.conf"
},
{
"mountPath": "/var/www/nginx",
@ -76,8 +77,14 @@
"volumes": [
{
"configMap": {
"name": "firstconfig",
"defaultMode": 644
"defaultMode": 644,
"items": [
{
"key": "second_config.txt",
"path": "nginx.conf"
}
],
"name": "firstconfig"
},
"name": "firstconfig"
},