From 53cb3febb420a3028b646c4796c7ab3b0be9aa3c Mon Sep 17 00:00:00 2001 From: Yael Harel Date: Mon, 12 Sep 2022 16:21:58 -0400 Subject: [PATCH] Enable setting SOURCE_DATA_EPOCH This environment variable is the timestamp for created time in app image config Fixes #1009 Signed-off-by: Yael Harel --- pkg/apis/build/v1alpha2/build_pod.go | 17 ++++++++++++++--- pkg/apis/build/v1alpha2/build_pod_test.go | 4 ++++ pkg/apis/build/v1alpha2/build_types.go | 1 + pkg/apis/build/v1alpha2/image_builds.go | 1 + pkg/apis/build/v1alpha2/image_types.go | 1 + 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/pkg/apis/build/v1alpha2/build_pod.go b/pkg/apis/build/v1alpha2/build_pod.go index 556020e12..1bd1085f6 100644 --- a/pkg/apis/build/v1alpha2/build_pod.go +++ b/pkg/apis/build/v1alpha2/build_pod.go @@ -4,6 +4,7 @@ import ( "fmt" "path/filepath" "strings" + "time" "github.com/Masterminds/semver/v3" "github.com/google/go-containerregistry/pkg/name" @@ -592,6 +593,13 @@ func (b *Build) BuildPod(images BuildPodImages, buildContext BuildContext) (*cor homeEnv, platformApiVersionEnvVar, }, + func() corev1.EnvVar { + if b.Spec.SetEpochTime { + return corev1.EnvVar{Name: "SOURCE_DATE_EPOCH", Value: time.Now().String()} + } else { + return corev1.EnvVar{Name: "", Value: ""} + } + }(), func() corev1.EnvVar { return corev1.EnvVar{ Name: "CNB_RUN_IMAGE", @@ -1211,9 +1219,12 @@ func cosignSecretArgs(secret corev1.Secret) []string { return cosignArgs } -func envs(envs []corev1.EnvVar, envVar corev1.EnvVar) []corev1.EnvVar { - if envVar.Name != "" && envVar.Value != "" { - envs = append(envs, envVar) +func envs(envs []corev1.EnvVar, envVar1 corev1.EnvVar, envVar2 corev1.EnvVar) []corev1.EnvVar { + if envVar1.Name != "" && envVar1.Value != "" { + envs = append(envs, envVar1) + } + if envVar2.Name != "" && envVar2.Value != "" { + envs = append(envs, envVar2) } return envs } diff --git a/pkg/apis/build/v1alpha2/build_pod_test.go b/pkg/apis/build/v1alpha2/build_pod_test.go index 93d45be38..d4c3bf32d 100644 --- a/pkg/apis/build/v1alpha2/build_pod_test.go +++ b/pkg/apis/build/v1alpha2/build_pod_test.go @@ -334,6 +334,7 @@ func testBuildPod(t *testing.T, when spec.G, it spec.S) { Tolerations: []corev1.Toleration{{Key: "some-key"}}, NodeSelector: map[string]string{"foo": "bar"}, Affinity: &corev1.Affinity{}, + SetEpochTime: true, }, } }) @@ -1069,6 +1070,9 @@ func testBuildPod(t *testing.T, when spec.G, it spec.S) { assert.Equal(t, pod.Spec.InitContainers[5].Name, "export") assert.Equal(t, pod.Spec.InitContainers[5].Image, builderImage) assert.Contains(t, pod.Spec.InitContainers[5].Env, corev1.EnvVar{Name: "CNB_PLATFORM_API", Value: "0.8"}) + _, ok := fetchEnvVar(pod.Spec.InitContainers[5].Env, "SOURCE_DATE_EPOCH") + assert.Equal(t, true, ok) + assert.Contains(t, pod.Spec.InitContainers[5].Env, corev1.EnvVar{Name: "CNB_RUN_IMAGE", Value: "builderregistry.io/run"}) assert.ElementsMatch(t, names(pod.Spec.InitContainers[5].VolumeMounts), []string{ "layers-dir", "workspace-dir", diff --git a/pkg/apis/build/v1alpha2/build_types.go b/pkg/apis/build/v1alpha2/build_types.go index 053fb2e8e..9f3b9006d 100644 --- a/pkg/apis/build/v1alpha2/build_types.go +++ b/pkg/apis/build/v1alpha2/build_types.go @@ -77,6 +77,7 @@ type BuildSpec struct { RuntimeClassName *string `json:"runtimeClassName,omitempty"` SchedulerName string `json:"schedulerName,omitempty"` PriorityClassName string `json:"priorityClassName,omitempty"` + SetEpochTime bool `json:"setEpochTime,omitempty"` } func (bs *BuildSpec) RegistryCacheTag() string { diff --git a/pkg/apis/build/v1alpha2/image_builds.go b/pkg/apis/build/v1alpha2/image_builds.go index 73756486e..278998501 100644 --- a/pkg/apis/build/v1alpha2/image_builds.go +++ b/pkg/apis/build/v1alpha2/image_builds.go @@ -80,6 +80,7 @@ func (im *Image) Build(sourceResolver *SourceResolver, builder BuilderResource, SchedulerName: im.SchedulerName(), PriorityClassName: priorityClass, ActiveDeadlineSeconds: im.BuildTimeout(), + SetEpochTime: im.Spec.Build.SetEpochTime, }, } } diff --git a/pkg/apis/build/v1alpha2/image_types.go b/pkg/apis/build/v1alpha2/image_types.go index 81b202743..8f87ed132 100644 --- a/pkg/apis/build/v1alpha2/image_types.go +++ b/pkg/apis/build/v1alpha2/image_types.go @@ -78,6 +78,7 @@ type ImageBuild struct { RuntimeClassName *string `json:"runtimeClassName,omitempty"` SchedulerName string `json:"schedulerName,omitempty"` BuildTimeout *int64 `json:"buildTimeout,omitempty"` + SetEpochTime bool `json:"setEpochTime,omitempty"` } // +k8s:openapi-gen=true