/
swiftstorage_controller.go
214 lines (185 loc) · 7.79 KB
/
swiftstorage_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
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
/*
Copyright 2022.
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"
"fmt"
"github.com/go-logr/logr"
"time"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
helper "github.com/openstack-k8s-operators/lib-common/modules/common/helper"
service "github.com/openstack-k8s-operators/lib-common/modules/common/service"
statefulset "github.com/openstack-k8s-operators/lib-common/modules/common/statefulset"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/kubernetes"
swiftv1beta1 "github.com/openstack-k8s-operators/swift-operator/api/v1beta1"
"github.com/openstack-k8s-operators/swift-operator/pkg/swiftstorage"
"github.com/openstack-k8s-operators/lib-common/modules/common/condition"
"github.com/openstack-k8s-operators/lib-common/modules/common/configmap"
"github.com/openstack-k8s-operators/lib-common/modules/common/env"
)
// SwiftStorageReconciler reconciles a SwiftStorage object
type SwiftStorageReconciler struct {
client.Client
Scheme *runtime.Scheme
Log logr.Logger
Kclient kubernetes.Interface
}
//+kubebuilder:rbac:groups=swift.openstack.org,resources=swiftstorages,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=swift.openstack.org,resources=swiftstorages/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=swift.openstack.org,resources=swiftstorages/finalizers,verbs=update
//+kubebuilder:rbac:groups=apps,resources=statefulsets,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=core,resources=services,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=core,resources=configmaps,verbs=get;list;watch;create;update;patch;delete
// 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 SwiftStorage 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.12.2/pkg/reconcile
func (r *SwiftStorageReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = r.Log.WithValues("swiftstorage", req.NamespacedName)
instance := &swiftv1beta1.SwiftStorage{}
err := r.Get(ctx, req.NamespacedName, instance)
if err != nil {
if apierrors.IsNotFound(err) {
// If the custom resource is not found then, it usually means that it was deleted or not created
// In this way, we will stop the reconciliation
r.Log.Info("SwiftStorage resource not found. Ignoring since object must be deleted")
return ctrl.Result{}, nil
}
// Error reading the object - requeue the request.
r.Log.Error(err, "Failed to get SwiftStorage")
return ctrl.Result{}, err
}
if instance.Status.Conditions == nil {
instance.Status.Conditions = condition.Conditions{}
cl := condition.CreateList(
condition.UnknownCondition(condition.ReadyCondition, condition.InitReason, condition.ReadyInitMessage),
condition.UnknownCondition(swiftv1beta1.SwiftStorageReadyCondition, condition.InitReason, condition.ReadyInitMessage),
)
instance.Status.Conditions.Init(&cl)
if err := r.Status().Update(ctx, instance); err != nil {
return ctrl.Result{}, err
}
}
if !instance.DeletionTimestamp.IsZero() {
return ctrl.Result{}, nil
}
helper, err := helper.NewHelper(
instance,
r.Client,
r.Kclient,
r.Scheme,
r.Log,
)
if err != nil {
return ctrl.Result{}, err
}
serviceLabels := swiftstorage.Labels()
envVars := make(map[string]env.Setter)
// Check if there is already an existing ConfigMap and device list. If
// not, create an initial device list to bootstrap the cluster with The
// weights are simply set to the requested size, this will be changed
// once all StatefulSets are running
_, ctrlResult, err := configmap.GetConfigMap(ctx, helper, instance, swiftv1beta1.DeviceConfigMapName, 5*time.Second)
if err != nil {
return ctrlResult, err
} else if (ctrlResult != ctrl.Result{}) {
devices := swiftstorage.DeviceList(ctx, helper, instance)
tpl := swiftstorage.DeviceConfigMapTemplates(instance, devices)
err = configmap.EnsureConfigMaps(ctx, helper, instance, tpl, &envVars)
if err != nil {
return ctrl.Result{}, err
}
}
// Create a ConfigMap populated with content from templates/
tpl := swiftstorage.ConfigMapTemplates(instance, serviceLabels)
err = configmap.EnsureConfigMaps(ctx, helper, instance, tpl, &envVars)
if err != nil {
return ctrl.Result{}, err
}
// Headless Service
svc := service.NewService(swiftstorage.Service(instance), serviceLabels, 5*time.Second)
ctrlResult, err = svc.CreateOrPatch(ctx, helper)
if err != nil {
return ctrlResult, err
} else if (ctrlResult != ctrl.Result{}) {
return ctrlResult, nil
}
// Limit internal storage traffic to Swift services
np := swiftstorage.NewNetworkPolicy(swiftstorage.NetworkPolicy(instance), serviceLabels, 5*time.Second)
ctrlResult, err = np.CreateOrPatch(ctx, helper)
if err != nil {
return ctrlResult, err
} else if (ctrlResult != ctrl.Result{}) {
return ctrlResult, nil
}
// Ensure the StatefulSet is not resized after initial deployment
found, err := statefulset.GetStatefulSetWithName(ctx, helper, instance.Name, instance.Namespace)
if err != nil && !apierrors.IsNotFound(err) {
return ctrlResult, err
} else if err == nil {
if *found.Spec.Replicas > instance.Spec.Replicas {
r.Log.Info(fmt.Sprintf(
"Downsizing (%d -> %d) number of replicas not supported",
*found.Spec.Replicas, instance.Spec.Replicas))
instance.Spec.Replicas = *found.Spec.Replicas
if err := r.Client.Update(ctx, instance); err != nil {
return ctrl.Result{}, err
}
}
}
// Statefulset with all backend containers
sset := statefulset.NewStatefulSet(swiftstorage.StatefulSet(instance, serviceLabels), 5*time.Second)
ctrlResult, err = sset.CreateOrPatch(ctx, helper)
if err != nil {
return ctrlResult, err
} else if (ctrlResult != ctrl.Result{}) {
return ctrlResult, nil
}
if sset.GetStatefulSet().Status.ReadyReplicas == instance.Spec.Replicas {
envVars := make(map[string]env.Setter)
devices := swiftstorage.DeviceList(ctx, helper, instance)
tpl = swiftstorage.DeviceConfigMapTemplates(instance, devices)
err = configmap.EnsureConfigMaps(ctx, helper, instance, tpl, &envVars)
if err != nil {
return ctrl.Result{}, err
}
instance.Status.Conditions.MarkTrue(condition.ReadyCondition, condition.ReadyMessage)
instance.Status.Conditions.MarkTrue(swiftv1beta1.SwiftStorageReadyCondition, condition.ReadyMessage)
if err := r.Status().Update(ctx, instance); err != nil {
return ctrl.Result{}, err
}
}
r.Log.Info(fmt.Sprintf("Reconciled SwiftStorage '%s' successfully", instance.Name))
return ctrl.Result{}, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *SwiftStorageReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&swiftv1beta1.SwiftStorage{}).
Owns(&corev1.ConfigMap{}).
Owns(&appsv1.StatefulSet{}).
Owns(&corev1.Service{}).
Owns(&networkingv1.NetworkPolicy{}).
Complete(r)
}