-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
egress_network_policy.go
118 lines (93 loc) · 3.2 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package plugin
import (
"fmt"
"github.com/golang/glog"
osapi "github.com/openshift/origin/pkg/sdn/apis/network"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilwait "k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/cache"
)
func (plugin *OsdnNode) SetupEgressNetworkPolicy() error {
policies, err := plugin.osClient.EgressNetworkPolicies(metav1.NamespaceAll).List(metav1.ListOptions{})
if err != nil {
return fmt.Errorf("could not get EgressNetworkPolicies: %s", err)
}
plugin.egressPoliciesLock.Lock()
defer plugin.egressPoliciesLock.Unlock()
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)
plugin.egressDNS.Add(policy)
}
for vnid := range plugin.egressPolicies {
plugin.updateEgressNetworkPolicyRules(vnid)
}
go utilwait.Forever(plugin.syncEgressDNSPolicyRules, 0)
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)
}
plugin.egressPoliciesLock.Lock()
defer plugin.egressPoliciesLock.Unlock()
policies := plugin.egressPolicies[vnid]
for i, oldPolicy := range policies {
if oldPolicy.UID == policy.UID {
policies = append(policies[:i], policies[i+1:]...)
break
}
}
plugin.egressDNS.Delete(*policy)
if delta.Type != cache.Deleted && len(policy.Spec.Egress) > 0 {
policies = append(policies, *policy)
plugin.egressDNS.Add(*policy)
}
plugin.egressPolicies[vnid] = policies
plugin.updateEgressNetworkPolicyRules(vnid)
return nil
})
}
func (plugin *OsdnNode) UpdateEgressNetworkPolicyVNID(namespace string, oldVnid, newVnid uint32) {
var policy *osapi.EgressNetworkPolicy
plugin.egressPoliciesLock.Lock()
defer plugin.egressPoliciesLock.Unlock()
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)
}
}
func (plugin *OsdnNode) syncEgressDNSPolicyRules() {
go utilwait.Forever(plugin.egressDNS.Sync, 0)
for {
policyUpdates := <-plugin.egressDNS.updates
glog.V(5).Infof("Egress dns sync: updating policy: %v", policyUpdates.UID)
vnid, err := plugin.policy.GetVNID(policyUpdates.Namespace)
if err != nil {
glog.Warningf("Could not find netid for namespace %q: %v", policyUpdates.Namespace, err)
continue
}
func() {
plugin.egressPoliciesLock.Lock()
defer plugin.egressPoliciesLock.Unlock()
plugin.updateEgressNetworkPolicyRules(vnid)
}()
}
}