plugin - endpoints to slices (#9081)

Signed-off-by: tombokombo <tombo@sysart.tech>

Signed-off-by: tombokombo <tombo@sysart.tech>
This commit is contained in:
Tomas Hulata 2022-09-28 16:34:36 +02:00 committed by GitHub
parent 26fe69cb47
commit 261ce42517
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -22,11 +22,13 @@ import (
appsv1 "k8s.io/api/apps/v1" appsv1 "k8s.io/api/apps/v1"
apiv1 "k8s.io/api/core/v1" apiv1 "k8s.io/api/core/v1"
discoveryv1 "k8s.io/api/discovery/v1"
networking "k8s.io/api/networking/v1" networking "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/cli-runtime/pkg/genericclioptions"
appsv1client "k8s.io/client-go/kubernetes/typed/apps/v1" appsv1client "k8s.io/client-go/kubernetes/typed/apps/v1"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1" corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
discoveryv1client "k8s.io/client-go/kubernetes/typed/discovery/v1"
typednetworking "k8s.io/client-go/kubernetes/typed/networking/v1" typednetworking "k8s.io/client-go/kubernetes/typed/networking/v1"
"k8s.io/ingress-nginx/cmd/plugin/util" "k8s.io/ingress-nginx/cmd/plugin/util"
@ -129,55 +131,61 @@ func GetIngressDefinitions(flags *genericclioptions.ConfigFlags, namespace strin
return pods.Items, nil return pods.Items, nil
} }
// GetNumEndpoints counts the number of endpoints for the service with the given name // GetNumEndpoints counts the number of endpointslices adresses for the service with the given name
func GetNumEndpoints(flags *genericclioptions.ConfigFlags, namespace string, serviceName string) (*int, error) { func GetNumEndpoints(flags *genericclioptions.ConfigFlags, namespace string, serviceName string) (*int, error) {
endpoints, err := GetEndpointsByName(flags, namespace, serviceName) epss, err := GetEndpointSlicesByName(flags, namespace, serviceName)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if endpoints == nil { if len(epss) == 0 {
return nil, nil return nil, nil
} }
ret := 0 ret := 0
for _, subset := range endpoints.Subsets { for _, eps := range epss {
ret += len(subset.Addresses) for _, ep := range eps.Endpoints {
ret += len(ep.Addresses)
}
} }
return &ret, nil return &ret, nil
} }
// GetEndpointsByName returns the endpoints for the service with the given name // GetEndpointSlicesByName returns the endpointSlices for the service with the given name
func GetEndpointsByName(flags *genericclioptions.ConfigFlags, namespace string, name string) (*apiv1.Endpoints, error) { func GetEndpointSlicesByName(flags *genericclioptions.ConfigFlags, namespace string, name string) ([]discoveryv1.EndpointSlice, error) {
allEndpoints, err := getEndpoints(flags, namespace) allEndpointsSlices, err := getEndpointSlices(flags, namespace)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var eps []discoveryv1.EndpointSlice
for _, endpoints := range allEndpoints { for _, slice := range allEndpointsSlices {
if endpoints.Name == name { if svcName, ok := slice.ObjectMeta.GetLabels()[discoveryv1.LabelServiceName]; ok {
return &endpoints, nil if svcName == name {
eps = append(eps, slice)
}
} }
} }
return nil, nil return eps, nil
} }
var endpointsCache = make(map[string]*[]apiv1.Endpoints) var endpointSlicesCache = make(map[string]*[]discoveryv1.EndpointSlice)
func getEndpointSlices(flags *genericclioptions.ConfigFlags, namespace string) ([]discoveryv1.EndpointSlice, error) {
cachedEndpointSlices, ok := endpointSlicesCache[namespace]
func getEndpoints(flags *genericclioptions.ConfigFlags, namespace string) ([]apiv1.Endpoints, error) {
cachedEndpoints, ok := endpointsCache[namespace]
if ok { if ok {
return *cachedEndpoints, nil return *cachedEndpointSlices, nil
} }
if namespace != "" { if namespace != "" {
tryAllNamespacesEndpointsCache(flags) tryAllNamespacesEndpointSlicesCache(flags)
} }
cachedEndpoints = tryFilteringEndpointsFromAllNamespacesCache(flags, namespace) cachedEndpointSlices = tryFilteringEndpointSlicesFromAllNamespacesCache(flags, namespace)
if cachedEndpoints != nil {
return *cachedEndpoints, nil if cachedEndpointSlices != nil {
return *cachedEndpointSlices, nil
} }
rawConfig, err := flags.ToRESTConfig() rawConfig, err := flags.ToRESTConfig()
@ -185,42 +193,41 @@ func getEndpoints(flags *genericclioptions.ConfigFlags, namespace string) ([]api
return nil, err return nil, err
} }
api, err := corev1.NewForConfig(rawConfig) api, err := discoveryv1client.NewForConfig(rawConfig)
if err != nil { if err != nil {
return nil, err return nil, err
} }
endpointSlicesList, err := api.EndpointSlices(namespace).List(context.TODO(), metav1.ListOptions{})
endpointsList, err := api.Endpoints(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil { if err != nil {
return nil, err return nil, err
} }
endpoints := endpointsList.Items endpointSlices := endpointSlicesList.Items
endpointsCache[namespace] = &endpoints endpointSlicesCache[namespace] = &endpointSlices
return endpoints, nil return endpointSlices, nil
} }
func tryAllNamespacesEndpointsCache(flags *genericclioptions.ConfigFlags) { func tryAllNamespacesEndpointSlicesCache(flags *genericclioptions.ConfigFlags) {
_, ok := endpointsCache[""] _, ok := endpointSlicesCache[""]
if !ok { if !ok {
_, err := getEndpoints(flags, "") _, err := getEndpointSlices(flags, "")
if err != nil { if err != nil {
endpointsCache[""] = nil endpointSlicesCache[""] = nil
} }
} }
} }
func tryFilteringEndpointsFromAllNamespacesCache(flags *genericclioptions.ConfigFlags, namespace string) *[]apiv1.Endpoints { func tryFilteringEndpointSlicesFromAllNamespacesCache(flags *genericclioptions.ConfigFlags, namespace string) *[]discoveryv1.EndpointSlice {
allEndpoints := endpointsCache[""] allEndpointSlices := endpointSlicesCache[""]
if allEndpoints != nil { if allEndpointSlices != nil {
endpoints := make([]apiv1.Endpoints, 0) endpointSlices := make([]discoveryv1.EndpointSlice, 0)
for _, thisEndpoints := range *allEndpoints { for _, slice := range *allEndpointSlices {
if thisEndpoints.Namespace == namespace { if slice.Namespace == namespace {
endpoints = append(endpoints, thisEndpoints) endpointSlices = append(endpointSlices, slice)
} }
} }
endpointsCache[namespace] = &endpoints endpointSlicesCache[namespace] = &endpointSlices
return &endpoints return &endpointSlices
} }
return nil return nil
} }