-
Notifications
You must be signed in to change notification settings - Fork 1
/
ingress_controller.go
439 lines (301 loc) · 9.36 KB
/
ingress_controller.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
"github.com/urfave/cli"
"github.com/sirupsen/logrus"
"k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
"github.com/fortinet-solutions-cse/fortiweb_go_client"
)
var fwb = &fortiwebclient.FortiWebClient{
URL: "https://192.168.122.40:90/",
Username: "admin",
Password: "",
}
func deleteAll() error {
fmt.Println("Removing All K8S resources")
deleteAllK8SServerPolicies()
deleteAllK8SContentRoutingPolicies()
deleteAllK8SServerPoolRules()
deleteAllK8SVirtualServers()
return nil
}
func deleteAllK8SContentRoutingPolicies() error {
fmt.Println("Removing Content Routing Policies")
response, error := fwb.DoGet("api/v1.0/ServerObjects/Server/HTTPContentRoutingPolicy/")
if error != nil {
return error
}
bodyByteArray, error := ioutil.ReadAll(response.Body)
var bodyMap interface{}
err := json.Unmarshal(bodyByteArray, &bodyMap)
if err != nil {
return err
}
contentRoutingPolicyList := reflect.ValueOf(bodyMap)
for i := 0; i < contentRoutingPolicyList.Len(); i++ {
value := contentRoutingPolicyList.Index(i).Interface()
myMap := value.(map[string]interface{})
fwb.DeleteContentRoutingPolicy(myMap["name"].(string))
}
return nil
}
func deleteAllK8SServerPoolRules() error {
fmt.Println("Removing Server Pool Rules")
response, error := fwb.DoGet("api/v1.0/ServerObjects/Server/ServerPool")
if error != nil {
return error
}
bodyByteArray, error := ioutil.ReadAll(response.Body)
var bodyMap interface{}
err := json.Unmarshal(bodyByteArray, &bodyMap)
if err != nil {
return err
}
serverPoolList := reflect.ValueOf(bodyMap)
for i := 0; i < serverPoolList.Len(); i++ {
value := serverPoolList.Index(i).Interface()
myMap := value.(map[string]interface{})
fwb.DeleteServerPool(myMap["name"].(string))
}
return nil
}
func deleteAllK8SServerPolicies() error {
fmt.Println("Removing Server Policies")
response, error := fwb.DoGet("api/v1.0/Policy/ServerPolicy/ServerPolicy")
if error != nil {
return error
}
bodyByteArray, error := ioutil.ReadAll(response.Body)
var bodyMap interface{}
err := json.Unmarshal(bodyByteArray, &bodyMap)
if err != nil {
return err
}
serverPolicyList := reflect.ValueOf(bodyMap)
for i := 0; i < serverPolicyList.Len(); i++ {
value := serverPolicyList.Index(i).Interface()
myMap := value.(map[string]interface{})
fwb.DeleteServerPolicy(myMap["name"].(string))
}
return nil
}
func deleteAllK8SVirtualServers() error {
fmt.Println("Removing Virtual Servers")
response, error := fwb.DoGet("api/v1.0/ServerObjects/Server/VirtualServer")
if error != nil {
return error
}
bodyByteArray, error := ioutil.ReadAll(response.Body)
var bodyMap interface{}
err := json.Unmarshal(bodyByteArray, &bodyMap)
if err != nil {
return err
}
virtualServerList := reflect.ValueOf(bodyMap)
for i := 0; i < virtualServerList.Len(); i++ {
value := virtualServerList.Index(i).Interface()
myMap := value.(map[string]interface{})
fwb.DeleteVirtualServer(myMap["name"].(string))
}
return nil
}
func getNodePortFromService(service string) int32 {
kubeconfig := filepath.Join(
os.Getenv("HOME"), ".kube", "config")
clientset, err := getClient(kubeconfig)
if err != nil {
fmt.Println(strings.Join([]string{"Error:", err.Error()}, ""))
logrus.Error(err)
os.Exit(-1)
}
services, error := clientset.CoreV1().Services("default").List(v1.ListOptions{LabelSelector: "run=" + service})
if error != nil || len(services.Items) == 0 {
fmt.Println("Error getting services. Exiting...")
os.Exit(-1)
}
return services.Items[0].Spec.Ports[0].NodePort
}
func transformIngressToFWB(ingress v1beta1.Ingress) {
fmt.Println("Transforming ingress:" + ingress.GetName())
//Delete All Resources created previously from K8S in FWB
deleteAll()
fmt.Println("Creating Virtual Server...")
fwb.CreateVirtualServer("K8S_virtual_server",
"", "", "port1",
true, true)
fmt.Println("Creating Server Policy...")
fwb.CreateServerPolicy("K8S_Server_Policy",
"K8S_virtual_server", "",
"HTTP", "", "", "",
fortiwebclient.HTTPContentRouting, 8192,
false, false, false, false, false)
for _, rule := range ingress.Spec.Rules {
transformIngressRuleToFWB(rule)
}
}
func transformIngressRuleToFWB(rule v1beta1.IngressRule) {
transformHTTPRuleToFWB(rule.Host, rule.HTTP)
}
func transformHTTPRuleToFWB(host string, httpRule *v1beta1.HTTPIngressRuleValue) {
for _, path := range httpRule.Paths {
transformHTTPIngressPathToFWB(host, path)
}
}
func transformHTTPIngressPathToFWB(host string, ingressPath v1beta1.HTTPIngressPath) {
path := ingressPath.Path
service := ingressPath.Backend.ServiceName
// Get port number of Service
portNumber := getNodePortFromService(service)
// Get list of nodes
// CreateServerPool
fmt.Println("Creating Server Pool...")
fwb.CreateServerPool("K8S_Server_Pool_"+fwb.SafeName(service),
fortiwebclient.ServerBalance,
fortiwebclient.ReverseProxy,
fortiwebclient.RoundRobin,
"")
// CreateServerPoolRule
fmt.Println("Creating Server Pool Rule 1...")
fwb.CreateServerPoolRule("K8S_Server_Pool_"+fwb.SafeName(service), "10.192.0.3", portNumber, 2, 0)
fmt.Println("Creating Server Pool Rule 2...")
fwb.CreateServerPoolRule("K8S_Server_Pool_"+fwb.SafeName(service), "10.192.0.4", portNumber, 2, 0)
// CreateHTTPContentRoutingPolicy
fmt.Println("Creating HTTP Content Routing Policy...")
contentRoutingPolicyName := fwb.SafeName("K8S_HTTP_Content_Routing_Policy_" + host + "_" + path)
fwb.CreateHTTPContentRoutingPolicy(contentRoutingPolicyName,
fwb.SafeName("K8S_Server_Pool_"+service),
"( )")
// CreateHTTPContentRoutingUsingHost
fmt.Println("Creating HTTP Content Routing for Host...")
fwb.CreateHTTPContentRoutingUsingHost(contentRoutingPolicyName,
host,
3,
fortiwebclient.AND)
// CreateHTTPContentRoutingUsingURL
fmt.Println("Creating HTTP Content Routing for URL...")
fwb.CreateHTTPContentRoutingUsingURL(contentRoutingPolicyName,
strings.Replace(path, "/", " ", -1),
3,
fortiwebclient.AND)
fmt.Println("Creating Server Policy Content Rule")
fwb.CreateServerPolicyContentRule("K8S_Server_Policy",
"",
contentRoutingPolicyName,
"#navigate/InlineProtectionProfile/",
"",
true,
false)
}
func getClient(pathToCfg string) (*kubernetes.Clientset, error) {
var config *rest.Config
var err error
if pathToCfg == "" {
logrus.Info("Using in cluster config")
config, err = rest.InClusterConfig()
} else {
logrus.Info("Using out of cluster config")
config, err = clientcmd.BuildConfigFromFlags("", pathToCfg)
}
if err != nil {
return nil, err
}
return kubernetes.NewForConfig(config)
}
var clientset *kubernetes.Clientset
var controller cache.Controller
var store cache.Store
func init() {
fmt.Println("init")
}
func main() {
fmt.Println("main")
app := cli.NewApp()
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config",
Usage: "Kube config path for outside of cluster access",
},
}
kubeconfig := filepath.Join(
os.Getenv("HOME"), ".kube", "config")
fmt.Println("Reading K8S config from: " + kubeconfig)
clientset, err := getClient(kubeconfig)
if err != nil {
fmt.Println(strings.Join([]string{"Error:", err.Error()}, ""))
logrus.Error(err)
os.Exit(-1)
}
/* nodes, err := clientset.CoreV1().Nodes().List(v1.ListOptions{})
if err != nil {
fmt.Println("Error getting nodes")
os.Exit(-1)
}
fmt.Println("Length:" + strconv.Itoa(len(nodes.Items)))
if len(nodes.Items) > 0 {
fmt.Println(nodes.Items)
}
fmt.Println("Beautified:")
for index, element := range nodes.Items {
fmt.Println("Id: " + strconv.Itoa(index))
fmt.Println(element.Status.NodeInfo.MachineID)
fmt.Println(element.Status.NodeInfo.KernelVersion)
}
fmt.Print("\n Getting Ingress: \n\n")
ingresses, error := clientset.ExtensionsV1beta1().Ingresses("default").List(v1.ListOptions{})
if error != nil {
fmt.Println("Error getting ingress resources. Exiting...")
os.Exit(-1)
}
for index, element := range ingresses.Items {
fmt.Println(index)
fmt.Println(pretty.Formatter(element))
}
fmt.Print("\nGetting Services: \n\n")
services, error := clientset.CoreV1().Services("default").List(v1.ListOptions{})
if error != nil {
fmt.Println("Error getting services. Exiting...")
os.Exit(-1)
}
for index, element := range services.Items {
fmt.Println(index)
fmt.Println(pretty.Formatter(element.Name))
fmt.Println(pretty.Formatter(element.Spec.Selector))
selectors := element.Spec.Selector
for key, value := range selectors {
fmt.Println(key, " = ", value)
}
}
fmt.Print("\nGetting Pods: \n\n")
pods, error := clientset.CoreV1().Pods("default").List(v1.ListOptions{})
if error != nil {
fmt.Println("Error getting pods. Exiting...", error.Error())
}
for index, element := range pods.Items {
fmt.Println(index, element)
labels := element.Labels
fmt.Println(labels)
fmt.Println("Pod name: ", element.Name)
fmt.Println("Node IP: ", element.Status.HostIP)
}
*/
fmt.Println("\n Getting Ingress Data...")
ingresses, error := clientset.ExtensionsV1beta1().Ingresses("default").List(v1.ListOptions{})
if error != nil {
fmt.Println("Error getting ingress resources. Exiting...")
os.Exit(-1)
}
for _, ing := range ingresses.Items {
transformIngressToFWB(ing)
}
}