Skip to content

Commit 201b75a

Browse files
hasanawad94SaschaSchwarze0
authored andcommitted
Add volumes for trivy
Added env variables for trivy to configure writing to volumes instead of the root filesystem. Added a utility function `AppendWriteableVolumes` to be used for appending volumes to steps. 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 f00e9c0 commit 201b75a

File tree

3 files changed

+117
-2
lines changed

3 files changed

+117
-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 volume for trivy writes.
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: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package sources
66

77
import (
8+
"crypto/sha256"
89
"fmt"
910
"regexp"
1011
"strings"
@@ -87,3 +88,100 @@ func FindResultValue(results []pipelineapi.TaskRunResult, sourceName, resultName
8788

8889
return ""
8990
}
91+
92+
// AppendWriteableVolumes configures writable volumes for a specific step in a Tekton Task.
93+
// It ensures that these volumes are not shared with other steps in the same pod.
94+
func AppendWriteableVolumes(
95+
taskSpec *pipelineapi.TaskSpec,
96+
targetStep *pipelineapi.Step,
97+
) {
98+
// Define a custom, isolated path for temporary files and mount it.
99+
tmpDir := "/shp-tmp"
100+
addStepEmptyDirVolume(
101+
taskSpec,
102+
targetStep,
103+
generateVolumeName("shp-tmp-", targetStep.Name),
104+
tmpDir,
105+
)
106+
// Point the TMPDIR environment variable to the custom path.
107+
setEnvVar(targetStep, "TMPDIR", tmpDir)
108+
}
109+
110+
// generateVolumeName creates a unique, DNS-1123 compliant volume name for a step.
111+
// The function ensures uniqueness by appending a SHA256 hash of the original step name.
112+
func generateVolumeName(prefix, stepName string) string {
113+
// Create the full name first, then sanitize it
114+
name := fmt.Sprintf("%s%s", prefix, stepName)
115+
116+
// Convert to lowercase and remove forbidden characters
117+
sanitizedName := strings.ToLower(dnsLabel1123Forbidden.ReplaceAllString(name, "-"))
118+
119+
// Remove both leading and trailing hyphens
120+
sanitizedName = strings.Trim(sanitizedName, "-")
121+
122+
// Generate a short hash of the original stepName for uniqueness
123+
hash := fmt.Sprintf("%x", sha256.Sum256([]byte(stepName)))[:8]
124+
125+
// Ensure maximum length, leaving space for the hash
126+
maxLength := 63 - len(hash) - 1 // -1 for the hyphen separator
127+
if len(sanitizedName) > maxLength {
128+
sanitizedName = sanitizedName[:maxLength]
129+
}
130+
131+
// Combine sanitized name with hash
132+
result := fmt.Sprintf("%s-%s", sanitizedName, hash)
133+
134+
return result
135+
}
136+
137+
// addStepEmptyDirVolume creates a unique EmptyDir volume for a specific step and mounts it at the given path.
138+
func addStepEmptyDirVolume(taskSpec *pipelineapi.TaskSpec, step *pipelineapi.Step, volumeName, mountPath string) {
139+
ensureVolume(taskSpec, corev1.Volume{
140+
Name: volumeName,
141+
VolumeSource: corev1.VolumeSource{
142+
EmptyDir: &corev1.EmptyDirVolumeSource{},
143+
},
144+
})
145+
146+
ensureVolumeMount(step, corev1.VolumeMount{
147+
Name: volumeName,
148+
MountPath: mountPath,
149+
})
150+
}
151+
152+
// setEnvVar sets or overrides an environment variable in a Step.
153+
func setEnvVar(step *pipelineapi.Step, name, value string) {
154+
for i, env := range step.Env {
155+
if env.Name == name {
156+
// Override existing variable
157+
step.Env[i].Value = value
158+
return
159+
}
160+
}
161+
162+
// Append new variable if it does not exist
163+
step.Env = append(step.Env, corev1.EnvVar{
164+
Name: name,
165+
Value: value,
166+
})
167+
}
168+
169+
// ensureVolume adds a volume to the TaskSpec if a volume with the same name does not already exist.
170+
func ensureVolume(taskSpec *pipelineapi.TaskSpec, volume corev1.Volume) {
171+
for _, v := range taskSpec.Volumes {
172+
if v.Name == volume.Name {
173+
return
174+
}
175+
}
176+
taskSpec.Volumes = append(taskSpec.Volumes, volume)
177+
}
178+
179+
// ensureVolumeMount adds a VolumeMount to a Step if a mount with the same name does not already exist.
180+
func ensureVolumeMount(step *pipelineapi.Step, mount corev1.VolumeMount) {
181+
for _, m := range step.VolumeMounts {
182+
if m.Name == mount.Name {
183+
return
184+
}
185+
}
186+
step.VolumeMounts = append(step.VolumeMounts, mount)
187+
}

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)