-
Notifications
You must be signed in to change notification settings - Fork 59
/
apischeme_controller.go
337 lines (304 loc) · 12.6 KB
/
apischeme_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
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
package apischeme
import (
"context"
"fmt"
"time"
cloudingressv1alpha1 "github.com/openshift/cloud-ingress-operator/pkg/apis/cloudingress/v1alpha1"
"github.com/openshift/cloud-ingress-operator/pkg/cloudclient"
utils "github.com/openshift/cloud-ingress-operator/pkg/controller/utils"
cioerrors "github.com/openshift/cloud-ingress-operator/pkg/errors"
baseutils "github.com/openshift/cloud-ingress-operator/pkg/utils"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"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"
"sigs.k8s.io/controller-runtime/pkg/source"
)
const (
reconcileFinalizerDNS = "dns.cloudingress.managed.openshift.io"
elbAnnotationKey = "service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout"
elbAnnotationValue = "1800"
)
var (
log = logf.Log.WithName("controller_apischeme")
// for testing to set it to something else
cloudClient cloudclient.CloudClient
)
/**
* USER ACTION REQUIRED: This is a scaffold file intended for the user to modify with their own Controller
* business logic. Delete these comments after modifying this file.*
*/
// Add creates a new APIScheme 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) error {
return add(mgr, newReconciler(mgr))
}
// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager) reconcile.Reconciler {
return &ReconcileAPIScheme{client: mgr.GetClient(), scheme: mgr.GetScheme()}
}
// add adds a new Controller to mgr with r as the reconcile.Reconciler
func add(mgr manager.Manager, r reconcile.Reconciler) error {
// Create a new controller
c, err := controller.New("apischeme-controller", mgr, controller.Options{Reconciler: r})
if err != nil {
return err
}
// Watch for changes to primary resource APIScheme
err = c.Watch(&source.Kind{Type: &cloudingressv1alpha1.APIScheme{}}, &handler.EnqueueRequestForObject{})
if err != nil {
return err
}
return nil
}
// blank assignment to verify that ReconcileAPIScheme implements reconcile.Reconciler
var _ reconcile.Reconciler = &ReconcileAPIScheme{}
// ReconcileAPIScheme reconciles a APIScheme object
type ReconcileAPIScheme struct {
// This client, initialized using mgr.Client() above, is a split client
// that reads objects from the cache and writes to the apiserver
client client.Client
scheme *runtime.Scheme
}
// LoadBalancer contains the relevant information to create a Load Balancer
// TODO: Move this into pkg/client
type LoadBalancer struct {
EndpointName string // FQDN of what it should be called
BaseDomain string // What is the base domain (DNS zone) for the EndpointName record?
}
// Reconcile will ensure that the rh-api management api endpoint is created and ready.
// Rough Steps:
// 1. Create Service
// 2. Add DNS CNAME from rh-api to the ELB created by AWS provider
// 3. Ready for work (Ready)
func (r *ReconcileAPIScheme) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
reqLogger.Info("Reconciling APIScheme")
// Fetch the APIScheme instance
instance := &cloudingressv1alpha1.APIScheme{}
err := r.client.Get(context.TODO(), request.NamespacedName, instance)
if err != nil {
if errors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
reqLogger.Info("Couldn't find the APIScheme object")
return reconcile.Result{}, nil
}
// Error reading the object - requeue the request.
reqLogger.Error(err, "Error reading APIScheme object")
return reconcile.Result{}, err
}
// If the management API isn't enabled, we have nothing to do!
if !instance.Spec.ManagementAPIServerIngress.Enabled {
reqLogger.Info("Not enabled", "instance", instance)
return reconcile.Result{}, nil
}
if cloudClient == nil {
cloudPlatform, err := baseutils.GetPlatformType(r.client)
if err != nil {
r.SetAPISchemeStatus(instance, "Couldn't reconcile", "Couldn't create a Cloud Client", cloudingressv1alpha1.ConditionError)
return reconcile.Result{}, err
}
cloudClient = cloudclient.GetClientFor(r.client, *cloudPlatform)
}
serviceNamespacedName := types.NamespacedName{
Name: instance.Spec.ManagementAPIServerIngress.DNSName,
Namespace: "openshift-kube-apiserver",
}
// Check for a deletion timestamp.
if instance.DeletionTimestamp.IsZero() {
// Request object is alive, so ensure it has the DNS finalizer.
if !controllerutil.ContainsFinalizer(instance, reconcileFinalizerDNS) {
controllerutil.AddFinalizer(instance, reconcileFinalizerDNS)
if err = r.client.Update(context.TODO(), instance); err != nil {
return reconcile.Result{}, err
}
}
} else {
// Request object is being deleted.
if controllerutil.ContainsFinalizer(instance, reconcileFinalizerDNS) {
found := &corev1.Service{}
if err = r.client.Get(context.TODO(), serviceNamespacedName, found); err != nil {
if errors.IsNotFound(err) {
// Service was not found!
//
// Skip the DeleteAdminAPIDNS call and remove the
// finalizer anyway so the CR deletion can proceed.
// This could leave DNS entries behind!
//
// TODO As a future enhancement, the CloudClient
// provider should handle this scenario and
// look up the necessary information itself
// to proceed with the DNS deletion.
found = nil
} else {
reqLogger.Error(err, "Couldn't get the Service")
return reconcile.Result{}, err
}
}
if found != nil {
err = cloudClient.DeleteAdminAPIDNS(context.TODO(), r.client, instance, found)
switch err {
case nil:
// all good
case err.(*cioerrors.LoadBalancerNotReadyError):
// couldn't find the load balancer - it's likely still queued for creation
r.SetAPISchemeStatus(instance, "Couldn't reconcile", "Load balancer isn't ready", cloudingressv1alpha1.ConditionError)
return reconcile.Result{Requeue: true, RequeueAfter: 10 * time.Second}, nil
default:
reqLogger.Error(err, "Failed to delete the DNS record")
r.SetAPISchemeStatus(instance, "Couldn't reconcile", "Failed to delete the DNS record", cloudingressv1alpha1.ConditionError)
return reconcile.Result{}, err
}
}
// Remove the DNS finalizer and update the request object.
controllerutil.RemoveFinalizer(instance, reconcileFinalizerDNS)
if err = r.client.Update(context.TODO(), instance); err != nil {
return reconcile.Result{}, err
}
// Requeue once more after updating. Without a finalizer,
// the next pass should delete the request object.
return reconcile.Result{Requeue: true}, nil
}
// Halt the reconciliation.
return reconcile.Result{}, nil
}
// Does the Service exist already?
found := &corev1.Service{}
err = r.client.Get(context.TODO(), serviceNamespacedName, found)
if err != nil {
if errors.IsNotFound(err) {
// need to create it
dep := r.newServiceFor(instance)
reqLogger.Info("Service not found. Creating", "service", dep)
err = r.client.Create(context.TODO(), dep)
if err != nil {
reqLogger.Error(err, "Failure to create new Service")
return reconcile.Result{}, err
}
// Reconcile again to get the new Service and give AWS time to create the ELB
reqLogger.Info("Service was just created, so let's try to requeue to set it up")
return reconcile.Result{Requeue: true, RequeueAfter: 10 * time.Second}, nil
} else if err != nil {
reqLogger.Error(err, "Couldn't get the Service")
return reconcile.Result{}, err
}
}
// Reconcile the access list in the Service
if !sliceEquals(found.Spec.LoadBalancerSourceRanges, instance.Spec.ManagementAPIServerIngress.AllowedCIDRBlocks) {
reqLogger.Info(fmt.Sprintf("Mismatch svc %s != %s\n", found.Spec.LoadBalancerSourceRanges, instance.Spec.ManagementAPIServerIngress.AllowedCIDRBlocks))
reqLogger.Info(fmt.Sprintf("Mismatch between %s/service/%s LoadBalancerSourceRanges and AllowedCIDRBlocks. Updating...", found.GetNamespace(), found.GetName()))
found.Spec.LoadBalancerSourceRanges = instance.Spec.ManagementAPIServerIngress.AllowedCIDRBlocks
err = r.client.Update(context.TODO(), found)
if err != nil {
reqLogger.Error(err, fmt.Sprintf("Failed to update the %s/service/%s LoadBalancerSourceRanges", found.GetNamespace(), found.GetName()))
return reconcile.Result{}, err
}
// let's re-queue just in case
reqLogger.Info("Requeuing after svc update")
return reconcile.Result{Requeue: true, RequeueAfter: 10 * time.Second}, nil
}
if !metav1.HasAnnotation(found.ObjectMeta, elbAnnotationKey) ||
found.Annotations[elbAnnotationKey] != elbAnnotationValue {
metav1.SetMetaDataAnnotation(&found.ObjectMeta, elbAnnotationKey, elbAnnotationValue)
err = r.client.Update(context.TODO(), found)
if err != nil {
reqLogger.Error(err, "Error updating service annotation")
return reconcile.Result{}, err
}
reqLogger.Info(fmt.Sprintf("Updated %s svc idle timeout to %s", found.Name, elbAnnotationValue))
}
err = cloudClient.EnsureAdminAPIDNS(context.TODO(), r.client, instance, found)
// Check for error types that this operator knows about
switch err {
case nil:
// no problems
r.SetAPISchemeStatus(instance, "Success", "Admin API Endpoint created", cloudingressv1alpha1.ConditionReady)
return reconcile.Result{RequeueAfter: 60 * time.Second}, nil
case err.(*cioerrors.DnsUpdateError):
// couldn't update DNS
r.SetAPISchemeStatus(instance, "Couldn't reconcile", "Couldn't ensure the admin API endpoint: "+err.Error(), cloudingressv1alpha1.ConditionError)
return reconcile.Result{}, err
case err.(*cioerrors.LoadBalancerNotReadyError):
r.SetAPISchemeStatus(instance, "Couldn't reconcile", "Load balancer isn't ready", cloudingressv1alpha1.ConditionError)
return reconcile.Result{Requeue: true, RequeueAfter: 10 * time.Second}, nil
default:
// not one of ours
log.Error(err, "Error ensuring Admin API", "instance", instance, "Service", found)
return reconcile.Result{}, err
}
}
func (r *ReconcileAPIScheme) newServiceFor(instance *cloudingressv1alpha1.APIScheme) *corev1.Service {
labels := map[string]string{
"app": "cloud-ingress-operator-" + instance.Spec.ManagementAPIServerIngress.DNSName,
"apischeme_cr": instance.GetName(),
}
selector := map[string]string{
"apiserver": "true",
"app": "openshift-kube-apiserver",
}
annotations := map[string]string{
elbAnnotationKey: elbAnnotationValue,
}
// Note: This owner reference should nbnot be expected to work
//ref := metav1.NewControllerRef(instance, instance.GetObjectKind().GroupVersionKind())
return &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: instance.Spec.ManagementAPIServerIngress.DNSName,
Namespace: "openshift-kube-apiserver",
Labels: labels,
Annotations: annotations,
//OwnerReferences: []metav1.OwnerReference{*ref},
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Protocol: "TCP",
Port: 6443,
TargetPort: intstr.FromInt(6443),
},
},
Selector: selector,
Type: corev1.ServiceTypeLoadBalancer,
LoadBalancerSourceRanges: instance.Spec.ManagementAPIServerIngress.AllowedCIDRBlocks,
},
}
}
// SetAPISchemeStatus will set the status on the APISscheme object with a human message, as in an error situation
func (r *ReconcileAPIScheme) SetAPISchemeStatus(crObject *cloudingressv1alpha1.APIScheme, reason, message string, ctype cloudingressv1alpha1.APISchemeConditionType) {
crObject.Status.Conditions = utils.SetAPISchemeCondition(
crObject.Status.Conditions,
ctype,
corev1.ConditionTrue,
reason,
message,
utils.UpdateConditionNever)
crObject.Status.State = ctype
err := r.client.Status().Update(context.TODO(), crObject)
// TODO: Should we return an error here if this update fails?
if err != nil {
log.Error(err, "Error updating cr status")
}
}
func sliceEquals(left, right []string) bool {
if len(left) != len(right) {
return false
}
for i := 0; i < len(left); i++ {
if left[i] != right[i] {
fmt.Printf("Mismatch %s != %s\n", left[i], right[i])
return false
}
}
return true
}