-
Notifications
You must be signed in to change notification settings - Fork 11
/
resources.go
300 lines (255 loc) · 9.03 KB
/
resources.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright (c) 2016 Pani Networks
// All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package kubernetes
import (
"encoding/json"
"fmt"
"github.com/romana/core/common"
"github.com/romana/core/tenant"
"log"
"net/http"
"strings"
)
const (
selector = "podSelector"
)
// Done is an alias for empty struct, used to make broadcast channels
// for terminating goroutines.
type Done struct{}
// Event is a representation of a structure that we receive from kubernetes API.
type Event struct {
Type string `json:"Type"`
Object KubeObject `json:"object"`
}
const (
KubeEventAdded = "ADDED"
KubeEventDeleted = "DELETED"
KubeEventModified = "MODIFIED"
InternalEventDeleteAll = "_DELETE_ALL"
)
// KubeObject is a representation of object in kubernetes.
type KubeObject struct {
Kind string `json:"kind"`
Spec Spec `json:"spec"`
ApiVersion string `json:"apiVersion"`
Metadata Metadata `json:"metadata"`
Status map[string]string `json:"status,omitempty"`
}
// makeId makes id to identify kube object.
func (o KubeObject) makeId() string {
id := o.Metadata.Name + "/" + o.Metadata.Namespace
return id
}
type PodSelector struct {
MatchLabels map[string]string `json:"matchLabels"`
}
type FromEntry struct {
Pods PodSelector `json:"podSelector"`
}
type Ingress struct {
From []FromEntry `json:"from"`
ToPorts []ToPort `json:"ports"`
}
type ToPort struct {
Port uint `json:"port"`
Protocol string `json:"protocol"`
}
// TODO need to find a way to use different specs for different resources.
type Spec struct {
Ingress []Ingress `json:"ingress"`
PodSelector PodSelector `json:"podSelector"`
}
// Metadata is a representation of metadata in kubernetes object
type Metadata struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
SelfLink string `json:"selfLink"`
Uid string `json:"uid"`
ResourceVersion string `json:"resourceVersion"`
CreationTimestamp string `json:"creationTimestamp"`
Labels map[string]string `json:"labels"`
Annotations map[string]string `json:"annotations,omitempty"`
}
// handle kubernetes events according to their type.
func (e Event) handle(l *kubeListener) {
log.Printf("Processing %s request for %s", e.Type, e.Object.Metadata.Name)
if e.Object.Kind == "NetworkPolicy" && e.Type != KubeEventModified {
e.handleNetworkPolicyEvent(l)
} else if e.Object.Kind == "Namespace" {
e.handleNamespaceEvent(l)
} else {
log.Printf("Received unindentified request %s for %s", e.Type, e.Object.Metadata.Name)
}
}
// handleNetworkPolicyEvent by creating or deleting romana policies.
func (e Event) handleNetworkPolicyEvent(l *kubeListener) {
var action networkPolicyAction
if e.Type == KubeEventAdded {
action = networkPolicyActionAdd
} else {
action = networkPolicyActionDelete
}
policy, err := l.translateNetworkPolicy(&e.Object)
if err == nil {
j1, _ := json.Marshal(e)
j2, _ := json.Marshal(policy)
log.Printf("handleNetworkPolicyEvent(): translated\n\t%s\n\tto\n\t%s", j1, j2)
l.applyNetworkPolicy(action, policy)
} else {
log.Println(err)
}
}
// handleNamespaceEvent by creating or deleting romana tenants.
func (e Event) handleNamespaceEvent(l *kubeListener) {
log.Printf("KubeEvent: Processing namespace event == %v and phase %v", e.Type, e.Object.Status)
if e.Type == KubeEventAdded {
tenantReq := tenant.Tenant{Name: e.Object.Metadata.Name, ExternalID: e.Object.Metadata.Uid}
tenantResp := tenant.Tenant{}
log.Printf("KubeEventAdded: Posting to /tenants: %+v", tenantReq)
tenantUrl, err := l.restClient.GetServiceUrl("tenant")
if err != nil {
log.Printf("KubeEventAdded:Error adding tenant %s: %+v", tenantReq.Name, err)
} else {
err := l.restClient.Post(fmt.Sprintf("%s/tenants", tenantUrl), tenantReq, &tenantResp)
if err != nil {
log.Printf("KubeEventAdded: Error adding tenant %s: %+v", tenantReq.Name, err)
} else {
log.Printf("KubeEventAdded: Added tenant: %+v", tenantResp)
}
}
} else if e.Type == KubeEventDeleted {
// TODO
}
// Ignore repeated events during namespace termination
if e.Object.Status["phase"] == "Terminating" {
if e.Type != KubeEventModified {
e.Object.handleAnnotations(l)
}
} else {
e.Object.handleAnnotations(l)
}
}
// handleAnnotations on a namespace by implementing extra features requested through the annotation
func (o KubeObject) handleAnnotations(l *kubeListener) {
log.Printf("In handleAnnotations")
if o.Kind != "Namespace" {
log.Printf("Error handling annotations on a namespace - object is not a namespace %s \n", o.Kind)
return
}
CreateDefaultPolicy(o, l)
}
func CreateDefaultPolicy(o KubeObject, l *kubeListener) {
log.Printf("In CreateDefaultPolicy for %v\n", o)
tenant, err := l.resolveTenantByName(o.Metadata.Name)
if err != nil {
log.Printf("In CreateDefaultPolicy :: Error :: failed to resolve tenant %s \n", err)
return
}
policyName := fmt.Sprintf("ns%d", tenant.NetworkID)
romanaPolicy := &common.Policy{
Direction: common.PolicyDirectionIngress,
Name: policyName,
AppliedTo: []common.Endpoint{{TenantNetworkID: &tenant.NetworkID}},
Peers: []common.Endpoint{{Peer: common.Wildcard}},
Rules: []common.Rule{{Protocol: common.Wildcard}},
}
log.Printf("In CreateDefaultPolicy with policy %v\n", romanaPolicy)
var desiredAction networkPolicyAction
if np, ok := o.Metadata.Annotations["net.beta.kubernetes.io/networkpolicy"]; ok {
log.Printf("Handling default policy on a namespace %s, policy is now %s \n", o.Metadata.Name, np)
policy := struct {
Ingress struct {
Isolation string `json:"isolation"`
} `json:"ingress"`
}{}
err := json.NewDecoder(strings.NewReader(np)).Decode(&policy)
if err != nil {
log.Printf("In CreateDefaultPolicy :: Error decoding network policy: %s", err)
return
}
log.Println("Decoded to policy:", policy)
if policy.Ingress.Isolation == "DefaultDeny" {
log.Println("Isolation enabled")
desiredAction = networkPolicyActionDelete
} else {
desiredAction = networkPolicyActionAdd
}
} else {
log.Printf("Handling default policy on a namespace, no annotation detected assuming non isolated namespace\n")
desiredAction = networkPolicyActionAdd
}
if err2 := l.applyNetworkPolicy(desiredAction, *romanaPolicy); err2 != nil {
log.Printf("In CreateDefaultPolicy :: Error :: failed to apply %v to the policy %s \n", desiredAction, err2)
}
}
// watchEvents maintains goroutine fired by NsWatch, restarts it in case HTTP GET times out.
func (l *kubeListener) watchEvents(done <-chan Done, url string, resp *http.Response, out chan Event) {
log.Println("Received namespace related event from kubernetes", resp.Body)
dec := json.NewDecoder(resp.Body)
var e Event
for {
select {
case <-done:
return
default:
// Flush e to ensure nothing gets carried over
e = Event{}
// Attempting to read event from HTTP connection
err := dec.Decode(&e)
if err != nil {
// If fail
log.Printf("Failed to decode message from connection %s due to %s\n. Attempting to re-establish", url, err)
// Then stop all goroutines
out <- Event{Type: InternalEventDeleteAll}
// And try to re-establish HTTP connection
resp, err2 := http.Get(url)
if err2 != nil {
log.Printf("Failed establish connection %s due to %s\n.", url, err)
} else if err2 == nil {
dec = json.NewDecoder(resp.Body)
}
} else {
// Else submit event
out <- e
}
}
}
}
// NsWatch is a generator that watches namespace related events in
// kubernetes API and publishes this events to a channel.
func (l *kubeListener) nsWatch(done <-chan Done, url string) (<-chan Event, error) {
out := make(chan Event)
resp, err := http.Get(url)
if err != nil {
return out, err
}
go l.watchEvents(done, url, resp, out)
return out, nil
}
// Produce method listens for resource updates happening within given namespace
// and publishes these updates in a channel.
func (ns KubeObject) produce(out chan Event, done <-chan Done, kubeListener *kubeListener) error {
url, err := common.CleanURL(fmt.Sprintf("%s/%s/%s%s", kubeListener.kubeURL, kubeListener.policyNotificationPathPrefix, ns.Metadata.Name, kubeListener.policyNotificationPathPostfix))
if err != nil {
return err
}
log.Printf("Launching producer to listen for policy notifications on namespace %s at URL %s ", ns.Metadata.Name, url)
resp, err := http.Get(url)
if err != nil {
return err
}
go kubeListener.watchEvents(done, url, resp, out)
return nil
}