diff --git a/generator/service.go b/generator/service.go index 5573e06..730577f 100644 --- a/generator/service.go +++ b/generator/service.go @@ -1,6 +1,7 @@ package generator import ( + "fmt" "katenary/utils" "regexp" "strings" @@ -59,6 +60,7 @@ func (s *Service) AddPort(port types.ServicePortConfig, serviceName ...string) { if targetPort := utils.GetServiceNameByPort(int(port.Target)); targetPort == "" { finalport = intstr.FromInt(int(port.Target)) + name = fmt.Sprintf("port-%d", port.Target) } else { finalport = intstr.FromString(targetPort) name = targetPort diff --git a/generator/service_test.go b/generator/service_test.go index 205c944..1afad1f 100644 --- a/generator/service_test.go +++ b/generator/service_test.go @@ -47,3 +47,37 @@ services: t.Errorf("Expected 2 ports, got %d", foundPort) } } + +func TestWithSeveralUnknownPorts(t *testing.T) { + composeFile := ` +services: + multi: + image: nginx + ports: + - 12443 + - 12480 + labels: + katenary.v3/ingress: |- + port: 12443 +` + tmpDir := setup(composeFile) + defer teardown(tmpDir) + + currentDir, _ := os.Getwd() + os.Chdir(tmpDir) + defer os.Chdir(currentDir) + + output := internalCompileTest(t, "-s", "templates/multi/service.yaml") + service := v1.Service{} + if err := yaml.Unmarshal([]byte(output), &service); err != nil { + t.Errorf(unmarshalError, err) + } + + if len(service.Spec.Ports) != 2 { + t.Errorf("Expected 2 ports, got %d", len(service.Spec.Ports)) + } + // ensure that both port names are different + if service.Spec.Ports[0].Name == service.Spec.Ports[1].Name { + t.Errorf("Expected different port names, got %s and %s", service.Spec.Ports[0].Name, service.Spec.Ports[1].Name) + } +}