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
13 changed files with 209 additions and 77 deletions
+22
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
}
+5 -11
View File
@@ -798,18 +798,12 @@ 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")
}
if tmpConfig.UID != "" {
keysFound = append(keysFound, "long syntax config uid")
}
if tmpConfig.GID != "" {
keysFound = append(keysFound, "long syntax config gid")
}
if tmpConfig.UID != "" {
keysFound = append(keysFound, "long syntax config uid")
}
}
+41 -28
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
for _, value := range service.Configs {
if value.Target == "/" {
log.Warnf("Long syntax config, target path can not be /")
continue
}
tmpKey := FormatFileName(value.Source)
volumeMounts = append(volumeMounts,
api.VolumeMount{
Name: tmpKey,
MountPath: "/" + FormatFileName(value.Target),
})
log.Debugf("fuck config: %+v", service.Configs)
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)
for _, value := range service.Configs {
cmVolName := FormatFileName(value.Source)
target := value.Target
if target == "" {
// short syntax, = /<source>
target = "/" + value.Source
}
} else {
//This is for SHORT SYNTAX, unsupported
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
}
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: cmVolName,
MountPath: target,
SubPath: subPath,
})
volumes = append(volumes, cmVol)
}
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 {