Skip to content

Commit

Permalink
fix: use map instead of slice
Browse files Browse the repository at this point in the history
Signed-off-by: Vladislav Sukhin <vladislav@kubeshop.io>
  • Loading branch information
vsukhin committed Dec 18, 2024
1 parent f242e45 commit 978e7aa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
20 changes: 11 additions & 9 deletions pkg/testworkflows/testworkflowprocessor/intermediate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Intermediate interface {
ConfigMaps() []corev1.ConfigMap
Secrets() []corev1.Secret
Volumes() []corev1.Volume
Pvcs() []corev1.PersistentVolumeClaim
Pvcs() map[string]corev1.PersistentVolumeClaim

AppendJobConfig(cfg *testworkflowsv1.JobConfig) Intermediate
AppendPodConfig(cfg *testworkflowsv1.PodConfig) Intermediate
Expand Down Expand Up @@ -50,9 +50,9 @@ type intermediate struct {
Job testworkflowsv1.JobConfig `expr:"include"`

// Actual Kubernetes resources to use
Secs []corev1.Secret `expr:"force"`
Cfgs []corev1.ConfigMap `expr:"force"`
Ps []corev1.PersistentVolumeClaim `expr:"force"`
Secs []corev1.Secret `expr:"force"`
Cfgs []corev1.ConfigMap `expr:"force"`
Ps map[string]corev1.PersistentVolumeClaim `expr:"force"`

// Storing files
Files ConfigMapFiles `expr:"include"`
Expand All @@ -64,7 +64,9 @@ func NewIntermediate() Intermediate {
RefCounter: ref,
Root: stage.NewGroupStage("", true),
Container: stage.NewContainer(),
Files: NewConfigMapFiles(fmt.Sprintf("{{resource.id}}-%s", ref.NextRef()), nil)}
Files: NewConfigMapFiles(fmt.Sprintf("{{resource.id}}-%s", ref.NextRef()), nil),
Ps: make(map[string]corev1.PersistentVolumeClaim),
}
}

func (s *intermediate) ContainerDefaults() stage.Container {
Expand All @@ -91,7 +93,7 @@ func (s *intermediate) Volumes() []corev1.Volume {
return append(s.Pod.Volumes, s.Files.Volumes()...)
}

func (s *intermediate) Pvcs() []corev1.PersistentVolumeClaim {
func (s *intermediate) Pvcs() map[string]corev1.PersistentVolumeClaim {
return s.Ps
}

Expand All @@ -108,12 +110,12 @@ func (s *intermediate) AppendPodConfig(cfg *testworkflowsv1.PodConfig) Intermedi
func (s *intermediate) AppendPvc(cfg map[string]corev1.PersistentVolumeClaimSpec) Intermediate {
ref := s.NextRef()
for name, spec := range cfg {
s.Ps = append(s.Ps, corev1.PersistentVolumeClaim{
s.Ps[name] = corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-%s", name, ref),
Name: ref,
},
Spec: spec,
})
}
}
return s
}
Expand Down
30 changes: 13 additions & 17 deletions pkg/testworkflows/testworkflowprocessor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"maps"
"path/filepath"
"strings"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -116,18 +115,11 @@ func (p *processor) Bundle(ctx context.Context, workflow *testworkflowsv1.TestWo
AppendVolumeMounts(layer.AddEmptyDirVolume(nil, constants.DefaultDataPath))

mapEnv := make(map[string]corev1.EnvVarSource)
mapPvc := make(map[string]string)
for _, pvc := range layer.Pvcs() {
if index := strings.LastIndex(pvc.Name, "-"); index != -1 {
mapPvc[pvc.Name[:index]] = pvc.Name[index+1:]
}
}

machines = append(machines,
createSecretMachine(mapEnv),
testworkflowconfig.CreateWorkerMachine(&options.Config.Worker),
testworkflowconfig.CreateResourceMachine(&options.Config.Resource),
testworkflowconfig.CreatePvcMachine(mapPvc),
testworkflowconfig.CreatePvcMachine(common.MapMap(layer.Pvcs(), func(v corev1.PersistentVolumeClaim) string { return v.Name })),
)

// Fetch resource root and resource ID
Expand Down Expand Up @@ -174,19 +166,23 @@ func (p *processor) Bundle(ctx context.Context, workflow *testworkflowsv1.TestWo
}

// Finalize Pvcs
pvcs := layer.Pvcs()
mapPvc = make(map[string]string)
for i := range pvcs {
AnnotateControlledBy(&pvcs[i], options.Config.Resource.RootId, options.Config.Resource.Id)
err = expressions.FinalizeForce(&pvcs[i], machines...)
pvcs := make([]corev1.PersistentVolumeClaim, 0)
mapPvc := make(map[string]string)
for name, spec := range layer.Pvcs() {
AnnotateControlledBy(&spec, options.Config.Resource.RootId, options.Config.Resource.Id)
err = expressions.FinalizeForce(&spec, machines...)
if err != nil {
return nil, errors.Wrap(err, "finalizing Pvc")
}

if index := strings.LastIndex(pvcs[i].Name, "-"); index != -1 {
mapPvc[pvcs[i].Name[:index]] = pvcs[i].Name[index+1:]
pvcs[i].Name = pvcs[i].Name[index+1:]
data := struct{ value string }{value: name}
err = expressions.FinalizeForce(&data, machines...)
if err != nil {
return nil, errors.Wrap(err, "finalizing name")
}

mapPvc[data.value] = spec.Name
pvcs = append(pvcs, spec)
}
options.Config.Execution.PvcNames = common.MergeMaps(options.Config.Execution.PvcNames, mapPvc)

Expand Down

0 comments on commit 978e7aa

Please sign in to comment.