Skip to content

Commit

Permalink
fix: dedup code
Browse files Browse the repository at this point in the history
Signed-off-by: Vladislav Sukhin <[email protected]>
  • Loading branch information
vsukhin committed Sep 13, 2024
1 parent 73eebee commit ded6628
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
33 changes: 14 additions & 19 deletions pkg/testworkflows/testworkflowprocessor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,25 +395,8 @@ func (p *processor) Bundle(ctx context.Context, workflow *testworkflowsv1.TestWo
}
jobSpec.Spec.Template = podSpec

for i := range jobSpec.Spec.Template.Spec.InitContainers {
for envName, envSource := range mapEnv {
e := corev1.EnvVar{
Name: envName,
ValueFrom: envSource.DeepCopy(),
}
jobSpec.Spec.Template.Spec.InitContainers[i].Env = append(jobSpec.Spec.Template.Spec.InitContainers[i].Env, e)
}
}

for i := range jobSpec.Spec.Template.Spec.Containers {
for envName, envSource := range mapEnv {
e := corev1.EnvVar{
Name: envName,
ValueFrom: envSource.DeepCopy(),
}
jobSpec.Spec.Template.Spec.Containers[i].Env = append(jobSpec.Spec.Template.Spec.Containers[i].Env, e)
}
}
addEnvVarToContainerSpec(mapEnv, jobSpec.Spec.Template.Spec.InitContainers)
addEnvVarToContainerSpec(mapEnv, jobSpec.Spec.Template.Spec.Containers)

// Build running instructions
sigSerialized, _ := json.Marshal(sig)
Expand All @@ -436,3 +419,15 @@ func (p *processor) Bundle(ctx context.Context, workflow *testworkflowsv1.TestWo
}
return bundle, nil
}

func addEnvVarToContainerSpec(mapEnv map[string]corev1.EnvVarSource, containers []corev1.Container) {
for i := range containers {
for envName, envSource := range mapEnv {
e := corev1.EnvVar{
Name: envName,
ValueFrom: envSource.DeepCopy(),
}
containers[i].Env = append(containers[i].Env, e)
}
}
}
30 changes: 18 additions & 12 deletions pkg/testworkflows/testworkflowresolver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,9 @@ func createConfigMachine(cfg map[string]intstr.IntOrString, schema map[string]te
return nil, errors.Wrap(err, "config."+k)
}
if schema[k].Sensitive && externalize != nil {
envVar, err := externalize(k, expr.String())
expr, err = getSecretCallExpression(expr, k, externalize)
if err != nil {
return nil, errors.Wrap(err, "config."+k)
}
if envVar.SecretKeyRef != nil {
expr = expressions.NewValue(fmt.Sprintf("{{%ssecret(\"%s\", \"%s\")}}", expressions.InternalFnCall,
envVar.SecretKeyRef.Name, envVar.SecretKeyRef.Key))
return nil, err
}
}
machine.Register("config."+k, expr)
Expand All @@ -69,13 +65,9 @@ func createConfigMachine(cfg map[string]intstr.IntOrString, schema map[string]te
return nil, errors.Wrap(err, "config."+k)
}
if schema[k].Sensitive && externalize != nil {
envVar, err := externalize(k, expr.String())
expr, err = getSecretCallExpression(expr, k, externalize)
if err != nil {
return nil, errors.Wrap(err, "config."+k)
}
if envVar.SecretKeyRef != nil {
expr = expressions.NewValue(fmt.Sprintf("{{%ssecret(\"%s\", \"%s\")}}", expressions.InternalFnCall,
envVar.SecretKeyRef.Name, envVar.SecretKeyRef.Key))
return nil, err
}
}
machine.Register("config."+k, expr)
Expand All @@ -84,6 +76,20 @@ func createConfigMachine(cfg map[string]intstr.IntOrString, schema map[string]te
return machine, nil
}

func getSecretCallExpression(expr expressions.Expression, k string, externalize func(key, value string) (*corev1.EnvVarSource, error)) (
expressions.Expression, error) {
envVar, err := externalize(k, expr.String())
if err != nil {
return nil, errors.Wrap(err, "config."+k)
}
if envVar.SecretKeyRef != nil {
expr = expressions.NewValue(fmt.Sprintf("{{%ssecret(\"%s\", \"%s\")}}", expressions.InternalFnCall,
envVar.SecretKeyRef.Name, envVar.SecretKeyRef.Key))
}

return expr, nil
}

func ApplyWorkflowConfig(t *testworkflowsv1.TestWorkflow, cfg map[string]intstr.IntOrString,
externalize func(key, value string) (*corev1.EnvVarSource, error)) (*testworkflowsv1.TestWorkflow, error) {
if t == nil {
Expand Down

0 comments on commit ded6628

Please sign in to comment.