Skip to content

Commit

Permalink
removing address level filtering.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmurret committed Sep 12, 2023
1 parent 6f5850a commit e1392e0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 163 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package builder

import (
Expand Down Expand Up @@ -39,46 +42,7 @@ func newServicePortInfo(serviceEndpoints *pbcatalog.ServiceEndpoints) *servicePo
}
seen := make(map[string]*seenData)
for epIdx, ep := range serviceEndpoints.GetEndpoints() {
for _, address := range ep.Addresses {

hasAddressLevelPorts := false
if len(address.Ports) > 0 {
hasAddressLevelPorts = true
}

// if address has specific ports, add those to the seen array
for _, portName := range address.Ports {
// check that it is not service mesh port because we don't
// want to add that to the service ports map.
epPort, epOK := ep.Ports[portName]
if isMeshPort(epPort) {
continue
}

_, ok := seen[portName]
if ok {
// port is in the seen map
if epOK {
// port is also in the endpoints list which we want to verify.

// if this port has been seen already, add the current endpoint as a seenBy if it is not already
// present.
if seen[portName].seenBy == nil {
// port is in the seen map but has no seenBy indexes.
seen[portName].seenBy = append([]*int{}, &epIdx)
} else if len(seen[portName].seenBy)-1 < epIdx {
// port is in the seen map and has seenBy indexes but not this one.
seen[portName].seenBy = append(seen[portName].seenBy, &epIdx)
}
// else do nothing since this endpoint has already marked it as seen.
}
} else {
// port is not yet in the seen map
seenBy := append([]*int{}, &epIdx)
seen[portName] = &seenData{port: epPort, seenBy: seenBy}
}
}

for range ep.Addresses {
// iterate through endpoint ports and set the mesh port
// as well as all endpoint ports for this workload if there
// are no specific workload ports.
Expand All @@ -90,11 +54,6 @@ func newServicePortInfo(serviceEndpoints *pbcatalog.ServiceEndpoints) *servicePo
continue
}

// if address specifies a subset, it has already been accounted
// for in the seen list.
if hasAddressLevelPorts {
continue
}
// otherwise, add all ports for this endpoint.
portData, ok := seen[epPortName]
if ok {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package builder

import (
Expand All @@ -7,135 +10,40 @@ import (
"github.com/stretchr/testify/require"
)

// Test_newServicePortInfo test case shows:
// - endpoint 1 has one address with no specific ports.
// - endpoint 2 has one address with no specific ports.
// - the cumulative effect is then the union of endpoints 1 and 2.
func Test_newServicePortInfo(t *testing.T) {
cases := map[string]struct {
serviceEndpoints *pbcatalog.ServiceEndpoints
expectedResult *servicePortInfo
}{
"address specific ports union between endpoints": {
// this test case shows endpoints 1 and endpoints 2 each having an effective port of api-port
// which leads to the service having an effective port of api-port
serviceEndpoints: &pbcatalog.ServiceEndpoints{
Endpoints: []*pbcatalog.Endpoint{
// effective ports = api-port
{
Addresses: []*pbcatalog.WorkloadAddress{
{Host: "10.0.0.1", Ports: []string{"api-port"}},
},
Ports: map[string]*pbcatalog.WorkloadPort{
"admin-port": {Port: 8080, Protocol: pbcatalog.Protocol_PROTOCOL_TCP},
"api-port": {Port: 9090, Protocol: pbcatalog.Protocol_PROTOCOL_TCP},
"mesh": {Port: 20000, Protocol: pbcatalog.Protocol_PROTOCOL_MESH},
},
},
// effective ports = api-port
{
Addresses: []*pbcatalog.WorkloadAddress{
{Host: "10.0.0.2", Ports: []string{"api-port"}},
},
Ports: map[string]*pbcatalog.WorkloadPort{
"api-port": {Port: 9090, Protocol: pbcatalog.Protocol_PROTOCOL_TCP},
"mesh": {Port: 20000, Protocol: pbcatalog.Protocol_PROTOCOL_MESH},
},
},
},
},

// cumulative effective ports = api-port
expectedResult: &servicePortInfo{
meshPortName: "mesh",
meshPort: &pbcatalog.WorkloadPort{Port: 20000, Protocol: pbcatalog.Protocol_PROTOCOL_MESH},
servicePorts: map[string]*pbcatalog.WorkloadPort{
"api-port": {Port: 9090, Protocol: pbcatalog.Protocol_PROTOCOL_TCP},
},
},
},
"address specific ports union with ports aggregated across workload addresses": {
// this test case shows:
// - endpoint 1 has two address with specific ports and the effective ports are the combination of the two,
// not the union and also excludes a endpoint port that is not exposed on any address.
// - endpoint 2 has an address with no specific ports, so its effective ports are all non-"mesh" ports
// defined on the endpoint.
// - the cumulative effect is then the union of endpoints 1 and 2.
serviceEndpoints: &pbcatalog.ServiceEndpoints{
Endpoints: []*pbcatalog.Endpoint{
// effective ports = api-port, admin-port
{
Addresses: []*pbcatalog.WorkloadAddress{
{Host: "10.0.0.1", Ports: []string{"api-port"}},
{Host: "10.0.0.2", Ports: []string{"admin-port"}},
},
Ports: map[string]*pbcatalog.WorkloadPort{
"some-port": {Port: 6060, Protocol: pbcatalog.Protocol_PROTOCOL_TCP},
"admin-port": {Port: 8080, Protocol: pbcatalog.Protocol_PROTOCOL_TCP},
"api-port": {Port: 9090, Protocol: pbcatalog.Protocol_PROTOCOL_TCP},
"mesh": {Port: 20000, Protocol: pbcatalog.Protocol_PROTOCOL_MESH},
},
},
// effective ports = api-port, admin-port, another-port
{
Addresses: []*pbcatalog.WorkloadAddress{
{Host: "10.0.0.3"},
},
Ports: map[string]*pbcatalog.WorkloadPort{
"another-port": {Port: 7070, Protocol: pbcatalog.Protocol_PROTOCOL_TCP},
"admin-port": {Port: 8080, Protocol: pbcatalog.Protocol_PROTOCOL_TCP},
"api-port": {Port: 9090, Protocol: pbcatalog.Protocol_PROTOCOL_TCP},
"mesh": {Port: 20000, Protocol: pbcatalog.Protocol_PROTOCOL_MESH},
},
},
serviceEndpoints := &pbcatalog.ServiceEndpoints{
Endpoints: []*pbcatalog.Endpoint{
{
Addresses: []*pbcatalog.WorkloadAddress{
{Host: "10.0.0.1"},
},
},
// cumulative effective ports = admin-port, api-port
expectedResult: &servicePortInfo{
meshPortName: "mesh",
meshPort: &pbcatalog.WorkloadPort{Port: 20000, Protocol: pbcatalog.Protocol_PROTOCOL_MESH},
servicePorts: map[string]*pbcatalog.WorkloadPort{
Ports: map[string]*pbcatalog.WorkloadPort{
"admin-port": {Port: 8080, Protocol: pbcatalog.Protocol_PROTOCOL_TCP},
"api-port": {Port: 9090, Protocol: pbcatalog.Protocol_PROTOCOL_TCP},
"mesh": {Port: 20000, Protocol: pbcatalog.Protocol_PROTOCOL_MESH},
},
},
},
"no address specific ports create union of workload ports": {
// this test case shows:
// - endpoint 1 has one address with no specific ports.
// - endpoint 2 has one address with no specific ports.
// - the cumulative effect is then the union of endpoints 1 and 2.
serviceEndpoints: &pbcatalog.ServiceEndpoints{
Endpoints: []*pbcatalog.Endpoint{
{
Addresses: []*pbcatalog.WorkloadAddress{
{Host: "10.0.0.1"},
},
Ports: map[string]*pbcatalog.WorkloadPort{
"admin-port": {Port: 8080, Protocol: pbcatalog.Protocol_PROTOCOL_TCP},
"api-port": {Port: 9090, Protocol: pbcatalog.Protocol_PROTOCOL_TCP},
"mesh": {Port: 20000, Protocol: pbcatalog.Protocol_PROTOCOL_MESH},
},
},
{
Addresses: []*pbcatalog.WorkloadAddress{
{Host: "10.0.0.2"},
},
Ports: map[string]*pbcatalog.WorkloadPort{
"api-port": {Port: 9090, Protocol: pbcatalog.Protocol_PROTOCOL_TCP},
"mesh": {Port: 20000, Protocol: pbcatalog.Protocol_PROTOCOL_MESH},
},
},
{
Addresses: []*pbcatalog.WorkloadAddress{
{Host: "10.0.0.2"},
},
},
expectedResult: &servicePortInfo{
meshPortName: "mesh",
meshPort: &pbcatalog.WorkloadPort{Port: 20000, Protocol: pbcatalog.Protocol_PROTOCOL_MESH},
servicePorts: map[string]*pbcatalog.WorkloadPort{
Ports: map[string]*pbcatalog.WorkloadPort{
"api-port": {Port: 9090, Protocol: pbcatalog.Protocol_PROTOCOL_TCP},
"mesh": {Port: 20000, Protocol: pbcatalog.Protocol_PROTOCOL_MESH},
},
},
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
require.Equal(t, tc.expectedResult, newServicePortInfo(tc.serviceEndpoints))
})
expectedResult := &servicePortInfo{
meshPortName: "mesh",
meshPort: &pbcatalog.WorkloadPort{Port: 20000, Protocol: pbcatalog.Protocol_PROTOCOL_MESH},
servicePorts: map[string]*pbcatalog.WorkloadPort{
"api-port": {Port: 9090, Protocol: pbcatalog.Protocol_PROTOCOL_TCP},
},
}
require.Equal(t, expectedResult, newServicePortInfo(serviceEndpoints))
}

0 comments on commit e1392e0

Please sign in to comment.