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:
@@ -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
|
||||
|
@@ -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)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user