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

feat: add k8s topology zone info for nodePort service #4301

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions .changelog/4301.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feature
control-plane: Add available Zone Kubernetes Topology Metadata for NodePort Service when Syncing Kubernetes Services.
```
44 changes: 25 additions & 19 deletions control-plane/catalog/to-consul/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ func (t *ServiceResource) generateRegistrations(key string) {
r.Service = &rs
r.Service.ID = serviceID(r.Service.Service, endpointAddr)
r.Service.Address = address.Address

r.Service.Meta = updateServiceMeta(baseService.Meta, endpoint)
t.consulMap[key] = append(t.consulMap[key], &r)
// Only consider the first address that matches. In some cases
// there will be multiple addresses like when using AWS CNI.
Expand All @@ -683,7 +683,7 @@ func (t *ServiceResource) generateRegistrations(key string) {
r.Service = &rs
r.Service.ID = serviceID(r.Service.Service, endpointAddr)
r.Service.Address = address.Address

r.Service.Meta = updateServiceMeta(baseService.Meta, endpoint)
t.consulMap[key] = append(t.consulMap[key], &r)
// Only consider the first address that matches. In some cases
// there will be multiple addresses like when using AWS CNI.
Expand Down Expand Up @@ -778,23 +778,7 @@ func (t *ServiceResource) registerServiceInstance(
r.Service.ID = serviceID(r.Service.Service, addr)
r.Service.Address = addr
r.Service.Port = epPort
r.Service.Meta = make(map[string]string)
// Deepcopy baseService.Meta into r.Service.Meta as baseService is shared
// between all nodes of a service
for k, v := range baseService.Meta {
r.Service.Meta[k] = v
}
if endpoint.TargetRef != nil {
r.Service.Meta[ConsulK8SRefValue] = endpoint.TargetRef.Name
r.Service.Meta[ConsulK8SRefKind] = endpoint.TargetRef.Kind
}
if endpoint.NodeName != nil {
r.Service.Meta[ConsulK8SNodeName] = *endpoint.NodeName
}
if endpoint.Zone != nil {
r.Service.Meta[ConsulK8STopologyZone] = *endpoint.Zone
}

r.Service.Meta = updateServiceMeta(baseService.Meta, endpoint)
r.Check = &consulapi.AgentCheck{
CheckID: consulHealthCheckID(endpointSlice.Namespace, serviceID(r.Service.Service, addr)),
Name: consulKubernetesCheckName,
Expand Down Expand Up @@ -1110,3 +1094,25 @@ func getServiceWeight(weight string) (int, error) {

return weightI, nil
}

// deepcopy baseService.Meta into r.Service.Meta as baseService is shared between all nodes of a service.
// update service meta with k8s topology info.
func updateServiceMeta(baseServiceMeta map[string]string, endpoint discoveryv1.Endpoint) map[string]string {

serviceMeta := make(map[string]string)

for k, v := range baseServiceMeta {
serviceMeta[k] = v
}
if endpoint.TargetRef != nil {
serviceMeta[ConsulK8SRefValue] = endpoint.TargetRef.Name
serviceMeta[ConsulK8SRefKind] = endpoint.TargetRef.Kind
}
if endpoint.NodeName != nil {
serviceMeta[ConsulK8SNodeName] = *endpoint.NodeName
}
if endpoint.Zone != nil {
serviceMeta[ConsulK8STopologyZone] = *endpoint.Zone
}
return serviceMeta
}
2 changes: 2 additions & 0 deletions control-plane/catalog/to-consul/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,8 @@ func TestServiceResource_nodePort(t *testing.T) {
require.Equal(r, "3.4.5.6", actual[2].Service.Address)
require.Equal(r, 30000, actual[2].Service.Port)
require.Equal(r, "k8s-sync", actual[2].Node)
require.Equal(r, "us-west-2a", actual[0].Service.Meta[ConsulK8STopologyZone])
require.Equal(r, "us-west-2b", actual[1].Service.Meta[ConsulK8STopologyZone])
require.NotEqual(r, actual[0].Service.ID, actual[1].Service.ID)
require.NotEqual(r, actual[0].Service.ID, actual[2].Service.ID)
require.NotEqual(r, actual[1].Service.ID, actual[2].Service.ID)
Expand Down
Loading