-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
egress_network_policy.go
81 lines (66 loc) · 2.24 KB
/
egress_network_policy.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
package plugin
import (
"fmt"
"github.com/golang/glog"
osapi "github.com/openshift/origin/pkg/sdn/api"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/client/cache"
utilwait "k8s.io/kubernetes/pkg/util/wait"
)
func (plugin *OsdnNode) SetupEgressNetworkPolicy() error {
policies, err := plugin.osClient.EgressNetworkPolicies(kapi.NamespaceAll).List(kapi.ListOptions{})
if err != nil {
return fmt.Errorf("could not get EgressNetworkPolicies: %s", err)
}
for _, policy := range policies.Items {
vnid, err := plugin.policy.GetVNID(policy.Namespace)
if err != nil {
glog.Warningf("Could not find netid for namespace %q: %v", policy.Namespace, err)
continue
}
plugin.egressPolicies[vnid] = append(plugin.egressPolicies[vnid], policy)
}
for vnid := range plugin.egressPolicies {
plugin.updateEgressNetworkPolicyRules(vnid)
}
go utilwait.Forever(plugin.watchEgressNetworkPolicies, 0)
return nil
}
func (plugin *OsdnNode) watchEgressNetworkPolicies() {
RunEventQueue(plugin.osClient, EgressNetworkPolicies, func(delta cache.Delta) error {
policy := delta.Object.(*osapi.EgressNetworkPolicy)
vnid, err := plugin.policy.GetVNID(policy.Namespace)
if err != nil {
return fmt.Errorf("Could not find netid for namespace %q: %v", policy.Namespace, err)
}
policies := plugin.egressPolicies[vnid]
for i, oldPolicy := range policies {
if oldPolicy.UID == policy.UID {
policies = append(policies[:i], policies[i+1:]...)
break
}
}
if delta.Type != cache.Deleted && len(policy.Spec.Egress) > 0 {
policies = append(policies, *policy)
}
plugin.egressPolicies[vnid] = policies
plugin.updateEgressNetworkPolicyRules(vnid)
return nil
})
}
func (plugin *OsdnNode) UpdateEgressNetworkPolicyVNID(namespace string, oldVnid, newVnid uint32) {
var policy *osapi.EgressNetworkPolicy
policies := plugin.egressPolicies[oldVnid]
for i, oldPolicy := range policies {
if oldPolicy.Namespace == namespace {
policy = &oldPolicy
plugin.egressPolicies[oldVnid] = append(policies[:i], policies[i+1:]...)
plugin.updateEgressNetworkPolicyRules(oldVnid)
break
}
}
if policy != nil {
plugin.egressPolicies[newVnid] = append(plugin.egressPolicies[newVnid], *policy)
plugin.updateEgressNetworkPolicyRules(newVnid)
}
}