fix(service): Fixes naming service ports

- ports should be named "port-XXX" with XXX to be the port number if the
port name cannot be discovered
- it follows what we do in Deployment objects
- this should fix #132
This commit is contained in:
2025-07-13 15:02:26 +02:00
parent 89fd516b20
commit ce479629f6
2 changed files with 36 additions and 0 deletions

View File

@@ -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

View File

@@ -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)
}
}