feat: switch from endpoints to endpointslices (#8890)

* endpointslices

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

* cleanup

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

* fix rbac

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

* fix comments

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

* cleanup store, add store tests

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

* fix copyright date

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

Signed-off-by: tombokombo <tombo@sysart.tech>
This commit is contained in:
Tomas Hulata 2022-09-23 21:38:04 +02:00 committed by GitHub
parent 0f5bf530ae
commit 3579ed0487
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 520 additions and 301 deletions

View file

@ -0,0 +1,55 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package store
import (
"fmt"
"strings"
discoveryv1 "k8s.io/api/discovery/v1"
"k8s.io/client-go/tools/cache"
)
// EndpointSliceLister makes a Store that lists Endpoints.
type EndpointSliceLister struct {
cache.Store
}
// MatchByKey returns the EndpointsSlices of the Service matching key in the local Endpoint Store.
func (s *EndpointSliceLister) MatchByKey(key string) ([]*discoveryv1.EndpointSlice, error) {
var eps []*discoveryv1.EndpointSlice
// filter endpointSlices owned by svc
for _, listKey := range s.ListKeys() {
if !strings.HasPrefix(listKey, key) {
continue
}
epss, exists, err := s.GetByKey(listKey)
if exists && err == nil {
// check for svc owner label
if svcName, ok := epss.(*discoveryv1.EndpointSlice).ObjectMeta.GetLabels()[discoveryv1.LabelServiceName]; ok {
namespace := epss.(*discoveryv1.EndpointSlice).ObjectMeta.GetNamespace()
if key == fmt.Sprintf("%s/%s", namespace, svcName) {
eps = append(eps, epss.(*discoveryv1.EndpointSlice))
}
}
}
}
if len(eps) == 0 {
return nil, NotExistsError(key)
}
return eps, nil
}