Skip to content

Commit fe6663f

Browse files
committed
issue(106): Fix service names with dashes
See #106, I need to add a test on "same-pod" label.
1 parent 7068dc2 commit fe6663f

File tree

2 files changed

+165
-3
lines changed

2 files changed

+165
-3
lines changed

generator/deployment_test.go

+130-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"testing"
88

9+
yaml3 "gopkg.in/yaml.v3"
910
v1 "k8s.io/api/apps/v1"
1011
corev1 "k8s.io/api/core/v1"
1112
"sigs.k8s.io/yaml"
@@ -324,14 +325,140 @@ services:
324325
Environment map[string]string `yaml:"environment"`
325326
} `yaml:"web"`
326327
}{}
327-
if err := yaml.Unmarshal(valuesContent, &mapping); err != nil {
328+
if err := yaml3.Unmarshal(valuesContent, &mapping); err != nil {
328329
t.Errorf(unmarshalError, err)
329330
}
330331

331-
if _, ok := mapping.Web.Environment["FOO"]; !ok {
332+
if v, ok := mapping.Web.Environment["FOO"]; !ok {
332333
t.Errorf("Expected FOO in web environment")
334+
if v != "bar" {
335+
t.Errorf("Expected FOO to be bar, got %s", v)
336+
}
333337
}
334-
if _, ok := mapping.Web.Environment["BAZ"]; ok {
338+
if v, ok := mapping.Web.Environment["BAZ"]; ok {
335339
t.Errorf("Expected BAZ not in web environment")
340+
if v != "qux" {
341+
t.Errorf("Expected BAZ to be qux, got %s", v)
342+
}
343+
}
344+
}
345+
346+
func TestWithDashes(t *testing.T) {
347+
composeFile := `
348+
services:
349+
web-app:
350+
image: nginx:1.29
351+
environment:
352+
FOO: BAR
353+
labels:
354+
%s/values: |
355+
- FOO
356+
`
357+
358+
composeFile = fmt.Sprintf(composeFile, labels.Prefix())
359+
tmpDir := setup(composeFile)
360+
defer teardown(tmpDir)
361+
362+
currentDir, _ := os.Getwd()
363+
os.Chdir(tmpDir)
364+
defer os.Chdir(currentDir)
365+
366+
output := internalCompileTest(t, "-s", "templates/web_app/deployment.yaml")
367+
dt := v1.Deployment{}
368+
if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
369+
t.Errorf(unmarshalError, err)
370+
}
371+
372+
valuesFile := "./chart/values.yaml"
373+
if _, err := os.Stat(valuesFile); os.IsNotExist(err) {
374+
t.Errorf("values.yaml does not exist")
375+
}
376+
valuesContent, err := os.ReadFile(valuesFile)
377+
if err != nil {
378+
t.Errorf("Error reading values.yaml: %s", err)
379+
}
380+
mapping := struct {
381+
Web struct {
382+
Environment map[string]string `yaml:"environment"`
383+
} `yaml:"web_app"`
384+
}{}
385+
if err := yaml3.Unmarshal(valuesContent, &mapping); err != nil {
386+
t.Errorf(unmarshalError, err)
387+
}
388+
389+
// we must have FOO in web_app environment (not web-app)
390+
// this validates that the service name is converted to a valid k8s name
391+
if v, ok := mapping.Web.Environment["FOO"]; !ok {
392+
t.Errorf("Expected FOO in web_app environment")
393+
if v != "BAR" {
394+
t.Errorf("Expected FOO to be BAR, got %s", v)
395+
}
396+
}
397+
}
398+
399+
func TestDashesWithValueFrom(t *testing.T) {
400+
composeFile := `
401+
services:
402+
web-app:
403+
image: nginx:1.29
404+
environment:
405+
FOO: BAR
406+
labels:
407+
%[1]s/values: |
408+
- FOO
409+
web2:
410+
image: nginx:1.29
411+
labels:
412+
%[1]s/values-from: |
413+
BAR: web-app.FOO
414+
`
415+
416+
composeFile = fmt.Sprintf(composeFile, labels.Prefix())
417+
tmpDir := setup(composeFile)
418+
defer teardown(tmpDir)
419+
420+
currentDir, _ := os.Getwd()
421+
os.Chdir(tmpDir)
422+
defer os.Chdir(currentDir)
423+
424+
output := internalCompileTest(t, "-s", "templates/web2/deployment.yaml")
425+
dt := v1.Deployment{}
426+
if err := yaml.Unmarshal([]byte(output), &dt); err != nil {
427+
t.Errorf(unmarshalError, err)
428+
}
429+
430+
valuesFile := "./chart/values.yaml"
431+
if _, err := os.Stat(valuesFile); os.IsNotExist(err) {
432+
t.Errorf("values.yaml does not exist")
433+
}
434+
valuesContent, err := os.ReadFile(valuesFile)
435+
if err != nil {
436+
t.Errorf("Error reading values.yaml: %s", err)
437+
}
438+
mapping := struct {
439+
Web struct {
440+
Environment map[string]string `yaml:"environment"`
441+
} `yaml:"web_app"`
442+
}{}
443+
if err := yaml3.Unmarshal(valuesContent, &mapping); err != nil {
444+
t.Errorf(unmarshalError, err)
445+
}
446+
447+
// we must have FOO in web_app environment (not web-app)
448+
// this validates that the service name is converted to a valid k8s name
449+
if v, ok := mapping.Web.Environment["FOO"]; !ok {
450+
t.Errorf("Expected FOO in web_app environment")
451+
if v != "BAR" {
452+
t.Errorf("Expected FOO to be BAR, got %s", v)
453+
}
454+
}
455+
456+
// ensure that the deployment has the value from the other service
457+
barenv := dt.Spec.Template.Spec.Containers[0].Env[0]
458+
if barenv.Value != "" {
459+
t.Errorf("Expected value to be empty")
460+
}
461+
if barenv.ValueFrom == nil {
462+
t.Errorf("Expected valueFrom to be set")
336463
}
337464
}

generator/generator.go

+35
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import (
44
"bytes"
55
"fmt"
66
"katenary/generator/labels"
7+
"katenary/generator/labels/labelStructs"
78
"katenary/utils"
89
"log"
910
"regexp"
1011
"strings"
1112

1213
"github.com/compose-spec/compose-go/types"
1314
corev1 "k8s.io/api/core/v1"
15+
"sigs.k8s.io/yaml"
1416
)
1517

1618
// Generate a chart from a compose project.
@@ -43,6 +45,39 @@ func Generate(project *types.Project) (*HelmChart, error) {
4345
Annotations[labels.LabelName("compose-hash")] = hash
4446
chart.composeHash = &hash
4547

48+
// rename all services name to remove dashes
49+
for i, service := range project.Services {
50+
if service.Name != utils.AsResourceName(service.Name) {
51+
fixed := utils.AsResourceName(service.Name)
52+
for j, s := range project.Services {
53+
// for the same-pod services, we need to keep the original name
54+
if samepod, ok := s.Labels[labels.LabelSamePod]; ok && samepod == service.Name {
55+
s.Labels[labels.LabelSamePod] = fixed
56+
project.Services[j] = s
57+
}
58+
// also, the value-from label should be updated
59+
if valuefrom, ok := s.Labels[labels.LabelValueFrom]; ok {
60+
vf, err := labelStructs.GetValueFrom(valuefrom)
61+
if err != nil {
62+
return nil, err
63+
}
64+
for varname, bind := range *vf {
65+
log.Printf("service %s, varname %s, bind %s", service.Name, varname, bind)
66+
bind := strings.ReplaceAll(bind, service.Name, fixed)
67+
(*vf)[varname] = bind
68+
}
69+
output, err := yaml.Marshal(vf)
70+
if err != nil {
71+
return nil, err
72+
}
73+
s.Labels[labels.LabelValueFrom] = string(output)
74+
}
75+
}
76+
service.Name = fixed
77+
project.Services[i] = service
78+
}
79+
}
80+
4681
// find the "main-app" label, and set chart.AppVersion to the tag if exists
4782
mainCount := 0
4883
for _, service := range project.Services {

0 commit comments

Comments
 (0)