Skip to content

Commit c9c7e04

Browse files
committed
Add volumes for trivy
Added env variables for trivy to configure writing to volumes instead of the root filesystem. Volumes introduced to trivy: - volume for trivy cache - volume for tmp data Set env variables: - TRIVY_CACHE_DIR - TMPDIR Signed-off-by: Hasan Awad <[email protected]>
1 parent f008fec commit c9c7e04

File tree

3 files changed

+113
-2
lines changed

3 files changed

+113
-2
lines changed

pkg/reconciler/buildrun/resources/image_processing.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,24 @@ func SetupImageProcessing(taskRun *pipelineapi.TaskRun, cfg *config.Config, crea
200200
"--secret-path", secretMountPath,
201201
)
202202
}
203-
203+
// add the trivy cache volume
204+
taskRun.Spec.TaskSpec.Volumes = append(taskRun.Spec.TaskSpec.Volumes, core.Volume{
205+
Name: "shp-trivy-cache-data",
206+
VolumeSource: core.VolumeSource{
207+
EmptyDir: &core.EmptyDirVolumeSource{},
208+
},
209+
})
210+
imageProcessingStep.VolumeMounts = append(imageProcessingStep.VolumeMounts, core.VolumeMount{
211+
Name: "shp-trivy-cache-data",
212+
MountPath: "/trivy-cache-data",
213+
})
214+
215+
imageProcessingStep.Env = append(imageProcessingStep.Env, core.EnvVar{
216+
Name: "TRIVY_CACHE_DIR",
217+
Value: "/trivy-cache-data",
218+
})
219+
// add the writeable volumes
220+
sources.AppendWriteableVolumes(taskRun.Spec.TaskSpec, &imageProcessingStep)
204221
// append the mutate step
205222
taskRun.Spec.TaskSpec.Steps = append(taskRun.Spec.TaskSpec.Steps, imageProcessingStep)
206223
}

pkg/reconciler/buildrun/resources/sources/utils.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,97 @@ func FindResultValue(results []pipelineapi.TaskRunResult, sourceName, resultName
8787

8888
return ""
8989
}
90+
91+
// ensureVolume adds a volume to the TaskSpec if a volume with the same name does not already exist.
92+
func ensureVolume(taskSpec *pipelineapi.TaskSpec, volume corev1.Volume) {
93+
for _, v := range taskSpec.Volumes {
94+
if v.Name == volume.Name {
95+
return
96+
}
97+
}
98+
taskSpec.Volumes = append(taskSpec.Volumes, volume)
99+
}
100+
101+
// ensureVolumeMount adds a VolumeMount to a Step if a mount with the same name does not already exist.
102+
func ensureVolumeMount(step *pipelineapi.Step, mount corev1.VolumeMount) {
103+
for _, m := range step.VolumeMounts {
104+
if m.Name == mount.Name {
105+
return
106+
}
107+
}
108+
step.VolumeMounts = append(step.VolumeMounts, mount)
109+
}
110+
111+
// AppendWriteableVolumes configures distinct, writable volumes for tmp directory
112+
// for a specific step in a Tekton Task. It ensures that these volumes are not shared with
113+
// other steps in the same pod.
114+
func AppendWriteableVolumes(
115+
taskSpec *pipelineapi.TaskSpec,
116+
targetStep *pipelineapi.Step,
117+
) {
118+
// Define a custom, isolated path for temporary files and mount a volume there.
119+
// This avoids overwriting the base image's /tmp and is a container best practice.
120+
tmpDir := "/shp-tmp"
121+
addStepEmptyDirVolume(
122+
taskSpec,
123+
targetStep,
124+
generateVolumeName("shp-tmp-", targetStep.Name),
125+
tmpDir,
126+
)
127+
// Point the TMPDIR environment variable to the custom path.
128+
setEnvVar(targetStep, "TMPDIR", tmpDir)
129+
}
130+
131+
// generateVolumeName creates a sanitized, unique volume name for a step.
132+
// It combines a prefix with a sanitized version of the step name, ensuring
133+
// the result is a valid DNS-1123 label.
134+
func generateVolumeName(prefix, stepName string) string {
135+
// Sanitize the step name by replacing forbidden characters.
136+
sanitizedStepName := dnsLabel1123Forbidden.ReplaceAllString(stepName, "-")
137+
138+
// Calculate the maximum length for the step name portion.
139+
maxStepNameLength := 63 - len(prefix)
140+
if len(sanitizedStepName) > maxStepNameLength {
141+
sanitizedStepName = sanitizedStepName[:maxStepNameLength]
142+
}
143+
144+
// Combine the prefix and the sanitized step name.
145+
name := prefix + sanitizedStepName
146+
147+
// Trim any trailing dashes, as they are not allowed at the end of a label.
148+
name = strings.TrimSuffix(name, "-")
149+
150+
return name
151+
}
152+
153+
// addStepEmptyDirVolume creates a unique EmptyDir volume for a specific step and mounts it at the given path.
154+
func addStepEmptyDirVolume(taskSpec *pipelineapi.TaskSpec, step *pipelineapi.Step, volumeName, mountPath string) {
155+
ensureVolume(taskSpec, corev1.Volume{
156+
Name: volumeName,
157+
VolumeSource: corev1.VolumeSource{
158+
EmptyDir: &corev1.EmptyDirVolumeSource{},
159+
},
160+
})
161+
162+
ensureVolumeMount(step, corev1.VolumeMount{
163+
Name: volumeName,
164+
MountPath: mountPath,
165+
})
166+
}
167+
168+
// setEnvVar sets or overrides an environment variable in a Step.
169+
func setEnvVar(step *pipelineapi.Step, name, value string) {
170+
for i, env := range step.Env {
171+
if env.Name == name {
172+
// Override existing variable
173+
step.Env[i].Value = value
174+
return
175+
}
176+
}
177+
178+
// Append new variable if it does not exist
179+
step.Env = append(step.Env, corev1.EnvVar{
180+
Name: name,
181+
Value: value,
182+
})
183+
}

pkg/reconciler/buildrun/resources/taskrun_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ var _ = Describe("GenerateTaskrun", func() {
122122
})
123123

124124
It("should ensure top level volumes are populated", func() {
125-
Expect(len(got.Volumes)).To(Equal(1))
125+
Expect(len(got.Volumes)).To(Equal(3))
126126
})
127127

128128
It("should contain the shipwright system parameters", func() {

0 commit comments

Comments
 (0)