From 7cf7311e5133e0ae06fcc4f8d746108c9deac503 Mon Sep 17 00:00:00 2001 From: Kenny Leung Date: Wed, 29 May 2024 11:47:49 -0700 Subject: [PATCH] add flag to allow using memory for tmpfs for packages with defined resources Signed-off-by: Kenny Leung --- pkg/cli/build.go | 4 +++- pkg/internal/bundle/bundle.go | 11 +++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/pkg/cli/build.go b/pkg/cli/build.go index 3efafa497..5e632d9d7 100644 --- a/pkg/cli/build.go +++ b/pkg/cli/build.go @@ -156,6 +156,7 @@ func cmdBuild() *cobra.Command { cmd.Flags().StringVar(&cfg.k8sNamespace, "k8s-namespace", "default", "namespace to deploy pods into for builds.") cmd.Flags().StringVar(&cfg.machineFamily, "machine-family", "", "machine family for amd64 builds") cmd.Flags().StringVar(&cfg.serviceAccount, "service-account", "default", "service-account to run pods as.") + cmd.Flags().BoolVar(&cfg.memTmpfs, "memory-tmpfs", false, "whether to use memory-backed emptydir tmpfs for pods or not when resource are specified.") return cmd } @@ -602,6 +603,7 @@ type global struct { k8sNamespace string serviceAccount string machineFamily string + memTmpfs bool } func (g *global) logdir(arch string) string { @@ -831,7 +833,7 @@ func (t *task) buildBundleArch(ctx context.Context, arch string) (*bundleResult, log := clog.FromContext(ctx) - pod, err := bundle.Podspec(*t.bundle, t.ref, arch, t.cfg.machineFamily, t.cfg.serviceAccount, t.cfg.k8sNamespace) + pod, err := bundle.Podspec(*t.bundle, t.ref, arch, t.cfg.machineFamily, t.cfg.serviceAccount, t.cfg.k8sNamespace, t.cfg.memTmpfs) if err != nil { return nil, fmt.Errorf("creating podspec for %s: %w", t.pkg, err) } diff --git a/pkg/internal/bundle/bundle.go b/pkg/internal/bundle/bundle.go index 7b2157f72..d065dd84c 100644 --- a/pkg/internal/bundle/bundle.go +++ b/pkg/internal/bundle/bundle.go @@ -338,7 +338,7 @@ func escapeRFC1123(s string) string { // Podspec returns bytes of yaml representing a podspec. // This is a terrible API that we should change. -func Podspec(task Task, ref name.Reference, arch, mFamily, sa, ns string) (*corev1.Pod, error) { +func Podspec(task Task, ref name.Reference, arch, mFamily, sa, ns string, memTmpfs bool) (*corev1.Pod, error) { goarch := types.ParseArchitecture(arch).String() // Set some sane default resource requests if none are specified by flag or config. @@ -348,10 +348,15 @@ func Podspec(task Task, ref name.Reference, arch, mFamily, sa, ns string) (*core Memory: "4Gi", } + emptyDirMedium := corev1.StorageMediumDefault // Copy resources out of the task rather than overwriting in place because both archs share the same task. if in := task.Resources; in != nil { if in.CPU != "" { resources.CPU = in.CPU + + if memTmpfs { + emptyDirMedium = corev1.StorageMediumMemory + } } if in.Memory != "" { resources.Memory = in.Memory @@ -459,7 +464,9 @@ func Podspec(task Task, ref name.Reference, arch, mFamily, sa, ns string) (*core { Name: "tmp-dir-bundle-builder", VolumeSource: corev1.VolumeSource{ - EmptyDir: &corev1.EmptyDirVolumeSource{}, + EmptyDir: &corev1.EmptyDirVolumeSource{ + Medium: emptyDirMedium, + }, }, }, },