-
Notifications
You must be signed in to change notification settings - Fork 88
/
namespaces.go
63 lines (49 loc) · 1.48 KB
/
namespaces.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
package client
import (
"log"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/k8sutil"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/informers"
"k8s.io/client-go/tools/cache"
)
func (c *Client) runNamespacesInformer() error {
clientset, err := k8sutil.GetClientset()
if err != nil {
return errors.Wrap(err, "failed to get clientset")
}
c.namespaceStopChan = make(chan struct{})
factory := informers.NewSharedInformerFactory(clientset, 0)
nsInformer := factory.Core().V1().Namespaces().Informer()
nsInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
addedNamespace := obj.(*corev1.Namespace)
for _, watchedNamespace := range c.watchedNamespaces {
deployImagePullSecret := false
if watchedNamespace == "*" {
deployImagePullSecret = true
} else {
if watchedNamespace == addedNamespace.Name {
deployImagePullSecret = true
}
}
if !deployImagePullSecret {
continue
}
if err := c.ensureImagePullSecretsPresent(addedNamespace.Name, c.imagePullSecrets); err != nil {
// we don't fail here...
log.Printf("error ensuring image pull secrets for namespace %s: %s", addedNamespace.Name, err.Error())
}
c.ApplyHooksInformer([]string{addedNamespace.Name})
}
},
})
go nsInformer.Run(c.namespaceStopChan)
return nil
}
func (c *Client) shutdownNamespacesInformer() {
if c.namespaceStopChan != nil {
c.namespaceStopChan <- struct{}{}
}
c.namespaceStopChan = nil
}