Skip to content

Commit 17b87f3

Browse files
Merge pull request #240 from lpiwowar/fix/clouds-cm-creation
[OSPRH-11235] Do not fail when clouds CM exists
2 parents 1f917b1 + fa3c1dc commit 17b87f3

File tree

6 files changed

+90
-103
lines changed

6 files changed

+90
-103
lines changed

controllers/common.go

Lines changed: 70 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,72 @@ 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+
testOperatorCloudsConfigMapName,
572+
time.Second*10,
573+
)
574+
if cm.Name == testOperatorCloudsConfigMapName {
575+
return ctrl.Result{}, nil
576+
}
577+
578+
cm, _, _ = configmap.GetConfigMap(
579+
ctx,
580+
helper,
581+
instance,
582+
openstackConfigMapName,
583+
time.Second*10,
584+
)
585+
586+
result := make(map[string]interface{})
587+
588+
err := yaml.Unmarshal([]byte(cm.Data["clouds.yaml"]), &result)
589+
if err != nil {
590+
return ctrl.Result{}, err
591+
}
592+
593+
clouds := result["clouds"].(map[string]interface{})
594+
defaultValue := clouds["default"].(map[string]interface{})
595+
auth := defaultValue["auth"].(map[string]interface{})
596+
597+
if _, ok := auth["password"].(string); !ok {
598+
auth["password"] = "12345678"
599+
}
600+
601+
yamlString, err := yaml.Marshal(result)
602+
if err != nil {
603+
return ctrl.Result{}, err
604+
}
605+
606+
cms := []util.Template{
607+
{
608+
Name: testOperatorCloudsConfigMapName,
609+
Namespace: instance.GetNamespace(),
610+
Labels: labels,
611+
CustomData: map[string]string{
612+
"clouds.yaml": string(yamlString),
613+
},
614+
},
615+
}
616+
err = configmap.EnsureConfigMaps(ctx, helper, instance, cms, nil)
617+
if err != nil {
618+
return ctrl.Result{}, err
619+
}
620+
621+
return ctrl.Result{}, nil
622+
}

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
@@ -35,7 +35,6 @@ import (
3535
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
3636
testv1beta1 "github.com/openstack-k8s-operators/test-operator/api/v1beta1"
3737
"github.com/openstack-k8s-operators/test-operator/pkg/tobiko"
38-
"gopkg.in/yaml.v3"
3938
batchv1 "k8s.io/api/batch/v1"
4039
corev1 "k8s.io/api/core/v1"
4140
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
@@ -183,7 +182,7 @@ func (r *TobikoReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
183182
"operator": "test-operator",
184183
}
185184

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

188187
if err != nil {
189188
return yamlResult, err
@@ -388,49 +387,6 @@ func (r *TobikoReconciler) SetupWithManager(mgr ctrl.Manager) error {
388387
Complete(r)
389388
}
390389

391-
// Tobiko requires password value to be present in clouds.yaml
392-
// This code ensures that we set a default value of 12345678 when
393-
// password value is missing in the clouds.yaml
394-
func (r *TobikoReconciler) EnsureTobikoCloudsYAML(ctx context.Context, instance client.Object, helper *helper.Helper, labels map[string]string) (ctrl.Result, error) {
395-
cm, _, _ := configmap.GetConfigMap(ctx, helper, instance, "openstack-config", time.Second*10)
396-
result := make(map[string]interface{})
397-
398-
err := yaml.Unmarshal([]byte(cm.Data["clouds.yaml"]), &result)
399-
if err != nil {
400-
return ctrl.Result{}, err
401-
}
402-
403-
clouds := result["clouds"].(map[string]interface{})
404-
defaultValue := clouds["default"].(map[string]interface{})
405-
auth := defaultValue["auth"].(map[string]interface{})
406-
407-
if _, ok := auth["password"].(string); !ok {
408-
auth["password"] = "12345678"
409-
}
410-
411-
yamlString, err := yaml.Marshal(result)
412-
if err != nil {
413-
return ctrl.Result{}, err
414-
}
415-
416-
cms := []util.Template{
417-
{
418-
Name: "tobiko-clouds-config",
419-
Namespace: instance.GetNamespace(),
420-
Labels: labels,
421-
CustomData: map[string]string{
422-
"clouds.yaml": string(yamlString),
423-
},
424-
},
425-
}
426-
err = configmap.EnsureConfigMaps(ctx, helper, instance, cms, nil)
427-
if err != nil {
428-
return ctrl.Result{}, err
429-
}
430-
431-
return ctrl.Result{}, nil
432-
}
433-
434390
// This function prepares env variables for a single workflow step.
435391
func (r *TobikoReconciler) PrepareTobikoEnvVars(
436392
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)