Skip to content

Commit

Permalink
fix: debugger ports not creating services (#2606)
Browse files Browse the repository at this point in the history
Signed-off-by: Javier López Barba <javier@okteto.com>
  • Loading branch information
jLopezbarb committed Apr 28, 2022
1 parent 83b5135 commit 40844cb
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
22 changes: 18 additions & 4 deletions pkg/cmd/stack/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,10 +765,8 @@ func translateContainerPorts(svc *model.Service) []apiv1.ContainerPort {

func translateServicePorts(svc model.Service) []apiv1.ServicePort {
result := []apiv1.ServicePort{}
for _, p := range svc.Ports {
if model.IsSkippablePort(p.HostPort) {
continue
}
ports := getPortsToAddToSvc(svc.Ports)
for _, p := range ports {
if !isServicePortAdded(p.ContainerPort, result) {
result = append(
result,
Expand All @@ -795,6 +793,22 @@ func translateServicePorts(svc model.Service) []apiv1.ServicePort {
return result
}

func getPortsToAddToSvc(ports []model.Port) []model.Port {
privatePorts := []model.Port{}
publicPorts := []model.Port{}
for _, p := range ports {
if model.IsSkippablePort(p.HostPort) {
privatePorts = append(privatePorts, p)
} else {
publicPorts = append(publicPorts, p)
}
}
if len(publicPorts) > 0 {
return publicPorts
}
return privatePorts
}

func isServicePortAdded(newPort int32, existentPorts []apiv1.ServicePort) bool {
for _, p := range existentPorts {
if p.Port == newPort {
Expand Down
55 changes: 55 additions & 0 deletions pkg/cmd/stack/translate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,61 @@ func Test_translateService(t *testing.T) {
},
},
},
{
name: "translate svc private endpoints by private annotation",
stack: &model.Stack{
Name: "stackName",
Services: map[string]*model.Service{
"svcName": {
Labels: model.Labels{
"label1": "value1",
"label2": "value2",
},
Annotations: model.Annotations{
"annotation1": "value1",
"annotation2": "value2",
},
Ports: []model.Port{
{
HostPort: 6379,
ContainerPort: 6379,
Protocol: apiv1.ProtocolTCP,
},
},
},
},
},
expected: &apiv1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "svcName",
Labels: map[string]string{
"label1": "value1",
"label2": "value2",
model.StackNameLabel: "stackName",
model.StackServiceNameLabel: "svcName",
},
Annotations: map[string]string{
"annotation1": "value1",
"annotation2": "value2",
},
},
Spec: apiv1.ServiceSpec{
Type: apiv1.ServiceTypeClusterIP,
Selector: map[string]string{
model.StackNameLabel: "stackName",
model.StackServiceNameLabel: "svcName",
},
Ports: []apiv1.ServicePort{
{
Name: "p-6379-6379-tcp",
Port: 6379,
TargetPort: intstr.IntOrString{IntVal: 6379},
Protocol: apiv1.ProtocolTCP,
},
},
},
},
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 40844cb

Please sign in to comment.