@@ -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+ }
0 commit comments