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 adminapi endpoints helpers #3509

Merged
merged 1 commit into from
Feb 8, 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
83 changes: 83 additions & 0 deletions internal/adminapi/endpoints.go
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
}
Copy link
Contributor

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see #3815.

Copy link
Contributor

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.

Copy link
Contributor

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?

Copy link
Contributor

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.

}
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
}
Loading