-
Notifications
You must be signed in to change notification settings - Fork 3
/
clientmount_controller.go
131 lines (108 loc) · 4.61 KB
/
clientmount_controller.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
/*
* Copyright 2021-2023 Hewlett Packard Enterprise Development LP
* Other additional copyright holders may be indicated within.
*
* The entirety of this work is 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 controllers
import (
"context"
"strings"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/predicate"
dwsv1alpha2 "github.com/DataWorkflowServices/dws/api/v1alpha2"
"github.com/DataWorkflowServices/dws/utils/updater"
)
// ClientMountReconciler reconciles a ClientMount object
type ClientMountReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
}
const (
// finalizerClientMount defines the key used for the finalizer
finalizerClientMount = "dataworkflowservices.github.io/client_mount"
)
//+kubebuilder:rbac:groups=dataworkflowservices.github.io,resources=clientmounts,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=dataworkflowservices.github.io,resources=clientmounts/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=dataworkflowservices.github.io,resources=clientmounts/finalizers,verbs=update
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
func (r *ClientMountReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res ctrl.Result, err error) {
clientMount := &dwsv1alpha2.ClientMount{}
if err := r.Get(ctx, req.NamespacedName, clientMount); err != nil {
// ignore not-found errors, since they can't be fixed by an immediate
// requeue (we'll need to wait for a new notification), and we can get them
// on deleted requests.
return ctrl.Result{}, client.IgnoreNotFound(err)
}
// Create a status updater that handles the call to r.Status().Update() if any of the fields
// in clientMount.Status{} change
statusUpdater := updater.NewStatusUpdater[*dwsv1alpha2.ClientMountStatus](clientMount)
defer func() { err = statusUpdater.CloseWithStatusUpdate(ctx, r.Client.Status(), err) }()
defer func() { clientMount.Status.SetResourceError(err) }()
// Handle cleanup if the resource is being deleted
if !clientMount.GetDeletionTimestamp().IsZero() {
if !controllerutil.ContainsFinalizer(clientMount, finalizerClientMount) {
return ctrl.Result{}, nil
}
controllerutil.RemoveFinalizer(clientMount, finalizerClientMount)
if err := r.Update(ctx, clientMount); err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
// Create the status section if it doesn't exist yet
if len(clientMount.Status.Mounts) != len(clientMount.Spec.Mounts) {
clientMount.Status.Mounts = make([]dwsv1alpha2.ClientMountInfoStatus, len(clientMount.Spec.Mounts))
}
// Initialize the status section if the desired state doesn't match the status state
if clientMount.Status.Mounts[0].State != clientMount.Spec.DesiredState {
for i := 0; i < len(clientMount.Status.Mounts); i++ {
clientMount.Status.Mounts[i].State = clientMount.Spec.DesiredState
clientMount.Status.Mounts[i].Ready = false
}
return ctrl.Result{}, nil
}
// Add finalizer if it doesn't exist
if !controllerutil.ContainsFinalizer(clientMount, finalizerClientMount) {
controllerutil.AddFinalizer(clientMount, finalizerClientMount)
if err := r.Update(ctx, clientMount); err != nil {
return ctrl.Result{Requeue: true}, nil
}
return ctrl.Result{}, nil
}
for i := range clientMount.Spec.Mounts {
clientMount.Status.Mounts[i].Ready = true
}
clientMount.Status.Error = nil
return ctrl.Result{}, nil
}
func filterByComputeNamespacePrefix() predicate.Predicate {
return predicate.NewPredicateFuncs(func(object client.Object) bool {
return strings.HasPrefix(object.GetNamespace(), "compute")
})
}
// SetupWithManager sets up the controller with the Manager.
func (r *ClientMountReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&dwsv1alpha2.ClientMount{}).
WithEventFilter(filterByComputeNamespacePrefix()).
Complete(r)
}