-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApiWatcher.go
More file actions
146 lines (131 loc) · 4.29 KB
/
ApiWatcher.go
File metadata and controls
146 lines (131 loc) · 4.29 KB
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
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
"k8s.io/api/apps/v1"
v1core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/kubernetes/pkg/apis/core"
"os"
"path/filepath"
)
func WatchList(resource string, resourceType runtime.Object) cache.Controller {
var restInterface rest.Interface
switch resourceType.(type) {
default:
restInterface = clientset.AppsV1().RESTClient()
case *v1core.Service, *v1core.Secret, *v1core.ConfigMap:
restInterface = clientset.CoreV1().RESTClient()
}
watchlist := cache.NewListWatchFromClient(restInterface, resource, core.NamespaceAll, fields.Everything())
_, controller := cache.NewInformer(
watchlist,
resourceType,
-1,
cache.ResourceEventHandlerFuncs{
AddFunc: ResourceAdded,
UpdateFunc: ResourceUpdated,
DeleteFunc: ResourceDeleted,
})
return controller
}
/*
This Code Will listen to the Kube API and run file saver based on the resource returned by API Server
*/
func WatchApis() {
go WatchList("statefulsets", &v1.StatefulSet{}).Run(wait.NeverStop)
go WatchList("deployments", &v1.Deployment{}).Run(wait.NeverStop)
go WatchList("daemonsets", &v1.DaemonSet{}).Run(wait.NeverStop)
// Only use Replica Sets if we need to since deploys == rs
if deck_config.UseReplicaSets == true {
go WatchList("relicasets", &v1.ReplicaSet{}).Run(wait.NeverStop)
}
// Only get Secrets, Config Maps, Services only if store_all is set
if deck_config.STORE_ALL == true {
go WatchList(string(v1core.ResourceServices), &v1core.Service{}).Run(wait.NeverStop)
go WatchList("secrets", &v1core.Secret{}).Run(wait.NeverStop)
go WatchList(string(v1core.ResourceConfigMaps), &v1core.ConfigMap{}).Run(wait.NeverStop)
}
// Watch for namespaces are different
namespaceList := cache.NewListWatchFromClient(clientset.CoreV1().RESTClient(), "namespaces", core.NamespaceAll, fields.Everything())
_, namespaceController := cache.NewInformer(
namespaceList,
&v1core.Namespace{},
-1,
cache.ResourceEventHandlerFuncs{
AddFunc: namespaceAdded,
DeleteFunc: namespaceDeleted,
})
go namespaceController.Run(wait.NeverStop)
}
// Only used in the resource deleted field
func getResourceInfo(obj interface{}) (string, string, string) {
switch val := obj.(type) {
default:
log.Panic("unknown type in deletion")
case *v1.DaemonSet:
return val.Name, "DaemonSet", val.Namespace
case *v1.StatefulSet:
return val.Name, "StatefulSet", val.Namespace
case *v1.Deployment:
return val.Name, "Deployment", val.Namespace
case *v1.ReplicaSet:
return val.Name, "ReplicaSet", val.Namespace
case *v1core.Service:
return val.Name, "Service", val.Namespace
case *v1core.Secret:
return val.Name, "Secret", val.Namespace
case *v1core.ConfigMap:
return val.Name, "ConfigMap", val.Namespace
}
return "", "", ""
}
func ResourceDeleted(obj interface{}) {
name, rtype, namespace := getResourceInfo(obj)
log.Println("Resource Deleted")
filename := fmt.Sprintf("%s.%s.yaml", name, rtype)
file := filepath.Join(createPath, namespace, filename)
if name != "" {
deleteFile(file)
}
}
func ResourceAdded(obj interface{}) {
log.Debug("Resource Added")
SaveResource(obj)
}
func ResourceUpdated(old interface{}, obj interface{}) {
// Because syncs also call updatefunc we will need to do this
// create kctl deployment struct
log.Debug("Resource Updated")
SaveResource(obj)
}
func namespaceAdded(obj interface{}) {
namespace := obj.(*v1core.Namespace)
// CHECK IF EXISTS, if it doesn't then create
dirPath := filepath.Join(createPath, namespace.Name)
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
// need to create this path
log.Info("namespace: ", namespace.Name, " Added")
if os.MkdirAll(dirPath, 0777) != nil {
log.Error("Error creating namespace path")
}
}
}
func namespaceDeleted(obj interface{}) {
namespace := obj.(*v1core.Namespace)
log.Info("namespace", namespace.Name, " Deleted")
// if Path doesnt exist
dirPath := filepath.Join(createPath, namespace.Name)
if _, err := os.Stat(dirPath); !os.IsNotExist(err) {
// need to create this path
if os.RemoveAll(dirPath+"/") != nil {
log.Error("Could not clear old dir struct")
}
} else {
log.Error("Tried to delete a namespace that was not found in repo")
}
}