Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(env variable): port information to environment variables #16

Merged
merged 9 commits into from
Oct 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions cmd/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,27 @@ func updateWorkingVolumeMounts(targetContainerSpec []corev1.Container, volumeNam
return patch
}

// This is to add env variables, this is similar to updateWorkingVolumeMounts
// except there is no `isFirst` because the container this is patching will always have
// an environment variable existing, so we need to just _append_
func updateUserEnvVars(targetContainerSpec []corev1.Container, variableName string, variableValue string) (patch []patchOperation) {
// We only want to modify the container with NB_PREFIX in it, because that's the user container
for key := range targetContainerSpec {
for envVars := range targetContainerSpec[key].Env {
if targetContainerSpec[key].Env[envVars].Name == "NB_PREFIX" {
valueA := M{"name": variableName, "value": variableValue}
// it will never be the first environment variable (NB_prefix will exist)
patch = append(patch, patchOperation{
Op: "add",
Path: "/spec/containers/0/env/-",
Value: valueA,
})
}
}
}
return patch
}

// createPatch function handles the mutation patch creation
func createPatch(pod *corev1.Pod, sidecarConfigTemplate *Config, annotations map[string]string, clientset *kubernetes.Clientset,
svmShareList *corev1.ConfigMap, svmInfoMap map[string]SvmInfo) ([]byte, error) {
Expand Down Expand Up @@ -252,6 +273,10 @@ func createPatch(pod *corev1.Pod, sidecarConfigTemplate *Config, annotations map
" for ns:" + pod.Namespace + " so mounting will be skipped")
continue
}
// must set ACCESS and SECRET keys as well as svm url in the patch
patch = append(patch, updateUserEnvVars(pod.Spec.Containers, svmName+"_access", s3Access)...)
patch = append(patch, updateUserEnvVars(pod.Spec.Containers, svmName+"_secret", s3Secret)...)
patch = append(patch, updateUserEnvVars(pod.Spec.Containers, svmName+"_url", s3Url)...)
// iterate through and do the patch
for share := range shareList {
// Deep copy to avoid changes in original sidecar config
Expand Down Expand Up @@ -294,14 +319,16 @@ func createPatch(pod *corev1.Pod, sidecarConfigTemplate *Config, annotations map
patch = append(patch, addVolume(pod.Spec.Volumes, sidecarConfig.Volumes, "/spec/volumes")...)
patch = append(patch, updateAnnotation(pod.Annotations, annotations)...)
patch = append(patch, updateWorkingVolumeMounts(pod.Spec.Containers, csiEphemeralVolumeountName, bucketMount, svmName, isFirstVol)...)
// Add the environment variables
patch = append(patch, updateUserEnvVars(pod.Spec.Containers, svmName+"_"+cleanAndSanitizeName(bucketMount), initialHashedBucketName)...)
isFirstVol = false // Update such that no longer the first value

} // end shareList loop
} // end loop through user configmap
return json.Marshal(patch)
}

// Function to clean and sanitize the name by removing illegal characters
// Used for variable insertion, as dashes are no good but underscores are
func cleanAndSanitizeName(name string) string {
// Define the allowed regex pattern: lowercase letters, numbers, and dashes
validNameRegex := regexp.MustCompile(`[^a-z0-9-]`)
Expand All @@ -313,8 +340,8 @@ func cleanAndSanitizeName(name string) string {
pattern := regexp.MustCompile(`-+`)
name = pattern.ReplaceAllString(name, "-")

// Replace underscores with dashes
name = strings.ReplaceAll(name, "_", "-")
// Replace dashes with underscores
name = strings.ReplaceAll(name, "-", "_")

// Remove trailing dashes
name = strings.TrimRight(name, "-")
Expand Down
Loading