-
Notifications
You must be signed in to change notification settings - Fork 44
/
operator-config.go
214 lines (187 loc) · 6.51 KB
/
operator-config.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
package cpoperator
import (
"context"
"fmt"
"os"
"time"
"github.com/go-logr/logr"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/informers"
kubeclient "k8s.io/client-go/kubernetes"
kubescheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/manager"
configclient "github.com/openshift/client-go/config/clientset/versioned"
configinformers "github.com/openshift/client-go/config/informers/externalversions"
common "github.com/openshift/ibm-roks-toolkit/pkg/controllers"
)
type ControllerSetupFunc func(*ControlPlaneOperatorConfig) error
func NewControlPlaneOperatorConfig(targetKubeconfig, namespace string, initialCA []byte, versions map[string]string, controllers []string, controllerFuncs map[string]ControllerSetupFunc) *ControlPlaneOperatorConfig {
return &ControlPlaneOperatorConfig{
targetKubeconfig: targetKubeconfig,
namespace: namespace,
initialCA: initialCA,
controllers: controllers,
controllerFuncs: controllerFuncs,
versions: versions,
}
}
func NewControlPlaneOperatorConfigWithRestConfig(config, targetConfig *rest.Config, namespace string, initialCA []byte, versions map[string]string, controllers []string, controllerFuncs map[string]ControllerSetupFunc) *ControlPlaneOperatorConfig {
return &ControlPlaneOperatorConfig{
config: config,
targetConfig: targetConfig,
namespace: namespace,
initialCA: initialCA,
controllers: controllers,
controllerFuncs: controllerFuncs,
versions: versions,
}
}
type ControlPlaneOperatorConfig struct {
manager ctrl.Manager
config *rest.Config
targetConfig *rest.Config
targetKubeClient kubeclient.Interface
kubeClient kubeclient.Interface
logger logr.Logger
scheme *runtime.Scheme
versions map[string]string
targetKubeconfig string
namespace string
initialCA []byte
controllers []string
controllerFuncs map[string]ControllerSetupFunc
namespacedInformers map[string]informers.SharedInformerFactory
}
func (c *ControlPlaneOperatorConfig) Scheme() *runtime.Scheme {
if c.scheme == nil {
c.scheme = runtime.NewScheme()
kubescheme.AddToScheme(c.scheme)
apiextensionsv1.AddToScheme(c.scheme)
}
return c.scheme
}
func (c *ControlPlaneOperatorConfig) Manager() ctrl.Manager {
if c.manager == nil {
var err error
c.manager, err = ctrl.NewManager(c.TargetConfig(), ctrl.Options{
Scheme: c.Scheme(),
LeaderElection: true,
LeaderElectionNamespace: c.TargetNamespace(),
LeaderElectionID: "control-plane-operator",
Namespace: c.TargetNamespace(),
})
if err != nil {
c.Fatal(err, "failed to create controller manager")
}
}
return c.manager
}
func (c *ControlPlaneOperatorConfig) Namespace() string {
return c.namespace
}
func (c *ControlPlaneOperatorConfig) TargetNamespace() string {
return "openshift-config-managed"
}
func (c *ControlPlaneOperatorConfig) Config() *rest.Config {
if c.config == nil {
c.config = ctrl.GetConfigOrDie()
}
return c.config
}
func (c *ControlPlaneOperatorConfig) Logger() logr.Logger {
if c.logger.GetSink() == nil {
c.logger = ctrl.Log.WithName("control-plane-operator")
}
return c.logger
}
func (c *ControlPlaneOperatorConfig) TargetConfig() *rest.Config {
if c.targetConfig == nil {
var err error
c.targetConfig, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: c.targetKubeconfig},
&clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
c.Fatal(err, "cannot get the target cluster's rest config")
}
}
return c.targetConfig
}
func (c *ControlPlaneOperatorConfig) TargetKubeClient() kubeclient.Interface {
if c.targetKubeClient == nil {
var err error
c.targetKubeClient, err = kubeclient.NewForConfig(c.TargetConfig())
if err != nil {
c.Fatal(err, "cannot get target kube client")
}
}
return c.targetKubeClient
}
func (c *ControlPlaneOperatorConfig) TargetConfigClient() configclient.Interface {
client, err := configclient.NewForConfig(c.TargetConfig())
if err != nil {
c.Fatal(err, "cannot get target config client")
}
return client
}
func (c *ControlPlaneOperatorConfig) TargetConfigInformers() configinformers.SharedInformerFactory {
informerFactory := configinformers.NewSharedInformerFactory(c.TargetConfigClient(), common.DefaultResync)
c.Manager().Add(manager.RunnableFunc(func(ctx context.Context) error {
informerFactory.Start(ctx.Done())
return nil
}))
return informerFactory
}
func (c *ControlPlaneOperatorConfig) TargetKubeInformersForNamespace(namespace string) informers.SharedInformerFactory {
return c.TargetKubeInformersForNamespaceWithInterval(namespace, common.DefaultResync)
}
func (c *ControlPlaneOperatorConfig) TargetKubeInformersForNamespaceWithInterval(namespace string, syncInterval time.Duration) informers.SharedInformerFactory {
informer, exists := c.namespacedInformers[namespace]
if !exists {
informer = informers.NewSharedInformerFactoryWithOptions(c.TargetKubeClient(), syncInterval, informers.WithNamespace(namespace))
if c.namespacedInformers == nil {
c.namespacedInformers = map[string]informers.SharedInformerFactory{}
}
c.namespacedInformers[namespace] = informer
c.Manager().Add(manager.RunnableFunc(func(ctx context.Context) error {
informer.Start(ctx.Done())
return nil
}))
}
return informer
}
func (c *ControlPlaneOperatorConfig) KubeClient() kubeclient.Interface {
if c.kubeClient == nil {
var err error
c.kubeClient, err = kubeclient.NewForConfig(c.Config())
if err != nil {
c.Fatal(err, "cannot get management kube client")
}
}
return c.kubeClient
}
func (c *ControlPlaneOperatorConfig) Versions() map[string]string {
return c.versions
}
func (c *ControlPlaneOperatorConfig) InitialCA() string {
return string(c.initialCA)
}
func (c *ControlPlaneOperatorConfig) Fatal(err error, msg string) {
c.Logger().Error(err, msg)
os.Exit(1)
}
func (c *ControlPlaneOperatorConfig) Start(ctx context.Context) error {
for _, controllerName := range c.controllers {
setupFunc, ok := c.controllerFuncs[controllerName]
if !ok {
return fmt.Errorf("unknown controller specified: %s", controllerName)
}
if err := setupFunc(c); err != nil {
return fmt.Errorf("cannot setup controller %s: %v", controllerName, err)
}
}
return c.Manager().Start(ctx)
}