-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathconsoleplugin_reconciler.go
More file actions
274 lines (240 loc) · 8.88 KB
/
consoleplugin_reconciler.go
File metadata and controls
274 lines (240 loc) · 8.88 KB
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
package consoleplugin
import (
"context"
"reflect"
osv1 "github.com/openshift/api/console/v1"
operatorsv1 "github.com/openshift/api/operator/v1"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
appsv1 "k8s.io/api/apps/v1"
ascv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/log"
flowslatest "github.com/netobserv/netobserv-operator/api/flowcollector/v1beta2"
"github.com/netobserv/netobserv-operator/internal/controller/constants"
"github.com/netobserv/netobserv-operator/internal/controller/reconcilers"
"github.com/netobserv/netobserv-operator/internal/pkg/helper"
"github.com/netobserv/netobserv-operator/internal/pkg/manager/status"
"github.com/netobserv/netobserv-operator/internal/pkg/resources"
apierrors "k8s.io/apimachinery/pkg/api/errors"
)
// Type alias
type pluginSpec = flowslatest.FlowCollectorConsolePlugin
// CPReconciler reconciles the current console plugin state with the desired configuration
type CPReconciler struct {
*reconcilers.Instance
deployment *appsv1.Deployment
service *corev1.Service
metricsService *corev1.Service
hpa *ascv2.HorizontalPodAutoscaler
serviceAccount *corev1.ServiceAccount
configMap *corev1.ConfigMap
serviceMonitor *monitoringv1.ServiceMonitor
}
func NewReconciler(cmn *reconcilers.Instance) CPReconciler {
rec := CPReconciler{
Instance: cmn,
deployment: cmn.Managed.NewDeployment(constants.PluginName),
service: cmn.Managed.NewService(constants.PluginName),
metricsService: cmn.Managed.NewService(metricsSvcName),
hpa: cmn.Managed.NewHPA(constants.PluginName),
serviceAccount: cmn.Managed.NewServiceAccount(constants.PluginName),
configMap: cmn.Managed.NewConfigMap(configMapName),
}
if cmn.ClusterInfo.HasSvcMonitor() {
rec.serviceMonitor = cmn.Managed.NewServiceMonitor(constants.PluginName)
}
return rec
}
// Reconcile is the reconciler entry point to reconcile the current plugin state with the desired configuration
func (r *CPReconciler) Reconcile(ctx context.Context, desired *flowslatest.FlowCollector, lokiStatus status.ComponentStatus) error {
l := log.FromContext(ctx).WithName("web-console")
ctx = log.IntoContext(ctx, l)
defer r.Status.Commit(ctx, r.Client)
err := r.reconcile(ctx, desired, lokiStatus)
if err != nil {
l.Error(err, "Web Console reconcile failure")
// Set status failure unless it was already set
if !r.Status.HasFailure() {
r.Status.SetFailure("WebConsoleError", err.Error())
}
return err
}
return nil
}
func (r *CPReconciler) reconcile(ctx context.Context, desired *flowslatest.FlowCollector, lokiStatus status.ComponentStatus) error {
// Retrieve current owned objects
err := r.Managed.FetchAll(ctx)
if err != nil {
return err
}
if r.ClusterInfo.HasConsolePlugin() {
if err = r.checkAutoPatch(ctx, desired, constants.PluginName); err != nil {
return err
}
}
if desired.Spec.UseConsolePlugin() && (r.ClusterInfo.HasConsolePlugin() || desired.Spec.ConsolePlugin.Standalone) && !desired.Spec.OnHold() {
// Create object builder
builder := newBuilder(r.Instance, &desired.Spec, constants.PluginName)
if err := r.reconcilePermissions(ctx, &builder, constants.PluginName); err != nil {
return err
}
if r.ClusterInfo.HasConsolePlugin() {
if err = r.reconcilePlugin(ctx, &builder, &desired.Spec, constants.PluginName, "NetObserv plugin"); err != nil {
return err
}
}
cmDigest, err := r.reconcileConfigMap(ctx, &builder, lokiStatus)
if err != nil {
return err
}
if err = r.reconcileDeployment(ctx, &builder, &desired.Spec, constants.PluginName, cmDigest); err != nil {
return err
}
if err = r.reconcileServices(ctx, &builder, constants.PluginName); err != nil {
return err
}
if err = r.reconcileHPA(ctx, &builder, &desired.Spec); err != nil {
return err
}
if desired.Spec.UseLoki() {
// Watch for Loki certificates if necessary; we'll ignore in that case the returned digest, as we don't need to restart pods on cert rotation
// because certificate is always reloaded from file
if _, err = r.Watcher.ProcessCACert(ctx, r.Client, &r.Loki.TLS, r.Namespace); err != nil {
return err
}
if _, _, err = r.Watcher.ProcessMTLSCerts(ctx, r.Client, &r.Loki.StatusTLS, r.Namespace); err != nil {
return err
}
}
} else {
// delete any existing owned object
r.Managed.TryDeleteAll(ctx)
if desired.Spec.OnHold() {
r.Status.SetUnused("FlowCollector is on hold")
} else {
r.Status.SetUnused("Web console not enabled")
}
}
return nil
}
func (r *CPReconciler) checkAutoPatch(ctx context.Context, desired *flowslatest.FlowCollector, name string) error {
console := operatorsv1.Console{}
advancedConfig := helper.GetAdvancedPluginConfig(desired.Spec.ConsolePlugin.Advanced)
reg := desired.Spec.UseConsolePlugin() && *advancedConfig.Register
if err := r.Client.Get(ctx, types.NamespacedName{Name: "cluster"}, &console); err != nil {
// Console operator CR not found => warn but continue execution
if reg {
log.FromContext(ctx).Error(err, "Could not get the Console Operator resource for plugin registration. Please register manually.")
}
return nil
}
registered := helper.ContainsString(console.Spec.Plugins, name)
if reg && !registered {
console.Spec.Plugins = append(console.Spec.Plugins, name)
return r.Client.Update(ctx, &console)
}
return nil
}
func (r *CPReconciler) reconcilePermissions(ctx context.Context, builder *builder, name string) error {
if !r.Managed.Exists(r.serviceAccount) {
return r.CreateOwned(ctx, builder.serviceAccount(name))
} // update not needed for now
binding := resources.GetClusterRoleBinding(
r.Namespace,
constants.PluginShortName,
name,
name,
constants.ConsoleTokenReviewRole,
)
return r.ReconcileClusterRoleBinding(ctx, binding)
}
func (r *CPReconciler) reconcilePlugin(ctx context.Context, builder *builder, desired *flowslatest.FlowCollectorSpec, name, displayName string) error {
// Console plugin is cluster-scope (it's not deployed in our namespace) however it must still be updated if our namespace changes
oldPlg := osv1.ConsolePlugin{}
pluginExists := true
err := r.Get(ctx, types.NamespacedName{Name: name}, &oldPlg)
if err != nil {
if apierrors.IsNotFound(err) {
pluginExists = false
} else {
return err
}
}
// Check if objects need update
consolePlugin := builder.consolePlugin(name, displayName)
if !pluginExists {
if err := r.CreateOwned(ctx, consolePlugin); err != nil {
return err
}
} else if pluginNeedsUpdate(&oldPlg, &desired.ConsolePlugin) {
if err := r.UpdateIfOwned(ctx, &oldPlg, consolePlugin); err != nil {
return err
}
}
return nil
}
func (r *CPReconciler) reconcileConfigMap(ctx context.Context, builder *builder, lokiStatus status.ComponentStatus) (string, error) {
newCM, configDigest, err := builder.configMap(ctx, lokiStatus)
if err != nil {
return "", err
}
if !r.Managed.Exists(r.configMap) {
if err := r.CreateOwned(ctx, newCM); err != nil {
return "", err
}
} else if !reflect.DeepEqual(newCM.Data, r.configMap.Data) {
if err := r.UpdateIfOwned(ctx, r.configMap, newCM); err != nil {
return "", err
}
}
return configDigest, nil
}
func (r *CPReconciler) reconcileDeployment(ctx context.Context, builder *builder, desired *flowslatest.FlowCollectorSpec, name string, cmDigest string) error {
report := helper.NewChangeReport("Console deployment")
defer report.LogIfNeeded(ctx)
return reconcilers.ReconcileDeployment(
ctx,
r.Instance,
r.deployment,
builder.deployment(name, cmDigest),
name,
desired.ConsolePlugin.UnmanagedReplicas,
&report,
)
}
func (r *CPReconciler) reconcileServices(ctx context.Context, builder *builder, name string) error {
report := helper.NewChangeReport("Console services")
defer report.LogIfNeeded(ctx)
if err := r.ReconcileService(ctx, r.service, builder.mainService(name), &report); err != nil {
return err
}
if r.metricsService != nil {
if err := r.ReconcileService(ctx, r.metricsService, builder.metricsService(), &report); err != nil {
return err
}
}
if r.serviceMonitor != nil && r.ClusterInfo.HasSvcMonitor() {
serviceMonitor := builder.serviceMonitor()
if err := reconcilers.GenericReconcile(ctx, r.Managed, &r.Client, r.serviceMonitor, serviceMonitor, &report, helper.ServiceMonitorChanged); err != nil {
return err
}
}
return nil
}
func (r *CPReconciler) reconcileHPA(ctx context.Context, builder *builder, desired *flowslatest.FlowCollectorSpec) error {
report := helper.NewChangeReport("Console autoscaler")
defer report.LogIfNeeded(ctx)
return reconcilers.ReconcileHPA(
ctx,
r.Instance,
r.hpa,
builder.autoScaler(),
&desired.ConsolePlugin.Autoscaler,
&report,
)
}
func pluginNeedsUpdate(plg *osv1.ConsolePlugin, desired *pluginSpec) bool {
advancedConfig := helper.GetAdvancedPluginConfig(desired.Advanced)
return plg.Spec.Backend.Service.Port != *advancedConfig.Port
}