Skip to content

Commit

Permalink
Merge pull request #248 from lpiwowar/18.0-fr1
Browse files Browse the repository at this point in the history
[18.0-fr1][OSPRH-11235] Do not fail when clouds CM exists
  • Loading branch information
openshift-merge-bot[bot] authored Nov 19, 2024
2 parents 4d00f64 + 0c24f37 commit 41bb402
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 103 deletions.
70 changes: 70 additions & 0 deletions controllers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/openstack-k8s-operators/lib-common/modules/common/pvc"
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
v1beta1 "github.com/openstack-k8s-operators/test-operator/api/v1beta1"
"gopkg.in/yaml.v3"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -532,3 +533,72 @@ func (r *Reconciler) OverwriteValueWithWorkflow(

return nil
}

// Some frameworks like (e.g., Tobiko and Horizon) require password value to be
// present in clouds.yaml. This code ensures that we set a default value of
// 12345678 when password value is missing in the clouds.yaml
func EnsureCloudsConfigMapExists(
ctx context.Context,
instance client.Object,
helper *helper.Helper,
labels map[string]string,
) (ctrl.Result, error) {
const openstackConfigMapName = "openstack-config"
const testOperatorCloudsConfigMapName = "test-operator-clouds-config"

cm, _, _ := configmap.GetConfigMap(
ctx,
helper,
instance,
testOperatorCloudsConfigMapName,
time.Second*10,
)
if cm.Name == testOperatorCloudsConfigMapName {
return ctrl.Result{}, nil
}

cm, _, _ = configmap.GetConfigMap(
ctx,
helper,
instance,
openstackConfigMapName,
time.Second*10,
)

result := make(map[string]interface{})

err := yaml.Unmarshal([]byte(cm.Data["clouds.yaml"]), &result)
if err != nil {
return ctrl.Result{}, err
}

clouds := result["clouds"].(map[string]interface{})
defaultValue := clouds["default"].(map[string]interface{})
auth := defaultValue["auth"].(map[string]interface{})

if _, ok := auth["password"].(string); !ok {
auth["password"] = "12345678"
}

yamlString, err := yaml.Marshal(result)
if err != nil {
return ctrl.Result{}, err
}

cms := []util.Template{
{
Name: testOperatorCloudsConfigMapName,
Namespace: instance.GetNamespace(),
Labels: labels,
CustomData: map[string]string{
"clouds.yaml": string(yamlString),
},
},
}
err = configmap.EnsureConfigMaps(ctx, helper, instance, cms, nil)
if err != nil {
return ctrl.Result{}, err
}

return ctrl.Result{}, nil
}
49 changes: 1 addition & 48 deletions controllers/horizontest_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,17 @@ import (
"github.com/go-logr/logr"
"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/configmap"
"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/job"
common_rbac "github.com/openstack-k8s-operators/lib-common/modules/common/rbac"
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
testv1beta1 "github.com/openstack-k8s-operators/test-operator/api/v1beta1"
"github.com/openstack-k8s-operators/test-operator/pkg/horizontest"
"gopkg.in/yaml.v3"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
)

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

yamlResult, err := r.EnsureHorizonTestCloudsYAML(ctx, instance, helper, serviceLabels)
yamlResult, err := EnsureCloudsConfigMapExists(ctx, instance, helper, serviceLabels)

