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 Sep 3, 2024
1 parent 4389f85 commit 04ebc1e
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 @@ -661,7 +661,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 @@ -682,7 +682,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 @@ -1104,3 +1088,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
}

Check failure on line 1112 in control-plane/catalog/to-consul/resource.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofmt`-ed with `-s` (gofmt)
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 @@ -854,6 +854,8 @@ func TestServiceResource_nodePort(t *testing.T) {
require.Equal(r, "2.3.4.5", actual[1].Service.Address)
require.Equal(r, 30000, actual[1].Service.Port)
require.Equal(r, "k8s-sync", actual[1].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)
})
}
Expand Down

0 comments on commit 04ebc1e

Please sign in to comment.