/
controllers.go
290 lines (255 loc) · 10 KB
/
controllers.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package staticpod
import (
"fmt"
"github.com/openshift/library-go/pkg/controller/manager"
"github.com/openshift/library-go/pkg/operator/resource/resourceapply"
"github.com/openshift/library-go/pkg/operator/staticresourcecontroller"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/klog"
operatorv1 "github.com/openshift/api/operator/v1"
"github.com/openshift/library-go/pkg/operator/events"
"github.com/openshift/library-go/pkg/operator/loglevel"
"github.com/openshift/library-go/pkg/operator/revisioncontroller"
"github.com/openshift/library-go/pkg/operator/staticpod/controller/backingresource"
"github.com/openshift/library-go/pkg/operator/staticpod/controller/installer"
"github.com/openshift/library-go/pkg/operator/staticpod/controller/installerstate"
"github.com/openshift/library-go/pkg/operator/staticpod/controller/monitoring"
"github.com/openshift/library-go/pkg/operator/staticpod/controller/node"
"github.com/openshift/library-go/pkg/operator/staticpod/controller/prune"
"github.com/openshift/library-go/pkg/operator/staticpod/controller/staticpodstate"
"github.com/openshift/library-go/pkg/operator/status"
"github.com/openshift/library-go/pkg/operator/unsupportedconfigoverridescontroller"
"github.com/openshift/library-go/pkg/operator/v1helpers"
)
type staticPodOperatorControllerBuilder struct {
// clients and related
staticPodOperatorClient v1helpers.StaticPodOperatorClient
kubeClient kubernetes.Interface
kubeInformers v1helpers.KubeInformersForNamespaces
dynamicClient dynamic.Interface
eventRecorder events.Recorder
// resource information
operandNamespace string
staticPodName string
revisionConfigMaps []revisioncontroller.RevisionResource
revisionSecrets []revisioncontroller.RevisionResource
// cert information
certDir string
certConfigMaps []revisioncontroller.RevisionResource
certSecrets []revisioncontroller.RevisionResource
// versioner information
versionRecorder status.VersionGetter
operatorNamespace string
operandName string
// installer information
installCommand []string
installerPodMutationFunc installer.InstallerPodMutationFunc
// pruning information
pruneCommand []string
// TODO de-dupe this. I think it's actually a directory name
staticPodPrefix string
// TODO: remove this after all operators get rid of service monitor controller
enableServiceMonitorController bool
}
func NewBuilder(
staticPodOperatorClient v1helpers.StaticPodOperatorClient,
kubeClient kubernetes.Interface,
kubeInformers v1helpers.KubeInformersForNamespaces,
) Builder {
return &staticPodOperatorControllerBuilder{
staticPodOperatorClient: staticPodOperatorClient,
kubeClient: kubeClient,
kubeInformers: kubeInformers,
}
}
// Builder allows the caller to construct a set of static pod controllers in pieces
type Builder interface {
WithEvents(eventRecorder events.Recorder) Builder
WithServiceMonitor(dynamicClient dynamic.Interface) Builder
WithVersioning(operatorNamespace, operandName string, versionRecorder status.VersionGetter) Builder
WithResources(operandNamespace, staticPodName string, revisionConfigMaps, revisionSecrets []revisioncontroller.RevisionResource) Builder
WithCerts(certDir string, certConfigMaps, certSecrets []revisioncontroller.RevisionResource) Builder
WithInstaller(command []string) Builder
// WithCustomInstaller allows mutating the installer pod definition just before
// the installer pod is created for a revision.
WithCustomInstaller(command []string, installerPodMutationFunc installer.InstallerPodMutationFunc) Builder
WithPruning(command []string, staticPodPrefix string) Builder
ToControllers() (manager.ControllerManager, error)
}
func (b *staticPodOperatorControllerBuilder) WithEvents(eventRecorder events.Recorder) Builder {
b.eventRecorder = eventRecorder
return b
}
// DEPRECATED: We have moved all our operators now to have this manifest with customized content.
func (b *staticPodOperatorControllerBuilder) WithServiceMonitor(dynamicClient dynamic.Interface) Builder {
klog.Warning("DEPRECATED: MonitoringResourceController is no longer needed")
b.enableServiceMonitorController = true
b.dynamicClient = dynamicClient
return b
}
func (b *staticPodOperatorControllerBuilder) WithVersioning(operatorNamespace, operandName string, versionRecorder status.VersionGetter) Builder {
b.operatorNamespace = operatorNamespace
b.operandName = operandName
b.versionRecorder = versionRecorder
return b
}
func (b *staticPodOperatorControllerBuilder) WithResources(operandNamespace, staticPodName string, revisionConfigMaps, revisionSecrets []revisioncontroller.RevisionResource) Builder {
b.operandNamespace = operandNamespace
b.staticPodName = staticPodName
b.revisionConfigMaps = revisionConfigMaps
b.revisionSecrets = revisionSecrets
return b
}
func (b *staticPodOperatorControllerBuilder) WithCerts(certDir string, certConfigMaps, certSecrets []revisioncontroller.RevisionResource) Builder {
b.certDir = certDir
b.certConfigMaps = certConfigMaps
b.certSecrets = certSecrets
return b
}
func (b *staticPodOperatorControllerBuilder) WithInstaller(command []string) Builder {
b.installCommand = command
b.installerPodMutationFunc = func(pod *corev1.Pod, nodeName string, operatorSpec *operatorv1.StaticPodOperatorSpec, revision int32) error {
return nil
}
return b
}
// WithCustomInstaller allows mutating the installer pod definition just before
// the installer pod is created for a revision.
func (b *staticPodOperatorControllerBuilder) WithCustomInstaller(command []string, installerPodMutationFunc installer.InstallerPodMutationFunc) Builder {
b.installCommand = command
b.installerPodMutationFunc = installerPodMutationFunc
return b
}
func (b *staticPodOperatorControllerBuilder) WithPruning(command []string, staticPodPrefix string) Builder {
b.pruneCommand = command
b.staticPodPrefix = staticPodPrefix
return b
}
func (b *staticPodOperatorControllerBuilder) ToControllers() (manager.ControllerManager, error) {
manager := manager.NewControllerManager()
eventRecorder := b.eventRecorder
if eventRecorder == nil {
eventRecorder = events.NewLoggingEventRecorder("static-pod-operator-controller")
}
versionRecorder := b.versionRecorder
if versionRecorder == nil {
versionRecorder = status.NewVersionGetter()
}
configMapClient := v1helpers.CachedConfigMapGetter(b.kubeClient.CoreV1(), b.kubeInformers)
secretClient := v1helpers.CachedSecretGetter(b.kubeClient.CoreV1(), b.kubeInformers)
podClient := b.kubeClient.CoreV1()
eventsClient := b.kubeClient.CoreV1()
operandInformers := b.kubeInformers.InformersFor(b.operandNamespace)
clusterInformers := b.kubeInformers.InformersFor("")
var errs []error
if len(b.operandNamespace) > 0 {
manager.WithController(revisioncontroller.NewRevisionController(
b.operandNamespace,
b.revisionConfigMaps,
b.revisionSecrets,
operandInformers,
revisioncontroller.StaticPodLatestRevisionClient{StaticPodOperatorClient: b.staticPodOperatorClient},
configMapClient,
secretClient,
eventRecorder,
), 1)
} else {
errs = append(errs, fmt.Errorf("missing revisionController; cannot proceed"))
}
if len(b.installCommand) > 0 {
manager.WithController(installer.NewInstallerController(
b.operandNamespace,
b.staticPodName,
b.revisionConfigMaps,
b.revisionSecrets,
b.installCommand,
operandInformers,
b.staticPodOperatorClient,
configMapClient,
secretClient,
podClient,
eventRecorder,
).WithCerts(
b.certDir,
b.certConfigMaps,
b.certSecrets,
).WithInstallerPodMutationFn(
b.installerPodMutationFunc,
), 1)
manager.WithController(installerstate.NewInstallerStateController(
operandInformers,
podClient,
eventsClient,
b.staticPodOperatorClient,
b.operandNamespace,
eventRecorder,
), 1)
} else {
errs = append(errs, fmt.Errorf("missing installerController; cannot proceed"))
}
if len(b.operandName) > 0 {
// TODO add handling for operator configmap changes to get version-mapping changes
manager.WithController(staticpodstate.NewStaticPodStateController(
b.operandNamespace,
b.staticPodName,
b.operatorNamespace,
b.operandName,
operandInformers,
b.staticPodOperatorClient,
configMapClient,
podClient,
versionRecorder,
eventRecorder,
), 1)
} else {
eventRecorder.Warning("StaticPodStateControllerMissing", "not enough information provided, not all functionality is present")
}
if len(b.pruneCommand) > 0 {
manager.WithController(prune.NewPruneController(
b.operandNamespace,
b.staticPodPrefix,
b.pruneCommand,
configMapClient,
secretClient,
podClient,
b.staticPodOperatorClient,
eventRecorder,
), 1)
} else {
eventRecorder.Warning("PruningControllerMissing", "not enough information provided, not all functionality is present")
}
manager.WithController(node.NewNodeController(
b.staticPodOperatorClient,
clusterInformers,
eventRecorder,
), 1)
// this cleverly sets the same condition that used to be set because of the way that the names are constructed
manager.WithController(staticresourcecontroller.NewStaticResourceController(
"BackingResourceController",
backingresource.StaticPodManifests(b.operandNamespace),
[]string{
"manifests/installer-sa.yaml",
"manifests/installer-cluster-rolebinding.yaml",
},
resourceapply.NewKubeClientHolder(b.kubeClient),
b.staticPodOperatorClient,
eventRecorder,
).AddKubeInformers(b.kubeInformers), 1)
if b.dynamicClient != nil && b.enableServiceMonitorController {
manager.WithController(monitoring.NewMonitoringResourceController(
b.operandNamespace,
b.operandNamespace,
b.staticPodOperatorClient,
operandInformers,
b.kubeClient,
b.dynamicClient,
eventRecorder,
), 1)
}
manager.WithController(unsupportedconfigoverridescontroller.NewUnsupportedConfigOverridesController(b.staticPodOperatorClient, eventRecorder), 1)
manager.WithController(loglevel.NewClusterOperatorLoggingController(b.staticPodOperatorClient, eventRecorder), 1)
return manager, errors.NewAggregate(errs)
}