forked from tigera/operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.go
More file actions
277 lines (235 loc) · 10.9 KB
/
controller.go
File metadata and controls
277 lines (235 loc) · 10.9 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
275
276
277
// Copyright (c) 2025 Tigera, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package whisker
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
operatorv1 "github.com/tigera/operator/api/v1"
crdv1 "github.com/tigera/operator/pkg/apis/crd.projectcalico.org/v1"
"github.com/tigera/operator/pkg/common"
"github.com/tigera/operator/pkg/controller/certificatemanager"
"github.com/tigera/operator/pkg/controller/options"
"github.com/tigera/operator/pkg/controller/status"
"github.com/tigera/operator/pkg/controller/utils"
"github.com/tigera/operator/pkg/controller/utils/imageset"
"github.com/tigera/operator/pkg/ctrlruntime"
"github.com/tigera/operator/pkg/dns"
"github.com/tigera/operator/pkg/ptr"
"github.com/tigera/operator/pkg/render"
rcertificatemanagement "github.com/tigera/operator/pkg/render/certificatemanagement"
"github.com/tigera/operator/pkg/render/goldmane"
"github.com/tigera/operator/pkg/render/whisker"
"github.com/tigera/operator/pkg/tls/certificatemanagement"
)
const (
controllerName = "whisker-controller"
ResourceName = "whisker"
)
var log = logf.Log.WithName(controllerName)
// Add creates a new Reconciler Controller and adds it to the Manager. The Manager will set fields on the Controller
// and start it when the Manager is started.
func Add(mgr manager.Manager, opts options.AddOptions) error {
statusManager := status.New(mgr.GetClient(), "whisker", opts.KubernetesVersion)
reconciler := newReconciler(mgr.GetClient(), mgr.GetScheme(), statusManager, opts.DetectedProvider, opts)
c, err := ctrlruntime.NewController(controllerName, mgr, controller.Options{Reconciler: reconciler})
if err != nil {
return fmt.Errorf("failed to create %s: %w", controllerName, err)
}
if err = c.WatchObject(&operatorv1.Whisker{}, &handler.EnqueueRequestForObject{}); err != nil {
return fmt.Errorf("%s failed to watch primary resource: %w", controllerName, err)
}
if err = c.WatchObject(&operatorv1.Goldmane{}, &handler.EnqueueRequestForObject{}); err != nil {
return fmt.Errorf("%s failed to watch for goldmane resource: %w", controllerName, err)
}
if err = utils.AddInstallationWatch(c); err != nil {
return fmt.Errorf("%s failed to watch Installation resource: %w", controllerName, err)
}
for _, secretName := range []string{
certificatemanagement.CASecretName,
goldmane.GoldmaneKeyPairSecret,
} {
if err = utils.AddSecretsWatch(c, secretName, common.OperatorNamespace()); err != nil {
return fmt.Errorf("failed to add watch for secret %s/%s: %w", common.OperatorNamespace(), secretName, err)
}
}
if err = utils.AddConfigMapWatch(c, certificatemanagement.TrustedBundleName("whisker", false), common.OperatorNamespace(), &handler.EnqueueRequestForObject{}); err != nil {
return fmt.Errorf("failed to add watch for config map %s/%s: %w", common.OperatorNamespace(), certificatemanagement.TrustedCertConfigMapName, err)
}
if err = imageset.AddImageSetWatch(c); err != nil {
return fmt.Errorf("%s failed to watch ImageSet: %w", controllerName, err)
}
if err := utils.AddDeploymentWatch(c, whisker.WhiskerDeploymentName, whisker.WhiskerNamespace); err != nil {
return fmt.Errorf("%s failed to watch Whisker deployment: %w", controllerName, err)
}
// Watch for changes to TigeraStatus.
if err = utils.AddTigeraStatusWatch(c, ResourceName); err != nil {
return fmt.Errorf("whisker-controller failed to watch Tigerastatus: %w", err)
}
if err = c.WatchObject(&crdv1.ClusterInformation{}, &handler.EnqueueRequestForObject{}); err != nil {
return fmt.Errorf("whisker-controller failed to watch ClusterInformation")
}
// Perform periodic reconciliation. This acts as a backstop to catch reconcile issues,
// and also makes sure we spot when things change that might not trigger a reconciliation.
if err = utils.AddPeriodicReconcile(c, utils.PeriodicReconcileTime, &handler.EnqueueRequestForObject{}); err != nil {
return fmt.Errorf("whisker-controller failed to create periodic reconcile watch: %w", err)
}
return nil
}
// newReconciler returns a new reconcile.Reconciler
func newReconciler(
cli client.Client,
schema *runtime.Scheme,
statusMgr status.StatusManager,
p operatorv1.Provider,
opts options.AddOptions,
) *Reconciler {
c := &Reconciler{
cli: cli,
scheme: schema,
provider: p,
status: statusMgr,
clusterDomain: opts.ClusterDomain,
}
c.status.Run(opts.ShutdownContext)
return c
}
// blank assignment to verify that ReconcileConnection implements reconcile.Reconciler
var _ reconcile.Reconciler = &Reconciler{}
type Reconciler struct {
cli client.Client
scheme *runtime.Scheme
provider operatorv1.Provider
status status.StatusManager
clusterDomain string
}
// Reconcile reads that state of the cluster for a Whisker object and makes changes based on the
// state read and what is in the Whisker.Spec. The Controller will requeue the Request to be
// processed again if the returned error is non-nil or Result.Requeue is true, otherwise upon completion it will
// remove the work from the queue.
func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
reqLogger.Info("Reconciling Whisker")
whiskerCR, err := utils.GetIfExists[operatorv1.Whisker](ctx, utils.DefaultInstanceKey, r.cli)
if err != nil {
r.status.SetDegraded(operatorv1.ResourceReadError, "Error querying Whisker CR", err, reqLogger)
return reconcile.Result{}, err
} else if whiskerCR == nil {
r.status.OnCRNotFound()
return reconcile.Result{}, nil
}
r.status.OnCRFound()
// SetMetaData in the TigeraStatus such as observedGenerations.
defer r.status.SetMetaData(&whiskerCR.ObjectMeta)
variant, installation, err := utils.GetInstallation(ctx, r.cli)
if err != nil {
return reconcile.Result{}, err
} else if installation == nil {
return reconcile.Result{}, nil
}
if goldmaneCR, err := utils.GetIfExists[operatorv1.Goldmane](ctx, utils.DefaultInstanceKey, r.cli); err != nil {
r.status.SetDegraded(operatorv1.ResourceReadError, "Error querying for Goldmane CR", err, reqLogger)
return reconcile.Result{}, err
} else if goldmaneCR == nil {
r.status.SetDegraded(operatorv1.ResourceNotFound, "Goldmane CR not present; Goldmane is pre requisite for Whisker", err, reqLogger)
return reconcile.Result{}, nil
}
pullSecrets, err := utils.GetNetworkingPullSecrets(installation, r.cli)
if err != nil {
r.status.SetDegraded(operatorv1.ResourceReadError, "Error retrieving pull secrets", err, reqLogger)
return reconcile.Result{}, err
}
certificateManager, err := certificatemanager.Create(r.cli, installation, r.clusterDomain, common.OperatorNamespace())
if err != nil {
r.status.SetDegraded(operatorv1.ResourceCreateError, "Unable to create the certificate manager", err, reqLogger)
return reconcile.Result{}, err
}
whiskerBackendCertificateNames := dns.GetServiceDNSNames("whisker-backend", whisker.WhiskerNamespace, r.clusterDomain)
whiskerBackendCertificateNames = append(whiskerBackendCertificateNames, "localhost", "127.0.0.1")
backendKeyPair, err := certificateManager.GetOrCreateKeyPair(r.cli, whisker.WhiskerBackendKeyPairSecret, whisker.WhiskerNamespace, whiskerBackendCertificateNames)
if err != nil {
r.status.SetDegraded(operatorv1.ResourceCreateError, "Error creating whisker-backend TLS certificate", err, log)
return reconcile.Result{}, err
}
trustedBundle, err := certificateManager.CreateNamedTrustedBundleFromSecrets(
whisker.WhiskerDeploymentName,
r.cli,
common.OperatorNamespace(),
false,
goldmane.GoldmaneKeyPairSecret,
)
if err != nil {
r.status.SetDegraded(operatorv1.ResourceCreateError, "Unable to create the trusted bundle", err, reqLogger)
}
preDefaultPatchFrom := client.MergeFrom(whiskerCR.DeepCopy())
// update Installation with defaults
updateWhiskerWithDefaults(whiskerCR)
// Write the whisker CR configuration back to the API. This is essentially a poor-man's defaulting, and
// ensures that we don't surprise anyone by changing defaults in a future version of the operator.
if err := r.cli.Patch(ctx, whiskerCR, preDefaultPatchFrom); err != nil {
r.status.SetDegraded(operatorv1.ResourceUpdateError, "Failed to write defaults", err, reqLogger)
return reconcile.Result{}, err
}
ch := utils.NewComponentHandler(log, r.cli, r.scheme, whiskerCR)
cfg := &whisker.Configuration{
PullSecrets: pullSecrets,
OpenShift: r.provider.IsOpenShift(),
Installation: installation,
TrustedCertBundle: trustedBundle,
WhiskerBackendKeyPair: backendKeyPair,
Whisker: whiskerCR,
}
clusterInfo := &crdv1.ClusterInformation{}
err = r.cli.Get(ctx, utils.DefaultInstanceKey, clusterInfo)
if err != nil {
reqLogger.Info("Unable to retrieve ClusterInformation", "error", err)
} else {
cfg.CalicoVersion = clusterInfo.Spec.CalicoVersion
cfg.ClusterType = clusterInfo.Spec.ClusterType
cfg.ClusterID = clusterInfo.Spec.ClusterGUID
}
certComponent := rcertificatemanagement.CertificateManagement(&rcertificatemanagement.Config{
Namespace: goldmane.GoldmaneNamespace,
TruthNamespace: common.OperatorNamespace(),
ServiceAccounts: []string{whisker.WhiskerServiceAccountName},
KeyPairOptions: []rcertificatemanagement.KeyPairOption{
rcertificatemanagement.NewKeyPairOption(backendKeyPair, true, true),
},
TrustedBundle: trustedBundle,
})
components := []render.Component{certComponent, whisker.Whisker(cfg)}
if err = imageset.ApplyImageSet(ctx, r.cli, variant, components...); err != nil {
r.status.SetDegraded(operatorv1.ResourceUpdateError, "Error with images from ImageSet", err, reqLogger)
return reconcile.Result{}, err
}
for _, component := range components {
if err := ch.CreateOrUpdateOrDelete(ctx, component, r.status); err != nil {
r.status.SetDegraded(operatorv1.ResourceUpdateError, "Error creating / updating resource", err, reqLogger)
return reconcile.Result{}, err
}
}
r.status.ReadyToMonitor()
r.status.ClearDegraded()
return reconcile.Result{}, nil
}
func updateWhiskerWithDefaults(instance *operatorv1.Whisker) {
if instance.Spec.Notifications == nil {
instance.Spec.Notifications = ptr.ToPtr(operatorv1.Enabled)
}
}