This repository has been archived by the owner on Mar 23, 2020. It is now read-only.
forked from openshift/machine-config-operator
/
controller_context.go
87 lines (75 loc) · 4.12 KB
/
controller_context.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
package common
import (
"math/rand"
"time"
"github.com/golang/glog"
configinformers "github.com/openshift/client-go/config/informers/externalversions"
"github.com/openshift/machine-config-operator/internal/clients"
mcfginformers "github.com/openshift/machine-config-operator/pkg/generated/informers/externalversions"
apiextinformers "k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/informers"
)
const (
minResyncPeriod = 20 * time.Minute
)
func resyncPeriod() func() time.Duration {
return func() time.Duration {
factor := rand.Float64() + 1
return time.Duration(float64(minResyncPeriod.Nanoseconds()) * factor)
}
}
// ControllerContext stores all the informers for a variety of kubernetes objects.
type ControllerContext struct {
ClientBuilder *clients.Builder
NamespacedInformerFactory mcfginformers.SharedInformerFactory
InformerFactory mcfginformers.SharedInformerFactory
KubeInformerFactory informers.SharedInformerFactory
KubeNamespacedInformerFactory informers.SharedInformerFactory
OpenShiftConfigKubeNamespacedInformerFactory informers.SharedInformerFactory
APIExtInformerFactory apiextinformers.SharedInformerFactory
ConfigInformerFactory configinformers.SharedInformerFactory
AvailableResources map[schema.GroupVersionResource]bool
Stop <-chan struct{}
InformersStarted chan struct{}
ResyncPeriod func() time.Duration
}
// CreateControllerContext creates the ControllerContext with the ClientBuilder.
func CreateControllerContext(cb *clients.Builder, stop <-chan struct{}, targetNamespace string) *ControllerContext {
client := cb.MachineConfigClientOrDie("machine-config-shared-informer")
kubeClient := cb.KubeClientOrDie("kube-shared-informer")
apiExtClient := cb.APIExtClientOrDie("apiext-shared-informer")
configClient := cb.ConfigClientOrDie("config-shared-informer")
sharedInformers := mcfginformers.NewSharedInformerFactory(client, resyncPeriod()())
sharedNamespacedInformers := mcfginformers.NewFilteredSharedInformerFactory(client, resyncPeriod()(), targetNamespace, nil)
kubeSharedInformer := informers.NewSharedInformerFactory(kubeClient, resyncPeriod()())
kubeNamespacedSharedInformer := informers.NewFilteredSharedInformerFactory(kubeClient, resyncPeriod()(), targetNamespace, nil)
openShiftConfigKubeNamespacedSharedInformer := informers.NewFilteredSharedInformerFactory(kubeClient, resyncPeriod()(), "openshift-config", nil)
// filter out CRDs that do not have the MCO label
assignFilterLabels := func(opts *metav1.ListOptions) {
labelsMap, err := labels.ConvertSelectorToLabelsMap(opts.LabelSelector)
if err != nil {
glog.Warningf("unable to convert selector %q to map: %v", opts.LabelSelector, err)
return
}
opts.LabelSelector = labels.Merge(labelsMap, map[string]string{"openshift.io/operator-managed": ""}).String()
}
apiExtSharedInformer := apiextinformers.NewSharedInformerFactoryWithOptions(apiExtClient, resyncPeriod()(),
apiextinformers.WithNamespace(targetNamespace), apiextinformers.WithTweakListOptions(assignFilterLabels))
configSharedInformer := configinformers.NewSharedInformerFactory(configClient, resyncPeriod()())
return &ControllerContext{
ClientBuilder: cb,
NamespacedInformerFactory: sharedNamespacedInformers,
InformerFactory: sharedInformers,
KubeInformerFactory: kubeSharedInformer,
KubeNamespacedInformerFactory: kubeNamespacedSharedInformer,
OpenShiftConfigKubeNamespacedInformerFactory: openShiftConfigKubeNamespacedSharedInformer,
APIExtInformerFactory: apiExtSharedInformer,
ConfigInformerFactory: configSharedInformer,
Stop: stop,
InformersStarted: make(chan struct{}),
ResyncPeriod: resyncPeriod(),
}
}