|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + * |
| 14 | + * Copyright 2025 Red Hat, Inc. |
| 15 | + */ |
| 16 | + |
| 17 | +package volume |
| 18 | + |
| 19 | +import ( |
| 20 | + corev1 "k8s.io/api/core/v1" |
| 21 | + "k8s.io/apimachinery/pkg/api/resource" |
| 22 | + "k8s.io/klog/v2" |
| 23 | +) |
| 24 | + |
| 25 | +// VolumeType represents the type of volume to create |
| 26 | +type VolumeType string |
| 27 | + |
| 28 | +const ( |
| 29 | + TypeConfigMap VolumeType = "configmap" |
| 30 | + TypeSecret VolumeType = "secret" |
| 31 | + TypeHostPath VolumeType = "hostpath" |
| 32 | + TypeEmptyDir VolumeType = "emptydir" |
| 33 | +) |
| 34 | + |
| 35 | +const ( |
| 36 | + DefaultMode = 420 |
| 37 | +) |
| 38 | + |
| 39 | +// Options holds the configuration for creating volumes and volume mounts |
| 40 | +type Options struct { |
| 41 | + // Common fields |
| 42 | + VolumeName string |
| 43 | + MountPath string |
| 44 | + ReadOnly bool |
| 45 | + SubPath string |
| 46 | + Type VolumeType |
| 47 | + |
| 48 | + // ConfigMap/Secret specific |
| 49 | + ResourceName string |
| 50 | + DefaultMode int32 |
| 51 | + Optional bool |
| 52 | + |
| 53 | + // HostPath specific |
| 54 | + HostPath string |
| 55 | + HostPathType *corev1.HostPathType |
| 56 | + |
| 57 | + // EmptyDir specific |
| 58 | + SizeLimit *resource.Quantity |
| 59 | + Medium corev1.StorageMedium |
| 60 | +} |
| 61 | + |
| 62 | +// Add adds a volume to the pod spec and a corresponding volume mount to the container |
| 63 | +func Add(podSpec *corev1.PodSpec, container *corev1.Container, opts Options) { |
| 64 | + // Add volume mount to container |
| 65 | + volumeMount := corev1.VolumeMount{ |
| 66 | + Name: opts.VolumeName, |
| 67 | + MountPath: opts.MountPath, |
| 68 | + ReadOnly: opts.ReadOnly, |
| 69 | + } |
| 70 | + if opts.SubPath != "" { |
| 71 | + volumeMount.SubPath = opts.SubPath |
| 72 | + } |
| 73 | + container.VolumeMounts = append(container.VolumeMounts, volumeMount) |
| 74 | + |
| 75 | + // Create volume based on type |
| 76 | + volume := corev1.Volume{ |
| 77 | + Name: opts.VolumeName, |
| 78 | + } |
| 79 | + |
| 80 | + switch opts.Type { |
| 81 | + case TypeConfigMap: |
| 82 | + volume.VolumeSource = corev1.VolumeSource{ |
| 83 | + ConfigMap: &corev1.ConfigMapVolumeSource{ |
| 84 | + LocalObjectReference: corev1.LocalObjectReference{ |
| 85 | + Name: opts.ResourceName, |
| 86 | + }, |
| 87 | + Optional: &opts.Optional, |
| 88 | + DefaultMode: &opts.DefaultMode, |
| 89 | + }, |
| 90 | + } |
| 91 | + case TypeSecret: |
| 92 | + volume.VolumeSource = corev1.VolumeSource{ |
| 93 | + Secret: &corev1.SecretVolumeSource{ |
| 94 | + SecretName: opts.ResourceName, |
| 95 | + Optional: &opts.Optional, |
| 96 | + DefaultMode: &opts.DefaultMode, |
| 97 | + }, |
| 98 | + } |
| 99 | + case TypeHostPath: |
| 100 | + volume.VolumeSource = corev1.VolumeSource{ |
| 101 | + HostPath: &corev1.HostPathVolumeSource{ |
| 102 | + Path: opts.HostPath, |
| 103 | + Type: opts.HostPathType, |
| 104 | + }, |
| 105 | + } |
| 106 | + case TypeEmptyDir: |
| 107 | + emptyDirSource := &corev1.EmptyDirVolumeSource{} |
| 108 | + if opts.Medium != "" { |
| 109 | + emptyDirSource.Medium = opts.Medium |
| 110 | + } |
| 111 | + if opts.SizeLimit != nil { |
| 112 | + emptyDirSource.SizeLimit = opts.SizeLimit |
| 113 | + } |
| 114 | + volume.VolumeSource = corev1.VolumeSource{ |
| 115 | + EmptyDir: emptyDirSource, |
| 116 | + } |
| 117 | + default: |
| 118 | + klog.InfoS("unsupported volume type, skipping", "type", opts.Type) |
| 119 | + } |
| 120 | + podSpec.Volumes = append(podSpec.Volumes, volume) |
| 121 | +} |
| 122 | + |
| 123 | +// AddHostPath adds a HostPath volume with the specified path and type |
| 124 | +func AddHostPath(podSpec *corev1.PodSpec, container *corev1.Container, volumeName, mountPath, hostPath string, hostPathType *corev1.HostPathType, readOnly bool) { |
| 125 | + Add(podSpec, container, Options{ |
| 126 | + VolumeName: volumeName, |
| 127 | + MountPath: mountPath, |
| 128 | + ReadOnly: readOnly, |
| 129 | + Type: TypeHostPath, |
| 130 | + HostPath: hostPath, |
| 131 | + HostPathType: hostPathType, |
| 132 | + }) |
| 133 | +} |
| 134 | + |
| 135 | +// AddEmptyDir adds an EmptyDir volume with the specified options |
| 136 | +func AddEmptyDir(podSpec *corev1.PodSpec, container *corev1.Container, volumeName, mountPath string, medium corev1.StorageMedium, sizeLimit *resource.Quantity) { |
| 137 | + Add(podSpec, container, Options{ |
| 138 | + VolumeName: volumeName, |
| 139 | + MountPath: mountPath, |
| 140 | + Type: TypeEmptyDir, |
| 141 | + Medium: medium, |
| 142 | + SizeLimit: sizeLimit, |
| 143 | + }) |
| 144 | +} |
| 145 | + |
| 146 | +// AddSecret adds a Secret volume with the specified options |
| 147 | +func AddSecret(podSpec *corev1.PodSpec, container *corev1.Container, volumeName, mountPath, secretName string, defaultMode int32, optional bool, readOnly bool) { |
| 148 | + Add(podSpec, container, Options{ |
| 149 | + VolumeName: volumeName, |
| 150 | + MountPath: mountPath, |
| 151 | + ReadOnly: readOnly, |
| 152 | + Type: TypeSecret, |
| 153 | + ResourceName: secretName, |
| 154 | + DefaultMode: defaultMode, |
| 155 | + Optional: optional, |
| 156 | + }) |
| 157 | +} |
| 158 | + |
| 159 | +// AddConfigMap adds a ConfigMap volume with the specified options |
| 160 | +func AddConfigMap(podSpec *corev1.PodSpec, container *corev1.Container, volumeName, mountPath, configMapName string, defaultMode int32, optional bool, readOnly bool) { |
| 161 | + Add(podSpec, container, Options{ |
| 162 | + VolumeName: volumeName, |
| 163 | + MountPath: mountPath, |
| 164 | + ReadOnly: readOnly, |
| 165 | + Type: TypeConfigMap, |
| 166 | + ResourceName: configMapName, |
| 167 | + DefaultMode: defaultMode, |
| 168 | + Optional: optional, |
| 169 | + }) |
| 170 | +} |
| 171 | + |
| 172 | +// AddMemoryVolume adds an EmptyDir volume with memory storage |
| 173 | +func AddMemoryVolume(podSpec *corev1.PodSpec, container *corev1.Container, volumeName, mountPath string, sizeMiB int64) { |
| 174 | + AddEmptyDir(podSpec, container, volumeName, mountPath, corev1.StorageMediumMemory, resource.NewQuantity(sizeMiB, resource.BinarySI)) |
| 175 | +} |
0 commit comments