-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathlinodeobjectstoragebucket_controller.go
More file actions
270 lines (233 loc) · 10.6 KB
/
Copy pathlinodeobjectstoragebucket_controller.go
File metadata and controls
270 lines (233 loc) · 10.6 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
/*
Copyright 2023 Akamai Technologies, Inc.
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 controller
import (
"context"
"fmt"
"time"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/tools/events"
"k8s.io/client-go/util/retry"
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
kutil "sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/predicates"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
crcontroller "sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
infrav1alpha2 "github.com/linode/cluster-api-provider-linode/api/v1alpha2"
"github.com/linode/cluster-api-provider-linode/cloud/scope"
"github.com/linode/cluster-api-provider-linode/cloud/services"
wrappedruntimeclient "github.com/linode/cluster-api-provider-linode/observability/wrappers/runtimeclient"
"github.com/linode/cluster-api-provider-linode/util"
"github.com/linode/cluster-api-provider-linode/util/reconciler"
)
// LinodeObjectStorageBucketReconciler reconciles a LinodeObjectStorageBucket object
type LinodeObjectStorageBucketReconciler struct {
client.Client
Logger logr.Logger
Recorder events.EventRecorder
LinodeClientConfig scope.ClientConfig
WatchFilterValue string
ReconcileTimeout time.Duration
}
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=linodeobjectstoragebuckets,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=linodeobjectstoragebuckets/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=linodeobjectstoragebuckets/finalizers,verbs=update
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=linodeobjectstoragekeys/finalizers,verbs=update
// +kubebuilder:rbac:groups="",resources=events,verbs=get;list;watch;create;update;patch
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;patch
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// TODO(user): Modify the Reconcile function to compare the state specified by
// the LinodeObjectStorageBucket object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.17.0/pkg/reconcile
func (r *LinodeObjectStorageBucketReconciler) Reconcile(ctx context.Context, objectStorageBucket *infrav1alpha2.LinodeObjectStorageBucket) (ctrl.Result, error) {
ctx, cancel := context.WithTimeout(ctx, reconciler.DefaultedLoopTimeout(r.ReconcileTimeout))
defer cancel()
logger := r.Logger.WithValues("name", objectStorageBucket.Name, "namespace", objectStorageBucket.Namespace)
if _, ok := objectStorageBucket.Labels[clusterv1.ClusterNameLabel]; ok {
cluster, err := kutil.GetClusterFromMetadata(ctx, r.TracedClient(), objectStorageBucket.ObjectMeta)
if err != nil {
if client.IgnoreNotFound(err) != nil {
logger.Error(err, "failed to fetch cluster from metadata")
return ctrl.Result{}, err
}
logger.Info("Cluster not found but LinodeObjectStorageBucket is being deleted, continuing with deletion")
}
// It will handle the case where the cluster is not found
if err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
return util.SetOwnerReferenceToLinodeCluster(ctx, r.TracedClient(), cluster, objectStorageBucket, r.Scheme())
}); err != nil {
logger.Error(err, "Failed to set owner reference to LinodeCluster")
return ctrl.Result{}, err
}
}
bScope, err := scope.NewObjectStorageBucketScope(
ctx,
r.LinodeClientConfig,
scope.ObjectStorageBucketScopeParams{
Client: r.TracedClient(),
Bucket: objectStorageBucket,
Logger: &logger,
},
)
if err != nil {
logger.Error(err, "Failed to create object storage bucket scope")
return ctrl.Result{}, fmt.Errorf("failed to create object storage bucket scope: %w", err)
}
return r.reconcile(ctx, bScope)
}
func (r *LinodeObjectStorageBucketReconciler) reconcile(ctx context.Context, bScope *scope.ObjectStorageBucketScope) (res ctrl.Result, reterr error) {
// Always close the scope when exiting this function so we can persist any LinodeObjectStorageBucket changes.
defer func() {
// Filter out any IsNotFound message since client.IgnoreNotFound does not handle aggregate errors
if err := bScope.Close(ctx); utilerrors.FilterOut(err, apierrors.IsNotFound) != nil && reterr == nil {
bScope.Logger.Error(err, "failed to patch LinodeObjectStorageBucket")
reterr = err
}
}()
// Handle deleted buckets
if !bScope.Bucket.DeletionTimestamp.IsZero() {
if err := r.reconcileDelete(ctx, bScope); err != nil {
return retryIfTransient(err, bScope.Logger)
}
return res, nil
}
return r.reconcileApply(ctx, bScope)
}
func (r *LinodeObjectStorageBucketReconciler) setFailure(bScope *scope.ObjectStorageBucketScope, failureReason, failureMessage string) {
bScope.Bucket.Status.FailureMessage = util.Pointer(failureMessage)
bScope.Bucket.Status.FailureReason = util.Pointer(failureReason)
bScope.Bucket.SetCondition(metav1.Condition{
Type: clusterv1.ReadyCondition,
Status: metav1.ConditionFalse,
Reason: failureReason,
Message: failureMessage,
})
}
func (r *LinodeObjectStorageBucketReconciler) reconcileApply(ctx context.Context, bScope *scope.ObjectStorageBucketScope) (ctrl.Result, error) {
bScope.Logger.Info("Reconciling apply")
bScope.Bucket.Status.Ready = false
bScope.Bucket.Status.FailureMessage = nil
if bScope.Bucket.Spec.AccessKeyRef != nil {
// Only add finalizers if the access key reference is set, without one the bucket can immediately be deleted.
if err := bScope.AddFinalizer(ctx); err != nil {
bScope.Logger.Error(err, "failed to update bucket finalizer, requeuing")
return ctrl.Result{RequeueAfter: reconciler.WithJitter(reconciler.DefaultObjectStorageBucketControllerReconcileDelay)}, nil
}
// It's critical to add the access key finalizer, or else the access key could be deleted before the associated bucket with its contents deleted.
if err := bScope.AddAccessKeyRefFinalizer(ctx, bScope.Bucket.Name); err != nil {
bScope.Logger.Error(err, "failed to update access key finalizer, requeuing")
return ctrl.Result{RequeueAfter: reconciler.WithJitter(reconciler.DefaultObjectStorageBucketControllerReconcileDelay)}, nil
}
}
bucket, err := services.EnsureAndUpdateObjectStorageBucket(ctx, bScope)
if err != nil {
bScope.Logger.Error(err, "Failed to ensure bucket or update bucket")
r.setFailure(bScope, "EnsureAndUpdateFailed", err.Error())
r.Recorder.Eventf(
bScope.Bucket,
nil,
corev1.EventTypeWarning,
"EnsureAndUpdateFailed",
"EnsureAndUpdate",
err.Error(),
)
return ctrl.Result{}, err
}
bScope.Bucket.Status.Hostname = util.Pointer(bucket.Hostname)
bScope.Bucket.Status.CreationTime = &metav1.Time{Time: *bucket.Created}
bScope.Bucket.Status.Ready = true
bScope.Bucket.SetCondition(metav1.Condition{
Type: clusterv1.ReadyCondition,
Status: metav1.ConditionTrue,
Reason: "ObjectStorageBucketReady", // We have to set the reason to not fail object patching
})
return ctrl.Result{}, nil
}
func (r *LinodeObjectStorageBucketReconciler) reconcileDelete(ctx context.Context, bScope *scope.ObjectStorageBucketScope) error {
// Delete the bucket if force deletion is enabled
if bScope.Bucket.Spec.ForceDeleteBucket {
if err := services.DeleteBucket(ctx, bScope); err != nil {
bScope.Logger.Error(err, "failed to delete bucket")
r.setFailure(bScope, util.DeleteError, err.Error())
r.Recorder.Eventf(
bScope.Bucket,
nil,
corev1.EventTypeWarning,
util.DeleteError,
"DeleteBucket",
err.Error(),
)
return err
}
}
// Don't delete the bucket if the ForceDeleteBucket is false since there could be data in it that causes deletion to fail.
if bScope.Bucket.Spec.AccessKeyRef != nil {
// Retry on conflict to handle the case where the access key is being updated concurrently.
if err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
return bScope.RemoveAccessKeyRefFinalizer(ctx, bScope.Bucket.Name)
}); err != nil {
bScope.Logger.Error(err, "failed to remove access key finalizer")
r.setFailure(bScope, util.DeleteError, err.Error())
return err
}
controllerutil.RemoveFinalizer(bScope.Bucket, infrav1alpha2.BucketFinalizer)
}
return nil
}
// SetupWithManager sets up the controller with the Manager.
//
//nolint:dupl // This follows the pattern used for the LinodeObjectStorageBucket controller.
func (r *LinodeObjectStorageBucketReconciler) SetupWithManager(mgr ctrl.Manager, options crcontroller.Options) error {
linodeObjectStorageBucketMapper, err := kutil.ClusterToTypedObjectsMapper(
r.TracedClient(),
&infrav1alpha2.LinodeObjectStorageBucketList{},
mgr.GetScheme(),
)
if err != nil {
return fmt.Errorf("failed to create mapper for LinodeObjectStorageBuckets: %w", err)
}
err = ctrl.NewControllerManagedBy(mgr).
For(&infrav1alpha2.LinodeObjectStorageBucket{}).
WithOptions(options).
Owns(&corev1.Secret{}).
WithEventFilter(predicate.And(
predicates.ResourceNotPausedAndHasFilterLabel(mgr.GetScheme(), mgr.GetLogger(), r.WatchFilterValue),
predicate.GenerationChangedPredicate{},
)).
Watches(
&clusterv1.Cluster{},
handler.EnqueueRequestsFromMapFunc(linodeObjectStorageBucketMapper),
builder.WithPredicates(predicates.ClusterPausedTransitionsOrInfrastructureProvisioned(mgr.GetScheme(), mgr.GetLogger())),
).Complete(reconciler.AsReconcilerWithTracing(r.TracedClient(), r))
if err != nil {
return fmt.Errorf("failed to build controller: %w", err)
}
return nil
}
func (r *LinodeObjectStorageBucketReconciler) TracedClient() client.Client {
return wrappedruntimeclient.NewRuntimeClientWithTracing(r.Client, wrappedruntimeclient.DefaultDecorator())
}