-
Notifications
You must be signed in to change notification settings - Fork 236
/
cache.go
100 lines (87 loc) · 2.21 KB
/
cache.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
package common
import (
"errors"
"net"
"github.com/Azure/azure-container-networking/npm/util"
)
// error type
var (
ErrSetNotExist = errors.New("set does not exists")
ErrInvalidIPAddress = errors.New("invalid ipaddress, no equivalent pod found")
ErrInvalidInput = errors.New("invalid input")
ErrSetType = errors.New("invalid set type")
)
type Input struct {
Content string
Type InputType
}
// InputType indicates allowed typle for source and destination input
type InputType int32
// GetInputType returns the type of the input for GetNetworkTuple.
func GetInputType(input string) InputType {
if input == "External" {
return EXTERNAL
} else if ip := net.ParseIP(input); ip != nil {
return IPADDRS
} else {
return NSPODNAME
}
}
const (
// IPADDRS indicates the IP Address input type, example: 10.0.0.1
IPADDRS InputType = 0
// NSPODNAME indicates the podname input type, example: x/a
NSPODNAME InputType = 1
// EXTERNAL indicates the external input type
EXTERNAL InputType = 2
)
type GenericCache interface {
GetPod(*Input) (*NpmPod, error)
GetNamespaceLabel(namespace string, key string) string
GetListMap() map[string]string
GetSetMap() map[string]string
}
type Cache struct {
NodeName string
NsMap map[string]*Namespace
PodMap map[string]*NpmPod
SetMap map[string]string
ListMap map[string]string // not used in NPMV2
}
func (c *Cache) GetPod(input *Input) (*NpmPod, error) {
switch input.Type {
case NSPODNAME:
if pod, ok := c.PodMap[input.Content]; ok {
return pod, nil
}
return nil, ErrInvalidInput
case IPADDRS:
for _, pod := range c.PodMap {
if pod.PodIP == input.Content {
return pod, nil
}
}
return nil, ErrInvalidIPAddress
case EXTERNAL:
return &NpmPod{}, nil
default:
return nil, ErrInvalidInput
}
}
func (c *Cache) GetNamespaceLabel(namespace, labelkey string) string {
if _, ok := c.NsMap[namespace]; ok {
return c.NsMap[namespace].LabelsMap[labelkey]
}
return ""
}
func (c *Cache) GetSetMap() map[string]string {
return c.SetMap
}
func (c *Cache) GetListMap() map[string]string {
listMap := make(map[string]string)
for k := range c.ListMap {
hashedName := util.GetHashedName(k)
listMap[hashedName] = k
}
return listMap
}