-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathdesignateapi_controller.go
888 lines (769 loc) · 30.5 KB
/
designateapi_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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
/*
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.
*/
// FIXME(dkehn): Adjust this file.
package controllers
import (
"context"
"fmt"
"time"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
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/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"github.com/go-logr/logr"
networkv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
designatev1beta1 "github.com/openstack-k8s-operators/designate-operator/api/v1beta1"
"github.com/openstack-k8s-operators/designate-operator/pkg/designate"
designateapi "github.com/openstack-k8s-operators/designate-operator/pkg/designateapi"
keystonev1 "github.com/openstack-k8s-operators/keystone-operator/api/v1beta1"
"github.com/openstack-k8s-operators/lib-common/modules/common"
"github.com/openstack-k8s-operators/lib-common/modules/common/condition"
"github.com/openstack-k8s-operators/lib-common/modules/common/deployment"
"github.com/openstack-k8s-operators/lib-common/modules/common/endpoint"
"github.com/openstack-k8s-operators/lib-common/modules/common/env"
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
"github.com/openstack-k8s-operators/lib-common/modules/common/labels"
nad "github.com/openstack-k8s-operators/lib-common/modules/common/networkattachment"
"github.com/openstack-k8s-operators/lib-common/modules/common/secret"
"github.com/openstack-k8s-operators/lib-common/modules/common/service"
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
)
// GetClient -
func (r *DesignateAPIReconciler) GetClient() client.Client {
return r.Client
}
// GetKClient -
func (r *DesignateAPIReconciler) GetKClient() kubernetes.Interface {
return r.Kclient
}
// GetScheme -
func (r *DesignateAPIReconciler) GetScheme() *runtime.Scheme {
return r.Scheme
}
// DesignateAPIReconciler reconciles a DesignateAPI object
type DesignateAPIReconciler struct {
client.Client
Kclient kubernetes.Interface
Scheme *runtime.Scheme
}
// GetLogger returns a logger object with a prefix of "controller.name" and additional controller context fields
func (r *DesignateAPIReconciler) GetLogger(ctx context.Context) logr.Logger {
return log.FromContext(ctx).WithName("Controllers").WithName("DesignateAPI")
}
var keystoneServices = []map[string]string{
{
"type": designate.ServiceType,
"name": designate.ServiceName,
"desc": "Designate Service",
},
}
//+kubebuilder:rbac:groups=designate.openstack.org,resources=designateapis,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=designate.openstack.org,resources=designateapis/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=designate.openstack.org,resources=designateapis/finalizers,verbs=update
// +kubebuilder:rbac:groups=core,resources=configmaps,verbs=get;list;create;update;patch;delete;watch
// +kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=core,resources=services,verbs=get;list;create;update;patch;delete;watch
// +kubebuilder:rbac:groups=core,resources=pods,verbs=get;list;
// +kubebuilder:rbac:groups=route.openshift.io,resources=routes,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=batch,resources=jobs,verbs=get;list;create;update;patch;delete;watch
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;create;update;patch;delete;watch
// +kubebuilder:rbac:groups=keystone.openstack.org,resources=keystoneservices,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=keystone.openstack.org,resources=keystoneendpoints,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=k8s.cni.cncf.io,resources=network-attachment-definitions,verbs=get;list;watch
// 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 DesignateAPI 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/[email protected]/pkg/reconcile
func (r *DesignateAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, _err error) {
Log := r.GetLogger(ctx)
// Fetch the DesignateAPI instance
instance := &designatev1beta1.DesignateAPI{}
err := r.Client.Get(ctx, req.NamespacedName, instance)
if err != nil {
if k8s_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.
return ctrl.Result{}, nil
}
// Error reading the object - requeue the request.
return ctrl.Result{}, err
}
helper, err := helper.NewHelper(
instance,
r.Client,
r.Kclient,
r.Scheme,
Log,
)
if err != nil {
return ctrl.Result{}, err
}
// Always patch the instance status when exiting this function so we can persist any changes.
defer func() {
// update the overall status condition if service is ready
if instance.IsReady() {
instance.Status.Conditions.MarkTrue(condition.ReadyCondition, condition.ReadyMessage)
}
err := helper.PatchInstance(ctx, instance)
if err != nil {
_err = err
return
}
}()
// If we're not deleting this and the service object doesn't have our finalizer, add it.
if instance.DeletionTimestamp.IsZero() && controllerutil.AddFinalizer(instance, helper.GetFinalizer()) {
return ctrl.Result{}, nil
}
//
// initialize status
//
if instance.Status.Conditions == nil {
instance.Status.Conditions = condition.Conditions{}
cl := condition.CreateList(
condition.UnknownCondition(condition.DBReadyCondition, condition.InitReason, condition.DBReadyInitMessage),
condition.UnknownCondition(condition.DBSyncReadyCondition, condition.InitReason, condition.DBSyncReadyInitMessage),
condition.UnknownCondition(condition.ExposeServiceReadyCondition, condition.InitReason, condition.ExposeServiceReadyInitMessage),
condition.UnknownCondition(condition.InputReadyCondition, condition.InitReason, condition.InputReadyInitMessage),
condition.UnknownCondition(condition.ServiceConfigReadyCondition, condition.InitReason, condition.ServiceConfigReadyInitMessage),
condition.UnknownCondition(condition.DeploymentReadyCondition, condition.InitReason, condition.DeploymentReadyInitMessage),
// right now we have no dedicated KeystoneServiceReadyInitMessage
condition.UnknownCondition(condition.KeystoneServiceReadyCondition, condition.InitReason, ""),
condition.UnknownCondition(condition.KeystoneEndpointReadyCondition, condition.InitReason, ""),
condition.UnknownCondition(condition.NetworkAttachmentsReadyCondition, condition.InitReason, condition.NetworkAttachmentsReadyInitMessage),
)
instance.Status.Conditions.Init(&cl)
// Register overall status immediately to have an early feedback e.g. in the cli
return ctrl.Result{}, nil
}
if instance.Status.Hash == nil {
instance.Status.Hash = map[string]string{}
}
if instance.Status.APIEndpoints == nil {
instance.Status.APIEndpoints = map[string]map[string]string{}
}
if instance.Status.NetworkAttachments == nil {
instance.Status.NetworkAttachments = map[string][]string{}
}
// Handle service delete
if !instance.DeletionTimestamp.IsZero() {
return r.reconcileDelete(ctx, instance, helper)
}
// Handle non-deleted clusters
return r.reconcileNormal(ctx, instance, helper)
}
// SetupWithManager sets up the controller with the Manager.
func (r *DesignateAPIReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
// Watch for changes to any CustomServiceConfigSecrets. Global secrets
// (e.g. TransportURLSecret) are handled by the top designate controller.
Log := r.GetLogger(ctx)
svcSecretFn := func(ctx context.Context, o client.Object) []reconcile.Request {
var namespace string = o.GetNamespace()
var secretName string = o.GetName()
result := []reconcile.Request{}
// get all API CRs
apis := &designatev1beta1.DesignateAPIList{}
listOpts := []client.ListOption{
client.InNamespace(namespace),
}
if err := r.Client.List(context.Background(), apis, listOpts...); err != nil {
Log.Error(err, "Unable to retrieve API CRs")
return nil
}
for _, cr := range apis.Items {
for _, v := range cr.Spec.CustomServiceConfigSecrets {
if v == secretName {
name := client.ObjectKey{
Namespace: namespace,
Name: cr.Name,
}
Log.Info(fmt.Sprintf("Secret %s is used by Designate CR %s", secretName, cr.Name))
result = append(result, reconcile.Request{NamespacedName: name})
}
}
}
if len(result) > 0 {
return result
}
return nil
}
// watch for configmap where the CM owner label AND the CR.Spec.ManagingCrName label matches
// Watch for changes to NADs
// NOTE: Dkehn/DPrince are configMap and NAD one in the same??
nadFn := func(ctx context.Context, o client.Object) []reconcile.Request {
result := []reconcile.Request{}
// configMapFn := func(o client.Object) []reconcile.Request {
// result := []reconcile.Request{}
// get all API CRs
designateAPIs := &designatev1beta1.DesignateAPIList{}
listOpts := []client.ListOption{
client.InNamespace(o.GetNamespace()),
}
if err := r.Client.List(context.Background(), designateAPIs, listOpts...); err != nil {
Log.Error(err, "Unable to retrieve DesignateAPI CRs %v")
return nil
}
for _, cr := range designateAPIs.Items {
if util.StringInSlice(o.GetName(), cr.Spec.NetworkAttachments) {
name := client.ObjectKey{
Namespace: cr.GetNamespace(),
Name: cr.GetName(),
}
Log.Info(fmt.Sprintf("NAD %s is used by DesignateAPI CR %s", o.GetName(), cr.GetName()))
result = append(result, reconcile.Request{NamespacedName: name})
}
}
if len(result) > 0 {
return result
}
return nil
}
return ctrl.NewControllerManagedBy(mgr).
For(&designatev1beta1.DesignateAPI{}).
Owns(&keystonev1.KeystoneService{}).
Owns(&keystonev1.KeystoneEndpoint{}).
Owns(&corev1.Service{}).
Owns(&appsv1.Deployment{}).
Watches(&corev1.Secret{},
handler.EnqueueRequestsFromMapFunc(svcSecretFn)).
Watches(&networkv1.NetworkAttachmentDefinition{},
handler.EnqueueRequestsFromMapFunc(nadFn)).
Complete(r)
}
func (r *DesignateAPIReconciler) reconcileDelete(ctx context.Context, instance *designatev1beta1.DesignateAPI, helper *helper.Helper) (ctrl.Result, error) {
Log := r.GetLogger(ctx)
Log.Info(fmt.Sprintf("Reconciling Service '%s' delete", instance.Name))
for _, ksSvc := range keystoneServices {
// Remove the finalizer from our KeystoneEndpoint CR
keystoneEndpoint, err := keystonev1.GetKeystoneEndpointWithName(ctx, helper, ksSvc["name"], instance.Namespace)
if err != nil && !k8s_errors.IsNotFound(err) {
return ctrl.Result{}, err
}
if err == nil {
controllerutil.RemoveFinalizer(keystoneEndpoint, helper.GetFinalizer())
if err = helper.GetClient().Update(ctx, keystoneEndpoint); err != nil && !k8s_errors.IsNotFound(err) {
return ctrl.Result{}, err
}
util.LogForObject(helper, "Removed finalizer from our KeystoneEndpoint", instance)
}
// Remove the finalizer from our KeystoneService CR
keystoneService, err := keystonev1.GetKeystoneServiceWithName(ctx, helper, ksSvc["name"], instance.Namespace)
if err != nil && !k8s_errors.IsNotFound(err) {
return ctrl.Result{}, err
}
if err == nil {
controllerutil.RemoveFinalizer(keystoneService, helper.GetFinalizer())
if err = helper.GetClient().Update(ctx, keystoneService); err != nil && !k8s_errors.IsNotFound(err) {
return ctrl.Result{}, err
}
util.LogForObject(helper, "Removed finalizer from our KeystoneService", instance)
}
}
// We did all the cleanup on the objects we created so we can remove the
// finalizer from ourselves to allow the deletion
controllerutil.RemoveFinalizer(instance, helper.GetFinalizer())
Log.Info(fmt.Sprintf("Reconciled Service '%s' delete successfully", instance.Name))
return ctrl.Result{}, nil
}
func (r *DesignateAPIReconciler) reconcileInit(
ctx context.Context,
instance *designatev1beta1.DesignateAPI,
helper *helper.Helper,
serviceLabels map[string]string,
) (ctrl.Result, error) {
Log := r.GetLogger(ctx)
Log.Info(fmt.Sprintf("Reconciling Service '%s' init", instance.Name))
//
// expose the service (create service, route and return the created endpoint URLs)
//
// Endpoint
publicEndpointData := endpoint.Data{
Port: designate.DesignatePublicPort,
Path: "/",
}
internalEndpointData := endpoint.Data{
Port: designate.DesignateInternalPort,
Path: "/",
}
designateEndpoints := map[service.Endpoint]endpoint.Data{
service.EndpointPublic: publicEndpointData,
service.EndpointInternal: internalEndpointData,
}
apiEndpoints := make(map[string]string)
for endpointType, data := range designateEndpoints {
endpointTypeStr := string(endpointType)
endpointName := designate.ServiceName + "-" + endpointTypeStr
svcOverride := instance.Spec.Override.Service[endpointType]
if svcOverride.EmbeddedLabelsAnnotations == nil {
svcOverride.EmbeddedLabelsAnnotations = &service.EmbeddedLabelsAnnotations{}
}
exportLabels := util.MergeStringMaps(
serviceLabels,
map[string]string{
service.AnnotationEndpointKey: endpointTypeStr,
},
)
// Create the service
svc, err := service.NewService(
service.GenericService(&service.GenericServiceDetails{
Name: endpointName,
Namespace: instance.Namespace,
Labels: exportLabels,
Selector: serviceLabels,
Port: service.GenericServicePort{
Name: endpointName,
Port: data.Port,
Protocol: corev1.ProtocolTCP,
},
}),
5,
&svcOverride.OverrideSpec,
)
if err != nil {
instance.Status.Conditions.Set(condition.FalseCondition(
condition.ExposeServiceReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.ExposeServiceReadyErrorMessage,
err.Error()))
return ctrl.Result{}, err
}
svc.AddAnnotation(map[string]string{
service.AnnotationEndpointKey: endpointTypeStr,
})
// add Annotation to whether creating an ingress is required or not
if endpointType == service.EndpointPublic && svc.GetServiceType() == corev1.ServiceTypeClusterIP {
svc.AddAnnotation(map[string]string{
service.AnnotationIngressCreateKey: "true",
})
} else {
svc.AddAnnotation(map[string]string{
service.AnnotationIngressCreateKey: "false",
})
if svc.GetServiceType() == corev1.ServiceTypeLoadBalancer {
svc.AddAnnotation(map[string]string{
service.AnnotationHostnameKey: svc.GetServiceHostname(), // add annotation to register service name in dnsmasq
})
}
}
ctrlResult, err := svc.CreateOrPatch(ctx, helper)
if err != nil {
instance.Status.Conditions.Set(condition.FalseCondition(
condition.ExposeServiceReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.ExposeServiceReadyErrorMessage,
err.Error()))
return ctrlResult, err
} else if (ctrlResult != ctrl.Result{}) {
instance.Status.Conditions.Set(condition.FalseCondition(
condition.ExposeServiceReadyCondition,
condition.RequestedReason,
condition.SeverityInfo,
condition.ExposeServiceReadyRunningMessage))
return ctrlResult, nil
}
// create service - end
// TODO: TLS, pass in https as protocol, create TLS cert
apiEndpoints[string(endpointType)], err = svc.GetAPIEndpoint(
svcOverride.EndpointURL, data.Protocol, data.Path)
if err != nil {
return ctrl.Result{}, err
}
}
// Endpoint - end
instance.Status.Conditions.MarkTrue(condition.ExposeServiceReadyCondition, condition.ExposeServiceReadyMessage)
// expose service - end
//
// Update instance status with service endpoint url from route host information
//
if instance.Status.APIEndpoints == nil {
instance.Status.APIEndpoints = map[string]map[string]string{}
}
// instance.Status.APIEndpoints = apiEndpoints
instance.Status.APIEndpoints[designate.ServiceName] = apiEndpoints
//
// create service and user in keystone - - https://docs.openstack.org/Designate/latest/install/install-rdo.html#configure-user-and-endpoints
// TODO: rework this
for _, ksSvc := range keystoneServices {
ksSvcSpec := keystonev1.KeystoneServiceSpec{
ServiceType: ksSvc["type"],
ServiceName: ksSvc["name"],
ServiceDescription: ksSvc["desc"],
Enabled: true,
ServiceUser: instance.Spec.ServiceUser,
Secret: instance.Spec.Secret,
PasswordSelector: instance.Spec.PasswordSelectors.Service,
}
ksSvcObj := keystonev1.NewKeystoneService(ksSvcSpec, instance.Namespace, serviceLabels, time.Duration(10)*time.Second)
ctrlResult, err := ksSvcObj.CreateOrPatch(ctx, helper)
if err != nil {
return ctrlResult, err
}
// mirror the Status, Reason, Severity and Message of the latest keystoneservice condition
// into a local condition with the type condition.KeystoneServiceReadyCondition
c := ksSvcObj.GetConditions().Mirror(condition.KeystoneServiceReadyCondition)
if c != nil {
instance.Status.Conditions.Set(c)
}
if (ctrlResult != ctrl.Result{}) {
return ctrlResult, nil
}
ksEndptSpec := keystonev1.KeystoneEndpointSpec{
ServiceName: ksSvc["name"],
Endpoints: apiEndpoints,
}
ksEndptObj := keystonev1.NewKeystoneEndpoint(
ksSvc["name"],
instance.Namespace,
ksEndptSpec,
serviceLabels,
time.Duration(10)*time.Second)
ctrlResult, err = ksEndptObj.CreateOrPatch(ctx, helper)
if err != nil {
return ctrlResult, err
}
// mirror the Status, Reason, Severity and Message of the latest keystoneendpoint condition
// into a local condition with the type condition.KeystoneEndpointReadyCondition
c = ksEndptObj.GetConditions().Mirror(condition.KeystoneEndpointReadyCondition)
if c != nil {
instance.Status.Conditions.Set(c)
}
if (ctrlResult != ctrl.Result{}) {
return ctrl.Result{}, nil
}
}
Log.Info(fmt.Sprintf("Reconciled Service '%s' init successfully", instance.Name))
return ctrl.Result{}, nil
}
func (r *DesignateAPIReconciler) reconcileNormal(ctx context.Context, instance *designatev1beta1.DesignateAPI, helper *helper.Helper) (ctrl.Result, error) {
Log := r.GetLogger(ctx)
Log.Info("Reconciling Service")
// ConfigMap
configMapVars := make(map[string]env.Setter)
//
// check for required OpenStack secret holding passwords for service/admin user and add hash to the vars map
//
ctrlResult, err := r.getSecret(ctx, helper, instance, instance.Spec.Secret, &configMapVars, "secret-")
if err != nil {
return ctrlResult, err
}
// run check OpenStack secret - end
//
// check for required TransportURL secret holding transport URL string
//
ctrlResult, err = r.getSecret(ctx, helper, instance, instance.Spec.TransportURLSecret, &configMapVars, "secret-")
if err != nil {
return ctrlResult, err
}
// run check TransportURL secret - end
//
// check for required service secrets
//
for _, secretName := range instance.Spec.CustomServiceConfigSecrets {
ctrlResult, err = r.getSecret(ctx, helper, instance, secretName, &configMapVars, "secret-")
if err != nil {
return ctrlResult, err
}
}
// run check service secrets - end
//
// check for required Designate config maps that should have been created by parent Designate CR
//
parentDesignateName := designate.GetOwningDesignateName(instance)
ctrlResult, err = r.getSecret(ctx, helper, instance, fmt.Sprintf("%s-scripts", parentDesignateName), &configMapVars, "")
if err != nil {
return ctrlResult, err
}
ctrlResult, err = r.getSecret(ctx, helper, instance, fmt.Sprintf("%s-config-data", parentDesignateName), &configMapVars, "")
// note r.getSecret adds Conditions with condition.InputReadyWaitingMessage
// when secret is not found
if err != nil {
return ctrlResult, err
}
instance.Status.Conditions.MarkTrue(condition.InputReadyCondition, condition.InputReadyMessage)
// run check parent Designate CR config maps - end
//
// Create ConfigMaps required as input for the Service and calculate an overall hash of hashes
//
serviceLabels := map[string]string{
common.AppSelector: designate.ServiceName,
common.ComponentSelector: designateapi.Component,
}
//
// create custom Configmap for this designate volume service
//
err = r.generateServiceConfigMaps(ctx, helper, instance, &configMapVars, serviceLabels)
if err != nil {
instance.Status.Conditions.Set(condition.FalseCondition(
condition.ServiceConfigReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.ServiceConfigReadyErrorMessage,
err.Error()))
return ctrl.Result{}, err
}
// Create ConfigMaps - end
//
// create hash over all the different input resources to identify if any those changed
// and a restart/recreate is required.
//
inputHash, hashChanged, err := r.createHashOfInputHashes(ctx, instance, configMapVars)
if err != nil {
instance.Status.Conditions.Set(condition.FalseCondition(
condition.ServiceConfigReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.ServiceConfigReadyErrorMessage,
err.Error()))
return ctrl.Result{}, err
} else if hashChanged {
// Hash changed and instance status should be updated (which will be done by main defer func),
// so we need to return and reconcile again
return ctrl.Result{}, nil
}
instance.Status.Conditions.MarkTrue(condition.ServiceConfigReadyCondition, condition.ServiceConfigReadyMessage)
// Create ConfigMaps and Secrets - end
//
// TODO check when/if Init, Update, or Upgrade should/could be skipped
//
// networks to attach to
for _, netAtt := range instance.Spec.NetworkAttachments {
_, err := nad.GetNADWithName(ctx, helper, netAtt, instance.Namespace)
if err != nil {
if k8s_errors.IsNotFound(err) {
instance.Status.Conditions.Set(condition.FalseCondition(
condition.NetworkAttachmentsReadyCondition,
condition.RequestedReason,
condition.SeverityInfo,
condition.NetworkAttachmentsReadyWaitingMessage,
netAtt))
return ctrl.Result{RequeueAfter: time.Second * 10}, fmt.Errorf("network-attachment-definition %s not found", netAtt)
}
instance.Status.Conditions.Set(condition.FalseCondition(
condition.NetworkAttachmentsReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.NetworkAttachmentsReadyErrorMessage,
err.Error()))
return ctrl.Result{}, err
}
}
serviceAnnotations, err := nad.CreateNetworksAnnotation(instance.Namespace, instance.Spec.NetworkAttachments)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed create network annotation from %s: %w",
instance.Spec.NetworkAttachments, err)
}
// Handle service init
ctrlResult, err = r.reconcileInit(ctx, instance, helper, serviceLabels)
if err != nil {
return ctrlResult, err
} else if (ctrlResult != ctrl.Result{}) {
return ctrlResult, nil
}
// Handle service update
ctrlResult, err = r.reconcileUpdate(ctx, instance, helper)
if err != nil {
return ctrlResult, err
} else if (ctrlResult != ctrl.Result{}) {
return ctrlResult, nil
}
// Handle service upgrade
ctrlResult, err = r.reconcileUpgrade(ctx, instance, helper)
if err != nil {
return ctrlResult, err
} else if (ctrlResult != ctrl.Result{}) {
return ctrlResult, nil
}
//
// normal reconcile tasks
//
// Define a new Deployment object
deplDef := designateapi.Deployment(instance, inputHash, serviceLabels, serviceAnnotations)
depl := deployment.NewDeployment(
deplDef,
time.Duration(5)*time.Second,
)
ctrlResult, err = depl.CreateOrPatch(ctx, helper)
if err != nil {
instance.Status.Conditions.Set(condition.FalseCondition(
condition.DeploymentReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.DeploymentReadyErrorMessage,
err.Error()))
return ctrlResult, err
} else if (ctrlResult != ctrl.Result{}) {
instance.Status.Conditions.Set(condition.FalseCondition(
condition.DeploymentReadyCondition,
condition.RequestedReason,
condition.SeverityInfo,
condition.DeploymentReadyRunningMessage))
return ctrlResult, nil
}
instance.Status.ReadyCount = depl.GetDeployment().Status.ReadyReplicas
// verify if network attachment matches expectations
networkReady := false
networkAttachmentStatus := map[string][]string{}
if *(instance.Spec.Replicas) > 0 {
networkReady, networkAttachmentStatus, err = nad.VerifyNetworkStatusFromAnnotation(
ctx,
helper,
instance.Spec.NetworkAttachments,
serviceLabels,
instance.Status.ReadyCount,
)
if err != nil {
return ctrl.Result{}, err
}
} else {
networkReady = true
}
instance.Status.NetworkAttachments = networkAttachmentStatus
if networkReady {
instance.Status.Conditions.MarkTrue(condition.NetworkAttachmentsReadyCondition, condition.NetworkAttachmentsReadyMessage)
} else {
err := fmt.Errorf("not all pods have interfaces with ips as configured in NetworkAttachments: %s", instance.Spec.NetworkAttachments)
instance.Status.Conditions.Set(condition.FalseCondition(
condition.NetworkAttachmentsReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.NetworkAttachmentsReadyErrorMessage,
err.Error()))
return ctrl.Result{}, err
}
if instance.Status.ReadyCount > 0 {
instance.Status.Conditions.MarkTrue(condition.DeploymentReadyCondition, condition.DeploymentReadyMessage)
}
// create Deployment - end
Log.Info("Reconciled Service successfully")
return ctrl.Result{}, nil
}
func (r *DesignateAPIReconciler) reconcileUpdate(ctx context.Context, instance *designatev1beta1.DesignateAPI, helper *helper.Helper) (ctrl.Result, error) {
Log := r.GetLogger(ctx)
Log.Info(fmt.Sprintf("Reconciling Service '%s' update", instance.Name))
// TODO: should have minor update tasks if required
// - delete dbsync hash from status to rerun it?
Log.Info(fmt.Sprintf("Reconciled Service '%s' update successfully", instance.Name))
return ctrl.Result{}, nil
}
func (r *DesignateAPIReconciler) reconcileUpgrade(ctx context.Context, instance *designatev1beta1.DesignateAPI, helper *helper.Helper) (ctrl.Result, error) {
Log := r.GetLogger(ctx)
Log.Info(fmt.Sprintf("Reconciling Service '%s' upgrade", instance.Name))
// TODO: should have major version upgrade tasks
// -delete dbsync hash from status to rerun it?
Log.Info(fmt.Sprintf("Reconciled Service '%s' upgrade successfully", instance.Name))
return ctrl.Result{}, nil
}
// getSecret - get the specified secret, and add its hash to envVars
func (r *DesignateAPIReconciler) getSecret(
ctx context.Context,
h *helper.Helper,
instance *designatev1beta1.DesignateAPI,
secretName string,
envVars *map[string]env.Setter,
prefix string,
) (ctrl.Result, error) {
secret, hash, err := secret.GetSecret(ctx, h, secretName, instance.Namespace)
if err != nil {
if k8s_errors.IsNotFound(err) {
instance.Status.Conditions.Set(condition.FalseCondition(
condition.InputReadyCondition,
condition.RequestedReason,
condition.SeverityInfo,
condition.InputReadyWaitingMessage))
return ctrl.Result{RequeueAfter: time.Duration(10) * time.Second}, fmt.Errorf("Secret %s not found", secretName)
}
instance.Status.Conditions.Set(condition.FalseCondition(
condition.InputReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.InputReadyErrorMessage,
err.Error()))
return ctrl.Result{}, err
}
// Add a prefix to the var name to avoid accidental collision with other non-secret
// vars. The secret names themselves will be unique.
(*envVars)[prefix+secret.Name] = env.SetValue(hash)
return ctrl.Result{}, nil
}
// generateServiceConfigMaps - create custom configmap to hold service-specific config
// TODO add DefaultConfigOverwrite
func (r *DesignateAPIReconciler) generateServiceConfigMaps(
ctx context.Context,
h *helper.Helper,
instance *designatev1beta1.DesignateAPI,
envVars *map[string]env.Setter,
serviceLabels map[string]string,
) error {
//
// create custom Configmap for designate-api-specific config input
// - %-config-data configmap holding custom config for the service's designate.conf
//
cmLabels := labels.GetLabels(instance, labels.GetGroupLabel(designate.ServiceName), serviceLabels)
// customData hold any customization for the service.
// custom.conf is going to be merged into /etc/designate/conder.conf
// TODO: make sure custom.conf can not be overwritten
customData := map[string]string{common.CustomServiceConfigFileName: instance.Spec.CustomServiceConfig}
for key, data := range instance.Spec.DefaultConfigOverwrite {
customData[key] = data
}
customData[common.CustomServiceConfigFileName] = instance.Spec.CustomServiceConfig
cms := []util.Template{
// Custom ConfigMap
{
Name: fmt.Sprintf("%s-config-data", instance.Name),
Namespace: instance.Namespace,
Type: util.TemplateTypeConfig,
InstanceType: instance.Kind,
CustomData: customData,
Labels: cmLabels,
},
}
return secret.EnsureSecrets(ctx, h, instance, cms, envVars)
}
// createHashOfInputHashes - creates a hash of hashes which gets added to the resources which requires a restart
// if any of the input resources change, like configs, passwords, ...
//
// returns the hash, whether the hash changed (as a bool) and any error
func (r *DesignateAPIReconciler) createHashOfInputHashes(
ctx context.Context,
instance *designatev1beta1.DesignateAPI,
envVars map[string]env.Setter,
) (string, bool, error) {
Log := r.GetLogger(ctx)
var hashMap map[string]string
changed := false
mergedMapVars := env.MergeEnvs([]corev1.EnvVar{}, envVars)
hash, err := util.ObjectHash(mergedMapVars)
if err != nil {
return hash, changed, err
}
if hashMap, changed = util.SetHash(instance.Status.Hash, common.InputHashName, hash); changed {
instance.Status.Hash = hashMap
Log.Info(fmt.Sprintf("Input maps hash %s - %s", common.InputHashName, hash))
}
return hash, changed, nil
}