catalog: service endpoints inherits protocol from service when workload doesn't have one (#18792)

This commit is contained in:
Iryna Shustava 2023-09-14 08:52:12 -06:00 committed by GitHub
parent 02259ef964
commit 7f2a1d9812
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 2 deletions

View File

@ -311,8 +311,13 @@ func workloadToEndpoint(svc *pbcatalog.Service, data *workloadData) *pbcatalog.E
continue
}
if workloadPort.Protocol != svcPort.Protocol {
// workload port mismatch - ignore it
// If workload protocol is not specified, we will default to service's protocol.
// This is because on some platforms (kubernetes), workload protocol is not always
// known, and so we need to inherit from the service instead.
if workloadPort.Protocol == pbcatalog.Protocol_PROTOCOL_UNSPECIFIED {
workloadPort.Protocol = svcPort.Protocol
} else if workloadPort.Protocol != svcPort.Protocol {
// Otherwise, there's workload port mismatch - ignore it.
continue
}

View File

@ -185,6 +185,51 @@ func TestWorkloadToEndpoint_AllAddressesFiltered(t *testing.T) {
require.Nil(t, workloadToEndpoint(service, data))
}
func TestWorkloadToEndpoint_MissingWorkloadProtocol(t *testing.T) {
// This test checks that when a workload is missing its protocol,
// we will default to service's protocol.
service := &pbcatalog.Service{
Ports: []*pbcatalog.ServicePort{
{TargetPort: "test-port", Protocol: pbcatalog.Protocol_PROTOCOL_HTTP},
},
}
workload := &pbcatalog.Workload{
Addresses: []*pbcatalog.WorkloadAddress{
{Host: "127.0.0.1"},
},
Ports: map[string]*pbcatalog.WorkloadPort{
"test-port": {Port: 8080},
},
}
data := &workloadData{
resource: rtest.Resource(types.WorkloadType, "foo").
WithData(t, workload).
Build(),
workload: workload,
}
expected := &pbcatalog.Endpoint{
TargetRef: data.resource.Id,
Addresses: []*pbcatalog.WorkloadAddress{
{Host: "127.0.0.1", Ports: []string{"test-port"}},
},
Ports: map[string]*pbcatalog.WorkloadPort{
"test-port": {Port: 8080, Protocol: pbcatalog.Protocol_PROTOCOL_HTTP},
},
// The health is critical because we are not setting the workload's
// health status. The tests for determineWorkloadHealth will ensure
// that we can properly determine the health status and the overall
// controller tests will prove that the integration works as expected.
HealthStatus: pbcatalog.Health_HEALTH_CRITICAL,
Identity: workload.Identity,
}
prototest.AssertDeepEqual(t, expected, workloadToEndpoint(service, data))
}
func TestServiceUnderManagement(t *testing.T) {
// This test ensures that we can properly detect when a service
// should have endpoints generated for it vs when those endpoints