Skip to content

Commit

Permalink
feat: add k8s topology zone info for nodePort service
Browse files Browse the repository at this point in the history
  • Loading branch information
kolorful committed Dec 9, 2024
1 parent 19ab7e0 commit f742b81
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
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

0 comments on commit f742b81

Please sign in to comment.