Skip to content

Commit 62d33c4

Browse files
authored
Merge pull request #1952 from jvitor83/feature/config-content
Add support for content in config
2 parents 0d989a2 + a495420 commit 62d33c4

File tree

4 files changed

+153
-5
lines changed

4 files changed

+153
-5
lines changed

Diff for: docs/conversion.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ We're doing our best to keep it up to date as soon as possible in our releases t
3838
| cap_add |||| Container.SecurityContext.Capabilities.Add | |
3939
| cap_drop |||| Container.SecurityContext.Capabilities.Drop | |
4040
| command |||| Container.Args | |
41-
| configs | n | n || | |
41+
| configs | n | n || | |
4242
| configs: short-syntax | n | n || | Only create configMap |
4343
| configs: long-syntax | n | n || | If target path is /, ignore this and only create configMap |
4444
| cgroup_parent | x | x | x | | Not supported within Kubernetes. See issue https://github.com/kubernetes/kubernetes/issues/11986 |
@@ -110,3 +110,10 @@ We're doing our best to keep it up to date as soon as possible in our releases t
110110
| internal | x | x | x | | |
111111
| labels | x | x | x | | |
112112
| external | x | x | x | | |
113+
| | | | | | |
114+
| **Configs** | x | x | x | | |
115+
| environment | x | y | y | | |
116+
| file | y | y | y | | |
117+
| content | x | y | y | | |
118+
| labels | x | x | x | | |
119+
| external | x | x | x | | |

Diff for: pkg/kobject/kobject.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,24 @@ func (s *ServiceConfig) GetConfigMapKeyFromMeta(name string) (string, error) {
257257
return "", errors.Errorf("config %s is external", name)
258258
}
259259

260-
return filepath.Base(config.File), nil
260+
if config.File != "" {
261+
return filepath.Base(config.File), nil
262+
} else if config.Content != "" {
263+
// loop through s.Configs to find the config with the same name
264+
for _, cfg := range s.Configs {
265+
if cfg.Source == name {
266+
if cfg.Target == "" {
267+
return filepath.Base(cfg.Source), nil
268+
} else {
269+
return filepath.Base(cfg.Target), nil
270+
}
271+
}
272+
}
273+
} else {
274+
return "", errors.Errorf("config %s is empty", name)
275+
}
276+
277+
return "", errors.Errorf("config %s not found", name)
261278
}
262279

263280
// GetKubernetesUpdateStrategy from compose update_config

Diff for: pkg/transformer/kubernetes/k8sutils_test.go

+92
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"sort"
2525
"testing"
2626

27+
"github.com/compose-spec/compose-go/v2/types"
2728
"github.com/kubernetes/kompose/pkg/kobject"
2829
"github.com/kubernetes/kompose/pkg/loader/compose"
2930
"github.com/kubernetes/kompose/pkg/testutils"
@@ -328,6 +329,97 @@ func TestCreateServiceWithServiceUser(t *testing.T) {
328329
}
329330
}
330331

