Skip to content

Commit 2bb008f

Browse files
committed
[OSPRH-11235] Do not fail when clouds CM exists
The Ensure[Horizon|Tobiko]CloudsYAML function was failing when the CM containing the modified clouds.yaml already existed: Object openstack/horizontest-clouds-config is already owned by another HorizonTest controller horizontest-tests This patch does two things: - Ensures that we do not create the modified clouds.yaml when it already exists. - Removes the duplicate implementation of of the Ensure*CloudsYAML function and replaces it with EnsureCloudsConfigMapExists.
1 parent 06ff0ea commit 2bb008f

File tree

6 files changed

+82
-103
lines changed

6 files changed

+82
-103
lines changed

controllers/common.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/openstack-k8s-operators/lib-common/modules/common/pvc"
1717
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
1818
v1beta1 "github.com/openstack-k8s-operators/test-operator/api/v1beta1"
19+
"gopkg.in/yaml.v3"
1920
batchv1 "k8s.io/api/batch/v1"
2021
corev1 "k8s.io/api/core/v1"
2122
rbacv1 "k8s.io/api/rbac/v1"
@@ -550,3 +551,64 @@ func GetCommonRbacRules(privileged bool) []rbacv1.PolicyRule {
550551

551552
return []rbacv1.PolicyRule{rbacPolicyRule}
552553
}
554+
555+
// Some frameworks like (e.g., Tobiko and Horizon) require password value to be
556+
// present in clouds.yaml. This code ensures that we set a default value of
557+
// 12345678 when password value is missing in the clouds.yaml
558+
func EnsureCloudsConfigMapExists(
559+
ctx context.Context,
560+
instance client.Object,
561+
helper *helper.Helper,
562+
labels map[string]string,
563+
) (ctrl.Result, error) {
564+
const openstackConfigMapName = "openstack-config"
565+
const testOperatorCloudsConfigMapName = "test-operator-clouds-config"
566+
567+
cm, _, _ := configmap.GetConfigMap(
568+
ctx,
569+
helper,
570+
instance,
571+
openstackConfigMapName,
572+
time.Second*10,
573+
)
574+
if cm.Name == testOperatorCloudsConfigMapName {
575+
return ctrl.Result{}, nil
576+
}
577+
578+
result := make(map[string]interface{})
579+
580+
err := yaml.Unmarshal([]byte(cm.Data["clouds.yaml"]), &result)
581+
if err != nil {
582+
return ctrl.Result{}, err
583+
}
584+
585+
clouds := result["clouds"].(map[string]interface{})
586+
defaultValue := clouds["default"].(map[string]interface{})
587+
auth := defaultValue["auth"].(map[string]interface{})
588+
589+
if _, ok := auth["password"].(string); !ok {
590+
auth["password"] = "12345678"
591+
}
592+
593+
yamlString, err := yaml.Marshal(result)
594+
if err != nil {
595+
return ctrl.Result{}, err
596+
}
597+
598+
cms := []util.Template{
599+
{
600+
Name: testOperatorCloudsConfigMapName,
601+
Namespace: instance.GetNamespace(),
602+
Labels: labels,
603+
CustomData: map[string]string{
604+
"clouds.yaml": string(yamlString),
605+
},
606+
},
607+
}
608+
err = configmap.EnsureConfigMaps(ctx, helper, instance, cms, nil)
609+
if err != nil {
610+
return ctrl.Result{}, err
611+
}
612+
613+
return ctrl.Result{}, nil
614+
}

controllers/horizontest_controller.go

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,16 @@ import (
2323
"github.com/go-logr/logr"
2424
"github.com/openstack-k8s-operators/lib-common/modules/common"
2525
"github.com/openstack-k8s-operators/lib-common/modules/common/condition"
26-
"github.com/openstack-k8s-operators/lib-common/modules/common/configmap"
2726
"github.com/openstack-k8s-operators/lib-common/modules/common/env"
2827
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
2928
"github.com/openstack-k8s-operators/lib-common/modules/common/job"
3029
common_rbac "github.com/openstack-k8s-operators/lib-common/modules/common/rbac"
31-
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
3230
testv1beta1 "github.com/openstack-k8s-operators/test-operator/api/v1beta1"
3331
"github.com/openstack-k8s-operators/test-operator/pkg/horizontest"
34-
"gopkg.in/yaml.v3"
3532
batchv1 "k8s.io/api/batch/v1"
3633
corev1 "k8s.io/api/core/v1"
3734
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
3835
ctrl "sigs.k8s.io/controller-runtime"
39-
"sigs.k8s.io/controller-runtime/pkg/client"
4036
"sigs.k8s.io/controller-runtime/pkg/log"
4137
)
4238

@@ -155,7 +151,7 @@ func (r *HorizonTestReconciler) Reconcile(ctx context.Context, req ctrl.Request)
155151
"workflowStep": "0",
156152
}
157153

