-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
86 lines (72 loc) · 2.43 KB
/
utils.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"fmt"
v1 "k8s.io/api/core/v1"
discoveryv1 "k8s.io/api/discovery/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"github.com/utilitywarehouse/semaphore-service-mirror/log"
)
const (
// Separator is inserted between the namespace and name in the mirror
// name to prevent clashes
Separator = "73736d"
)
var (
// DefaultLocalEndpointZones holds the configured availability zones for
// the local cluster
DefaultLocalEndpointZones []discoveryv1.ForZone
)
// generateMirrorName generates a name for mirrored objects based on the name
// and namespace of the remote object: <prefix>-<namespace>-73736d-<name>
func generateMirrorName(prefix, namespace, name string) string {
return fmt.Sprintf("%s-%s-%s-%s", prefix, namespace, Separator, name)
}
// generateGlobalServiceName generates a name for mirrored objects based on the
// name and namespace of the remote object: gl-<namespace>-73736d-<name>
func generateGlobalServiceName(name, namespace string) string {
return fmt.Sprintf("gl-%s-%s-%s", namespace, Separator, name)
}
// generateGlobalEndpointSliceName just prefixes the name with `gl-`, and relies
// on kubernetes suffixes for endpointslices to not collide.
func generateGlobalEndpointSliceName(name string) string {
return fmt.Sprintf("gl-%s", name)
}
func generateEndpointSliceLabels(baseLabels map[string]string, targetService string) map[string]string {
labels := baseLabels
labels["kubernetes.io/service-name"] = targetService
labels["endpointslice.kubernetes.io/managed-by"] = "semaphore-service-mirror"
return labels
}
func setLocalEndpointZones(zones []string) {
for _, z := range zones {
DefaultLocalEndpointZones = append(DefaultLocalEndpointZones, discoveryv1.ForZone{Name: z})
}
}
func inSlice(slice []string, val string) (int, bool) {
for i, item := range slice {
if item == val {
return i, true
}
}
return -1, false
}
func removeFromSlice(slice []string, i int) []string {
slice[len(slice)-1], slice[i] = slice[i], slice[len(slice)-1]
return slice[:len(slice)-1]
}
func isHeadless(svc *v1.Service) bool {
if svc.Spec.ClusterIP == "None" {
return true
}
return false
}
func matchSelector(selector labels.Selector, obj runtime.Object) bool {
metadata, err := meta.Accessor(obj)
if err != nil {
log.Logger.Error("creating object accessor", "err", err)
return false
}
return selector.Matches(labels.Set(metadata.GetLabels()))
}