/
certsync_controller.go
277 lines (235 loc) · 9.99 KB
/
certsync_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
package certsyncpod
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"reflect"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
corev1interface "k8s.io/client-go/kubernetes/typed/core/v1"
v1 "k8s.io/client-go/listers/core/v1"
"k8s.io/klog/v2"
"github.com/openshift/library-go/pkg/controller/factory"
"github.com/openshift/library-go/pkg/operator/events"
"github.com/openshift/library-go/pkg/operator/staticpod"
"github.com/openshift/library-go/pkg/operator/staticpod/controller/installer"
)
type CertSyncController struct {
destinationDir string
namespace string
configMaps []installer.UnrevisionedResource
secrets []installer.UnrevisionedResource
configmapGetter corev1interface.ConfigMapInterface
configMapLister v1.ConfigMapLister
secretGetter corev1interface.SecretInterface
secretLister v1.SecretLister
eventRecorder events.Recorder
}
func NewCertSyncController(targetDir, targetNamespace string, configmaps, secrets []installer.UnrevisionedResource, kubeClient kubernetes.Interface, informers informers.SharedInformerFactory, eventRecorder events.Recorder) factory.Controller {
c := &CertSyncController{
destinationDir: targetDir,
namespace: targetNamespace,
configMaps: configmaps,
secrets: secrets,
eventRecorder: eventRecorder.WithComponentSuffix("cert-sync-controller"),
configmapGetter: kubeClient.CoreV1().ConfigMaps(targetNamespace),
configMapLister: informers.Core().V1().ConfigMaps().Lister(),
secretLister: informers.Core().V1().Secrets().Lister(),
secretGetter: kubeClient.CoreV1().Secrets(targetNamespace),
}
return factory.New().WithInformers(informers.Core().V1().ConfigMaps().Informer(), informers.Core().V1().Secrets().Informer()).WithSync(c.sync).ToController("CertSyncController", eventRecorder)
}
func getConfigMapDir(targetDir, configMapName string) string {
return filepath.Join(targetDir, "configmaps", configMapName)
}
func getSecretDir(targetDir, secretName string) string {
return filepath.Join(targetDir, "secrets", secretName)
}
func (c *CertSyncController) sync(ctx context.Context, syncCtx factory.SyncContext) error {
errors := []error{}
klog.Infof("Syncing configmaps: %v", c.configMaps)
for _, cm := range c.configMaps {
configMap, err := c.configMapLister.ConfigMaps(c.namespace).Get(cm.Name)
switch {
case apierrors.IsNotFound(err) && !cm.Optional:
errors = append(errors, err)
continue
case apierrors.IsNotFound(err) && cm.Optional:
configMapFile := getConfigMapDir(c.destinationDir, cm.Name)
if _, err := os.Stat(configMapFile); os.IsNotExist(err) {
// if the configmap file does not exist, there is no work to do, so skip making any live check and just return.
// if the configmap actually exists in the API, we'll eventually see it on the watch.
continue
}
// Check with the live call it is really missing
configMap, err = c.configmapGetter.Get(ctx, cm.Name, metav1.GetOptions{})
if err == nil {
klog.Infof("Caches are stale. They don't see configmap '%s/%s', yet it is present", configMap.Namespace, configMap.Name)
// We will get re-queued when we observe the change
continue
}
if !apierrors.IsNotFound(err) {
errors = append(errors, err)
continue
}
// remove missing content
if err := os.RemoveAll(configMapFile); err != nil {
c.eventRecorder.Warningf("CertificateUpdateFailed", "Failed removing file for configmap: %s/%s: %v", c.namespace, cm.Name, err)
errors = append(errors, err)
}
c.eventRecorder.Eventf("CertificateRemoved", "Removed file for configmap: %s/%s", c.namespace, cm.Name)
continue
case err != nil:
c.eventRecorder.Warningf("CertificateUpdateFailed", "Failed getting configmap: %s/%s: %v", c.namespace, cm.Name, err)
errors = append(errors, err)
continue
}
contentDir := getConfigMapDir(c.destinationDir, cm.Name)
data := map[string]string{}
for filename := range configMap.Data {
fullFilename := filepath.Join(contentDir, filename)
existingContent, err := ioutil.ReadFile(fullFilename)
if err != nil {
if !os.IsNotExist(err) {
klog.Error(err)
}
continue
}
data[filename] = string(existingContent)
}
// Check if cached configmap differs
if reflect.DeepEqual(configMap.Data, data) {
continue
}
klog.V(2).Infof("Syncing updated configmap '%s/%s'.", configMap.Namespace, configMap.Name)
// We need to do a live get here so we don't overwrite a newer file with one from a stale cache
configMap, err = c.configmapGetter.Get(ctx, configMap.Name, metav1.GetOptions{})
if err != nil {
// Even if the error is not exists we will act on it when caches catch up
c.eventRecorder.Warningf("CertificateUpdateFailed", "Failed getting configmap: %s/%s: %v", c.namespace, cm.Name, err)
errors = append(errors, err)
continue
}
// Check if the live configmap differs
if reflect.DeepEqual(configMap.Data, data) {
klog.Infof("Caches are stale. The live configmap '%s/%s' is reflected on filesystem, but cached one differs", configMap.Namespace, configMap.Name)
continue
}
klog.Infof("Creating directory %q ...", contentDir)
if err := os.MkdirAll(contentDir, 0755); err != nil && !os.IsExist(err) {
c.eventRecorder.Warningf("CertificateUpdateFailed", "Failed creating directory for configmap: %s/%s: %v", configMap.Namespace, configMap.Name, err)
errors = append(errors, err)
continue
}
for filename, content := range configMap.Data {
fullFilename := filepath.Join(contentDir, filename)
// if the existing is the same, do nothing
if reflect.DeepEqual(data[fullFilename], content) {
continue
}
klog.Infof("Writing configmap manifest %q ...", fullFilename)
if err := staticpod.WriteFileAtomic([]byte(content), 0644, fullFilename); err != nil {
c.eventRecorder.Warningf("CertificateUpdateFailed", "Failed writing file for configmap: %s/%s: %v", configMap.Namespace, configMap.Name, err)
errors = append(errors, err)
continue
}
}
c.eventRecorder.Eventf("CertificateUpdated", "Wrote updated configmap: %s/%s", configMap.Namespace, configMap.Name)
}
klog.Infof("Syncing secrets: %v", c.secrets)
for _, s := range c.secrets {
secret, err := c.secretLister.Secrets(c.namespace).Get(s.Name)
switch {
case apierrors.IsNotFound(err) && !s.Optional:
errors = append(errors, err)
continue
case apierrors.IsNotFound(err) && s.Optional:
secretFile := getSecretDir(c.destinationDir, s.Name)
if _, err := os.Stat(secretFile); os.IsNotExist(err) {
// if the secret file does not exist, there is no work to do, so skip making any live check and just return.
// if the secret actually exists in the API, we'll eventually see it on the watch.
continue
}
// Check with the live call it is really missing
secret, err = c.secretGetter.Get(ctx, s.Name, metav1.GetOptions{})
if err == nil {
klog.Infof("Caches are stale. They don't see secret '%s/%s', yet it is present", secret.Namespace, secret.Name)
// We will get re-queued when we observe the change
continue
}
if !apierrors.IsNotFound(err) {
errors = append(errors, err)
continue
}
// remove missing content
if err := os.RemoveAll(secretFile); err != nil {
c.eventRecorder.Warningf("CertificateUpdateFailed", "Failed removing file for missing secret: %s/%s: %v", c.namespace, s.Name, err)
errors = append(errors, err)
continue
}
c.eventRecorder.Warningf("CertificateRemoved", "Removed file for missing secret: %s/%s", c.namespace, s.Name)
continue
case err != nil:
c.eventRecorder.Warningf("CertificateUpdateFailed", "Failed getting secret: %s/%s: %v", c.namespace, s.Name, err)
errors = append(errors, err)
continue
}
contentDir := getSecretDir(c.destinationDir, s.Name)
data := map[string][]byte{}
for filename := range secret.Data {
fullFilename := filepath.Join(contentDir, filename)
existingContent, err := ioutil.ReadFile(fullFilename)
if err != nil {
if !os.IsNotExist(err) {
klog.Error(err)
}
continue
}
data[filename] = existingContent
}
// Check if cached secret differs
if reflect.DeepEqual(secret.Data, data) {
continue
}
klog.V(2).Infof("Syncing updated secret '%s/%s'.", secret.Namespace, secret.Name)
// We need to do a live get here so we don't overwrite a newer file with one from a stale cache
secret, err = c.secretGetter.Get(ctx, secret.Name, metav1.GetOptions{})
if err != nil {
// Even if the error is not exists we will act on it when caches catch up
c.eventRecorder.Warningf("CertificateUpdateFailed", "Failed getting secret: %s/%s: %v", c.namespace, s.Name, err)
errors = append(errors, err)
continue
}
// Check if the live secret differs
if reflect.DeepEqual(secret.Data, data) {
klog.Infof("Caches are stale. The live secret '%s/%s' is reflected on filesystem, but cached one differs", secret.Namespace, secret.Name)
continue
}
klog.Infof("Creating directory %q ...", contentDir)
if err := os.MkdirAll(contentDir, 0755); err != nil && !os.IsExist(err) {
c.eventRecorder.Warningf("CertificateUpdateFailed", "Failed creating directory for secret: %s/%s: %v", secret.Namespace, secret.Name, err)
errors = append(errors, err)
continue
}
for filename, content := range secret.Data {
// TODO fix permissions
fullFilename := filepath.Join(contentDir, filename)
// if the existing is the same, do nothing
if reflect.DeepEqual(data[fullFilename], content) {
continue
}
klog.Infof("Writing secret manifest %q ...", fullFilename)
if err := staticpod.WriteFileAtomic(content, 0644, fullFilename); err != nil {
c.eventRecorder.Warningf("CertificateUpdateFailed", "Failed writing file for secret: %s/%s: %v", secret.Namespace, secret.Name, err)
errors = append(errors, err)
continue
}
}
c.eventRecorder.Eventf("CertificateUpdated", "Wrote updated secret: %s/%s", secret.Namespace, secret.Name)
}
return utilerrors.NewAggregate(errors)
}