-
Notifications
You must be signed in to change notification settings - Fork 233
/
types.go
77 lines (68 loc) · 2.46 KB
/
types.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
package controlplane
import (
dp "github.com/Azure/azure-container-networking/npm/pkg/dataplane"
"github.com/Azure/azure-container-networking/npm/pkg/dataplane/ipsets"
)
const (
IpsetApply string = "IPSETAPPLY"
IpsetRemove string = "IPSETREMOVE"
PolicyApply string = "POLICYAPPLY"
PolicyRemove string = "POLICYREMOVE"
ListReference string = "LISTREFERENCE"
PolicyReference string = "POLICYREFERENCE"
)
// ControllerIPSets is used in fan-out design for controller pod to calculate
// and push to daemon pod
type ControllerIPSets struct {
*ipsets.IPSetMetadata
// IPPodMetadata is used for setMaps to store Ips and ports as keys
// and podMetadata as value
IPPodMetadata map[string]*dp.PodMetadata
// MemberIPSets is used for listMaps to store child IP Sets
MemberIPSets map[string]*ipsets.IPSetMetadata
// ipsetReference keeps track of how many lists in the cache refer to this ipset
ipsetReference map[string]struct{}
// NetPolReference holds networkpolicy names where this IPSet
// is being referred as part of rules
// NetpolReference is not used currently, depending on testing we may decide to keep it
// or delete it
NetPolReference map[string]struct{}
}
func NewControllerIPSets(metadata *ipsets.IPSetMetadata) *ControllerIPSets {
return &ControllerIPSets{
IPSetMetadata: metadata,
IPPodMetadata: make(map[string]*dp.PodMetadata),
MemberIPSets: make(map[string]*ipsets.IPSetMetadata),
ipsetReference: make(map[string]struct{}),
NetPolReference: make(map[string]struct{}),
}
}
// GetMetadata returns the metadata of the ipset
func (c *ControllerIPSets) GetMetadata() *ipsets.IPSetMetadata {
return c.IPSetMetadata
}
// HasReferences checks if an ipset has references
func (c *ControllerIPSets) HasReferences() bool {
return len(c.ipsetReference) > 0 || len(c.NetPolReference) > 0
}
// CanDelete checks for references and members
func (c *ControllerIPSets) CanDelete() bool {
return c.HasReferences() &&
(len(c.IPPodMetadata) > 0 || len(c.MemberIPSets) > 0)
}
func (c *ControllerIPSets) AddReference(referenceName, referenceType string) {
switch referenceType {
case ListReference:
c.ipsetReference[referenceName] = struct{}{}
case PolicyReference:
c.NetPolReference[referenceName] = struct{}{}
}
}
func (c *ControllerIPSets) DeleteReference(referenceName, referenceType string) {
switch referenceType {
case ListReference:
delete(c.ipsetReference, referenceName)
case PolicyReference:
delete(c.NetPolReference, referenceName)
}
}