332+
func TestCreateServiceWithConfigLongSyntax(t *testing.T) {
333+
content := "setting: true"
334+
target := "/etc/config.yaml"
335+
336+
// An example service
337+
service := kobject.ServiceConfig{
338+
ContainerName: "name",
339+
Image: "image",
340+
Environment: []kobject.EnvVar{{Name: "env", Value: "value"}},
341+
Port: []kobject.Ports{{HostPort: 123, ContainerPort: 456, Protocol: string(corev1.ProtocolTCP)}},
342+
Command: []string{"cmd"},
343+
Configs: []types.ServiceConfigObjConfig{{Source: "configmap", Target: target}},
344+
ConfigsMetaData: map[string]types.ConfigObjConfig{"configmap": {Content: content}},
345+
}
346+
347+
komposeObject := kobject.KomposeObject{
348+
ServiceConfigs: map[string]kobject.ServiceConfig{
349+
"app": service,
350+
},
351+
}
352+
353+
k := Kubernetes{}
354+
355+
objects, err := k.Transform(komposeObject, kobject.ConvertOptions{CreateD: true, Replicas: 1})
356+
if err != nil {
357+
t.Error(errors.Wrap(err, "k.Transform failed"))
358+
}
359+
360+
for _, obj := range objects {
361+
t.Log(obj)
362+
if configMap, ok := obj.(*api.ConfigMap); ok {
363+
fileContent := configMap.Data["config.yaml"]
364+
if fileContent != content {
365+
t.Errorf("Config map content not equal")
366+
}
367+
}
368+
if deployment, ok := obj.(*appsv1.Deployment); ok {
369+
spec := deployment.Spec.Template.Spec
370+
if spec.Containers[0].VolumeMounts[0].MountPath != target {
371+
t.Errorf("Config map mountPath not found")
372+
}
373+
}
374+
}
375+
}
376+
377+
func TestCreateServiceWithConfigShortSyntax(t *testing.T) {
378+
content := "setting: true"
379+
source := "configmap"
380+
target := "/" + source
381+
382+
// An example service
383+
service := kobject.ServiceConfig{
384+
ContainerName: "name",
385+
Image: "image",
386+
Environment: []kobject.EnvVar{{Name: "env", Value: "value"}},
387+
Port: []kobject.Ports{{HostPort: 123, ContainerPort: 456, Protocol: string(corev1.ProtocolTCP)}},
388+
Command: []string{"cmd"},
389+
Configs: []types.ServiceConfigObjConfig{{Source: source}},
390+
ConfigsMetaData: map[string]types.ConfigObjConfig{source: {Content: content}},
391+
}
392+
393+
komposeObject := kobject.KomposeObject{
394+
ServiceConfigs: map[string]kobject.ServiceConfig{
395+
"app": service,
396+
},
397+
}
398+
399+
k := Kubernetes{}
400+
401+
objects, err := k.Transform(komposeObject, kobject.ConvertOptions{CreateD: true, Replicas: 1})
402+
if err != nil {
403+
t.Error(errors.Wrap(err, "k.Transform failed"))
404+
}
405+
406+
for _, obj := range objects {
407+
t.Log(obj)
408+
if configMap, ok := obj.(*api.ConfigMap); ok {
409+
fileContent := configMap.Data[source]
410+
if fileContent != content {
411+
t.Errorf("Config map content not equal")
412+
}
413+
}
414+
if deployment, ok := obj.(*appsv1.Deployment); ok {
415+
spec := deployment.Spec.Template.Spec
416+
if spec.Containers[0].VolumeMounts[0].MountPath != target {
417+
t.Errorf("Config map mountPath not found")
418+
}
419+
}
420+
}
421+
}
422+
331423
func TestTransformWithPid(t *testing.T) {
332424
// An example service
333425
service := kobject.ServiceConfig{

Diff for: pkg/transformer/kubernetes/kubernetes.go

+35-3
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,24 @@ func initConfigMapData(configMap *api.ConfigMap, data map[string]string) {
332332
configMap.BinaryData = binData
333333
}
334334

335+
// InitConfigMapFromContent initializes a ConfigMap object
336+
func (k *Kubernetes) InitConfigMapFromContent(name string, service kobject.ServiceConfig, content string, currentConfigName string, target string) *api.ConfigMap {
337+
configMap := &api.ConfigMap{
338+
TypeMeta: metav1.TypeMeta{
339+
Kind: "ConfigMap",
340+
APIVersion: "v1",
341+
},
342+
ObjectMeta: metav1.ObjectMeta{
343+
Name: currentConfigName,
344+
Labels: transformer.ConfigLabels(name),
345+
},
346+
}
347+
filename := GetFileName(target)
348+
data := map[string]string{filename: content}
349+
initConfigMapData(configMap, data)
350+
return configMap
351+
}
352+
335353
// InitConfigMapFromFile initializes a ConfigMap object
336354
func (k *Kubernetes) InitConfigMapFromFile(name string, service kobject.ServiceConfig, fileName string) *api.ConfigMap {
337355
content, err := GetContentFromFile(fileName)
@@ -1325,12 +1343,26 @@ func (k *Kubernetes) createConfigMapFromComposeConfig(name string, service kobje
13251343
for _, config := range service.Configs {
13261344
currentConfigName := config.Source
13271345
currentConfigObj := service.ConfigsMetaData[currentConfigName]
1346+
if config.Target == "" {
1347+
config.Target = currentConfigName
1348+
}
13281349
if currentConfigObj.External {
13291350
continue
13301351
}
1331-
currentFileName := currentConfigObj.File
1332-
configMap := k.InitConfigMapFromFile(name, service, currentFileName)
1333-
objects = append(objects, configMap)
1352+
if currentConfigObj.File != "" {
1353+
currentFileName := currentConfigObj.File
1354+
configMap := k.InitConfigMapFromFile(name, service, currentFileName)
1355+
objects = append(objects, configMap)
1356+
} else if currentConfigObj.Content != "" {
1357+
content := currentConfigObj.Content
1358+
configMap := k.InitConfigMapFromContent(name, service, content, currentConfigName, config.Target)
1359+
objects = append(objects, configMap)
1360+
} else if currentConfigObj.Environment != "" {
1361+
// TODO: Add support for environment variables in configmaps
1362+
log.Warnf("Environment variables in configmaps are not supported yet")
1363+
} else {
1364+
log.Warnf("Configmap %s is empty", currentConfigName)
1365+
}
13341366
}
13351367
return objects
13361368
}

0 commit comments

Comments
 (0)