Skip to content

Commit

Permalink
fix endpoints and pods controller
Browse files Browse the repository at this point in the history
  • Loading branch information
ndhanushkodi committed Jan 30, 2024
1 parent 1e67acc commit eef95ee
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"net"
"sort"
"strconv"
"strings"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -445,10 +446,14 @@ func getEffectiveTargetPort(targetPort intstr.IntOrString, prefixedPods selector
var mostPrevalentContainerPort *corev1.ContainerPort
maxCount := 0
effectiveNameForPort := func(port *corev1.ContainerPort) string {
if port.Name != "" {
return port.Name
var isNum bool
if _, err := strconv.Atoi(port.Name); err == nil {
isNum = true
}
return targetPort.String()
if port.Name == "" || isNum {
return "cslport-" + targetPort.String()
}
return port.Name
}
for _, podData := range prefixedPods {
containerPort := getTargetContainerPort(targetPortInt, podData.samplePod)
Expand Down Expand Up @@ -493,7 +498,7 @@ func getEffectiveTargetPort(targetPort intstr.IntOrString, prefixedPods selector

// If still no match for the target port, fall back to string-ifying the target port name, which
// is what the PodController will do when converting unnamed ContainerPorts to Workload ports.
return targetPort.String()
return "cslport-" + targetPort.String()
}

// getTargetContainerPort returns the pod ContainerPort matching the given numeric port value, or nil if none is found.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func TestReconcile_CreateService(t *testing.T) {
Name: "other",
Port: 10001,
Protocol: "TCP",
TargetPort: intstr.FromString("10001"),
TargetPort: intstr.FromString("cslport-10001"),
// no app protocol specified
},
},
Expand Down Expand Up @@ -260,7 +260,7 @@ func TestReconcile_CreateService(t *testing.T) {
},
{
VirtualPort: 10001,
TargetPort: "10001",
TargetPort: "cslport-10001",
Protocol: pbcatalog.Protocol_PROTOCOL_TCP,
},
{
Expand Down Expand Up @@ -554,12 +554,12 @@ func TestReconcile_CreateService(t *testing.T) {
},
{
VirtualPort: 9090,
TargetPort: "6789", // Matches service target number
TargetPort: "cslport-6789", // Matches service target number
Protocol: pbcatalog.Protocol_PROTOCOL_GRPC,
},
{
VirtualPort: 10010,
TargetPort: "10010", // Matches service target number (unmatched by container ports)
TargetPort: "cslport-10010", // Matches service target number (unmatched by container ports)
Protocol: pbcatalog.Protocol_PROTOCOL_HTTP,
},
{
Expand Down Expand Up @@ -713,7 +713,7 @@ func TestReconcile_CreateService(t *testing.T) {
},
{
VirtualPort: 9090,
TargetPort: "6789", // Matches service target number due to unnamed being most common
TargetPort: "cslport-6789", // Matches service target number due to unnamed being most common
Protocol: pbcatalog.Protocol_PROTOCOL_GRPC,
},
{
Expand Down Expand Up @@ -1269,7 +1269,7 @@ func TestReconcile_UpdateService(t *testing.T) {
},
{
VirtualPort: 10001,
TargetPort: "10001",
TargetPort: "unspec-port", //this might need to be changed to "my_unspecified_port"
Protocol: pbcatalog.Protocol_PROTOCOL_UNSPECIFIED,
},
{
Expand Down Expand Up @@ -1390,7 +1390,7 @@ func TestReconcile_UpdateService(t *testing.T) {
Name: "other",
Port: 10001,
Protocol: "TCP",
TargetPort: intstr.FromString("10001"),
TargetPort: intstr.FromString("cslport-10001"),
// no app protocol specified
},
},
Expand Down Expand Up @@ -1421,7 +1421,7 @@ func TestReconcile_UpdateService(t *testing.T) {
},
{
VirtualPort: 10001,
TargetPort: "10001",
TargetPort: "cslport-10001",
Protocol: pbcatalog.Protocol_PROTOCOL_TCP,
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,12 @@ func getWorkloadPorts(pod corev1.Pod) ([]string, map[string]*pbcatalog.WorkloadP
for _, container := range pod.Spec.Containers {
for _, port := range container.Ports {
name := port.Name
if name == "" {
name = strconv.Itoa(int(port.ContainerPort))
var isNum bool
if _, err := strconv.Atoi(name); err == nil {
isNum = true
}
if name == "" || isNum {
name = "cslport-" + strconv.Itoa(int(port.ContainerPort))
}

// TODO: error check reserved "mesh" keyword and 20000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,14 @@ func TestWorkloadWrite(t *testing.T) {
},
expectedWorkload: &pbcatalog.Workload{
Addresses: []*pbcatalog.WorkloadAddress{
{Host: "10.0.0.1", Ports: []string{"80", "8080", "mesh"}},
{Host: "10.0.0.1", Ports: []string{"cslport-80", "cslport-8080", "mesh"}},
},
Ports: map[string]*pbcatalog.WorkloadPort{
"80": {
"cslport-80": {
Port: 80,
Protocol: pbcatalog.Protocol_PROTOCOL_UNSPECIFIED,
},
"8080": {
"cslport-8080": {
Port: 8080,
Protocol: pbcatalog.Protocol_PROTOCOL_UNSPECIFIED,
},
Expand Down

0 comments on commit eef95ee

Please sign in to comment.