forked from metallb/metallb
/
endpoint_slices.go
71 lines (59 loc) · 1.73 KB
/
endpoint_slices.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// SPDX-License-Identifier:Apache-2.0
package epslices
import (
"fmt"
v1 "k8s.io/api/core/v1"
discovery "k8s.io/api/discovery/v1"
"k8s.io/apimachinery/pkg/types"
)
type EpsOrSlices struct {
Type EpsOrSliceType
EpVal *v1.Endpoints
SlicesVal []discovery.EndpointSlice
}
type EpsOrSliceType int
const (
Unknown EpsOrSliceType = iota
Eps
Slices
)
const SlicesServiceIndexName = "ServiceName"
// IsConditionServing tells if the conditions represent a serving state, deferring
// to ready state if serving == nil.
func IsConditionServing(conditions discovery.EndpointConditions) bool {
if conditions.Serving == nil {
if conditions.Ready == nil {
return true
}
return *conditions.Ready
}
return *conditions.Serving
}
func ServiceKeyForSlice(endpointSlice *discovery.EndpointSlice) (types.NamespacedName, error) {
if endpointSlice == nil {
return types.NamespacedName{}, fmt.Errorf("nil EndpointSlice")
}
serviceName, err := serviceNameForSlice(endpointSlice)
if err != nil {
return types.NamespacedName{}, err
}
return types.NamespacedName{Namespace: endpointSlice.Namespace, Name: serviceName}, nil
}
func SlicesServiceIndex(obj interface{}) ([]string, error) {
endpointSlice, ok := obj.(*discovery.EndpointSlice)
if !ok {
return nil, fmt.Errorf("passed object is not a slice")
}
serviceKey, err := ServiceKeyForSlice(endpointSlice)
if err != nil {
return nil, err
}
return []string{serviceKey.String()}, nil
}
func serviceNameForSlice(endpointSlice *discovery.EndpointSlice) (string, error) {
serviceName, ok := endpointSlice.Labels[discovery.LabelServiceName]
if !ok || serviceName == "" {
return "", fmt.Errorf("endpointSlice missing the %s label", discovery.LabelServiceName)
}
return serviceName, nil
}