Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for specifying a health check port with an ExternalName Service #6230

Merged
merged 3 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions internal/envoy/v3/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,29 @@ func TestCluster(t *testing.T) {
},
}

s3 := &core_v1.Service{
ObjectMeta: meta_v1.ObjectMeta{
Name: "kuard",
Namespace: "default",
},
Spec: core_v1.ServiceSpec{
ExternalName: "foo.io",
Ports: []core_v1.ServicePort{
{
Name: "http",
Protocol: "TCP",
Port: 443,
TargetPort: intstr.FromInt(8080),
}, {
Name: "health-check",
Protocol: "TCP",
Port: 8998,
TargetPort: intstr.FromInt(8998),
},
},
},
}

svcExternal := &core_v1.Service{
ObjectMeta: meta_v1.ObjectMeta{
Name: "kuard",
Expand Down Expand Up @@ -196,6 +219,17 @@ func TestCluster(t *testing.T) {
LoadAssignment: ExternalNameClusterLoadAssignment(service(s2)),
},
},
"externalName service healthcheckport": {
cluster: &dag.Cluster{
Upstream: healthcheckService(s3),
},
want: &envoy_config_cluster_v3.Cluster{
Name: "default/kuard/443/da39a3ee5e",
AltStatName: "default_kuard_443",
ClusterDiscoveryType: ClusterDiscoveryType(envoy_config_cluster_v3.Cluster_STRICT_DNS),
LoadAssignment: ExternalNameClusterLoadAssignment(healthcheckService(s3)),
},
},
"externalName service - dns-lookup-family v4": {
cluster: &dag.Cluster{
Upstream: service(s2),
Expand Down Expand Up @@ -1192,3 +1226,16 @@ func service(s *core_v1.Service, protocols ...string) *dag.Service {
Protocol: protocol,
}
}

func healthcheckService(s *core_v1.Service) *dag.Service {
return &dag.Service{
Weighted: dag.WeightedService{
Weight: 1,
ServiceName: s.Name,
ServiceNamespace: s.Namespace,
ServicePort: s.Spec.Ports[0],
HealthPort: s.Spec.Ports[1],
},
ExternalName: s.Spec.ExternalName,
}
}
6 changes: 5 additions & 1 deletion internal/envoy/v3/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,15 @@ func ClusterLoadAssignment(name string, addrs ...*envoy_config_core_v3.Address)

// ExternalNameClusterLoadAssignment creates a *envoy_config_endpoint_v3.ClusterLoadAssignment pointing to service's ExternalName DNS address.
func ExternalNameClusterLoadAssignment(service *dag.Service) *envoy_config_endpoint_v3.ClusterLoadAssignment {
return ClusterLoadAssignment(
cla := ClusterLoadAssignment(
xds.ClusterLoadAssignmentName(
types.NamespacedName{Name: service.Weighted.ServiceName, Namespace: service.Weighted.ServiceNamespace},
service.Weighted.ServicePort.Name,
),
SocketAddress(service.ExternalName, int(service.Weighted.ServicePort.Port)),
)
if service.Weighted.ServicePort.Port != service.Weighted.HealthPort.Port {
cla.Endpoints[0].LbEndpoints[0].GetEndpoint().HealthCheckConfig = HealthCheckConfig(service.Weighted.HealthPort.Port)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks OK to me but I had to stop for a bit when reading - we know we always assign exactly 1 address and therefore it is safe to access Endpoints[0].LbEndpoints[0], right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah we pass only one address to ClusterLoadAssignment above so the returned cla should have one endpoint entry, but lets add another test of just the ExternalNameClusterLoadAssignment function to endpoint_test.go to be sure

}
return cla
}
Loading