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: Gateway Discovery produce DNS names instead of IP addresses #4044

Merged
merged 1 commit into from
May 22, 2023
Merged
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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ Adding a new version? You'll need three changes:
[#3998](https:/Kong/kubernetes-ingress-controller/pull/3998)
[#3980](https:/Kong/kubernetes-ingress-controller/pull/3980)
[#3977](https:/Kong/kubernetes-ingress-controller/pull/3977)

- Gateway Discovery now produces DNS names instead of IP addresses
[#4044](https:/Kong/kubernetes-ingress-controller/pull/4044)

### Fixed

- Fix paging in `GetAdminAPIsForService` which might have caused the controller
Expand Down
34 changes: 29 additions & 5 deletions internal/adminapi/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package adminapi
import (
"context"
"fmt"
"strings"

discoveryv1 "k8s.io/api/discovery/v1"
"k8s.io/apimachinery/pkg/labels"
Expand Down Expand Up @@ -78,6 +79,14 @@ func AdminAPIsFromEndpointSlice(endpoints discoveryv1.EndpointSlice, portNames s
continue
}

var serviceName string
for _, or := range endpoints.OwnerReferences {
if or.Kind == "Service" && or.APIVersion == "v1" {
serviceName = or.Name
break
}
}

for _, e := range endpoints.Endpoints {
if e.Conditions.Ready == nil || !*e.Conditions.Ready {
continue
Expand All @@ -95,18 +104,33 @@ func AdminAPIsFromEndpointSlice(endpoints discoveryv1.EndpointSlice, portNames s
if len(e.Addresses) < 1 {
continue
}

// Endpoint's addresses are assumed to be fungible, therefore we pick only the first one.
// For the context please see the `Endpoint.Addresses` godoc.
addr := e.Addresses[0]
addr := strings.ReplaceAll(e.Addresses[0], ".", "-")

var adminAPI DiscoveredAdminAPI
// NOTE: We assume https here because the referenced Admin API
// server will live in another Pod/elsewhere so allowing http would
// not be considered best practice.
adminAPI := DiscoveredAdminAPI{
Address: fmt.Sprintf("https://%s:%d", addr, *p.Port),
PodRef: podNN,
if serviceName == "" {
// If we couldn't find a service that's the owner of provided EndpointSlice
// then fallback to providing a DNS name for the Pod only.
adminAPI = DiscoveredAdminAPI{
Address: fmt.Sprintf("https://%s.%s.pod:%d",
addr, endpoints.Namespace, *p.Port,
),
PodRef: podNN,
}
} else {
adminAPI = DiscoveredAdminAPI{
Address: fmt.Sprintf("https://%s.%s.%s.svc:%d",
addr, serviceName, endpoints.Namespace, *p.Port,
),
PodRef: podNN,
}
}
discoveredAdminAPIs.Insert(adminAPI)
discoveredAdminAPIs = discoveredAdminAPIs.Insert(adminAPI)
}
}
return discoveredAdminAPIs
Expand Down
Loading