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 Dec 3, 2024
1 parent 28be6b4 commit acf536c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 18 deletions.
38 changes: 20 additions & 18 deletions api/v1beta1/common_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,39 +36,41 @@ const (

// 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
}
// TODO add other resources
switch spec := instance.(type) {
case *TempestSpec:
fmt.Println("Instance is type *TempestSpec")
default:
fmt.Println("Error, instance of unknown type.")
}

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

tRunReflect := reflect.ValueOf(tRun)
wtRunReflect := reflect.ValueOf(wtRun).Elem()
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)
func setNonZeroValues(src reflect.Value, dest reflect.Value, isStruct 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 wtRunValue.IsNil() && !tRunValue.IsNil() {
if tRunValue.Kind() == reflect.Struct {
setNonZeroValues(tRunValue, wtRunValue, true)
} else {
if is_struct {
if isStruct {
wtRunValue.Set(tRunValue)
} else {
tRunPtr := reflect.New(tRunValue.Type())
tRunPtr.Elem().Set(tRunValue)
wtRunValue.Set(tRunPtr)
}
}
}
}
}
}
}
37 changes: 37 additions & 0 deletions controllers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,3 +728,40 @@ func EnsureCloudsConfigMapExists(

return ctrl.Result{}, nil
}

// 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 add other types
switch typedInstance := instance.(type) {
case *v1beta1.Tempest:
var tRun v1beta1.TempestRunSpec
default:
fmt.Println("Error, instance has unknown type.")
}
wtRun := typedInstance.Spec.Workflow[workflowStepNum].TempestRun

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
}

0 comments on commit acf536c

Please sign in to comment.