Skip to content

Commit

Permalink
Provide a Service Port Name (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
maysunfaisal authored Aug 23, 2023
1 parent 8abc319 commit 1f9eb46
Show file tree
Hide file tree
Showing 2 changed files with 382 additions and 2 deletions.
22 changes: 21 additions & 1 deletion pkg/devfile/devfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,20 +341,40 @@ func GetResourceFromDevfile(log logr.Logger, devfileData data.DevfileData, deplo

if currentPort > 0 {
servicePort := corev1.ServicePort{
Name: strconv.Itoa(currentPort),
Port: int32(currentPort),
TargetPort: intstr.FromInt(currentPort),
}

isPresent := false
portNameMap := make(map[string]bool)
for _, port := range resources.Services[0].Spec.Ports {
portNameMap[port.Name] = true
if port.Port == servicePort.Port {
isPresent = true
break
}
}

if !isPresent {
if portNameMap[servicePort.Name] {
generatedName := fmt.Sprintf("%s-%s", servicePort.Name, util.GetRandomString(4, true))
portNameMap[generatedName] = true
servicePort.Name = generatedName
}
resources.Services[0].Spec.Ports = append(resources.Services[0].Spec.Ports, servicePort)

for i, port := range resources.Services[0].Spec.Ports {
if port.Name == "" {
// if port name is empty for other service ports, assign a name
// because name is required if there is more than one port
portName := strconv.Itoa(int(port.Port))
if portNameMap[portName] {
portName = fmt.Sprintf("%s-%s", portName, util.GetRandomString(4, true))
portNameMap[portName] = true
}
resources.Services[0].Spec.Ports[i].Name = portName
}
}
}
}
}
Expand Down
Loading

0 comments on commit 1f9eb46

Please sign in to comment.