Skip to content

Commit

Permalink
Merge pull request #570 from dprince/custom_vol_source
Browse files Browse the repository at this point in the history
A custom slimmed down version of VolumeSource
  • Loading branch information
dprince authored Oct 25, 2024
2 parents 287ec67 + 061cc8a commit 30baa23
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 4 deletions.
81 changes: 80 additions & 1 deletion modules/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// +kubebuilder:object:generate:=true

package storage

import (
"encoding/json"
"fmt"

corev1 "k8s.io/api/core/v1"
)

Expand All @@ -40,6 +44,64 @@ const (
Compute PropagationType = "Compute"
)

// VolumeSource our slimmed down version of the VolumeSource struct with deprecated and "removed" fields removed to save space
type VolumeSource struct {
HostPath *corev1.HostPathVolumeSource `json:"hostPath,omitempty" protobuf:"bytes,1,opt,name=hostPath"`
// emptyDir represents a temporary directory that shares a pod's lifetime.
// More info: https://kubernetes.io/docs/concepts/storage/volumes#emptydir
// +optional
EmptyDir *corev1.EmptyDirVolumeSource `json:"emptyDir,omitempty" protobuf:"bytes,2,opt,name=emptyDir"`
// secret represents a secret that should populate this volume.
// More info: https://kubernetes.io/docs/concepts/storage/volumes#secret
// +optional
Secret *corev1.SecretVolumeSource `json:"secret,omitempty" protobuf:"bytes,6,opt,name=secret"`
// nfs represents an NFS mount on the host that shares a pod's lifetime
// More info: https://kubernetes.io/docs/concepts/storage/volumes#nfs
// +optional
NFS *corev1.NFSVolumeSource `json:"nfs,omitempty" protobuf:"bytes,7,opt,name=nfs"`
// iscsi represents an ISCSI Disk resource that is attached to a
// kubelet's host machine and then exposed to the pod.
// More info: https://examples.k8s.io/volumes/iscsi/README.md
// +optional
ISCSI *corev1.ISCSIVolumeSource `json:"iscsi,omitempty" protobuf:"bytes,8,opt,name=iscsi"`
// persistentVolumeClaimVolumeSource represents a reference to a
// PersistentVolumeClaim in the same namespace.
// More info: https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistentvolumeclaims
// +optional
PersistentVolumeClaim *corev1.PersistentVolumeClaimVolumeSource `json:"persistentVolumeClaim,omitempty" protobuf:"bytes,10,opt,name=persistentVolumeClaim"`
// cephFS represents a Ceph FS mount on the host that shares a pod's lifetime
// +optional
CephFS *corev1.CephFSVolumeSource `json:"cephfs,omitempty" protobuf:"bytes,14,opt,name=cephfs"`
// downwardAPI represents downward API about the pod that should populate this volume
// +optional
DownwardAPI *corev1.DownwardAPIVolumeSource `json:"downwardAPI,omitempty" protobuf:"bytes,16,opt,name=downwardAPI"`
// fc represents a Fibre Channel resource that is attached to a kubelet's host machine and then exposed to the pod.
// +optional
FC *corev1.FCVolumeSource `json:"fc,omitempty" protobuf:"bytes,17,opt,name=fc"`
// configMap represents a configMap that should populate this volume
// +optional
ConfigMap *corev1.ConfigMapVolumeSource `json:"configMap,omitempty" protobuf:"bytes,19,opt,name=configMap"`
// scaleIO represents a ScaleIO persistent volume attached and mounted on Kubernetes nodes.
// +optional
ScaleIO *corev1.ScaleIOVolumeSource `json:"scaleIO,omitempty" protobuf:"bytes,25,opt,name=scaleIO"`
// storageOS represents a StorageOS volume attached and mounted on Kubernetes nodes.
// +optional
StorageOS *corev1.StorageOSVolumeSource `json:"storageos,omitempty" protobuf:"bytes,27,opt,name=storageos"`
// csi (Container Storage Interface) represents ephemeral storage that is handled by certain external CSI drivers (Beta feature).
// +optional
CSI *corev1.CSIVolumeSource `json:"csi,omitempty" protobuf:"bytes,28,opt,name=csi"`
}

// Volume our slimmed down version of Volume
type Volume struct {
// +kubebuilder:validation:Required
// Name of the volume
Name string `json:"name"`
// +kubebuilder:validation:Required
// VolumeSource defines the source of a volume to be mounted
VolumeSource VolumeSource `json:"volumeSource"`
}

// VolMounts is the data structure used to expose Volumes and Mounts that can
// be added to a pod according to the defined Propagation policy
type VolMounts struct {
Expand All @@ -50,7 +112,7 @@ type VolMounts struct {
// +kubebuilder:validation:Optional
ExtraVolType ExtraVolType `json:"extraVolType,omitempty"`
// +kubebuilder:validation:Required
Volumes []corev1.Volume `json:"volumes"`
Volumes []Volume `json:"volumes"`
// +kubebuilder:validation:Required
Mounts []corev1.VolumeMount `json:"mounts"`
}
Expand Down Expand Up @@ -82,3 +144,20 @@ func (v *VolMounts) Propagate(svc []PropagationType) []VolMounts {

return vl
}

// ToCoreVolumeSource - convert VolumeSource to corev1.VolumeSource
func (s *VolumeSource) ToCoreVolumeSource() (*corev1.VolumeSource, error) {
coreVolumeSource := &corev1.VolumeSource{}

coreVolumeBytes, err := json.Marshal(s)
if err != nil {
return nil, fmt.Errorf("error marshalling VolumeSource: %w", err)
}

err = json.Unmarshal(coreVolumeBytes, coreVolumeSource)
if err != nil {
return nil, fmt.Errorf("error unmarshalling VolumeSource: %w", err)
}

return coreVolumeSource, nil
}
107 changes: 104 additions & 3 deletions modules/storage/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 30baa23

Please sign in to comment.