Skip to content

Commit

Permalink
Move merging of workflow section into webhook
Browse files Browse the repository at this point in the history
Currently, the merging of workflow section is done in controllers
of related CRs. We want to move the merging logic into webhooks
to make the process cleaner.

Depends on: #160
  • Loading branch information
kstrenkova authored and lpiwowar committed Sep 30, 2024
1 parent 7120755 commit af17dde
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 16 deletions.
32 changes: 32 additions & 0 deletions api/v1beta1/common_webhook.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package v1beta1

import (
"fmt"
"reflect"
)

const (
// ErrPrivilegedModeRequired
ErrPrivilegedModeRequired = "%s.Spec.Privileged is requied in order to successfully " +
Expand All @@ -19,3 +24,30 @@ const (
"required for the tests to run successfully. Before enabling" +
"this parameter, consult documentation of the %[1]s CR."
)

// merge non-workflow section into workflow
func mergeSectionIntoWorkflow(instance interface{}, workflowStepNum int) {
spec, ok := instance.(*TempestSpec)
if !ok {
fmt.Println("Temporary error, instance is not of type *TempestSpec")
return
}

tRun := spec.TempestRun
wtRun := &spec.Workflow[workflowStepNum].TempestRun

tRunReflect := reflect.ValueOf(tRun)
wtRunReflect := reflect.ValueOf(wtRun).Elem()

for i := 0; i < tRunReflect.NumField(); i++ {
tRunName := tRunReflect.Type().Field(i).Name
tRunValue := tRunReflect.Field(i)

wRunValue := wtRunReflect.FieldByName(tRunName)
if wRunValue.IsZero() {
tRunPtr := reflect.New(tRunValue.Type())
tRunPtr.Elem().Set(tRunValue)
wRunValue.Set(tRunPtr)
}
}
}
6 changes: 5 additions & 1 deletion api/v1beta1/tempest_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,14 @@ func (spec *TempestSpec) Default() {
if spec.ContainerImage == "" {
spec.ContainerImage = tempestDefaults.ContainerImageURL
}

if spec.TempestconfRun == (TempestconfRunSpec{}) {
spec.TempestconfRun.Create = true
}
if len(spec.Workflow) > 0 {
for key, _ := range spec.Workflow {
mergeSectionIntoWorkflow(spec, key)
}
}
}

func (r *Tempest) PrivilegedRequired() bool {
Expand Down
37 changes: 37 additions & 0 deletions controllers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,3 +532,40 @@ func (r *Reconciler) OverwriteValueWithWorkflow(

return nil
}

// temporary tempest solution
func getTempestRun(instance *v1beta1.Tempest, workflowStepNum int) v1beta1.TempestRunSpec {
if workflowStepNum < len(instance.Spec.Workflow) {
// wtRun := instance.Spec.Workflow[workflowStepNum].TempestRun
tRun := changeType(instance, workflowStepNum)
return tRun
}
return instance.Spec.TempestRun
}

// TODO from v1beta1.TempestRunSpec to interface{}
func changeType(instance interface{}, workflowStepNum int) v1beta1.TempestRunSpec {
// TODO other types; else if typedInstance, ok := instance.(*v1beta1.HorizonTest);

// temporary Tempest solution
typedInstance, _ := instance.(*v1beta1.Tempest)
wtRun := typedInstance.Spec.Workflow[workflowStepNum].TempestRun

var tRun v1beta1.TempestRunSpec

wtReflected := reflect.ValueOf(wtRun)
tReflected := reflect.ValueOf(&tRun).Elem()

for i := 0; i < wtReflected.NumField(); i++ {
tName := tReflected.Type().Field(i).Name
tValue := tReflected.Field(i)

wtValue := wtReflected.FieldByName(tName)
if !wtValue.IsNil() {
wtValue = wtValue.Elem()
tValue.Set(wtValue)
}
}
// TODO typedInstance.Spec.TempestRun = tRun
return tRun
}
26 changes: 11 additions & 15 deletions controllers/tempest_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,30 +432,26 @@ func (r *TempestReconciler) setTempestConfigVars(envVars map[string]string,
instance *testv1beta1.Tempest,
workflowStepNum int,
) {
tRun := instance.Spec.TempestRun
wtRun := testv1beta1.WorkflowTempestRunSpec{}
if workflowStepNum < len(instance.Spec.Workflow) {
wtRun = instance.Spec.Workflow[workflowStepNum].TempestRun
}
tRun := getTempestRun(instance, workflowStepNum)

testOperatorDir := "/etc/test_operator/"

// Files
value := mergeWithWorkflow(tRun.WorkerFile, wtRun.WorkerFile)
value := tRun.WorkerFile
if len(value) != 0 {
workerFile := "worker_file.yaml"
customData[workerFile] = value
envVars["TEMPEST_WORKER_FILE"] = testOperatorDir + workerFile
}

value = mergeWithWorkflow(tRun.IncludeList, wtRun.IncludeList)
value = tRun.IncludeList
if len(value) != 0 {
includeListFile := "include.txt"
customData[includeListFile] = value
envVars["TEMPEST_INCLUDE_LIST"] = testOperatorDir + includeListFile
}

value = mergeWithWorkflow(tRun.ExcludeList, wtRun.ExcludeList)
value = tRun.ExcludeList
if len(value) != 0 {
excludeListFile := "exclude.txt"
customData[excludeListFile] = value
Expand All @@ -464,9 +460,9 @@ func (r *TempestReconciler) setTempestConfigVars(envVars map[string]string,

// Bool
tempestBoolEnvVars := map[string]bool{
"TEMPEST_SERIAL": mergeWithWorkflow(tRun.Serial, wtRun.Serial),
"TEMPEST_PARALLEL": mergeWithWorkflow(tRun.Parallel, wtRun.Parallel),
"TEMPEST_SMOKE": mergeWithWorkflow(tRun.Smoke, wtRun.Smoke),
"TEMPEST_SERIAL": tRun.Serial,
"TEMPEST_PARALLEL": tRun.Parallel,
"TEMPEST_SMOKE": tRun.Smoke,
"USE_EXTERNAL_FILES": true,
}

Expand All @@ -475,11 +471,11 @@ func (r *TempestReconciler) setTempestConfigVars(envVars map[string]string,
}

// Int
numValue := mergeWithWorkflow(tRun.Concurrency, wtRun.Concurrency)
numValue := tRun.Concurrency
envVars["TEMPEST_CONCURRENCY"] = r.GetDefaultInt(numValue)

// Dictionary
dictValue := mergeWithWorkflow(tRun.ExternalPlugin, wtRun.ExternalPlugin)
dictValue := tRun.ExternalPlugin
for _, externalPluginDictionary := range dictValue {
envVars["TEMPEST_EXTERNAL_PLUGIN_GIT_URL"] += externalPluginDictionary.Repository + ","

Expand All @@ -495,7 +491,7 @@ func (r *TempestReconciler) setTempestConfigVars(envVars map[string]string,

envVars["TEMPEST_WORKFLOW_STEP_DIR_NAME"] = r.GetJobName(instance, workflowStepNum)

extraImages := mergeWithWorkflow(tRun.ExtraImages, wtRun.ExtraImages)
extraImages := tRun.ExtraImages
for _, extraImageDict := range extraImages {
envVars["TEMPEST_EXTRA_IMAGES_URL"] += extraImageDict.URL + ","
envVars["TEMPEST_EXTRA_IMAGES_OS_CLOUD"] += extraImageDict.OsCloud + ","
Expand All @@ -513,7 +509,7 @@ func (r *TempestReconciler) setTempestConfigVars(envVars map[string]string,
envVars["TEMPEST_EXTRA_IMAGES_FLAVOR_VCPUS"] += r.GetDefaultInt(extraImageDict.Flavor.Vcpus, "-") + ","
}

extraRPMs := mergeWithWorkflow(tRun.ExtraRPMs, wtRun.ExtraRPMs)
extraRPMs := tRun.ExtraRPMs
for _, extraRPMURL := range extraRPMs {
envVars["TEMPEST_EXTRA_RPMS"] += extraRPMURL + ","
}
Expand Down

0 comments on commit af17dde

Please sign in to comment.