forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.go
163 lines (140 loc) · 5.25 KB
/
common.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package plugin
import (
"fmt"
"net"
"strings"
"time"
"github.com/golang/glog"
osclient "github.com/openshift/origin/pkg/client"
osapi "github.com/openshift/origin/pkg/sdn/api"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/apis/extensions"
kcache "k8s.io/kubernetes/pkg/client/cache"
"k8s.io/kubernetes/pkg/fields"
kcontainer "k8s.io/kubernetes/pkg/kubelet/container"
)
func getPodContainerID(pod *kapi.Pod) string {
if len(pod.Status.ContainerStatuses) > 0 {
return kcontainer.ParseContainerID(pod.Status.ContainerStatuses[0].ContainerID).ID
}
return ""
}
func hostSubnetToString(subnet *osapi.HostSubnet) string {
return fmt.Sprintf("%s (host: %q, ip: %q, subnet: %q)", subnet.Name, subnet.Host, subnet.HostIP, subnet.Subnet)
}
func clusterNetworkToString(n *osapi.ClusterNetwork) string {
return fmt.Sprintf("%s (network: %q, hostSubnetBits: %d, serviceNetwork: %q, pluginName: %q)", n.Name, n.Network, n.HostSubnetLength, n.ServiceNetwork, n.PluginName)
}
type NetworkInfo struct {
ClusterNetwork *net.IPNet
ServiceNetwork *net.IPNet
}
func parseNetworkInfo(clusterNetwork string, serviceNetwork string) (*NetworkInfo, error) {
_, cn, err := net.ParseCIDR(clusterNetwork)
if err != nil {
return nil, fmt.Errorf("failed to parse ClusterNetwork CIDR %s: %v", clusterNetwork, err)
}
_, sn, err := net.ParseCIDR(serviceNetwork)
if err != nil {
return nil, fmt.Errorf("failed to parse ServiceNetwork CIDR %s: %v", serviceNetwork, err)
}
return &NetworkInfo{
ClusterNetwork: cn,
ServiceNetwork: sn,
}, nil
}
func (ni *NetworkInfo) validateNodeIP(nodeIP string) error {
if nodeIP == "" || nodeIP == "127.0.0.1" {
return fmt.Errorf("invalid node IP %q", nodeIP)
}
// Ensure each node's NodeIP is not contained by the cluster network,
// which could cause a routing loop. (rhbz#1295486)
ipaddr := net.ParseIP(nodeIP)
if ipaddr == nil {
return fmt.Errorf("failed to parse node IP %s", nodeIP)
}
if ni.ClusterNetwork.Contains(ipaddr) {
return fmt.Errorf("node IP %s conflicts with cluster network %s", nodeIP, ni.ClusterNetwork.String())
}
if ni.ServiceNetwork.Contains(ipaddr) {
return fmt.Errorf("node IP %s conflicts with service network %s", nodeIP, ni.ServiceNetwork.String())
}
return nil
}
func getNetworkInfo(osClient *osclient.Client) (*NetworkInfo, error) {
cn, err := osClient.ClusterNetwork().Get(osapi.ClusterNetworkDefault)
if err != nil {
return nil, err
}
return parseNetworkInfo(cn.Network, cn.ServiceNetwork)
}
type ResourceName string
const (
Nodes ResourceName = "Nodes"
Namespaces ResourceName = "Namespaces"
NetNamespaces ResourceName = "NetNamespaces"
Services ResourceName = "Services"
HostSubnets ResourceName = "HostSubnets"
Pods ResourceName = "Pods"
EgressNetworkPolicies ResourceName = "EgressNetworkPolicies"
NetworkPolicies ResourceName = "NetworkPolicies"
)
func newEventQueue(client kcache.Getter, resourceName ResourceName, expectedType interface{}, namespace string) *EventQueue {
rn := strings.ToLower(string(resourceName))
lw := kcache.NewListWatchFromClient(client, rn, namespace, fields.Everything())
eventQueue := NewEventQueue(DeletionHandlingMetaNamespaceKeyFunc)
// Repopulate event queue every 30 mins
// Existing items in the event queue will have watch.Modified event type
kcache.NewReflector(lw, expectedType, eventQueue, 30*time.Minute).Run()
return eventQueue
}
// Run event queue for the given resource. The 'process' function is called
// repeatedly with each available cache.Delta that describes state changes
// to an object. If the process function returns an error queued changes
// for that object are dropped but processing continues with the next available
// object's cache.Deltas. The error is logged with call stack information.
//
// NOTE: this function will handle DeletedFinalStateUnknown delta objects
// automatically, which may not always be what you want since the now-deleted
// object may be stale.
func RunEventQueue(client kcache.Getter, resourceName ResourceName, process ProcessEventFunc) {
var expectedType interface{}
switch resourceName {
case HostSubnets:
expectedType = &osapi.HostSubnet{}
case NetNamespaces:
expectedType = &osapi.NetNamespace{}
case Nodes:
expectedType = &kapi.Node{}
case Namespaces:
expectedType = &kapi.Namespace{}
case Services:
expectedType = &kapi.Service{}
case Pods:
expectedType = &kapi.Pod{}
case EgressNetworkPolicies:
expectedType = &osapi.EgressNetworkPolicy{}
case NetworkPolicies:
expectedType = &extensions.NetworkPolicy{}
default:
glog.Fatalf("Unknown resource %s during initialization of event queue", resourceName)
}
eventQueue := newEventQueue(client, resourceName, expectedType, kapi.NamespaceAll)
for {
eventQueue.Pop(process, expectedType)
}
}
func RunNamespacedPodEventQueue(client kcache.Getter, namespace string, closeChan chan struct{}, process ProcessEventFunc) {
eventQueue := newEventQueue(client, Pods, &kapi.Pod{}, namespace)
// Loop calling eventQueue.Pop() until closeChan is closed. process() will be called
// once after closeChan is closed; this possibility is unavoidable anyway due to race
// conditions.
for {
select {
case <-closeChan:
return
default:
eventQueue.Pop(process, &kapi.Pod{})
}
}
}