-
Notifications
You must be signed in to change notification settings - Fork 346
/
helper.go
100 lines (86 loc) · 2.66 KB
/
helper.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
Copyright 2021 Antrea 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 common
import (
"crypto/sha1" // #nosec G505: not used for security purposes
"encoding/hex"
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/wait"
)
const labelIdentityHashLength = 16
// CleanUpRetry is the retry when the clean up method
// failed to clean up all stale resources.
var CleanUpRetry = wait.Backoff{
Steps: 12,
Duration: 500 * time.Millisecond,
Factor: 2.0,
Jitter: 1,
}
// TODO: Use NamespacedName stringer method instead of this. e.g. nsName.String()
func NamespacedName(namespace, name string) string {
return namespace + "/" + name
}
func ToMCResourceName(originalResourceName string) string {
return AntreaMCSPrefix + originalResourceName
}
func StringExistsInSlice(slice []string, s string) bool {
for _, item := range slice {
if item == s {
return true
}
}
return false
}
func RemoveStringFromSlice(slice []string, s string) (result []string) {
for _, item := range slice {
if item == s {
continue
}
result = append(result, item)
}
return
}
func GetServiceEndpointSubset(svc *corev1.Service) corev1.EndpointSubset {
var epSubset corev1.EndpointSubset
for _, ip := range svc.Spec.ClusterIPs {
epSubset.Addresses = append(epSubset.Addresses, corev1.EndpointAddress{IP: ip})
}
epSubset.Ports = GetServiceEndpointPorts(svc.Spec.Ports)
return epSubset
}
// GetServiceEndpointPorts converts Service's port to EndpointPort
func GetServiceEndpointPorts(ports []corev1.ServicePort) []corev1.EndpointPort {
if len(ports) == 0 {
return nil
}
var epPorts []corev1.EndpointPort
for _, p := range ports {
epPorts = append(epPorts, corev1.EndpointPort{
Name: p.Name,
Port: p.Port,
Protocol: p.Protocol,
})
}
return epPorts
}
// HashLabelIdentity generates a hash value for label identity string.
func HashLabelIdentity(l string) string {
hash := sha1.New() // #nosec G401: not used for security purposes
hash.Write([]byte(l))
hashValue := hex.EncodeToString(hash.Sum(nil))
return hashValue[:labelIdentityHashLength]
}
func IsMulticlusterService(service *corev1.Service) bool {
return service.Annotations[AntreaMCServiceAnnotation] == "true"
}