if err != nil {
return yamlResult, err
Expand Down Expand Up @@ -276,49 +272,6 @@ func (r *HorizonTestReconciler) SetupWithManager(mgr ctrl.Manager) error {
Complete(r)
}

// Horizon requires password value to be present in clouds.yaml
// This code ensures that we set a default value of 12345678 when
// password value is missing in the clouds.yaml
func (r *HorizonTestReconciler) EnsureHorizonTestCloudsYAML(ctx context.Context, instance client.Object, helper *helper.Helper, labels map[string]string) (ctrl.Result, error) {
cm, _, _ := configmap.GetConfigMap(ctx, helper, instance, "openstack-config", time.Second*10)
result := make(map[string]interface{})

err := yaml.Unmarshal([]byte(cm.Data["clouds.yaml"]), &result)
if err != nil {
return ctrl.Result{}, err
}

clouds := result["clouds"].(map[string]interface{})
defaultValue := clouds["default"].(map[string]interface{})
auth := defaultValue["auth"].(map[string]interface{})

if _, ok := auth["password"].(string); !ok {
auth["password"] = "12345678"
}

yamlString, err := yaml.Marshal(result)
if err != nil {
return ctrl.Result{}, err
}

cms := []util.Template{
{
Name: "horizontest-clouds-config",
Namespace: instance.GetNamespace(),
Labels: labels,
CustomData: map[string]string{
"clouds.yaml": string(yamlString),
},
},
}
err = configmap.EnsureConfigMaps(ctx, helper, instance, cms, nil)
if err != nil {
return ctrl.Result{}, err
}

return ctrl.Result{}, nil
}

func (r *HorizonTestReconciler) PrepareHorizonTestEnvVars(
instance *testv1beta1.HorizonTest,
) map[string]env.Setter {
Expand Down
46 changes: 1 addition & 45 deletions controllers/tobiko_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
testv1beta1 "github.com/openstack-k8s-operators/test-operator/api/v1beta1"
"github.com/openstack-k8s-operators/test-operator/pkg/tobiko"
"gopkg.in/yaml.v3"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
Expand Down Expand Up @@ -199,7 +198,7 @@ func (r *TobikoReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
"operator": "test-operator",
}

yamlResult, err := r.EnsureTobikoCloudsYAML(ctx, instance, helper, serviceLabels)
yamlResult, err := EnsureCloudsConfigMapExists(ctx, instance, helper, serviceLabels)

if err != nil {
return yamlResult, err
Expand Down Expand Up @@ -366,49 +365,6 @@ func (r *TobikoReconciler) SetupWithManager(mgr ctrl.Manager) error {
Complete(r)
}

// Tobiko requires password value to be present in clouds.yaml
// This code ensures that we set a default value of 12345678 when
// password value is missing in the clouds.yaml
func (r *TobikoReconciler) EnsureTobikoCloudsYAML(ctx context.Context, instance client.Object, helper *helper.Helper, labels map[string]string) (ctrl.Result, error) {
cm, _, _ := configmap.GetConfigMap(ctx, helper, instance, "openstack-config", time.Second*10)
result := make(map[string]interface{})

err := yaml.Unmarshal([]byte(cm.Data["clouds.yaml"]), &result)
if err != nil {
return ctrl.Result{}, err
}

clouds := result["clouds"].(map[string]interface{})
defaultValue := clouds["default"].(map[string]interface{})
auth := defaultValue["auth"].(map[string]interface{})

if _, ok := auth["password"].(string); !ok {
auth["password"] = "12345678"
}

yamlString, err := yaml.Marshal(result)
if err != nil {
return ctrl.Result{}, err
}

cms := []util.Template{
{
Name: "tobiko-clouds-config",
Namespace: instance.GetNamespace(),
Labels: labels,
CustomData: map[string]string{
"clouds.yaml": string(yamlString),
},
},
}
err = configmap.EnsureConfigMaps(ctx, helper, instance, cms, nil)
if err != nil {
return ctrl.Result{}, err
}

return ctrl.Result{}, nil
}

// This function prepares env variables for a single workflow step.
func (r *TobikoReconciler) PrepareTobikoEnvVars(
ctx context.Context,
Expand Down
12 changes: 6 additions & 6 deletions pkg/horizontest/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package horizontest

import (
testv1beta1 "github.com/openstack-k8s-operators/test-operator/api/v1beta1"
util "github.com/openstack-k8s-operators/test-operator/pkg/util"

corev1 "k8s.io/api/core/v1"
)

Expand All @@ -15,8 +17,6 @@ func GetVolumes(

var scriptsVolumeDefaultMode int32 = 0755
var scriptsVolumeConfidentialMode int32 = 0420
//var privateKeyMode int32 = 0600
//var publicKeyMode int32 = 0644
var tlsCertificateMode int32 = 0444
var publicInfoMode int32 = 0744

Expand All @@ -33,12 +33,12 @@ func GetVolumes(
},
},
{
Name: "horizontest-clouds-config",
Name: util.TestOperatorCloudsConfigMapName,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
DefaultMode: &scriptsVolumeConfidentialMode,
LocalObjectReference: corev1.LocalObjectReference{
Name: "horizontest-clouds-config",
Name: util.TestOperatorCloudsConfigMapName,
},
},
},
Expand Down Expand Up @@ -123,13 +123,13 @@ func GetVolumeMounts(mountCerts bool, mountKeys bool, mountKubeconfig bool, inst
ReadOnly: false,
},
{
Name: "horizontest-clouds-config",
Name: util.TestOperatorCloudsConfigMapName,
MountPath: "/var/lib/horizontest/.config/openstack/clouds.yaml",
SubPath: "clouds.yaml",
ReadOnly: true,
},
{
Name: "horizontest-clouds-config",
Name: util.TestOperatorCloudsConfigMapName,
MountPath: "/etc/openstack/clouds.yaml",
SubPath: "clouds.yaml",
ReadOnly: true,
Expand Down
9 changes: 5 additions & 4 deletions pkg/tobiko/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tobiko

import (
testv1beta1 "github.com/openstack-k8s-operators/test-operator/api/v1beta1"
"github.com/openstack-k8s-operators/test-operator/pkg/util"
corev1 "k8s.io/api/core/v1"
)

Expand Down Expand Up @@ -34,12 +35,12 @@ func GetVolumes(
},
},
{
Name: "tobiko-clouds-config",
Name: util.TestOperatorCloudsConfigMapName,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
DefaultMode: &scriptsVolumeConfidentialMode,
LocalObjectReference: corev1.LocalObjectReference{
Name: "tobiko-clouds-config",
Name: util.TestOperatorCloudsConfigMapName,
},
},
},
Expand Down Expand Up @@ -155,13 +156,13 @@ func GetVolumeMounts(mountCerts bool, mountKeys bool, mountKubeconfig bool, inst
ReadOnly: false,
},
{
Name: "tobiko-clouds-config",
Name: util.TestOperatorCloudsConfigMapName,
MountPath: "/var/lib/tobiko/.config/openstack/clouds.yaml",
SubPath: "clouds.yaml",
ReadOnly: true,
},
{
Name: "tobiko-clouds-config",
Name: util.TestOperatorCloudsConfigMapName,
MountPath: "/etc/openstack/clouds.yaml",
SubPath: "clouds.yaml",
ReadOnly: true,
Expand Down
7 changes: 7 additions & 0 deletions pkg/util/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import (
corev1 "k8s.io/api/core/v1"
)

const (
// TestOperatorCloudsConfigMapName is name of the ConfigMap which contains
// modified clouds.yaml obtained from openstack-config ConfigMap. The modified
// CM is needed by some test frameworks (e.g., HorizonTest and Tobiko)
TestOperatorCloudsConfigMapName = "test-operator-clouds-config"
)

func GetSecurityContext(
runAsUser int64,
addCapabilities []corev1.Capability,
Expand Down

0 comments on commit 41bb402

Please sign in to comment.