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.
  • Loading branch information
kstrenkova committed Oct 22, 2024
1 parent eae3554 commit 6b1c12c
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 15 deletions.
44 changes: 44 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 Down Expand Up @@ -28,3 +33,42 @@ const (
"ensures that the copying of the logs to the PV is completed without any " +
"complications."
)

// merge non-workflow section into workflow
func mergeSectionIntoWorkflow(instance interface{}, workflowStepNum int) {
spec, ok := instance.(*TempestSpec)
if !ok {
fmt.Println("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()

setNonZeroValues(tRunReflect, wtRunReflect, false)
}

func setNonZeroValues(src reflect.Value, dest reflect.Value, is_struct bool) {
for i := 0; i < src.NumField(); i++ {
tRunName := src.Type().Field(i).Name
tRunValue := src.Field(i)
wtRunValue := dest.FieldByName(tRunName)

if wtRunValue.IsZero() && !tRunValue.IsZero() {
if tRunValue.Kind() == reflect.Struct {
setNonZeroValues(tRunValue, wtRunValue, true)
} else {
if is_struct {
wtRunValue.Set(tRunValue)
} else {
tRunPtr := reflect.New(tRunValue.Type())
tRunPtr.Elem().Set(tRunValue)
wtRunValue.Set(tRunPtr)
}
}
}
}
}
6 changes: 6 additions & 0 deletions api/v1beta1/tempest_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ func (spec *TempestSpec) Default() {
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
35 changes: 35 additions & 0 deletions controllers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,3 +550,38 @@ func GetCommonRbacRules(privileged bool) []rbacv1.PolicyRule {

return []rbacv1.PolicyRule{rbacPolicyRule}
}

// TODO make general for all resources
func getResourceRun(instance *v1beta1.Tempest, workflowStepNum int) v1beta1.TempestRunSpec {
if workflowStepNum < len(instance.Spec.Workflow) {
newInstance := changeType(instance, workflowStepNum).(*v1beta1.Tempest)
return newInstance.Spec.TempestRun
}
return instance.Spec.TempestRun
}

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

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)
}
}
typedInstance.Spec.TempestRun = tRun

return typedInstance
}
26 changes: 11 additions & 15 deletions controllers/tempest_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,30 +414,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 := getResourceRun(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 @@ -446,9 +442,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 @@ -457,11 +453,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 @@ -477,7 +473,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 @@ -495,7 +491,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 6b1c12c

Please sign in to comment.