forked from kbind-dev/kbind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbindableresourcesrequest_controller.go
More file actions
390 lines (341 loc) · 14.4 KB
/
Copy pathbindableresourcesrequest_controller.go
File metadata and controls
390 lines (341 loc) · 14.4 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
Copyright 2025 The Kube Bind Authors.
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 bindableresourcesrequest
import (
"context"
"encoding/json"
"fmt"
"reflect"
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/log"
mcbuilder "sigs.k8s.io/multicluster-runtime/pkg/builder"
mcmanager "sigs.k8s.io/multicluster-runtime/pkg/manager"
mcreconcile "sigs.k8s.io/multicluster-runtime/pkg/reconcile"
"github.com/kube-bind/kube-bind/backend/kubernetes"
"github.com/kube-bind/kube-bind/pkg/indexers"
kubebindv1alpha2 "github.com/kube-bind/kube-bind/sdk/apis/kubebind/v1alpha2"
)
const (
controllerName = "kube-bind-backend-bindableresourcesrequest"
)
// BindableResourcesRequestReconciler reconciles a BindableResourcesRequest object.
// This controller handles direct binding requests created as CRDs when the
// backend is running without the HTTP API/OIDC flow (frontend disabled mode).
//
// The controller:
// 1. Reads the kubeconfig from the referenced secret
// 2. Creates a BindingResourceResponse secret with the kubeconfig
// 3. Creates an APIServiceExportRequest based on the template
type BindableResourcesRequestReconciler struct {
manager mcmanager.Manager
opts controller.TypedOptions[mcreconcile.Request]
informerScope kubebindv1alpha2.InformerScope
isolation kubebindv1alpha2.Isolation
reconciler reconciler
}
// NewBindableResourcesRequestReconciler returns a new BindableResourcesRequestReconciler.
func NewBindableResourcesRequestReconciler(
ctx context.Context,
mgr mcmanager.Manager,
opts controller.TypedOptions[mcreconcile.Request],
scope kubebindv1alpha2.InformerScope,
isolation kubebindv1alpha2.Isolation,
kubeManager *kubernetes.Manager,
) (*BindableResourcesRequestReconciler, error) {
// Set up field indexers for BindableResourcesRequests
if err := mgr.GetFieldIndexer().IndexField(ctx, &kubebindv1alpha2.BindableResourcesRequest{}, indexers.BindableResourcesRequestByTemplate,
indexers.IndexBindableResourcesRequestByTemplate); err != nil {
return nil, fmt.Errorf("failed to setup BindableResourcesRequestByTemplate indexer: %w", err)
}
r := &BindableResourcesRequestReconciler{
manager: mgr,
opts: opts,
informerScope: scope,
isolation: isolation,
reconciler: reconciler{
informerScope: scope,
isolation: isolation,
kubeManager: kubeManager,
},
}
return r, nil
}
//+kubebuilder:rbac:groups=kube-bind.io,resources=bindableresourcesrequests,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=kube-bind.io,resources=bindableresourcesrequests/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=kube-bind.io,resources=bindableresourcesrequests/finalizers,verbs=update
//+kubebuilder:rbac:groups=kube-bind.io,resources=apiserviceexportrequests,verbs=get;list;watch;create
//+kubebuilder:rbac:groups=kube-bind.io,resources=apiserviceexporttemplates,verbs=get;list;watch
//+kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update
// Reconcile is part of the main kubernetes reconciliation loop.
func (r *BindableResourcesRequestReconciler) Reconcile(ctx context.Context, req mcreconcile.Request) (ctrl.Result, error) {
logger := log.FromContext(ctx)
logger.Info("Reconciling BindableResourcesRequest", "cluster", req.ClusterName, "namespace", req.Namespace, "name", req.Name)
cl, err := r.manager.GetCluster(ctx, req.ClusterName)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to get client for cluster %q: %w", req.ClusterName, err)
}
client := cl.GetClient()
// Fetch the BindableResourcesRequest instance
bindableRequest := &kubebindv1alpha2.BindableResourcesRequest{}
if err := client.Get(ctx, req.NamespacedName, bindableRequest); err != nil {
if errors.IsNotFound(err) {
logger.Info("BindableResourcesRequest not found, ignoring")
return ctrl.Result{}, nil
}
return ctrl.Result{}, fmt.Errorf("failed to get BindableResourcesRequest: %w", err)
}
// Check TTL-based deletion for completed (ttl) requests
if bindableRequest.Status.Phase == kubebindv1alpha2.BindableResourcesRequestPhaseSucceeded ||
bindableRequest.Status.Phase == kubebindv1alpha2.BindableResourcesRequestPhaseFailed {
return r.handleTTL(ctx, client, bindableRequest)
}
// Create a copy to modify
original := bindableRequest.DeepCopy()
// Run the reconciliation logic
result, err := r.reconciler.reconcile(ctx, req.ClusterName, client, bindableRequest)
if err != nil {
logger.Error(err, "Failed to reconcile BindableResourcesRequest")
if !reflect.DeepEqual(original.Status, bindableRequest.Status) {
if updateErr := client.Status().Update(ctx, bindableRequest); updateErr != nil {
logger.Error(updateErr, "Failed to update BindableResourcesRequest status")
return ctrl.Result{}, fmt.Errorf("failed to update BindableResourcesRequest status: %w", updateErr)
}
}
return ctrl.Result{}, err
}
// Update status if it has changed
if !reflect.DeepEqual(original.Status, bindableRequest.Status) {
if err := client.Status().Update(ctx, bindableRequest); err != nil {
logger.Error(err, "Failed to update BindableResourcesRequest status")
return ctrl.Result{}, fmt.Errorf("failed to update BindableResourcesRequest status: %w", err)
}
logger.Info("BindableResourcesRequest status updated", "namespace", bindableRequest.Namespace, "name", bindableRequest.Name)
}
return result, nil
}
// handleTTL checks if the request should be deleted based on TTL settings.
func (r *BindableResourcesRequestReconciler) handleTTL(ctx context.Context, cl client.Client, req *kubebindv1alpha2.BindableResourcesRequest) (ctrl.Result, error) {
logger := log.FromContext(ctx)
// If no TTL is set, don't delete
if req.Spec.TTLAfterFinished == nil {
return ctrl.Result{}, nil
}
// If no completion time is set, something is wrong - skip
if req.Status.CompletionTime == nil {
return ctrl.Result{}, nil
}
ttl := req.Spec.TTLAfterFinished.Duration
expireTime := req.Status.CompletionTime.Add(ttl)
now := time.Now()
if now.After(expireTime) {
logger.Info("TTL expired, deleting BindableResourcesRequest", "name", req.Name, "namespace", req.Namespace)
if err := cl.Delete(ctx, req); err != nil {
if errors.IsNotFound(err) {
return ctrl.Result{}, nil
}
return ctrl.Result{}, fmt.Errorf("failed to delete expired BindableResourcesRequest: %w", err)
}
return ctrl.Result{}, nil
}
// Requeue to check again when TTL expires
requeueAfter := expireTime.Sub(now)
return ctrl.Result{RequeueAfter: requeueAfter}, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *BindableResourcesRequestReconciler) SetupWithManager(mgr mcmanager.Manager) error {
return mcbuilder.ControllerManagedBy(mgr).
For(&kubebindv1alpha2.BindableResourcesRequest{}).
Owns(&corev1.Secret{}).
WithOptions(r.opts).
Named(controllerName).
Complete(r)
}
type reconciler struct {
informerScope kubebindv1alpha2.InformerScope
isolation kubebindv1alpha2.Isolation
kubeManager *kubernetes.Manager
}
func (r *reconciler) reconcile(ctx context.Context, clusterName string, cl client.Client, req *kubebindv1alpha2.BindableResourcesRequest) (ctrl.Result, error) {
logger := log.FromContext(ctx)
// Determine the secret name and key to use for the binding response
var secretName, secretKey string
if req.Spec.KubeconfigSecretRef != nil {
// Use the specified secret reference
secretName = req.Spec.KubeconfigSecretRef.Name
secretKey = req.Spec.KubeconfigSecretRef.Key
if secretKey == "" {
secretKey = "kubeconfig"
}
} else {
// Create a new secret with default naming
secretName = req.Name + "-binding-response"
secretKey = "binding-response"
}
// Update status with the secret reference
req.Status.KubeconfigSecretRef = &kubebindv1alpha2.LocalSecretKeyRef{
Name: secretName,
Key: secretKey,
}
// Get the template if specified
var template kubebindv1alpha2.APIServiceExportTemplate
if req.Spec.TemplateRef.Name != "" {
if err := cl.Get(ctx, types.NamespacedName{Name: req.Spec.TemplateRef.Name}, &template); err != nil {
if errors.IsNotFound(err) {
req.Status.Phase = kubebindv1alpha2.BindableResourcesRequestPhaseFailed
now := metav1.Now()
req.Status.CompletionTime = &now
meta.SetStatusCondition(&req.Status.Conditions, metav1.Condition{
Type: string(kubebindv1alpha2.BindableResourcesRequestConditionReady),
Status: metav1.ConditionFalse,
Reason: "TemplateNotFound",
Message: fmt.Sprintf("APIServiceExportTemplate %q not found", req.Spec.TemplateRef.Name),
LastTransitionTime: now,
})
return ctrl.Result{}, nil // Don't retry - template doesn't exist
}
return ctrl.Result{}, fmt.Errorf("failed to get template %q: %w", req.Spec.TemplateRef.Name, err)
}
}
// Handle resources and get kubeconfig
kfg, err := r.kubeManager.HandleResources(ctx, req.Spec.Author, req.Spec.ClusterIdentity.Identity, clusterName)
if err != nil {
meta.SetStatusCondition(&req.Status.Conditions, metav1.Condition{
Type: string(kubebindv1alpha2.BindableResourcesRequestConditionReady),
Status: metav1.ConditionFalse,
Reason: "HandleResourcesFailed",
Message: fmt.Sprintf("Failed to handle resources: %v", err),
LastTransitionTime: metav1.Now(),
})
return ctrl.Result{}, fmt.Errorf("failed to handle resources for cluster identity %q: %w", req.Spec.ClusterIdentity.Identity, err)
}
// Create or update the BindingResourceResponse secret
if err := r.ensureBindingResponseSecret(ctx, cl, req, kfg, secretName, secretKey); err != nil {
meta.SetStatusCondition(&req.Status.Conditions, metav1.Condition{
Type: string(kubebindv1alpha2.BindableResourcesRequestConditionReady),
Status: metav1.ConditionFalse,
Reason: "SecretCreationFailed",
Message: fmt.Sprintf("Failed to create/update secret: %v", err),
LastTransitionTime: metav1.Now(),
})
return ctrl.Result{}, fmt.Errorf("failed to ensure binding response secret: %w", err)
}
// Set success status
now := metav1.Now()
req.Status.Phase = kubebindv1alpha2.BindableResourcesRequestPhaseSucceeded
req.Status.CompletionTime = &now
meta.SetStatusCondition(&req.Status.Conditions, metav1.Condition{
Type: string(kubebindv1alpha2.BindableResourcesRequestConditionReady),
Status: metav1.ConditionTrue,
Reason: "SecretReady",
Message: fmt.Sprintf("Binding response secret %q is ready", secretName),
LastTransitionTime: now,
})
logger.Info("BindableResourcesRequest succeeded", "name", req.Name, "namespace", req.Namespace, "secret", secretName)
// If TTL is set, requeue for deletion
if req.Spec.TTLAfterFinished != nil {
return ctrl.Result{RequeueAfter: req.Spec.TTLAfterFinished.Duration}, nil
}
return ctrl.Result{}, nil
}
// ensureBindingResponseSecret creates or updates a secret containing the BindingResourceResponse
// with only the kubeconfig set (no authentication or requests).
func (r *reconciler) ensureBindingResponseSecret(
ctx context.Context,
cl client.Client,
req *kubebindv1alpha2.BindableResourcesRequest,
kubeconfig []byte,
secretName string,
secretKey string,
) error {
logger := log.FromContext(ctx)
// Create the BindingResourceResponse with only kubeconfig
response := kubebindv1alpha2.BindingResourceResponse{
TypeMeta: metav1.TypeMeta{
APIVersion: kubebindv1alpha2.SchemeGroupVersion.String(),
Kind: kubebindv1alpha2.KindBindingResourceResponse,
},
Kubeconfig: kubeconfig,
}
responseBytes, err := json.Marshal(&response)
if err != nil {
return fmt.Errorf("failed to marshal BindingResourceResponse: %w", err)
}
// Check if secret already exists
existingSecret := &corev1.Secret{}
err = cl.Get(ctx, types.NamespacedName{Namespace: req.Namespace, Name: secretName}, existingSecret)
if err != nil && !errors.IsNotFound(err) {
return fmt.Errorf("failed to get existing secret: %w", err)
}
// Build owner reference - the secret is owned by the BindableResourcesRequest
ownerRef := metav1.OwnerReference{
APIVersion: kubebindv1alpha2.SchemeGroupVersion.String(),
Kind: kubebindv1alpha2.KindBindableResourcesRequest,
Name: req.Name,
UID: req.UID,
Controller: func() *bool { b := true; return &b }(),
}
if errors.IsNotFound(err) {
// Create new secret
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
Namespace: req.Namespace,
OwnerReferences: []metav1.OwnerReference{ownerRef},
},
Type: corev1.SecretTypeOpaque,
Data: map[string][]byte{
secretKey: responseBytes,
},
}
logger.Info("Creating BindingResourceResponse secret", "name", secretName, "namespace", req.Namespace, "key", secretKey)
if err := cl.Create(ctx, secret); err != nil {
if errors.IsAlreadyExists(err) {
return nil // Race condition, secret was created
}
return fmt.Errorf("failed to create secret: %w", err)
}
} else {
// Update existing secret
// Ensure owner reference is set
hasOwnerRef := false
for _, ref := range existingSecret.OwnerReferences {
if ref.UID == req.UID {
hasOwnerRef = true
break
}
}
if !hasOwnerRef {
existingSecret.OwnerReferences = append(existingSecret.OwnerReferences, ownerRef)
}
// Update data if changed
if existingSecret.Data == nil {
existingSecret.Data = make(map[string][]byte)
}
if string(existingSecret.Data[secretKey]) != string(responseBytes) {
existingSecret.Data[secretKey] = responseBytes
logger.Info("Updating BindingResourceResponse secret", "name", secretName, "namespace", req.Namespace, "key", secretKey)
if err := cl.Update(ctx, existingSecret); err != nil {
return fmt.Errorf("failed to update secret: %w", err)
}
}
}
return nil
}