|
5 | 5 | package sources |
6 | 6 |
|
7 | 7 | import ( |
| 8 | + "crypto/sha256" |
8 | 9 | "fmt" |
9 | 10 | "regexp" |
10 | 11 | "strings" |
@@ -87,3 +88,100 @@ func FindResultValue(results []pipelineapi.TaskRunResult, sourceName, resultName |
87 | 88 |
|
88 | 89 | return "" |
89 | 90 | } |
| 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 | +} |
0 commit comments