-
Notifications
You must be signed in to change notification settings - Fork 2
/
controller.go
239 lines (209 loc) · 7.33 KB
/
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
package main
import (
"fmt"
"time"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/util/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
coreinformers "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
corelisters "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
keyvaultsecretv1alpha1 "github.com/twendt/secret-controller/pkg/apis/secretcontroller/v1alpha1"
clientset "github.com/twendt/secret-controller/pkg/client/clientset/versioned"
secretscheme "github.com/twendt/secret-controller/pkg/client/clientset/versioned/scheme"
informers "github.com/twendt/secret-controller/pkg/client/informers/externalversions/secretcontroller/v1alpha1"
listers "github.com/twendt/secret-controller/pkg/client/listers/secretcontroller/v1alpha1"
"github.com/twendt/secret-controller/pkg/secretstore"
)
const controllerAgentName = "secret-controller"
const (
// SecretCreated is used as part of the Event 'reason' when a Foo is synced
SecretCreated = "Created"
// ErrResourceExists is used as part of the Event 'reason' when a Foo fails
// to sync due to a Deployment of the same name already existing.
ErrResourceExists = "ErrResourceExists"
// MessageSecretCreated is the message used for an Event fired when a KeyvaultSecret
// is created successfully
MessageSecretCreated = "Key Vault Secret created successfully"
)
// Controller is the controller implementation for KeyvaultSecret resources
type Controller struct {
kubeclientset kubernetes.Interface
crdclientset clientset.Interface
keyvaultClient secretstore.Client
keyvaultSecretInformer informers.KeyvaultSecretInformer
keyvaultSecretsLister listers.KeyvaultSecretLister
keyvaultSecretsSynced cache.InformerSynced
secretsLister corelisters.SecretLister
workqueue workqueue.RateLimitingInterface
recorder record.EventRecorder
logger *logrus.Entry
}
// NewController returns a new controller
func NewController(
kubeclientset kubernetes.Interface,
crdclientset clientset.Interface,
kubeInformer coreinformers.SecretInformer,
keyvaultSecretInformer informers.KeyvaultSecretInformer,
keyvaultClient secretstore.Client,
logger *logrus.Entry) *Controller {
utilruntime.Must(secretscheme.AddToScheme(scheme.Scheme))
logger.Info("Creating event broadcaster")
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(logger.Infof)
eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: kubeclientset.CoreV1().Events("")})
recorder := eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: controllerAgentName})
controller := &Controller{
kubeclientset: kubeclientset,
crdclientset: crdclientset,
keyvaultClient: keyvaultClient,
keyvaultSecretInformer: keyvaultSecretInformer,
keyvaultSecretsLister: keyvaultSecretInformer.Lister(),
keyvaultSecretsSynced: keyvaultSecretInformer.Informer().HasSynced,
secretsLister: kubeInformer.Lister(),
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "KeyvaultSecrets"),
recorder: recorder,
logger: logger,
}
return controller
}
// Run starts the worker go routines
func (c *Controller) Run(threadiness int, stopCh <-chan struct{}) error {
defer runtime.HandleCrash()
defer c.workqueue.ShutDown()
c.logger.Info("Starting Secret controller")
c.setupWatches()
c.logger.Info("Waiting for informer caches to sync")
if ok := cache.WaitForCacheSync(stopCh, c.keyvaultSecretsSynced); !ok {
return fmt.Errorf("failed to wait for caches to sync")
}
c.logger.Info("Starting workers")
for i := 0; i < threadiness; i++ {
go wait.Until(c.runWorker, time.Second, stopCh)
}
c.logger.Info("Started workers")
<-stopCh
c.logger.Info("Shutting down workers")
return nil
}
func (c *Controller) setupWatches() {
c.logger.Info("Setting up event handlers")
c.keyvaultSecretInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(obj); err != nil {
runtime.HandleError(err)
return
}
c.workqueue.AddRateLimited(key)
},
UpdateFunc: func(old, new interface{}) {
oldObj := old.(*keyvaultsecretv1alpha1.KeyvaultSecret)
newObj := new.(*keyvaultsecretv1alpha1.KeyvaultSecret)
if oldObj.GetResourceVersion() != newObj.GetResourceVersion() {
c.enqueueKeyvaultSecret(new)
}
},
DeleteFunc: func(obj interface{}) {
var key string
var err error
if key, err = cache.DeletionHandlingMetaNamespaceKeyFunc(obj); err != nil {
runtime.HandleError(err)
return
}
c.workqueue.AddRateLimited(key)
},
})
}
func (c *Controller) runWorker() {
for c.processNextWorkItem() {
}
}
func (c *Controller) processNextWorkItem() bool {
obj, shutdown := c.workqueue.Get()
if shutdown {
return false
}
err := c.processItem(obj)
if err != nil {
runtime.HandleError(err)
return true
}
return true
}
func (c *Controller) processItem(obj interface{}) error {
defer c.workqueue.Done(obj)
var key string
var ok bool
if key, ok = obj.(string); !ok {
c.workqueue.Forget(obj)
runtime.HandleError(fmt.Errorf("expected string in workqueue but got %#v", obj))
return nil
}
if err := c.secretHandler(key); err != nil {
c.workqueue.AddRateLimited(key)
return fmt.Errorf("error syncing '%s': %s, requeuing", key, err.Error())
}
c.workqueue.Forget(obj)
c.logger.Infof("Successfully synced '%s'", key)
return nil
}
func (c *Controller) secretHandler(key string) error {
// Convert the namespace/name string into a distinct namespace and name
namespace, secretName, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
runtime.HandleError(fmt.Errorf("invalid resource key: %s", key))
return nil
}
keyvaultSecret, err := c.keyvaultSecretsLister.KeyvaultSecrets(namespace).Get(secretName)
if errors.IsNotFound(err) {
c.logger.Infof("keyvault secret key '%s' deleted", key)
return nil
}
if err != nil {
return err
}
if err := c.createOrUpdateSecret(keyvaultSecret); err != nil {
return err
}
c.recorder.Event(keyvaultSecret, corev1.EventTypeNormal, SecretCreated, MessageSecretCreated)
return nil
}
func (c *Controller) createOrUpdateSecret(keyvaultSecret *keyvaultsecretv1alpha1.KeyvaultSecret) error {
converter := SecretConverter{keyvaultSecret: keyvaultSecret, storeClient: c.keyvaultClient}
secret, err := converter.getK8sSecret()
if err != nil {
return err
}
_, err = c.kubeclientset.CoreV1().Secrets(keyvaultSecret.Namespace).Update(secret)
if err != nil {
if errors.IsNotFound(err) {
_, err = c.kubeclientset.CoreV1().Secrets(keyvaultSecret.Namespace).Create(secret)
if err != nil {
c.logger.Errorf("Failed to create secret %s : %s", secret.Name, err)
return err
}
return nil
}
return err
}
return nil
}
func (c *Controller) enqueueKeyvaultSecret(obj interface{}) {
var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(obj); err != nil {
runtime.HandleError(err)
return
}
c.workqueue.AddRateLimited(key)
}