123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- package endpointslice
- import (
- "sync"
- discovery "k8s.io/api/discovery/v1beta1"
- "k8s.io/apimachinery/pkg/types"
- )
- type endpointSliceResourceVersions map[string]string
- type endpointSliceTracker struct {
-
- lock sync.Mutex
-
-
- resourceVersionsByService map[types.NamespacedName]endpointSliceResourceVersions
- }
- func newEndpointSliceTracker() *endpointSliceTracker {
- return &endpointSliceTracker{
- resourceVersionsByService: map[types.NamespacedName]endpointSliceResourceVersions{},
- }
- }
- func (est *endpointSliceTracker) Has(endpointSlice *discovery.EndpointSlice) bool {
- est.lock.Lock()
- defer est.lock.Unlock()
- rrv := est.relatedResourceVersions(endpointSlice)
- _, ok := rrv[endpointSlice.Name]
- return ok
- }
- func (est *endpointSliceTracker) Stale(endpointSlice *discovery.EndpointSlice) bool {
- est.lock.Lock()
- defer est.lock.Unlock()
- rrv := est.relatedResourceVersions(endpointSlice)
- return rrv[endpointSlice.Name] != endpointSlice.ResourceVersion
- }
- func (est *endpointSliceTracker) Update(endpointSlice *discovery.EndpointSlice) {
- est.lock.Lock()
- defer est.lock.Unlock()
- rrv := est.relatedResourceVersions(endpointSlice)
- rrv[endpointSlice.Name] = endpointSlice.ResourceVersion
- }
- func (est *endpointSliceTracker) Delete(endpointSlice *discovery.EndpointSlice) {
- est.lock.Lock()
- defer est.lock.Unlock()
- rrv := est.relatedResourceVersions(endpointSlice)
- delete(rrv, endpointSlice.Name)
- }
- func (est *endpointSliceTracker) relatedResourceVersions(endpointSlice *discovery.EndpointSlice) endpointSliceResourceVersions {
- serviceNN := getServiceNN(endpointSlice)
- vers, ok := est.resourceVersionsByService[serviceNN]
- if !ok {
- vers = endpointSliceResourceVersions{}
- est.resourceVersionsByService[serviceNN] = vers
- }
- return vers
- }
- func getServiceNN(endpointSlice *discovery.EndpointSlice) types.NamespacedName {
- serviceName, _ := endpointSlice.Labels[discovery.LabelServiceName]
- return types.NamespacedName{Name: serviceName, Namespace: endpointSlice.Namespace}
- }
- func managedByChanged(endpointSlice1, endpointSlice2 *discovery.EndpointSlice) bool {
- return managedByController(endpointSlice1) != managedByController(endpointSlice2)
- }
- func managedByController(endpointSlice *discovery.EndpointSlice) bool {
- managedBy, _ := endpointSlice.Labels[discovery.LabelManagedBy]
- return managedBy == controllerName
- }
|