-
Notifications
You must be signed in to change notification settings - Fork 592
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 adminapi endpoints helpers #3509
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package adminapi | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
discoveryv1 "k8s.io/api/discovery/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/selection" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// GetURLsForService performs an endpoint lookup, using provided kubeClient | ||
// to list provided Admin API Service EndpointSlices. | ||
func GetURLsForService(ctx context.Context, kubeClient client.Client, service types.NamespacedName) (sets.Set[string], error) { | ||
const ( | ||
defaultEndpointSliceListPagingLimit = 100 | ||
) | ||
|
||
// Get all the EndpointSlices assigned to the provided service. | ||
labelReq, err := labels.NewRequirement("kubernetes.io/service-name", selection.Equals, []string{service.Name}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var ( | ||
addresses = sets.New[string]() | ||
continueToken string | ||
labelSelector = labels.NewSelector().Add(*labelReq) | ||
) | ||
for { | ||
var endpointsList discoveryv1.EndpointSliceList | ||
if err := kubeClient.List(ctx, &endpointsList, &client.ListOptions{ | ||
LabelSelector: labelSelector, | ||
Namespace: service.Namespace, | ||
Continue: continueToken, | ||
Limit: defaultEndpointSliceListPagingLimit, | ||
}); err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, es := range endpointsList.Items { | ||
addresses = addresses.Union(AddressesFromEndpointSlice(es)) | ||
} | ||
|
||
if endpointsList.Continue == "" { | ||
break | ||
} | ||
} | ||
return addresses, nil | ||
} | ||
|
||
// AddressesFromEndpointSlice returns a list of Admin API addresses when given | ||
// an Endpointslice. | ||
func AddressesFromEndpointSlice(endpoints discoveryv1.EndpointSlice) sets.Set[string] { | ||
addresses := sets.New[string]() | ||
for _, p := range endpoints.Ports { | ||
if p.Name == nil { | ||
continue | ||
} | ||
|
||
// NOTE: consider making this configurable. | ||
if *p.Name != "admin" { | ||
continue | ||
} | ||
|
||
for _, e := range endpoints.Endpoints { | ||
if e.Conditions.Ready == nil || !*e.Conditions.Ready { | ||
continue | ||
} | ||
|
||
for _, addr := range e.Addresses { | ||
// 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. | ||
addresses.Insert(fmt.Sprintf("https://%s:%d", addr, *p.Port)) | ||
} | ||
} | ||
} | ||
return addresses | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pmalek, did you intend to assign to the
continueToken
variable defined at line 30 above? As written, you always submit an empty continuation token, so you don't accommodate two or more pages of slice entries.@czeslavo, what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see #3815.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I agree it's a bug.
For context why it's called with a client not using the controller's cache, I believe
GetURLsForService
is used before the controller manager gets created and that happens only once at startup time.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we need to be calling on this every time a watch fires on the
endpointslices
resource, in order to learn of the current—and presumably changed—set of proxy pods?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It turns out that we do read from the cache when the watches fire later, over in
(*internal/controllers/configuration.KongAdminAPIServiceReconciler).Reconcile
.I was in the middle of filing a new issue proposing that we use watch and cache here, but it turns out that it's already in place.