-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·333 lines (285 loc) · 7.26 KB
/
main.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
package main
import (
"flag"
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"net/http"
"os"
// "net/http"
"net"
"time"
// "k8s.io/apimachinery/pkg/runtime"
// apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
// apierrors "k8s.io/apimachinery/pkg/api/errors"
// "encoding/json"
// "github.com/imdario/mergo"
// "github.com/jinzhu/copier"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
// "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
rest "k8s.io/client-go/rest"
// "k8s.io/kubernetes/pkg/api"
// "reflect"
// k8sapiV1 "k8s.io/kubernetes/pkg/api/v1"
// clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
// "k8s.io/client-go/kubernetes"
)
// var NginxConfig_Backup *Nginx_Config
// func (s *Nginx_Server_Config) ServerInfo() {
// var serverInfo
// }
// var Client *clientset.Clientset
const (
terminatedFile = "/tmp/terminated"
)
func APISOutClusterConfig() *rest.Config {
certData, _ := ioutil.ReadFile("/data/suker/git/minikube/.minikube/apiserver.crt")
keyData, _ := ioutil.ReadFile("/data/suker/git/minikube/.minikube/apiserver.key")
config := &rest.Config{
Host: "https://192.168.99.100:8443",
APIPath: "/apis",
TLSClientConfig: rest.TLSClientConfig{
Insecure: true,
CertFile: "/data/suker/git/minikube/.minikube/apiserver.crt",
KeyFile: "/data/suker/git/minikube/.minikube/apiserver.key",
CertData: certData,
KeyData: keyData,
},
ContentConfig: rest.ContentConfig{
GroupVersion: &schema.GroupVersion{
Group: "suker200.com",
Version: "v1",
},
},
}
return config
}
func OutClusterConfig() *rest.Config {
certData, _ := ioutil.ReadFile("/data/suker/git/minikube/.minikube/apiserver.crt")
keyData, _ := ioutil.ReadFile("/data/suker/git/minikube/.minikube/apiserver.key")
config := &rest.Config{
Host: "https://192.168.99.100:8443",
APIPath: "/api",
TLSClientConfig: rest.TLSClientConfig{
Insecure: true,
CertFile: "/data/suker/git/minikube/.minikube/apiserver.crt",
KeyFile: "/data/suker/git/minikube/.minikube/apiserver.key",
CertData: certData,
KeyData: keyData,
},
ContentConfig: rest.ContentConfig{
GroupVersion: &schema.GroupVersion{
Version: "v1",
},
},
}
return config
}
func APISInClusterConfig() *rest.Config {
config, err := rest.InClusterConfig()
if err != nil {
panic(err)
}
config.APIPath = "/apis"
config.ContentConfig = rest.ContentConfig{
GroupVersion: &schema.GroupVersion{
Group: CRD_Group,
Version: CRD_Group_Version,
},
}
return config
}
func InClusterConfig() *rest.Config {
config, err := rest.InClusterConfig()
if err != nil {
panic(err)
}
config.APIPath = "/api"
config.ContentConfig = rest.ContentConfig{
GroupVersion: &schema.GroupVersion{
// Group: "v1",
Version: "v1",
},
}
return config
}
var TestMode bool
var CRD_Namespace string
var CRD_Plural string
var CRD_Kind string
var CRD_Group string
var CRD_Group_Version string
var WAFName string
var APISConfig *rest.Config
var Config *rest.Config
var ApisClient *dynamic.Client
var Client *dynamic.Client
var Config_Info *ConfigInfo
var TCPSock net.Listener
// var ClientCMD *clientset.Clientset
func ConfigWorker(c chan string) {
fmt.Println("We start ConfigWorker")
var resourceClient *dynamic.ResourceClient
resource := &metav1.APIResource{
Name: CRD_Plural, //"nginxcerts",
Kind: CRD_Kind, // "Nginxcert",
Namespaced: true,
Verbs: metav1.Verbs{"get"},
}
resourceClient = ApisClient.Resource(resource, CRD_Namespace)
// Init(resourceClient)
ConfigWatch(resourceClient, c)
}
func CertWorker(c chan string) {
fmt.Println("We start CertWorker")
var resourceClient *dynamic.ResourceClient
resource := &metav1.APIResource{
Name: "secrets", //"nginxcerts",
Kind: "secret", // "Nginxcert",
Namespaced: true,
Verbs: metav1.Verbs{"get"},
}
resourceClient = Client.Resource(resource, CRD_Namespace)
if !CertInitRunOnce {
CertInit(resourceClient)
}
CertWatch(resourceClient, c)
}
func Worker() {
// Start CRD Watch
var err error
cInfo := make(chan string)
if ApisClient, err = dynamic.NewClient(APISConfig); err != nil {
panic(err)
}
if Client, err = dynamic.NewClient(Config); err != nil {
panic(err)
}
// ClientCMD = clientset.NewForConfigOrDie(Config)
go ConfigWorker(cInfo)
go CertWorker(cInfo)
for {
c := <-cInfo
if c == "ConfigWatch" {
go ConfigWorker(cInfo)
} else if c == "CertWatch" {
go CertWorker(cInfo)
}
time.Sleep(time.Duration(1) * time.Second)
}
}
func ConfigLoad() {
data, err := ioutil.ReadFile("/config/config.yaml")
if err != nil {
panic(err)
}
err = yaml.Unmarshal(data, &Config_Info)
if err != nil {
panic(err)
}
}
func CheckTerminated() bool {
if _, err := os.Stat(terminatedFile); os.IsNotExist(err) {
return false
}
return true
}
func HealthCheckIngress() {
for {
fmt.Println("We start HealthChecking Ingress")
resp, err := http.Get("http://127.0.0.1:10254/healthz")
if err == nil {
if resp.StatusCode == 200 {
go TcpListen()
go NginxHealthCheck()
break
}
}
time.Sleep(time.Duration(1) * time.Second)
}
}
func NginxHealthCheck() {
for {
if ok := CheckTerminated(); ok {
fmt.Print("We remove ping.html for terminated process")
os.Remove("/var/www/html/ping.html")
}
}
}
func TcpListen() {
var err error
TCPSock, err = net.Listen("tcp4", ":9999")
if err != nil {
panic(err)
}
for {
_, err := TCPSock.Accept()
if err != nil {
panic(err)
}
if ok := CheckTerminated(); ok {
fmt.Print("We shutdown socket for terminated process")
if err := TCPSock.Close(); err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("The socket stopped")
fmt.Println(TCPSock)
break
}
}
}
}
func main() {
ConfigLoad()
local_test := flag.Bool("test", false, "local test mode")
waf_name := flag.String("waf_name", "waf-abc", "waf_name for applying change config")
flag.Parse()
fmt.Println(Config_Info)
CRD_Namespace = Config_Info.CRD_Info.Namespace
CRD_Kind = Config_Info.CRD_Info.Kind
CRD_Plural = Config_Info.CRD_Info.Plural
CRD_Group = Config_Info.CRD_Info.Group
CRD_Group_Version = Config_Info.CRD_Info.Version
WAFName = Config_Info.CRD_Info.Waf_Name
// var err error
// var config *rest.Config
if *local_test {
APISConfig = APISOutClusterConfig()
Config = OutClusterConfig()
TestMode = true
CRD_Namespace = "devops"
CRD_Kind = "Nginxcert"
CRD_Plural = "nginxcerts"
CRD_Group = "suker200.com"
CRD_Group_Version = "v1"
WAFName = *waf_name
} else {
APISConfig = APISInClusterConfig()
Config = InClusterConfig()
}
// go TcpListen()
go HealthCheckIngress()
Worker()
// time.Sleep(time.Duration(10) * time.Second)
// os.Exit(100)
// ApisClient, err := dynamic.NewClient(APISConfig)
// if err != nil {
// panic(err)
// } else {
// var resourceClient *dynamic.ResourceClient
// resource := &metav1.APIResource{
// Name: CRD_Plural, //"nginxcerts",
// Kind: CRD_Kind, // "Nginxcert",
// Namespaced: true,
// Verbs: metav1.Verbs{"get"},
// }
// resourceClient = ApisClient.Resource(resource, CRD_Namespace)
// Init(resourceClient)
// for {
// Watch(resourceClient)
// time.Sleep(time.Duration(1) * time.Second)
// }
// }
}