Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [TKC-2895] pvc config #6074

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pkg/testworkflows/testworkflowprocessor/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type BundleOptions struct {
type Bundle struct {
Secrets []corev1.Secret
ConfigMaps []corev1.ConfigMap
Pvcs []corev1.PersistentVolumeClaim
Job batchv1.Job
Signature []stage.Signature
FullSignature []stage.Signature
Expand All @@ -50,6 +51,9 @@ func (b *Bundle) SetGroupId(groupId string) {
for i := range b.Secrets {
AnnotateGroupId(&b.Secrets[i], groupId)
}
for i := range b.Pvcs {
AnnotateGroupId(&b.Pvcs[i], groupId)
}
}

func (b *Bundle) Deploy(ctx context.Context, clientSet kubernetes.Interface, namespace string) (err error) {
Expand All @@ -68,6 +72,13 @@ func (b *Bundle) Deploy(ctx context.Context, clientSet kubernetes.Interface, nam
return errors.Wrap(err, "failed to deploy config maps")
}
}
for _, item := range b.Pvcs {
_, err = clientSet.CoreV1().PersistentVolumeClaims(namespace).Create(ctx, &item, metav1.CreateOptions{})
if err != nil {
return errors.Wrap(err, "failed to deploy pvcs")
}
}

_, err = clientSet.BatchV1().Jobs(namespace).Create(ctx, &b.Job, metav1.CreateOptions{})
return errors.Wrap(err, "failed to deploy job")
}
16 changes: 14 additions & 2 deletions pkg/testworkflows/testworkflowprocessor/intermediate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ type Intermediate interface {
ConfigMaps() []corev1.ConfigMap
Secrets() []corev1.Secret
Volumes() []corev1.Volume
Pvcs() []corev1.PersistentVolumeClaim

AppendJobConfig(cfg *testworkflowsv1.JobConfig) Intermediate
AppendPodConfig(cfg *testworkflowsv1.PodConfig) Intermediate

AddConfigMap(configMap corev1.ConfigMap) Intermediate
AddSecret(secret corev1.Secret) Intermediate
AddVolume(volume corev1.Volume) Intermediate
AddPvc(pvc corev1.PersistentVolumeClaim) Intermediate

AddEmptyDirVolume(source *corev1.EmptyDirVolumeSource, mountPath string) corev1.VolumeMount

Expand All @@ -47,8 +49,9 @@ type intermediate struct {
Job testworkflowsv1.JobConfig `expr:"include"`

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

// Storing files
Files ConfigMapFiles `expr:"include"`
Expand Down Expand Up @@ -87,6 +90,10 @@ func (s *intermediate) Volumes() []corev1.Volume {
return append(s.Pod.Volumes, s.Files.Volumes()...)
}

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

func (s *intermediate) AppendJobConfig(cfg *testworkflowsv1.JobConfig) Intermediate {
s.Job = *testworkflowresolver.MergeJobConfig(&s.Job, cfg)
return s
Expand All @@ -102,6 +109,11 @@ func (s *intermediate) AddVolume(volume corev1.Volume) Intermediate {
return s
}

func (s *intermediate) AddPvc(pvc corev1.PersistentVolumeClaim) Intermediate {
s.Ps = append(s.Ps, pvc)
return s
}

func (s *intermediate) AddConfigMap(configMap corev1.ConfigMap) Intermediate {
s.Cfgs = append(s.Cfgs, configMap)
return s
Expand Down
11 changes: 11 additions & 0 deletions pkg/testworkflows/testworkflowprocessor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,16 @@ func (p *processor) Bundle(ctx context.Context, workflow *testworkflowsv1.TestWo
}
}

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

// Finalize Secrets
secrets := append(layer.Secrets(), options.Secrets...)
for i := range secrets {
Expand Down Expand Up @@ -420,6 +430,7 @@ func (p *processor) Bundle(ctx context.Context, workflow *testworkflowsv1.TestWo
bundle = &Bundle{
ConfigMaps: configMaps,
Secrets: secrets,
Pvcs: pvcs,
Job: jobSpec,
Signature: sig,
FullSignature: fullSig,
Expand Down
1 change: 1 addition & 0 deletions pkg/testworkflows/testworkflowresolver/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func injectTemplateToSpec(spec *testworkflowsv1.TestWorkflowSpec, template testw
spec.Services = MergeMap(common.MapMap(template.Spec.Services, ConvertIndependentServiceToService), spec.Services)
spec.Container = MergeContainerConfig(template.Spec.Container, spec.Container)
spec.System = MergeSystem(template.Spec.System, spec.System)
spec.Pvcs = MergeMap(template.Spec.Pvcs, spec.Pvcs)

// Include the steps from the template
setup := common.MapSlice(template.Spec.Setup, ConvertIndependentStepToStep)
Expand Down
33 changes: 33 additions & 0 deletions pkg/testworkflows/testworkflowresolver/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"maps"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

testworkflowsv1 "github.com/kubeshop/testkube-operator/api/testworkflows/v1"
"github.com/kubeshop/testkube/internal/common"
Expand Down Expand Up @@ -414,3 +415,35 @@ func MergeTags(dst, src map[string]string) map[string]string {

return dst
}

func MergePvcConfig(dst, include *testworkflowsv1.TestWorkflowPvcConfig) *testworkflowsv1.TestWorkflowPvcConfig {
if dst == nil {
return include
} else if include == nil {
return dst
}
if include.Shared {
vsukhin marked this conversation as resolved.
Show resolved Hide resolved
dst.Shared = include.Shared
}
dst.AccessModes = append(dst.AccessModes, include.AccessModes...)
if include.VolumeMode != "" {
dst.VolumeMode = include.VolumeMode
}
dst.Resources = MergeResources(dst.Resources, include.Resources)
if include.StorageClassName != "" {
dst.StorageClassName = include.StorageClassName
}
dst.Selector = MergeLabelSelector(dst.Selector, include.Selector)
return dst
}

func MergeLabelSelector(dst, include *metav1.LabelSelector) *metav1.LabelSelector {
if dst == nil {
return include
} else if include == nil {
return dst
}
dst.MatchLabels = common.MergeMaps(dst.MatchLabels, include.MatchLabels)
dst.MatchExpressions = append(dst.MatchExpressions, include.MatchExpressions...)
return dst
}
Loading