-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathapi.go
763 lines (702 loc) · 22.6 KB
/
api.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
package api
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"os"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
// setting a Global variable for the clientset so that I can resuse throughout the code
var Kconfig *kubernetes.Clientset
// These are all the Structs that are used in the API later in this code
type Pod struct {
Name string
Status string
CreatedAt string
UniqueID string
NodeName string
IP string
ContainersCount int
ContainersInfo []Container
Labels map[string]string
}
type Container struct {
Name string
Image string
ImagePullPolicy string
Container int
Port []v1.ContainerPort
}
type Deployment struct {
Name string
Status string
CreatedAt string
UniqueID string
Labels map[string]string
}
type Configmap struct {
Name string
}
type Service struct {
Name string
Ports string
}
type Secret struct {
Name string
SecretMap map[string]string
Type string
CreatedAt string
UniqueID string
}
type Replicationcontroller struct {
Name string
CreatedAt string
UniqueID string
Labels map[string]string
}
type Daemonset struct {
Name string
CreatedAt string
UniqueID string
Labels map[string]string
}
type Namespace struct {
Name string
CreatedAt string
UniqueID string
}
type Event struct {
Name string
Type string
ObjectName string
CreatedAt string
UniqueID string
}
//This function is used to interact with the Kubernetes Cluster to get the clienset
// It has two options:
// 1. Current it's setup to be used inside a cluster
// 2. We can configure it to be used outside the cluster
func Main() {
logrus.Info("Shared Informer app started")
// This checks if you have a Kubernetes config file in your home directory. If not it will try to create in in-cluster config and use that.
var config *rest.Config
var err error
config, err = clientcmd.BuildConfigFromFlags("", os.Getenv("HOME")+"/.kube/config")
if err != nil {
// If the Kubeconfig file is not available, use the in-cluster config
logrus.Info("Using in-cluster configuration. Since couldn't find a kubeconfig file.")
config, err = rest.InClusterConfig()
if err != nil {
fmt.Printf("Error loading in-cluster configuration: %s\n", err)
// BUS YHI TKK THA JO THA!!
// So, at this point we tried to connect with local config file. Also tried to connect to one inside a cluster.
logrus.Error(err.Error())
}
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
logrus.Error(err.Error())
}
// comment till here
Kconfig = clientset
}
// This function is used to get the list of all the pods in the cluster with container details
func Pods(AgentNamespace string, ContainerDetails bool, log *logrus.Entry) string {
// for Pods
clientset := Kconfig
if AgentNamespace == "" {
log.Info("Namespace is empty")
log.Info("Namespace = default")
AgentNamespace = "default"
}
var podInfo []Pod
var containerInfo []Container
pods, err := clientset.CoreV1().Pods(AgentNamespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
defer func() {
if r := recover(); r != nil {
log.Error("Recovered in Pods(): ", r)
}
}()
log.Panic("Unable to find pods. Error: " + err.Error())
} else {
for i := 0; i < len(pods.Items); i++ {
podInfo = append(podInfo,
Pod{
Name: pods.Items[i].Name,
Status: string(pods.Items[i].Status.Phase),
CreatedAt: pods.Items[i].CreationTimestamp.String(),
UniqueID: string(pods.Items[i].GetUID()),
NodeName: string(pods.Items[i].Spec.NodeName),
IP: string(pods.Items[i].Status.PodIP),
ContainersCount: len(pods.Items[i].Spec.Containers),
Labels: pods.Items[i].Labels,
})
if ContainerDetails {
for j := 0; j < len(pods.Items[i].Spec.Containers); j++ {
containerInfo = append(containerInfo,
Container{
Name: pods.Items[i].Spec.Containers[j].Name,
Container: j,
Image: pods.Items[i].Spec.Containers[j].Image,
ImagePullPolicy: string(pods.Items[i].Spec.Containers[j].ImagePullPolicy),
Port: pods.Items[i].Spec.Containers[j].Ports,
})
}
}
podInfo[i].ContainersInfo = containerInfo
}
pods_json, err := json.Marshal(podInfo)
if err != nil {
log.Error(err.Error())
}
return string(pods_json)
}
log.Error("Error in getting pods")
return "Error"
}
// This function is used to get the list of all the logs in a pod.
func PodLogs(AgentNamespace string, PodName string, log *logrus.Entry) string {
clientset := Kconfig
req := clientset.CoreV1().Pods(AgentNamespace).GetLogs(PodName, &(v1.PodLogOptions{}))
podLogs, err := req.Stream(context.Background())
if err != nil {
log.Error(err.Error())
return "error in opening stream"
}
defer podLogs.Close()
buf := new(bytes.Buffer)
_, err = io.Copy(buf, podLogs)
if err != nil {
log.Error(err.Error())
return "error in copy information from podLogs to buf"
}
str := buf.String()
return str
}
// This function is used to get the list of all the deployments in the cluster
func Deployments(AgentNamespace string, log *logrus.Entry) string {
clientset := Kconfig
if AgentNamespace == "" {
log.Info("Namespace is empty")
log.Info("Namespace = default")
AgentNamespace = "default"
}
//fmt.Printf("DEPLOYMENTS \n")
var deploymentInfo []Deployment
deployments, err := clientset.AppsV1().Deployments(AgentNamespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
defer func() {
if r := recover(); r != nil {
log.Error("Recovered in Deployments(): ", r)
}
}()
log.Panic("Unable to find Deployments. Error: " + err.Error())
} else {
for i := 0; i < len(deployments.Items); i++ {
//fmt.Println((deployments.Items[i].Status.Conditions))
deploymentInfo = append(deploymentInfo,
Deployment{
Name: deployments.Items[i].Name,
Status: string(deployments.Items[i].Status.Conditions[0].Type),
CreatedAt: deployments.Items[i].CreationTimestamp.String(),
UniqueID: string(deployments.Items[i].UID),
Labels: deployments.Items[i].Labels,
})
}
deployment_json, err := json.Marshal(deploymentInfo)
if err != nil {
log.Error(err.Error())
log.Fatal(err)
}
return string(deployment_json)
}
log.Error("Error in getting deployments")
return "Error"
}
// This function is used to get the list of all the Configmaps in the cluster
func Configmaps(AgentNamespace string, log *logrus.Entry) string {
clientset := Kconfig
if AgentNamespace == "" {
log.Info("Namespace is empty")
log.Info("Namespace = default")
AgentNamespace = "default"
}
var configmapsInfo []Configmap
configmaps, err := clientset.CoreV1().ConfigMaps(AgentNamespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
defer func() {
if r := recover(); r != nil {
log.Error("Recovered in Configmaps(): ", r)
}
}()
log.Panic("Unable to find Configmaps. Error: " + err.Error())
} else {
for i := 0; i < len(configmaps.Items); i++ {
configmapsInfo = append(configmapsInfo, Configmap{configmaps.Items[i].Name})
}
configmap_json, err := json.Marshal(configmapsInfo)
if err != nil {
log.Print(err.Error())
log.Fatal(err)
}
return string(configmap_json)
}
log.Error("Error in getting configmaps")
return "Error"
}
// This function is used to get the list of all the Services in the cluster
func Services(AgentNamespace string, log *logrus.Entry) string {
clientset := Kconfig
if AgentNamespace == "" {
log.Info("Namespace is empty")
log.Info("Namespace = default")
AgentNamespace = "default"
}
var servicesInfo []Service
services, err := clientset.CoreV1().Services(AgentNamespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
defer func() {
if r := recover(); r != nil {
log.Error("Recovered in Services()", r)
}
}()
log.Panic("Unable to find Services. Error: " + err.Error())
} else {
for i := 0; i < len(services.Items); i++ {
servicesInfo = append(servicesInfo, Service{Name: services.Items[i].Name, Ports: services.Items[i].Spec.Ports[0].TargetPort.String()})
}
service_json, err := json.Marshal(servicesInfo)
if err != nil {
log.Error(err.Error())
log.Fatal(err)
}
//fmt.Println(string(pods_json))
return string(service_json)
}
log.Error("Error in getting services")
return "Error"
}
// This function is used to get the list of all the events in the cluster
func Events(AgentNamespace string, log *logrus.Entry) string {
clientset := Kconfig
var eventsInfo []Event
if AgentNamespace == "" {
log.Info("Namespace is empty")
log.Info("Namespace = default")
AgentNamespace = "default"
}
events, err := clientset.CoreV1().Events(AgentNamespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
defer func() {
if r := recover(); r != nil {
log.Error("Recovered in Events()", r)
}
}()
log.Panic("Unable to find events. Error: " + err.Error())
} else {
for i := 0; i < len(events.Items); i++ {
eventsInfo = append(eventsInfo,
Event{
Name: events.Items[i].Name,
ObjectName: (events.Items[i].InvolvedObject.Name),
CreatedAt: events.Items[i].LastTimestamp.String(),
UniqueID: string(events.Items[i].UID),
Type: events.Items[i].Type,
})
}
event_json, err := json.Marshal(eventsInfo)
if err != nil {
defer func() {
if r := recover(); r != nil {
log.Error("Recovered in Events()", r)
}
}()
log.Panic("Issue in marshaling JSON in Events. Error: " + err.Error())
}
//fmt.Println(string(pods_json))
return string(event_json)
}
log.Error("Error in getting events")
return "Error"
}
// This function is used to get the list of all the secrets in the cluster
func Secrets(AgentNamespace string, log *logrus.Entry) string {
clientset := Kconfig
if AgentNamespace == "" {
log.Info("Namespace is empty")
log.Info("Namespace = default")
AgentNamespace = "default"
}
secrets, err := clientset.CoreV1().Secrets(AgentNamespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
defer func() {
if r := recover(); r != nil {
log.Error("Recovered in Secrets()", r)
}
}()
log.Panic("Unable to find secrets. Error: " + err.Error())
} else {
var secretInfo []Secret
for i := 0; i < len(secrets.Items); i++ {
secretInfo = append(secretInfo,
Secret{
Name: secrets.Items[i].Name,
Type: string(secrets.Items[i].Type),
CreatedAt: secrets.Items[i].CreationTimestamp.String(),
UniqueID: string(secrets.Items[i].UID),
})
tmp := make(map[string]string)
for key, value := range secrets.Items[i].Data {
tmp[key] = string(value)
}
secretInfo[i].SecretMap = tmp
}
secret_json, err := json.Marshal(secretInfo)
if err != nil {
defer func() {
if r := recover(); r != nil {
log.Error("Recovered in Secrets()", r)
}
}()
log.Panic("Error in Marshalling JSON in Secrets. Error: " + err.Error())
}
//fmt.Println(string(secret_json))
return string(secret_json)
}
log.Error("Error in getting secrets")
return "Error"
}
// This function is used to get the list of all the ReplicaController in the cluster
func ReplicationController(AgentNamespace string, log *logrus.Entry) string {
clientset := Kconfig
if AgentNamespace == "" {
log.Info("Namespace is empty")
log.Info("Namespace = default")
AgentNamespace = "default"
}
replicationcontrollers, err := clientset.CoreV1().ReplicationControllers(AgentNamespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
defer func() {
if r := recover(); r != nil {
log.Error("Recovered in ReplicaController()", r)
}
}()
log.Panic("Unable to find ReplicaControllers. Error: " + err.Error())
} else {
var replicationcontrollerInfo []Replicationcontroller
for i := 0; i < len(replicationcontrollers.Items); i++ {
replicationcontrollerInfo = append(replicationcontrollerInfo,
Replicationcontroller{
Name: replicationcontrollers.Items[i].Name,
CreatedAt: replicationcontrollers.Items[i].CreationTimestamp.String(),
UniqueID: string(replicationcontrollers.Items[i].UID),
Labels: (replicationcontrollers.Items[i].Labels),
})
}
replicationcontroller_json, err := json.Marshal(replicationcontrollerInfo)
if err != nil {
defer func() {
if r := recover(); r != nil {
log.Error("Recovered in ReplicaController()", r)
}
}()
log.Panic("Error in Marshalling JSON in ReplicaController. Error: " + err.Error())
}
//fmt.Println(string(replicationcontroller_json))
return string(replicationcontroller_json)
}
log.Error("Error in getting replicationcontrollers")
return "Error"
}
// This function is used to get the list of all the Daemonsets in the cluster
func DaemonSet(AgentNamespace string, log *logrus.Entry) string {
clientset := Kconfig
if AgentNamespace == "" {
log.Info("Namespace is empty")
log.Info("Namespace = default")
AgentNamespace = "default"
}
daemonsets, err := clientset.ExtensionsV1beta1().DaemonSets(AgentNamespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
log.Error(err.Error())
} else {
var daemonsetInfo []Daemonset
for i := 0; i < len(daemonsets.Items); i++ {
daemonsetInfo = append(daemonsetInfo,
Daemonset{
Name: daemonsets.Items[i].Name,
CreatedAt: daemonsets.Items[i].CreationTimestamp.String(),
UniqueID: string(daemonsets.Items[i].UID),
Labels: (daemonsets.Items[i].Labels),
})
}
daemonset_json, err := json.Marshal(daemonsetInfo)
if err != nil {
log.Error(err.Error())
}
//fmt.Println(string(daemonset_json))
return string(daemonset_json)
}
log.Error("Error in getting daemonsets")
return "Error"
}
// This function is used to get the list of all the Namespaces in the cluster
func NameSpace(log *logrus.Entry) string {
clientset := Kconfig
namespaces, err := clientset.CoreV1().Namespaces().List(context.Background(), metav1.ListOptions{})
if err != nil {
defer func() {
if r := recover(); r != nil {
log.Error("Recovered in NameSpace()", r)
}
}()
log.Panic("Unable to find namespaces. Error: " + err.Error())
} else {
var namespaceInfo []Namespace
for i := 0; i < len(namespaces.Items); i++ {
namespaceInfo = append(namespaceInfo,
Namespace{
Name: namespaces.Items[i].Name,
CreatedAt: namespaces.Items[i].CreationTimestamp.String(),
UniqueID: string(namespaces.Items[i].UID),
})
}
namespace_json, err := json.Marshal(namespaceInfo)
if err != nil {
log.Error(err.Error())
log.Fatal(err)
}
return string(namespace_json)
}
log.Error("Error in getting namespaces")
return "Error"
}
// This function creates Namespace in the cluster
func CreateNamespace(namespace string, log *logrus.Entry) string {
log.Info("Namespace=" + namespace)
clientset := Kconfig
ns := &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
Labels: map[string]string{
"name": namespace,
},
},
}
_, err := clientset.CoreV1().Namespaces().Create(context.Background(), ns, metav1.CreateOptions{})
if err != nil {
log.Error(err.Error())
return err.Error()
}
log.Info("Namespace" + namespace + "successfully")
return "Namespace: " + namespace + " Created!"
}
// This function deletes Namespace in the cluster
func DeleteNamespace(namespace string, log *logrus.Entry) string {
clientset := Kconfig
err := clientset.CoreV1().Namespaces().Delete(context.Background(), namespace, metav1.DeleteOptions{})
if err != nil {
log.Error(err.Error())
return err.Error()
}
log.Info("Namespace: " + namespace + " Deleted!")
return "Namespace: " + namespace + " Deleted!"
}
// This function Deletes the Deployments
func DeleteDeployment(namespace string, deployment string, log *logrus.Entry) string {
clientset := Kconfig
err := clientset.AppsV1().Deployments(namespace).Delete(context.Background(), deployment, metav1.DeleteOptions{})
if err != nil {
log.Error(err.Error())
return err.Error()
}
log.Info("Deployment: " + deployment + " Deleted!")
return "Deployment: " + deployment + " Deleted!"
}
// This function Deletes the services
func DeleteService(namespace string, service string, log *logrus.Entry) string {
clientset := Kconfig
err := clientset.CoreV1().Services(namespace).Delete(context.Background(), service, metav1.DeleteOptions{})
if err != nil {
log.Error(err.Error())
return err.Error()
}
log.Info("Service: " + service + " Deleted!")
return "Service: " + service + " Deleted!"
}
// This function Deletes the ConfigMap
func DeleteConfigMap(namespace string, configmap string, log *logrus.Entry) string {
clientset := Kconfig
err := clientset.CoreV1().ConfigMaps(namespace).Delete(context.Background(), configmap, metav1.DeleteOptions{})
if err != nil {
log.Error(err.Error())
return err.Error()
}
log.Info("ConfigMap: " + configmap + " Deleted!")
return "ConfigMap: " + configmap + " Deleted!"
}
// This function Deletes the Secrets
func DeleteSecret(namespace string, secret string, log *logrus.Entry) string {
clientset := Kconfig
err := clientset.CoreV1().Secrets(namespace).Delete(context.Background(), secret, metav1.DeleteOptions{})
if err != nil {
log.Error(err.Error())
return err.Error()
}
log.Info("Secret: " + secret + " Deleted!")
return "Secret: " + secret + " Deleted!"
}
// This function Deletes the ReplicationController
func DeleteReplicationController(namespace string, replicationcontroller string, log *logrus.Entry) string {
clientset := Kconfig
err := clientset.CoreV1().ReplicationControllers(namespace).Delete(context.Background(), replicationcontroller, metav1.DeleteOptions{})
if err != nil {
log.Error(err.Error())
return err.Error()
}
log.Info("ReplicationController: " + replicationcontroller + " Deleted!")
return "ReplicationController: " + replicationcontroller + " Deleted!"
}
// This function Deletes the DaemonSet
func DeleteDaemonSet(namespace string, daemonset string, log *logrus.Entry) string {
clientset := Kconfig
err := clientset.ExtensionsV1beta1().DaemonSets(namespace).Delete(context.Background(), daemonset, metav1.DeleteOptions{})
if err != nil {
log.Error(err.Error())
return err.Error()
}
log.Info("DaemonSet: " + daemonset + " Deleted!")
return "DaemonSet: " + daemonset + " Deleted!"
}
// This function Deletes the Pod
func DeletePod(namespace string, pod string) string {
clientset := Kconfig
err := clientset.CoreV1().Pods(namespace).Delete(context.Background(), pod, metav1.DeleteOptions{})
if err != nil {
log.Print(err.Error())
return err.Error()
}
return "Pod: " + pod + " Deleted!"
}
// This function Deletes the Event
func DeleteEvent(namespace string, event string, log *logrus.Entry) string {
clientset := Kconfig
err := clientset.CoreV1().Events(namespace).Delete(context.Background(), event, metav1.DeleteOptions{})
if err != nil {
log.Error(err.Error())
return err.Error()
}
log.Info("Event: " + event + " Deleted!")
return "Event: " + event + " Deleted!"
}
// This function Deletes EVERYTHING in the namespace. My lil nuke!! MUWAHAHAHA
func DeleteAll(namespace string, log *logrus.Entry) string {
clientset := Kconfig
deployments, err := clientset.AppsV1().Deployments(namespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
log.Error(err.Error())
return err.Error()
}
for i := 0; i < len(deployments.Items); i++ {
err := clientset.AppsV1().Deployments(namespace).Delete(context.Background(), deployments.Items[i].Name, metav1.DeleteOptions{})
if err != nil {
log.Error(err.Error())
return err.Error()
}
}
services, err := clientset.CoreV1().Services(namespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
log.Error(err.Error())
return err.Error()
}
for i := 0; i < len(services.Items); i++ {
err := clientset.CoreV1().Services(namespace).Delete(context.Background(), services.Items[i].Name, metav1.DeleteOptions{})
if err != nil {
log.Error(err.Error())
return err.Error()
}
}
configmaps, err := clientset.CoreV1().ConfigMaps(namespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
log.Error(err.Error())
return err.Error()
}
for i := 0; i < len(configmaps.Items); i++ {
err := clientset.CoreV1().ConfigMaps(namespace).Delete(context.Background(), configmaps.Items[i].Name, metav1.DeleteOptions{})
if err != nil {
log.Error(err.Error())
return err.Error()
}
}
secrets, err := clientset.CoreV1().Secrets(namespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
log.Error(err.Error())
return err.Error()
}
for i := 0; i < len(secrets.Items); i++ {
err := clientset.CoreV1().Secrets(namespace).Delete(context.Background(), secrets.Items[i].Name, metav1.DeleteOptions{})
if err != nil {
log.Error(err.Error())
return err.Error()
}
}
replicationcontrollers, err := clientset.CoreV1().ReplicationControllers(namespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
log.Print(err.Error())
return err.Error()
}
for i := 0; i < len(replicationcontrollers.Items); i++ {
err := clientset.CoreV1().ReplicationControllers(namespace).Delete(context.Background(), replicationcontrollers.Items[i].Name, metav1.DeleteOptions{})
if err != nil {
log.Error(err.Error())
return err.Error()
}
}
daemonsets, err := clientset.ExtensionsV1beta1().DaemonSets(namespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
log.Error(err.Error())
return err.Error()
}
for i := 0; i < len(daemonsets.Items); i++ {
err := clientset.ExtensionsV1beta1().DaemonSets(namespace).Delete(context.Background(), daemonsets.Items[i].Name, metav1.DeleteOptions{})
if err != nil {
log.Error(err.Error())
return err.Error()
}
}
pods, err := clientset.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
log.Error(err.Error())
return err.Error()
}
for i := 0; i < len(pods.Items); i++ {
err := clientset.CoreV1().Pods(namespace).Delete(context.Background(), pods.Items[i].Name, metav1.DeleteOptions{})
if err != nil {
log.Error(err.Error())
return err.Error()
}
}
events, err := clientset.CoreV1().Events(namespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
log.Error(err.Error())
return err.Error()
}
for i := 0; i < len(events.Items); i++ {
err := clientset.CoreV1().Events(namespace).Delete(context.Background(), events.Items[i].Name, metav1.DeleteOptions{})
if err != nil {
log.Error(err.Error())
return err.Error()
}
}
log.Info("Everything in " + namespace + " Deleted!")
return "All Deleted!"
}