forked from openstack-k8s-operators/ironic-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathironicneutronagent_controller.go
More file actions
791 lines (706 loc) · 26.3 KB
/
ironicneutronagent_controller.go
File metadata and controls
791 lines (706 loc) · 26.3 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
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
/*
Copyright 2023 Red Hat Inc.
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"
"time"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"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/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"github.com/go-logr/logr"
rabbitmqv1 "github.com/openstack-k8s-operators/infra-operator/apis/rabbitmq/v1beta1"
ironicv1 "github.com/openstack-k8s-operators/ironic-operator/api/v1beta1"
ironic "github.com/openstack-k8s-operators/ironic-operator/pkg/ironic"
"github.com/openstack-k8s-operators/ironic-operator/pkg/ironicneutronagent"
keystonev1 "github.com/openstack-k8s-operators/keystone-operator/api/v1beta1"
endpoint "github.com/openstack-k8s-operators/lib-common/modules/common/endpoint"
"github.com/openstack-k8s-operators/lib-common/modules/common/tls"
topologyv1 "github.com/openstack-k8s-operators/infra-operator/apis/topology/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/env"
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
"github.com/openstack-k8s-operators/lib-common/modules/common/labels"
common_rbac "github.com/openstack-k8s-operators/lib-common/modules/common/rbac"
"github.com/openstack-k8s-operators/lib-common/modules/common/secret"
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
)
// IronicNeutronAgentReconciler reconciles a IronicNeutronAgent object
type IronicNeutronAgentReconciler struct {
client.Client
Kclient kubernetes.Interface
Scheme *runtime.Scheme
}
// getlogger returns a logger object with a prefix of "conroller.name" and aditional controller context fields
func (r *IronicNeutronAgentReconciler) GetLogger(ctx context.Context) logr.Logger {
return log.FromContext(ctx).WithName("Controllers").WithName("IronicNeutronAgent")
}
// +kubebuilder:rbac:groups=ironic.openstack.org,resources=ironicneutronagents,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=ironic.openstack.org,resources=ironicneutronagents/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=ironic.openstack.org,resources=ironicneutronagents/finalizers,verbs=update;patch
// +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=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=core,resources=pods,verbs=get;list
// +kubebuilder:rbac:groups=keystone.openstack.org,resources=keystoneservices,verbs=get;list;watch
// +kubebuilder:rbac:groups=keystone.openstack.org,resources=keystoneendpoints,verbs=get;list;watch
// service account, role, rolebinding
// +kubebuilder:rbac:groups="",resources=serviceaccounts,verbs=get;list;watch;create;update;patch
// +kubebuilder:rbac:groups="rbac.authorization.k8s.io",resources=roles,verbs=get;list;watch;create;update;patch
// +kubebuilder:rbac:groups="rbac.authorization.k8s.io",resources=rolebindings,verbs=get;list;watch;create;update;patch
// service account permissions that are needed to grant permission to the above
// +kubebuilder:rbac:groups="security.openshift.io",resourceNames=anyuid;privileged,resources=securitycontextconstraints,verbs=use
// +kubebuilder:rbac:groups="",resources=pods,verbs=create;delete;get;list;patch;update;watch
// +kubebuilder:rbac:groups=topology.openstack.org,resources=topologies,verbs=get;list;watch;update
// Reconcile -
func (r *IronicNeutronAgentReconciler) Reconcile(
ctx context.Context,
req ctrl.Request,
) (result ctrl.Result, _err error) {
Log := r.GetLogger(ctx)
// Fetch the IronicNeutronAgent instance
instance := &ironicv1.IronicNeutronAgent{}
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
}
// initialize status if Conditions is nil, but do not reset if it already
// exists
isNewInstance := instance.Status.Conditions == nil
if isNewInstance {
instance.Status.Conditions = condition.Conditions{}
}
// Save a copy of the condtions so that we can restore the LastTransitionTime
// when a condition's state doesn't change.
savedConditions := instance.Status.Conditions.DeepCopy()
// Always patch the instance status when exiting this function so we can
// persist any changes.
defer func() {
condition.RestoreLastTransitionTimes(
&instance.Status.Conditions, savedConditions)
if instance.Status.Conditions.IsUnknown(condition.ReadyCondition) {
instance.Status.Conditions.Set(
instance.Status.Conditions.Mirror(condition.ReadyCondition))
}
err := helper.PatchInstance(ctx, instance)
if err != nil {
_err = err
return
}
}()
//
// initialize status
//
// initialize conditions used later as Status=Unknown
cl := condition.CreateList(
condition.UnknownCondition(
condition.InputReadyCondition,
condition.InitReason,
condition.InputReadyInitMessage),
condition.UnknownCondition(
condition.RabbitMqTransportURLReadyCondition,
condition.InitReason,
condition.RabbitMqTransportURLReadyInitMessage),
condition.UnknownCondition(
condition.ServiceConfigReadyCondition,
condition.InitReason,
condition.ServiceConfigReadyInitMessage),
condition.UnknownCondition(
condition.DeploymentReadyCondition,
condition.InitReason,
condition.DeploymentReadyInitMessage),
condition.UnknownCondition(
condition.TLSInputReadyCondition,
condition.InitReason,
condition.InputReadyInitMessage),
// service account, role, rolebinding conditions
condition.UnknownCondition(
condition.ServiceAccountReadyCondition,
condition.InitReason,
condition.ServiceAccountReadyInitMessage),
condition.UnknownCondition(
condition.RoleReadyCondition,
condition.InitReason,
condition.RoleReadyInitMessage),
condition.UnknownCondition(
condition.RoleBindingReadyCondition,
condition.InitReason,
condition.RoleBindingReadyInitMessage),
)
instance.Status.Conditions.Init(&cl)
instance.Status.ObservedGeneration = instance.Generation
// 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()) || isNewInstance {
return ctrl.Result{}, nil
}
if instance.Status.Hash == nil {
instance.Status.Hash = map[string]string{}
}
// Init Topology condition if there's a reference
if instance.Spec.TopologyRef != nil {
c := condition.UnknownCondition(condition.TopologyReadyCondition, condition.InitReason, condition.TopologyReadyInitMessage)
cl.Set(c)
}
// Handle service delete
if !instance.DeletionTimestamp.IsZero() {
return r.reconcileDelete(ctx, instance, helper)
}
// Handle non-delete
return r.reconcileNormal(ctx, instance, helper)
}
// SetupWithManager - sets up the controller with the Manager.
func (r *IronicNeutronAgentReconciler) SetupWithManager(
ctx context.Context, mgr ctrl.Manager,
) error {
// index passwordSecretField
if err := mgr.GetFieldIndexer().IndexField(ctx, &ironicv1.IronicNeutronAgent{}, passwordSecretField, func(rawObj client.Object) []string {
// Extract the secret name from the spec, if one is provided
cr := rawObj.(*ironicv1.IronicNeutronAgent)
if cr.Spec.Secret == "" {
return nil
}
return []string{cr.Spec.Secret}
}); err != nil {
return err
}
// index caBundleSecretNameField
if err := mgr.GetFieldIndexer().IndexField(ctx, &ironicv1.IronicNeutronAgent{}, caBundleSecretNameField, func(rawObj client.Object) []string {
// Extract the secret name from the spec, if one is provided
cr := rawObj.(*ironicv1.IronicNeutronAgent)
if cr.Spec.TLS.CaBundleSecretName == "" {
return nil
}
return []string{cr.Spec.TLS.CaBundleSecretName}
}); err != nil {
return err
}
// index topologyField
if err := mgr.GetFieldIndexer().IndexField(ctx, &ironicv1.IronicNeutronAgent{}, topologyField, func(rawObj client.Object) []string {
// Extract the topology name from the spec, if one is provided
cr := rawObj.(*ironicv1.IronicNeutronAgent)
if cr.Spec.TopologyRef == nil {
return nil
}
return []string{cr.Spec.TopologyRef.Name}
}); err != nil {
return err
}
return ctrl.NewControllerManagedBy(mgr).
For(&ironicv1.IronicNeutronAgent{}).
Owns(&appsv1.Deployment{}).
Owns(&corev1.Secret{}).
Owns(&corev1.ConfigMap{}).
Owns(&rabbitmqv1.TransportURL{}).
Owns(&corev1.ServiceAccount{}).
Owns(&rbacv1.Role{}).
Owns(&rbacv1.RoleBinding{}).
Watches(
&corev1.Secret{},
handler.EnqueueRequestsFromMapFunc(r.findObjectsForSrc),
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
).
Watches(&topologyv1.Topology{},
handler.EnqueueRequestsFromMapFunc(r.findObjectsForSrc),
builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Complete(r)
}
func (r *IronicNeutronAgentReconciler) findObjectsForSrc(ctx context.Context, src client.Object) []reconcile.Request {
requests := []reconcile.Request{}
l := log.FromContext(ctx).WithName("Controllers").WithName("IronicNeutronAgent")
for _, field := range ironicNeutronAgentWatchFields {
crList := &ironicv1.IronicNeutronAgentList{}
listOps := &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector(field, src.GetName()),
Namespace: src.GetNamespace(),
}
err := r.List(ctx, crList, listOps)
if err != nil {
l.Error(err, fmt.Sprintf("listing %s for field: %s - %s", crList.GroupVersionKind().Kind, field, src.GetNamespace()))
return requests
}
for _, item := range crList.Items {
l.Info(fmt.Sprintf("input source %s changed, reconcile: %s - %s", src.GetName(), item.GetName(), item.GetNamespace()))
requests = append(requests,
reconcile.Request{
NamespacedName: types.NamespacedName{
Name: item.GetName(),
Namespace: item.GetNamespace(),
},
},
)
}
}
return requests
}
func (r *IronicNeutronAgentReconciler) reconcileTransportURL(
ctx context.Context,
instance *ironicv1.IronicNeutronAgent,
helper *helper.Helper,
) (ctrl.Result, error) {
// Create RabbitMQ transport URL CR and get the actual URL from the
// associted secret that is created
//
Log := r.GetLogger(ctx)
transportURL, op, err := ironic.TransportURLCreateOrUpdate(
instance.Name,
instance.Namespace,
instance.Spec.RabbitMqClusterName,
instance,
helper,
)
if err != nil {
instance.Status.Conditions.Set(condition.FalseCondition(
condition.RabbitMqTransportURLReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.RabbitMqTransportURLReadyErrorMessage,
err.Error(),
))
return ctrl.Result{}, err
}
if op != controllerutil.OperationResultNone {
Log.Info(fmt.Sprintf(
"TransportURL %s successfully reconciled - operation: %s",
transportURL.Name, string(op)))
}
instance.Status.TransportURLSecret = transportURL.Status.SecretName
if instance.Status.TransportURLSecret == "" {
Log.Info(fmt.Sprintf(
"Waiting for TransportURL %s secret to be created",
transportURL.Name))
instance.Status.Conditions.Set(condition.FalseCondition(
condition.RabbitMqTransportURLReadyCondition,
condition.RequestedReason,
condition.SeverityInfo,
condition.RabbitMqTransportURLReadyRunningMessage))
return ctrl.Result{RequeueAfter: time.Second * 10}, nil
}
instance.Status.Conditions.MarkTrue(
condition.RabbitMqTransportURLReadyCondition,
condition.RabbitMqTransportURLReadyMessage)
return ctrl.Result{}, nil
}
func (r *IronicNeutronAgentReconciler) reconcileConfigMapsAndSecrets(
ctx context.Context,
instance *ironicv1.IronicNeutronAgent,
helper *helper.Helper,
) (ctrl.Result, string, error) {
// 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
ospSecret, hash, err := secret.GetSecret(ctx, helper, instance.Spec.Secret, instance.Namespace)
if err != nil {
if k8s_errors.IsNotFound(err) {
log.FromContext(ctx).Info(fmt.Sprintf("OpenStack secret %s not found", instance.Spec.Secret))
instance.Status.Conditions.Set(condition.FalseCondition(
condition.InputReadyCondition,
condition.RequestedReason,
condition.SeverityInfo,
condition.InputReadyWaitingMessage))
return ctrl.Result{RequeueAfter: time.Second * 10}, "", nil
}
instance.Status.Conditions.Set(condition.FalseCondition(
condition.InputReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.InputReadyErrorMessage,
err.Error()))
return ctrl.Result{}, "", err
}
configMapVars[ospSecret.Name] = env.SetValue(hash)
instance.Status.Conditions.MarkTrue(
condition.InputReadyCondition,
condition.InputReadyMessage)
// run check OpenStack secret - end
//
// TLS input validation
//
// Validate the CA cert secret if provided
if instance.Spec.TLS.CaBundleSecretName != "" {
hash, err := tls.ValidateCACertSecret(
ctx,
helper.GetClient(),
types.NamespacedName{
Name: instance.Spec.TLS.CaBundleSecretName,
Namespace: instance.Namespace,
},
)
if err != nil {
if k8s_errors.IsNotFound(err) {
instance.Status.Conditions.Set(condition.FalseCondition(
condition.TLSInputReadyCondition,
condition.RequestedReason,
condition.SeverityInfo,
fmt.Sprintf(condition.TLSInputReadyWaitingMessage, instance.Spec.TLS.CaBundleSecretName)))
return ctrl.Result{RequeueAfter: time.Second * 10}, "", nil
}
instance.Status.Conditions.Set(condition.FalseCondition(
condition.TLSInputReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.TLSInputErrorMessage,
err.Error()))
return ctrl.Result{}, "", err
}
if hash != "" {
configMapVars[tls.CABundleKey] = env.SetValue(hash)
}
}
// all cert input checks out so report InputReady
instance.Status.Conditions.MarkTrue(condition.TLSInputReadyCondition, condition.InputReadyMessage)
//
// Create ConfigMaps required as input for the Service and calculate an overall hash of hashes
//
// create custom Configmap for IronicNeutronAgent input
// - %-scripts configmap holding scripts to e.g. bootstrap the service
// - %-config configmap holding minimal neutron config required to get the
// service up, user can add additional files to be added to the service
// - parameters which has passwords gets added from the OpenStack secret via the init container
//
err = r.generateServiceConfigMaps(ctx, helper, 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
}
// 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{RequeueAfter: time.Second * 5}, "", nil
}
instance.Status.Conditions.MarkTrue(
condition.ServiceConfigReadyCondition,
condition.ServiceConfigReadyMessage)
// Create ConfigMaps and Secrets - end
return ctrl.Result{}, inputHash, nil
}
func (r *IronicNeutronAgentReconciler) reconcileDeployment(
ctx context.Context,
instance *ironicv1.IronicNeutronAgent,
helper *helper.Helper,
inputHash string,
serviceLabels map[string]string,
) (ctrl.Result, error) {
//
// Handle Topology
//
topology, err := ensureTopology(
ctx,
helper,
instance, // topologyHandler
instance.Name, // finalizer
&instance.Status.Conditions,
labels.GetLabelSelector(serviceLabels),
)
if err != nil {
instance.Status.Conditions.Set(condition.FalseCondition(
condition.TopologyReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.TopologyReadyErrorMessage,
err.Error()))
return ctrl.Result{}, fmt.Errorf("waiting for Topology requirements: %w", err)
}
// Define a new Deployment object
deplomentDef := ironicneutronagent.Deployment(
instance,
inputHash,
serviceLabels,
topology,
)
deployment := deployment.NewDeployment(deplomentDef, 5)
ctrlResult, err := deployment.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
}
// Only check ReadyCount if controller sees the last version of the CR
if deployment.GetDeployment().Generation == deployment.GetDeployment().Status.ObservedGeneration {
instance.Status.ReadyCount = deployment.GetDeployment().Status.ReadyReplicas
if instance.Status.ReadyCount == *instance.Spec.Replicas {
instance.Status.Conditions.MarkTrue(condition.DeploymentReadyCondition, condition.DeploymentReadyMessage)
}
}
return ctrl.Result{}, nil
}
func (r *IronicNeutronAgentReconciler) reconcileNormal(
ctx context.Context,
instance *ironicv1.IronicNeutronAgent,
helper *helper.Helper,
) (ctrl.Result, error) {
Log := r.GetLogger(ctx)
Log.Info("Reconciling IronicNeutronAgent")
if ironicv1.GetOwningIronicName(instance) == "" {
// Service account, role, binding
rbacResult, err := common_rbac.ReconcileRbac(ctx, helper, instance, getCommonRbacRules())
if err != nil {
return rbacResult, err
} else if (rbacResult != ctrl.Result{}) {
return rbacResult, nil
}
} else {
// TODO(hjensas): Mirror conditions from parent, or check resource exist first
instance.RbacConditionsSet(condition.TrueCondition(
condition.ServiceAccountReadyCondition,
condition.ServiceAccountReadyMessage))
instance.RbacConditionsSet(condition.TrueCondition(
condition.RoleReadyCondition,
condition.RoleReadyMessage))
instance.RbacConditionsSet(condition.TrueCondition(
condition.RoleBindingReadyCondition,
condition.RoleBindingReadyMessage))
}
ctrlResult, err := r.reconcileTransportURL(ctx, instance, helper)
if err != nil {
return ctrlResult, err
} else if (ctrlResult != ctrl.Result{}) {
return ctrlResult, nil
}
ctrlResult, inputHash, err := r.reconcileConfigMapsAndSecrets(ctx, instance, helper)
if err != nil {
return ctrlResult, err
} else if (ctrlResult != ctrl.Result{}) {
return ctrlResult, nil
}
//
// TODO check when/if Init, Update, or Upgrade should/could be skipped
//
serviceLabels := map[string]string{
common.AppSelector: ironicneutronagent.ServiceName,
common.ComponentSelector: ironicneutronagent.ServiceName,
}
// Handle service init
ctrlResult, err = r.reconcileInit(ctx)
if err != nil {
return ctrlResult, err
} else if (ctrlResult != ctrl.Result{}) {
return ctrlResult, nil
}
// Handle service update
ctrlResult, err = r.reconcileUpdate(ctx)
if err != nil {
return ctrlResult, err
} else if (ctrlResult != ctrl.Result{}) {
return ctrlResult, nil
}
// Handle service upgrade
ctrlResult, err = r.reconcileUpgrade(ctx)
if err != nil {
return ctrlResult, err
} else if (ctrlResult != ctrl.Result{}) {
return ctrlResult, nil
}
//
// normal reconcile tasks
//
ctrlResult, err = r.reconcileDeployment(ctx, instance, helper, inputHash, serviceLabels)
if err != nil {
return ctrlResult, err
} else if (ctrlResult != ctrl.Result{}) {
return ctrlResult, nil
}
// We reached the end of the Reconcile, update the Ready condition based on
// the sub conditions
if instance.Status.Conditions.AllSubConditionIsTrue() {
instance.Status.Conditions.MarkTrue(
condition.ReadyCondition, condition.ReadyMessage)
}
Log.Info("Reconciled IronicNeutronAgent Service successfully")
return ctrl.Result{}, nil
}
func (r *IronicNeutronAgentReconciler) reconcileInit(
ctx context.Context,
) (ctrl.Result, error) {
Log := r.GetLogger(ctx)
Log.Info("Reconciling IronicNeutronAgent init")
Log.Info("Reconciled IronicNeutronAgent init successfully")
return ctrl.Result{}, nil
}
func (r *IronicNeutronAgentReconciler) reconcileDelete(
ctx context.Context,
instance *ironicv1.IronicNeutronAgent,
helper *helper.Helper,
) (ctrl.Result, error) {
Log := r.GetLogger(ctx)
// Remove finalizer on the Topology CR
if ctrlResult, err := topologyv1.EnsureDeletedTopologyRef(
ctx,
helper,
instance.Status.LastAppliedTopology,
instance.Name,
); err != nil {
return ctrlResult, err
}
Log.Info("Reconciling IronicNeutronAgent delete")
// Service is deleted so remove the finalizer.
controllerutil.RemoveFinalizer(instance, helper.GetFinalizer())
Log.Info("Reconciled IronicNeutronAgent delete successfully")
return ctrl.Result{}, nil
}
func (r *IronicNeutronAgentReconciler) reconcileUpdate(
ctx context.Context,
) (ctrl.Result, error) {
Log := r.GetLogger(ctx)
Log.Info("Reconciling IronicNeutronAgent update")
Log.Info("Reconciled IronicNeutronAgent update successfully")
return ctrl.Result{}, nil
}
func (r *IronicNeutronAgentReconciler) reconcileUpgrade(
ctx context.Context,
) (ctrl.Result, error) {
Log := r.GetLogger(ctx)
Log.Info("Reconciling IronicNeutronAgent upgrade")
Log.Info("Reconciled IronicNeutronAgent upgrade successfully")
return ctrl.Result{}, nil
}
// generateServiceConfigMaps - create custom configmap to hold service-specific config
func (r *IronicNeutronAgentReconciler) generateServiceConfigMaps(
ctx context.Context,
h *helper.Helper,
instance *ironicv1.IronicNeutronAgent,
envVars *map[string]env.Setter,
) error {
//
// create custom Configmap for ironic-neutron-agnet-specific config input
// - %-config-data configmap holding custom config for the service config
//
cmLabels := labels.GetLabels(instance, labels.GetGroupLabel(ironic.ServiceName), map[string]string{})
// customData hold any customization for the service.
// custom.conf is going to be merged into /etc/ironic/ironic.conf
// TODO: make sure custom.conf can not be overwritten
customData := map[string]string{common.CustomServiceConfigFileName: instance.Spec.CustomServiceConfig}
keystoneAPI, err := keystonev1.GetKeystoneAPI(ctx, h, instance.Namespace, map[string]string{})
if err != nil {
return err
}
keystoneInternalURL, err := keystoneAPI.GetEndpoint(endpoint.EndpointInternal)
if err != nil {
return err
}
keystonePublicURL, err := keystoneAPI.GetEndpoint(endpoint.EndpointPublic)
if err != nil {
return err
}
templateParameters := make(map[string]interface{})
templateParameters["ServiceUser"] = instance.Spec.ServiceUser
templateParameters["KeystoneInternalURL"] = keystoneInternalURL
templateParameters["KeystonePublicURL"] = keystonePublicURL
cms := []util.Template{
// Scripts ConfigMap
{
Name: fmt.Sprintf("%s-scripts", instance.Name),
Namespace: instance.Namespace,
Type: util.TemplateTypeScripts,
InstanceType: instance.Kind,
AdditionalTemplate: map[string]string{
"common.sh": "/common/bin/common.sh",
},
Labels: cmLabels,
},
// Custom ConfigMap
{
Name: fmt.Sprintf("%s-config-data", instance.Name),
Namespace: instance.Namespace,
Type: util.TemplateTypeConfig,
InstanceType: instance.Kind,
CustomData: customData,
ConfigOptions: templateParameters,
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 *IronicNeutronAgentReconciler) createHashOfInputHashes(
ctx context.Context,
instance *ironicv1.IronicNeutronAgent,
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
}