158-
yamlResult, err := r.EnsureHorizonTestCloudsYAML(ctx, instance, helper, serviceLabels)
154+
yamlResult, err := EnsureCloudsConfigMapExists(ctx, instance, helper, serviceLabels)
159155

160156
if err != nil {
161157
return yamlResult, err
@@ -272,49 +268,6 @@ func (r *HorizonTestReconciler) SetupWithManager(mgr ctrl.Manager) error {
272268
Complete(r)
273269
}
274270

275-
// Horizon requires password value to be present in clouds.yaml
276-
// This code ensures that we set a default value of 12345678 when
277-
// password value is missing in the clouds.yaml
278-
func (r *HorizonTestReconciler) EnsureHorizonTestCloudsYAML(ctx context.Context, instance client.Object, helper *helper.Helper, labels map[string]string) (ctrl.Result, error) {
279-
cm, _, _ := configmap.GetConfigMap(ctx, helper, instance, "openstack-config", time.Second*10)
280-
result := make(map[string]interface{})
281-
282-
err := yaml.Unmarshal([]byte(cm.Data["clouds.yaml"]), &result)
283-
if err != nil {
284-
return ctrl.Result{}, err
285-
}
286-
287-
clouds := result["clouds"].(map[string]interface{})
288-
defaultValue := clouds["default"].(map[string]interface{})
289-
auth := defaultValue["auth"].(map[string]interface{})
290-
291-
if _, ok := auth["password"].(string); !ok {
292-
auth["password"] = "12345678"
293-
}
294-
295-
yamlString, err := yaml.Marshal(result)
296-
if err != nil {
297-
return ctrl.Result{}, err
298-
}
299-
300-
cms := []util.Template{
301-
{
302-
Name: "horizontest-clouds-config",
303-
Namespace: instance.GetNamespace(),
304-
Labels: labels,
305-
CustomData: map[string]string{
306-
"clouds.yaml": string(yamlString),
307-
},
308-
},
309-
}
310-
err = configmap.EnsureConfigMaps(ctx, helper, instance, cms, nil)
311-
if err != nil {
312-
return ctrl.Result{}, err
313-
}
314-
315-
return ctrl.Result{}, nil
316-
}
317-
318271
func (r *HorizonTestReconciler) PrepareHorizonTestEnvVars(
319272
instance *testv1beta1.HorizonTest,
320273
) map[string]env.Setter {

controllers/tobiko_controller.go

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
3535
testv1beta1 "github.com/openstack-k8s-operators/test-operator/api/v1beta1"
3636
"github.com/openstack-k8s-operators/test-operator/pkg/tobiko"
37-
"gopkg.in/yaml.v3"
3837
batchv1 "k8s.io/api/batch/v1"
3938
corev1 "k8s.io/api/core/v1"
4039
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
@@ -182,7 +181,7 @@ func (r *TobikoReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
182181
"operator": "test-operator",
183182
}
184183

185-
yamlResult, err := r.EnsureTobikoCloudsYAML(ctx, instance, helper, serviceLabels)
184+
yamlResult, err := EnsureCloudsConfigMapExists(ctx, instance, helper, serviceLabels)
186185

187186
if err != nil {
188187
return yamlResult, err
@@ -359,49 +358,6 @@ func (r *TobikoReconciler) SetupWithManager(mgr ctrl.Manager) error {
359358
Complete(r)
360359
}
361360

362-
// Tobiko requires password value to be present in clouds.yaml
363-
// This code ensures that we set a default value of 12345678 when
364-
// password value is missing in the clouds.yaml
365-
func (r *TobikoReconciler) EnsureTobikoCloudsYAML(ctx context.Context, instance client.Object, helper *helper.Helper, labels map[string]string) (ctrl.Result, error) {
366-
cm, _, _ := configmap.GetConfigMap(ctx, helper, instance, "openstack-config", time.Second*10)
367-
result := make(map[string]interface{})
368-
369-
err := yaml.Unmarshal([]byte(cm.Data["clouds.yaml"]), &result)
370-
if err != nil {
371-
return ctrl.Result{}, err
372-
}
373-
374-
clouds := result["clouds"].(map[string]interface{})
375-
defaultValue := clouds["default"].(map[string]interface{})
376-
auth := defaultValue["auth"].(map[string]interface{})
377-
378-
if _, ok := auth["password"].(string); !ok {
379-
auth["password"] = "12345678"
380-
}
381-
382-
yamlString, err := yaml.Marshal(result)
383-
if err != nil {
384-
return ctrl.Result{}, err
385-
}
386-
387-
cms := []util.Template{
388-
{
389-
Name: "tobiko-clouds-config",
390-
Namespace: instance.GetNamespace(),
391-
Labels: labels,
392-
CustomData: map[string]string{
393-
"clouds.yaml": string(yamlString),
394-
},
395-
},
396-
}
397-
err = configmap.EnsureConfigMaps(ctx, helper, instance, cms, nil)
398-
if err != nil {
399-
return ctrl.Result{}, err
400-
}
401-
402-
return ctrl.Result{}, nil
403-
}
404-
405361
// This function prepares env variables for a single workflow step.
406362
func (r *TobikoReconciler) PrepareTobikoEnvVars(
407363
ctx context.Context,

pkg/horizontest/volumes.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package horizontest
22

33
import (
44
testv1beta1 "github.com/openstack-k8s-operators/test-operator/api/v1beta1"
5+
util "github.com/openstack-k8s-operators/test-operator/pkg/util"
6+
57
corev1 "k8s.io/api/core/v1"
68
)
79

@@ -15,8 +17,6 @@ func GetVolumes(
1517

1618
var scriptsVolumeDefaultMode int32 = 0755
1719
var scriptsVolumeConfidentialMode int32 = 0420
18-
//var privateKeyMode int32 = 0600
19-
//var publicKeyMode int32 = 0644
2020
var tlsCertificateMode int32 = 0444
2121
var publicInfoMode int32 = 0744
2222

@@ -33,12 +33,12 @@ func GetVolumes(
3333
},
3434
},
3535
{
36-
Name: "horizontest-clouds-config",
36+
Name: util.TestOperatorCloudsConfigMapName,
3737
VolumeSource: corev1.VolumeSource{
3838
ConfigMap: &corev1.ConfigMapVolumeSource{
3939
DefaultMode: &scriptsVolumeConfidentialMode,
4040
LocalObjectReference: corev1.LocalObjectReference{
41-
Name: "horizontest-clouds-config",
41+
Name: util.TestOperatorCloudsConfigMapName,
4242
},
4343
},
4444
},
@@ -123,13 +123,13 @@ func GetVolumeMounts(mountCerts bool, mountKeys bool, mountKubeconfig bool, inst
123123
ReadOnly: false,
124124
},
125125
{
126-
Name: "horizontest-clouds-config",
126+
Name: util.TestOperatorCloudsConfigMapName,
127127
MountPath: "/var/lib/horizontest/.config/openstack/clouds.yaml",
128128
SubPath: "clouds.yaml",
129129
ReadOnly: true,
130130
},
131131
{
132-
Name: "horizontest-clouds-config",
132+
Name: util.TestOperatorCloudsConfigMapName,
133133
MountPath: "/etc/openstack/clouds.yaml",
134134
SubPath: "clouds.yaml",
135135
ReadOnly: true,

pkg/tobiko/volumes.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tobiko
22

33
import (
44
testv1beta1 "github.com/openstack-k8s-operators/test-operator/api/v1beta1"
5+
"github.com/openstack-k8s-operators/test-operator/pkg/util"
56
corev1 "k8s.io/api/core/v1"
67
)
78

@@ -34,12 +35,12 @@ func GetVolumes(
3435
},
3536
},
3637
{
37-
Name: "tobiko-clouds-config",
38+
Name: util.TestOperatorCloudsConfigMapName,
3839
VolumeSource: corev1.VolumeSource{
3940
ConfigMap: &corev1.ConfigMapVolumeSource{
4041
DefaultMode: &scriptsVolumeConfidentialMode,
4142
LocalObjectReference: corev1.LocalObjectReference{
42-
Name: "tobiko-clouds-config",
43+
Name: util.TestOperatorCloudsConfigMapName,
4344
},
4445
},
4546
},
@@ -155,13 +156,13 @@ func GetVolumeMounts(mountCerts bool, mountKeys bool, mountKubeconfig bool, inst
155156
ReadOnly: false,
156157
},
157158
{
158-
Name: "tobiko-clouds-config",
159+
Name: util.TestOperatorCloudsConfigMapName,
159160
MountPath: "/var/lib/tobiko/.config/openstack/clouds.yaml",
160161
SubPath: "clouds.yaml",
161162
ReadOnly: true,
162163
},
163164
{
164-
Name: "tobiko-clouds-config",
165+
Name: util.TestOperatorCloudsConfigMapName,
165166
MountPath: "/etc/openstack/clouds.yaml",
166167
SubPath: "clouds.yaml",
167168
ReadOnly: true,

pkg/util/common.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ import (
44
corev1 "k8s.io/api/core/v1"
55
)
66

7+
const (
8+
// TestOperatorCloudsConfigMapName is name of the ConfigMap which contains
9+
// modified clouds.yaml obtained from openstack-config ConfigMap. The modified
10+
// CM is needed by some test frameworks (e.g., HorizonTest and Tobiko)
11+
TestOperatorCloudsConfigMapName = "test-operator-clouds-config"
12+
)
13+
714
func GetSecurityContext(
815
runAsUser int64,
916
addCapabilities []corev1.Capability,

0 commit comments

Comments
 (0)