forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
netid.go
86 lines (73 loc) · 2.63 KB
/
netid.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 api
// Accessor methods to annotate NetNamespace for multitenant support
import (
"fmt"
"strings"
)
type PodNetworkAction string
const (
// Maximum VXLAN Virtual Network Identifier(VNID) as per RFC#7348
MaxVNID = uint32((1 << 24) - 1)
// VNID: 1 to 9 are internally reserved for any special cases in the future
MinVNID = uint32(10)
// VNID: 0 reserved for default namespace and can reach any network in the cluster
GlobalVNID = uint32(0)
// ChangePodNetworkAnnotation is an annotation on NetNamespace to request change of pod network
ChangePodNetworkAnnotation string = "pod.network.openshift.io/multitenant.change-network"
// Acceptable values for ChangePodNetworkAnnotation
GlobalPodNetwork PodNetworkAction = "global"
JoinPodNetwork PodNetworkAction = "join"
IsolatePodNetwork PodNetworkAction = "isolate"
)
var (
ErrorPodNetworkAnnotationNotFound = fmt.Errorf("ChangePodNetworkAnnotation not found")
)
// Check if the given vnid is valid or not
func ValidVNID(vnid uint32) error {
if vnid == GlobalVNID {
return nil
}
if vnid < MinVNID {
return fmt.Errorf("VNID must be greater than or equal to %d", MinVNID)
}
if vnid > MaxVNID {
return fmt.Errorf("VNID must be less than or equal to %d", MaxVNID)
}
return nil
}
// GetChangePodNetworkAnnotation fetches network change intent from NetNamespace
func GetChangePodNetworkAnnotation(netns *NetNamespace) (PodNetworkAction, string, error) {
value, ok := netns.Annotations[ChangePodNetworkAnnotation]
if !ok {
return PodNetworkAction(""), "", ErrorPodNetworkAnnotationNotFound
}
args := strings.Split(value, ":")
switch PodNetworkAction(args[0]) {
case GlobalPodNetwork:
return GlobalPodNetwork, "", nil
case JoinPodNetwork:
if len(args) != 2 {
return PodNetworkAction(""), "", fmt.Errorf("invalid namespace for join pod network: %s", value)
}
namespace := args[1]
return JoinPodNetwork, namespace, nil
case IsolatePodNetwork:
return IsolatePodNetwork, "", nil
}
return PodNetworkAction(""), "", fmt.Errorf("invalid ChangePodNetworkAnnotation: %s", value)
}
// SetChangePodNetworkAnnotation sets network change intent on NetNamespace
func SetChangePodNetworkAnnotation(netns *NetNamespace, action PodNetworkAction, params string) {
if netns.Annotations == nil {
netns.Annotations = make(map[string]string)
}
value := string(action)
if len(params) != 0 {
value = fmt.Sprintf("%s:%s", value, params)
}
netns.Annotations[ChangePodNetworkAnnotation] = value
}
// DeleteChangePodNetworkAnnotation removes network change intent from NetNamespace
func DeleteChangePodNetworkAnnotation(netns *NetNamespace) {
delete(netns.Annotations, ChangePodNetworkAnnotation)
}