Skip to content

Commit

Permalink
Enable setting SOURCE_DATA_EPOCH
Browse files Browse the repository at this point in the history
This environment variable is the timestamp for created time in app image config

Fixes #1009

Signed-off-by: Yael Harel <yharel@vmware.com>
  • Loading branch information
Yael Harel committed Sep 13, 2022
1 parent ad6c587 commit 53cb3fe
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
17 changes: 14 additions & 3 deletions pkg/apis/build/v1alpha2/build_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"path/filepath"
"strings"
"time"

"github.com/Masterminds/semver/v3"
"github.com/google/go-containerregistry/pkg/name"
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions pkg/apis/build/v1alpha2/build_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
})
Expand Down Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/build/v1alpha2/build_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/build/v1alpha2/image_builds.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/build/v1alpha2/image_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 53cb3fe

Please sign in to comment.