-
Notifications
You must be signed in to change notification settings - Fork 113
/
controller.go
104 lines (83 loc) · 3.31 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
package gardener
import (
"fmt"
"strings"
"time"
"github.com/kyma-project/control-plane/components/metris/internal/log"
ginformers "github.com/gardener/gardener/pkg/client/core/informers/externalversions"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
kubeinformers "k8s.io/client-go/informers"
"k8s.io/client-go/tools/cache"
)
const (
labelAccountID = "account"
labelSubAccountID = "subaccount"
labelHyperscalerType = "hyperscalerType"
fieldSecretBindingName = "spec.secretBindingName"
fieldCloudProfileName = "spec.cloudProfileName"
defaultResyncPeriod = time.Second * 30
)
// NewController return a new controller for watching shoots and secrets.
func NewController(client *Client, provider string, clusterChannel chan<- *Cluster, logger log.Logger) (*Controller, error) {
gardenerInformerFactory := ginformers.NewSharedInformerFactoryWithOptions(
client.GClientset,
defaultResyncPeriod,
ginformers.WithNamespace(client.Namespace),
ginformers.WithTweakListOptions(func(opts *metav1.ListOptions) {
opts.FieldSelector = fields.SelectorFromSet(fields.Set{fieldCloudProfileName: provider}).String()
}),
)
hyperscalertype := provider
if hyperscalertype == "az" {
hyperscalertype = "azure"
}
kubeInformerFactory := kubeinformers.NewSharedInformerFactoryWithOptions(
client.KClientset,
defaultResyncPeriod,
kubeinformers.WithNamespace(client.Namespace),
kubeinformers.WithTweakListOptions(func(opts *metav1.ListOptions) {
opts.LabelSelector = labels.SelectorFromSet(labels.Set{labelHyperscalerType: hyperscalertype}).String()
}),
)
shootInformer := gardenerInformerFactory.Core().V1beta1().Shoots()
secretInformer := kubeInformerFactory.Core().V1().Secrets()
controller := &Controller{
providertype: strings.ToLower(provider),
client: client,
gardenerInformerFactory: gardenerInformerFactory,
kubeInformerFactory: kubeInformerFactory,
shootInformer: shootInformer,
secretInformer: secretInformer,
clusterChannel: clusterChannel,
logger: logger,
}
// Set up event handlers for Shoot resources
shootInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.shootAddHandlerFunc,
UpdateFunc: controller.shootUpdateHandlerFunc,
DeleteFunc: controller.shootDeleteHandlerFunc,
})
// Set up event handler for Secret resources
secretInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: controller.secretUpdateHandlerFunc,
})
return controller, nil
}
// Run will set up the event handlers for secrets and shoots, as well as syncing informer caches.
func (c *Controller) Run(stop <-chan struct{}) error {
c.logger.Info("controller started")
defer c.logger.Info("controller stopped")
// Start the informer factories to begin populating the informer caches
c.gardenerInformerFactory.Start(stop)
c.kubeInformerFactory.Start(stop)
c.logger.Debug("waiting for informer caches to sync")
if ok := cache.WaitForCacheSync(stop, c.shootInformer.Informer().HasSynced, c.secretInformer.Informer().HasSynced); !ok {
return fmt.Errorf("error waiting for cache to sync")
}
c.logger.Debug("informer caches sync completed")
// wait for stop signal from the workgroup
<-stop
return nil
}