2022-09-23 19:38:04 +00:00
|
|
|
/*
|
|
|
|
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"
|
|
|
|
|
|
|
|
discoveryv1 "k8s.io/api/discovery/v1"
|
|
|
|
"k8s.io/client-go/tools/cache"
|
|
|
|
)
|
|
|
|
|
2025-01-10 15:28:46 +00:00
|
|
|
type getEpssForServiceFunc = func(key string) ([]*discoveryv1.EndpointSlice, error)
|
2024-04-30 11:27:51 +00:00
|
|
|
|
2022-09-23 19:38:04 +00:00
|
|
|
// EndpointSliceLister makes a Store that lists Endpoints.
|
|
|
|
type EndpointSliceLister struct {
|
|
|
|
cache.Store
|
2025-01-10 15:28:46 +00:00
|
|
|
endpointSliceIndex getEpssForServiceFunc
|
2022-09-23 19:38:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MatchByKey returns the EndpointsSlices of the Service matching key in the local Endpoint Store.
|
|
|
|
func (s *EndpointSliceLister) MatchByKey(key string) ([]*discoveryv1.EndpointSlice, error) {
|
2024-04-30 11:27:51 +00:00
|
|
|
epss, err := s.endpointSliceIndex(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-11-05 21:22:15 +00:00
|
|
|
}
|
2024-04-30 11:27:51 +00:00
|
|
|
|
|
|
|
if len(epss) == 0 {
|
|
|
|
return nil, NotExistsError(key)
|
|
|
|
}
|
|
|
|
return epss, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func epssIndexer() cache.Indexers {
|
|
|
|
return cache.Indexers{
|
|
|
|
discoveryv1.LabelServiceName: func(obj interface{}) ([]string, error) {
|
|
|
|
eps, ok := obj.(*discoveryv1.EndpointSlice)
|
|
|
|
if !ok {
|
|
|
|
// Skip object as it is not an endpointslice
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
parentService, ok := eps.Labels[discoveryv1.LabelServiceName]
|
|
|
|
if !ok {
|
|
|
|
// There is no parent service and thus we cannot match this endpointslice to any service
|
|
|
|
// As far as i'm aware, this is only possible if you create epps objects by hand
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
key := fmt.Sprintf("%s/%s", eps.Namespace, parentService)
|
|
|
|
|
|
|
|
return []string{key}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-10 15:28:46 +00:00
|
|
|
func epssForServiceFuncFromIndexer(indexer cache.Indexer) getEppsForServiceFunc {
|
2024-04-30 11:27:51 +00:00
|
|
|
return func(key string) ([]*discoveryv1.EndpointSlice, error) {
|
|
|
|
objs, err := indexer.ByIndex(discoveryv1.LabelServiceName, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-09-23 19:38:04 +00:00
|
|
|
}
|
2024-04-30 11:27:51 +00:00
|
|
|
|
|
|
|
epss := make([]*discoveryv1.EndpointSlice, 0, len(objs))
|
|
|
|
for _, obj := range objs {
|
|
|
|
eps, ok := obj.(*discoveryv1.EndpointSlice)
|
|
|
|
if !ok {
|
|
|
|
continue
|
2022-09-23 19:38:04 +00:00
|
|
|
}
|
2024-04-30 11:27:51 +00:00
|
|
|
|
|
|
|
epss = append(epss, eps)
|
2022-09-23 19:38:04 +00:00
|
|
|
}
|
2024-04-30 11:27:51 +00:00
|
|
|
|
|
|
|
return epss, nil
|
2022-09-23 19:38:04 +00:00
|
|
|
}
|
|
|
|
}
|