forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
egress_network_policy.go
95 lines (79 loc) · 2.43 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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.vnids.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 {
err := plugin.updateEgressNetworkPolicyRules(vnid)
if err != nil {
return err
}
}
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.vnids.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
err = plugin.updateEgressNetworkPolicyRules(vnid)
if err != nil {
return err
}
return nil
})
}
func (plugin *OsdnNode) UpdateEgressNetworkPolicyVNID(namespace string, oldVnid, newVnid uint32) error {
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:]...)
err := plugin.updateEgressNetworkPolicyRules(oldVnid)
if err != nil {
return err
}
break
}
}
if policy != nil {
plugin.egressPolicies[newVnid] = append(plugin.egressPolicies[newVnid], policy)
err := plugin.updateEgressNetworkPolicyRules(newVnid)
if err != nil {
return err
}
}
return nil
}