Skip to content

Commit 41bb402

Browse files
Merge pull request #248 from lpiwowar/18.0-fr1
[18.0-fr1][OSPRH-11235] Do not fail when clouds CM exists
2 parents 4d00f64 + 0c24f37 commit 41bb402

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
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
@@ -532,3 +533,72 @@ func (r *Reconciler) OverwriteValueWithWorkflow(
532533

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

controllers/horizontest_controller.go

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,17 @@ 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
rbacv1 "k8s.io/api/rbac/v1"
3835
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
3936
ctrl "sigs.k8s.io/controller-runtime"
40-
"sigs.k8s.io/controller-runtime/pkg/client"
4137
"sigs.k8s.io/controller-runtime/pkg/log"
4238
)
4339

@@ -169,7 +165,7 @@ func (r *HorizonTestReconciler) Reconcile(ctx context.Context, req ctrl.Request)
169165
"workflowStep": "0",
170166
}
171167

172-
yamlResult, err := r.EnsureHorizonTestCloudsYAML(ctx, instance, helper, serviceLabels)
168+
yamlResult, err := EnsureCloudsConfigMapExists(ctx, instance, helper, serviceLabels)
173169

174170
if err != nil {
175171
return yamlResult, err
@@ -276,49 +272,6 @@ func (r *HorizonTestReconciler) SetupWithManager(mgr ctrl.Manager) error {
276272
Complete(r)
277273
}
278274

279-
// Horizon requires password value to be present in clouds.yaml
280-
// This code ensures that we set a default value of 12345678 when
281-
// password value is missing in the clouds.yaml
282-
func (r *HorizonTestReconciler) EnsureHorizonTestCloudsYAML(ctx context.Context, instance client.Object, helper *helper.Helper, labels map[string]string) (ctrl.Result, error) {
283-
cm, _, _ := configmap.GetConfigMap(ctx, helper, instance, "openstack-config", time.Second*10)
284-
result := make(map[string]interface{})
285-
286-
err := yaml.Unmarshal([]byte(cm.Data["clouds.yaml"]), &result)
287-
if err != nil {
288-
return ctrl.Result{}, err
289-
}
290-
291-
clouds := result["clouds"].(map[string]interface{})
292-
defaultValue := clouds["default"].(map[string]interface{})
293-
auth := defaultValue["auth"].(map[string]interface{})
294-
295-
if _, ok := auth["password"].(string); !ok {
296-
auth["password"] = "12345678"
297-
}
298-
299-
yamlString, err := yaml.Marshal(result)
300-
if err != nil {
301-
return ctrl.Result{}, err
302-
}
303-
304-
cms := []util.Template{
305-
{
306-
Name: "horizontest-clouds-config",
307-
Namespace: instance.GetNamespace(),
308-
Labels: labels,
309-
CustomData: map[string]string{
310-
"clouds.yaml": string(yamlString),
311-
},
312-
},
313-
}
314-
err = configmap.EnsureConfigMaps(ctx, helper, instance, cms, nil)
315-
if err != nil {
316-
return ctrl.Result{}, err
317-
}
318-
319-
return ctrl.Result{}, nil
320-
}
321-
322275
func (r *HorizonTestReconciler) PrepareHorizonTestEnvVars(
323276
instance *testv1beta1.HorizonTest,
324277
) 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
rbacv1 "k8s.io/api/rbac/v1"
@@ -199,7 +198,7 @@ func (r *TobikoReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
199198
"operator": "test-operator",
200199
}
201200

202-
yamlResult, err := r.EnsureTobikoCloudsYAML(ctx, instance, helper, serviceLabels)
201+
yamlResult, err := EnsureCloudsConfigMapExists(ctx, instance, helper, serviceLabels)
203202

204203
if err != nil {
205204
return yamlResult, err
@@ -366,49 +365,6 @@ func (r *TobikoReconciler) SetupWithManager(mgr ctrl.Manager) error {
366365
Complete(r)
367366
}
368367

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