Skip to content

Commit b44b23c

Browse files
committed
Deduplicate getSecret function
The ensureSecret function is already a common function that can be used in both Ceilometer and Autoscaling controllers. This patch removes the existing getSecret function, defined twice, and invoke the underlying ensureSecret directly. It also enhance its functionality by passing the configMapVars to properly update the input hash within the function. Signed-off-by: Francesco Pantano <fpantano@redhat.com>
1 parent b0a51e0 commit b44b23c

3 files changed

Lines changed: 40 additions & 71 deletions

File tree

internal/controller/autoscaling_controller.go

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -375,13 +375,19 @@ func (r *AutoscalingReconciler) reconcileNormal(
375375
validateFields := map[string]secret.Validator{
376376
instance.Spec.Aodh.PasswordSelectors.AodhService: secret.PasswordValidator{},
377377
}
378-
ctrlResult, err := r.getSecret(
378+
379+
_, ctrlResult, err := ensureSecret(
379380
ctx,
380-
helper,
381-
instance,
382-
instance.Spec.Aodh.Secret,
381+
types.NamespacedName{
382+
Namespace: instance.Namespace,
383+
Name: instance.Spec.Aodh.Secret,
384+
},
383385
validateFields,
384-
&configMapVars)
386+
helper.GetClient(),
387+
&instance.Status.Conditions,
388+
&configMapVars,
389+
time.Duration(10)*time.Second,
390+
)
385391
if err != nil {
386392
return ctrlResult, err
387393
}
@@ -491,16 +497,23 @@ func (r *AutoscalingReconciler) reconcileNormal(
491497
transportValidateFields := map[string]secret.Validator{
492498
"transport_url": secret.NoOpValidator{},
493499
}
494-
ctrlResult, err = r.getSecret(
500+
501+
_, ctrlResult, err = ensureSecret(
495502
ctx,
496-
helper,
497-
instance,
498-
*instance.Status.NotificationsURLSecret,
503+
types.NamespacedName{
504+
Namespace: instance.Namespace,
505+
Name: *instance.Status.NotificationsURLSecret,
506+
},
499507
transportValidateFields,
500-
&configMapVars)
508+
helper.GetClient(),
509+
&instance.Status.Conditions,
510+
&configMapVars,
511+
time.Duration(10)*time.Second,
512+
)
501513
if err != nil {
502514
return ctrlResult, err
503515
}
516+
504517
// run check TransportURL secret - end
505518

506519
//
@@ -898,34 +911,6 @@ func (r *AutoscalingReconciler) getAutoscalingHeat(
898911
return heat, err
899912
}
900913

901-
// getSecret - get the specified secret, and add its hash to envVars
902-
func (r *AutoscalingReconciler) getSecret(
903-
ctx context.Context,
904-
h *helper.Helper,
905-
instance *telemetryv1.Autoscaling,
906-
secretName string,
907-
expectedFields map[string]secret.Validator,
908-
envVars *map[string]env.Setter,
909-
) (ctrl.Result, error) {
910-
secretHash, result, err := ensureSecret(
911-
ctx,
912-
types.NamespacedName{Namespace: instance.Namespace, Name: secretName},
913-
expectedFields,
914-
h.GetClient(),
915-
&instance.Status.Conditions,
916-
time.Duration(10)*time.Second,
917-
)
918-
if err != nil {
919-
return result, err
920-
}
921-
922-
// Add a prefix to the var name to avoid accidental collision with other non-secret
923-
// vars. The secret names themselves will be unique.
924-
(*envVars)["secret-"+secretName] = env.SetValue(secretHash)
925-
926-
return ctrl.Result{}, nil
927-
}
928-
929914
func (r *AutoscalingReconciler) transportURLCreateOrUpdate(
930915
ctx context.Context,
931916
instance *telemetryv1.Autoscaling,

internal/controller/ceilometer_controller.go

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -576,16 +576,23 @@ func (r *CeilometerReconciler) reconcileCeilometer(
576576
validateFields := map[string]secret.Validator{
577577
instance.Spec.PasswordSelectors.CeilometerService: secret.PasswordValidator{},
578578
}
579-
ctrlResult, err := r.getSecret(
579+
580+
_, ctrlResult, err := ensureSecret(
580581
ctx,
581-
helper,
582-
instance,
583-
instance.Spec.Secret,
582+
types.NamespacedName{
583+
Namespace: instance.Namespace,
584+
Name: instance.Spec.Secret,
585+
},
584586
validateFields,
585-
&configMapVars)
587+
helper.GetClient(),
588+
&instance.Status.Conditions,
589+
&configMapVars,
590+
time.Duration(10)*time.Second,
591+
)
586592
if err != nil {
587593
return ctrlResult, err
588594
}
595+
589596
// run check OpenStack secret - end
590597

591598
//
@@ -1218,34 +1225,6 @@ func (r *CeilometerReconciler) reconcileKSM(
12181225
return ctrl.Result{}, nil
12191226
}
12201227

1221-
// getSecret - get the specified secret, and add its hash to envVars
1222-
func (r *CeilometerReconciler) getSecret(
1223-
ctx context.Context,
1224-
h *helper.Helper,
1225-
instance *telemetryv1.Ceilometer,
1226-
secretName string,
1227-
expectedFields map[string]secret.Validator,
1228-
envVars *map[string]env.Setter,
1229-
) (ctrl.Result, error) {
1230-
secretHash, result, err := ensureSecret(
1231-
ctx,
1232-
types.NamespacedName{Namespace: instance.Namespace, Name: secretName},
1233-
expectedFields,
1234-
h.GetClient(),
1235-
&instance.Status.Conditions,
1236-
time.Duration(10)*time.Second,
1237-
)
1238-
if err != nil {
1239-
return result, err
1240-
}
1241-
1242-
// Add a prefix to the var name to avoid accidental collision with other non-secret
1243-
// vars. The secret names themselves will be unique.
1244-
(*envVars)["secret-"+secretName] = env.SetValue(secretHash)
1245-
1246-
return ctrl.Result{}, nil
1247-
}
1248-
12491228
func (r *CeilometerReconciler) generateServiceConfig(
12501229
ctx context.Context,
12511230
h *helper.Helper,

internal/controller/telemetry_common.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
topologyv1 "github.com/openstack-k8s-operators/infra-operator/apis/topology/v1beta1"
2626
condition "github.com/openstack-k8s-operators/lib-common/modules/common/condition"
27+
env "github.com/openstack-k8s-operators/lib-common/modules/common/env"
2728
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
2829
secret "github.com/openstack-k8s-operators/lib-common/modules/common/secret"
2930
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -112,6 +113,7 @@ func ensureSecret(
112113
expectedFields map[string]secret.Validator,
113114
reader client.Reader,
114115
conditionUpdater conditionUpdater,
116+
envVars *map[string]env.Setter,
115117
requeueTimeout time.Duration,
116118
) (string, ctrl.Result, error) {
117119

@@ -137,5 +139,8 @@ func ensureSecret(
137139
return "", res, nil
138140
}
139141

142+
// Add a prefix to the var name to avoid accidental collision with other non-secret
143+
// vars. The secret names themselves will be unique.
144+
(*envVars)["secret-"+secretName.Name] = env.SetValue(hash)
140145
return hash, ctrl.Result{}, nil
141146
}

0 commit comments

Comments
 (0)