-
Notifications
You must be signed in to change notification settings - Fork 0
/
certificate.go
executable file
·208 lines (189 loc) · 5.35 KB
/
certificate.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
package main
import (
"fmt"
"io/ioutil"
// "k8s.io/api/core/v1"
"encoding/json"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/dynamic"
// rest "k8s.io/client-go/rest"
"k8s.io/apimachinery/pkg/watch"
// "k8s.io/kubernetes/pkg/api"
// "k8s.io/client-go/pkg/api"
)
var Cert_path = "/etc/nginx/certs/"
var CertInitRunOnce = false
func CertWatch(rclient *dynamic.ResourceClient, c chan string) {
timeoutSecond := int64(60)
a, err := rclient.Watch(metav1.ListOptions{Watch: true, TimeoutSeconds: &timeoutSecond, ResourceVersion: "0", LabelSelector: "type=certfile"})
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println(a)
b := a.ResultChan()
for {
fmt.Println("new start watch cert")
msg := <-b
if msg.Type != "" {
if msg.Type != "ERROR" {
fmt.Println(msg)
CertCreate(rclient, msg)
}
} else {
// Cause of http session is closed, re-new session
c <- "CertWatch"
return
}
}
}
}
func CertCreate(rclient *dynamic.ResourceClient, event watch.Event) {
b := event.Object.GetObjectKind()
d := b.(*unstructured.Unstructured)
var secret *KubeSecret
// var hehe *test_secret
data, err := d.MarshalJSON()
if err != nil {
fmt.Println("Do something when errors appear") // telegram and rollback
fmt.Println(err.Error())
return
} else {
if err := json.Unmarshal(data, &secret); err != nil {
fmt.Println("Do something when errors appear") // telegram and rollback
fmt.Println(err.Error())
return
}
}
if TestMode {
Cert_path = "./"
}
if err := ioutil.WriteFile(Cert_path+secret.Metadata.Name+".pem", secret.Data["cert"], 0644); err != nil {
fmt.Println(err.Error())
fmt.Println("Do something when errors appear")
return
}
// if !TestMode {
if event.Type == "MODIFIED" {
if !TestMode {
NginxConfigTestOnly()
NgixnReloadOnly()
}
} else if event.Type == "ADDED" {
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)
ConfigCreate(resourceClient)
}
// }
}
func CertInit(rclient *dynamic.ResourceClient) {
fmt.Println("We start CertInit")
var secretList *KubeSecretList
a, err := rclient.List(metav1.ListOptions{LabelSelector: "type=certfile"})
// secretList, err := ClientCMD.Core().Secrets(CRD_Namespace).List(metav1.ListOptions{LabelSelector: "type=certfile"})
// if err != nil {
// fmt.Println(err.Error())
// return
// }
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("====")
b := a.GetObjectKind()
d := b.(*unstructured.UnstructuredList)
data, err := d.MarshalJSON()
// fmt.Println(data.String())
// fmt.Println(string(data))
if err != nil {
fmt.Println("Do something when errors appear") // telegram and rollback
fmt.Println(err.Error())
// panic(err)
} else {
if err := json.Unmarshal(data, &secretList); err != nil {
fmt.Println(err.Error())
fmt.Println("Do something when errors appear")
}
}
}
if TestMode {
Cert_path = "./"
}
for _, secret := range secretList.Items {
fmt.Println(Cert_path + secret.Metadata.Name + ".pem")
// fmt.Println("-------")
// fmt.Println(secret)
// fmt.Println("-------")
if err := ioutil.WriteFile(Cert_path+secret.Metadata.Name+".pem", secret.Data["cert"], 0644); err != nil {
fmt.Println(err.Error())
fmt.Println("Do something when errors appear")
return
}
}
CertInitRunOnce = true
}
// func (cfg *Nginx_Config) List(rclient *dynamic.ResourceClient) []CRD {
// var content CRDList
// a, err := rclient.List(metav1.ListOptions{})
// if err != nil {
// fmt.Println(err.Error())
// } else {
// fmt.Println("====")
// b := a.GetObjectKind()
// d := b.(*unstructured.UnstructuredList)
// data, err := d.MarshalJSON()
// if err != nil {
// fmt.Println("Do something when errors appear") // telegram and rollback
// fmt.Println(err.Error())
// // panic(err)
// } else {
// if err := json.Unmarshal(data, &content); err != nil {
// fmt.Println(err.Error())
// fmt.Println("Do something when errors appear")
// }
// }
// }
// return content.Items
// }
func GenerateCerts(client *dynamic.Client, nameSpace, secretCertName string) error {
resource := &metav1.APIResource{
Name: "secrets", //"nginxcerts",
Kind: "secret", // "Nginxcert",
Namespaced: true,
Verbs: metav1.Verbs{"get"},
}
resourceClient := client.Resource(resource, nameSpace)
resp, err := resourceClient.Get(secretCertName, metav1.GetOptions{})
if err != nil {
fmt.Println(err.Error())
return err
}
b := resp.GetObjectKind()
d := b.(*unstructured.Unstructured)
var content *KubeSecret
data, err := d.MarshalJSON()
if err != nil {
fmt.Println("Do something when errors appear") // telegram and rollback
fmt.Println(err.Error())
// panic(err)
return err
} else {
if err := json.Unmarshal(data, &content); err != nil {
fmt.Println(err.Error())
fmt.Println("Do something when errors appear")
return err
}
}
// fmt.Println(string(content.Data["cert"]))
if TestMode {
Cert_path = "./"
}
err = ioutil.WriteFile(Cert_path+secretCertName+".pem", content.Data["cert"], 0644)
// err := ioutil.WriteFile("./"+secretCertName, cfg, 0644)
return err
}