From 83d1d2fc28f0dfc7edc6b41a496bc674bec8fcca Mon Sep 17 00:00:00 2001 From: Dan Prince Date: Mon, 14 Oct 2024 17:06:17 -0400 Subject: [PATCH 1/3] A custom slimmed down version of VolumeSource Slim down VolumeSource by removing deprecated and "removed" fields from the struct. This will slim down our nested use of ExtraMounts Also, adds ConvertVolumeSource function to the storage module which converts from VolumeSource to a corev1.VolumeSource --- modules/storage/storage.go | 93 ++++++++++++++++- modules/storage/zz_generated.deepcopy.go | 122 ++++++++++++++++++++++- 2 files changed, 211 insertions(+), 4 deletions(-) diff --git a/modules/storage/storage.go b/modules/storage/storage.go index aeee8922..e5ae8ba7 100644 --- a/modules/storage/storage.go +++ b/modules/storage/storage.go @@ -13,6 +13,7 @@ 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 @@ -40,6 +41,74 @@ 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"` + // photonPersistentDisk represents a PhotonController persistent disk attached and mounted on kubelets host machine + PhotonPersistentDisk *corev1.PhotonPersistentDiskVolumeSource `json:"photonPersistentDisk,omitempty" protobuf:"bytes,23,opt,name=photonPersistentDisk"` + // projected items for all in one resources secrets, configmaps, and downward API + Projected *corev1.ProjectedVolumeSource `json:"projected,omitempty" protobuf:"bytes,26,opt,name=projected"` + // 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"` + // ephemeral represents a volume that is handled by a cluster storage driver. + // The volume's lifecycle is tied to the pod that defines it - it will be created before the pod starts, + // and deleted when the pod is removed. + // + // +optional + Ephemeral *corev1.EphemeralVolumeSource `json:"ephemeral,omitempty" protobuf:"bytes,29,opt,name=ephemeral"` +} + +// 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 { @@ -50,7 +119,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"` } @@ -82,3 +151,25 @@ func (v *VolMounts) Propagate(svc []PropagationType) []VolMounts { return vl } + +// ConvertVolumeSource function to convert from a VolumeSource to a corev1.VolumeSource +func ConvertVolumeSource(v *VolumeSource) corev1.VolumeSource { + return corev1.VolumeSource{ + HostPath: v.HostPath, + EmptyDir: v.EmptyDir, + Secret: v.Secret, + NFS: v.NFS, + ISCSI: v.ISCSI, + PersistentVolumeClaim: v.PersistentVolumeClaim, + CephFS: v.CephFS, + DownwardAPI: v.DownwardAPI, + FC: v.FC, + ConfigMap: v.ConfigMap, + PhotonPersistentDisk: v.PhotonPersistentDisk, + Projected: v.Projected, + ScaleIO: v.ScaleIO, + StorageOS: v.StorageOS, + CSI: v.CSI, + Ephemeral: v.Ephemeral, + } +} diff --git a/modules/storage/zz_generated.deepcopy.go b/modules/storage/zz_generated.deepcopy.go index 044024f0..06834f77 100644 --- a/modules/storage/zz_generated.deepcopy.go +++ b/modules/storage/zz_generated.deepcopy.go @@ -2,7 +2,7 @@ // +build !ignore_autogenerated /* -Copyright 2022. + Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -28,9 +28,14 @@ import ( // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *VolMounts) DeepCopyInto(out *VolMounts) { *out = *in + if in.Propagation != nil { + in, out := &in.Propagation, &out.Propagation + *out = make([]PropagationType, len(*in)) + copy(*out, *in) + } if in.Volumes != nil { in, out := &in.Volumes, &out.Volumes - *out = make([]v1.Volume, len(*in)) + *out = make([]Volume, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } @@ -44,7 +49,7 @@ func (in *VolMounts) DeepCopyInto(out *VolMounts) { } } -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CinderVolMounts. +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new VolMounts. func (in *VolMounts) DeepCopy() *VolMounts { if in == nil { return nil @@ -53,3 +58,114 @@ func (in *VolMounts) DeepCopy() *VolMounts { in.DeepCopyInto(out) return out } + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *Volume) DeepCopyInto(out *Volume) { + *out = *in + in.VolumeSource.DeepCopyInto(&out.VolumeSource) +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Volume. +func (in *Volume) DeepCopy() *Volume { + if in == nil { + return nil + } + out := new(Volume) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *VolumeSource) DeepCopyInto(out *VolumeSource) { + *out = *in + if in.HostPath != nil { + in, out := &in.HostPath, &out.HostPath + *out = new(v1.HostPathVolumeSource) + (*in).DeepCopyInto(*out) + } + if in.EmptyDir != nil { + in, out := &in.EmptyDir, &out.EmptyDir + *out = new(v1.EmptyDirVolumeSource) + (*in).DeepCopyInto(*out) + } + if in.Secret != nil { + in, out := &in.Secret, &out.Secret + *out = new(v1.SecretVolumeSource) + (*in).DeepCopyInto(*out) + } + if in.NFS != nil { + in, out := &in.NFS, &out.NFS + *out = new(v1.NFSVolumeSource) + **out = **in + } + if in.ISCSI != nil { + in, out := &in.ISCSI, &out.ISCSI + *out = new(v1.ISCSIVolumeSource) + (*in).DeepCopyInto(*out) + } + if in.PersistentVolumeClaim != nil { + in, out := &in.PersistentVolumeClaim, &out.PersistentVolumeClaim + *out = new(v1.PersistentVolumeClaimVolumeSource) + **out = **in + } + if in.CephFS != nil { + in, out := &in.CephFS, &out.CephFS + *out = new(v1.CephFSVolumeSource) + (*in).DeepCopyInto(*out) + } + if in.DownwardAPI != nil { + in, out := &in.DownwardAPI, &out.DownwardAPI + *out = new(v1.DownwardAPIVolumeSource) + (*in).DeepCopyInto(*out) + } + if in.FC != nil { + in, out := &in.FC, &out.FC + *out = new(v1.FCVolumeSource) + (*in).DeepCopyInto(*out) + } + if in.ConfigMap != nil { + in, out := &in.ConfigMap, &out.ConfigMap + *out = new(v1.ConfigMapVolumeSource) + (*in).DeepCopyInto(*out) + } + if in.PhotonPersistentDisk != nil { + in, out := &in.PhotonPersistentDisk, &out.PhotonPersistentDisk + *out = new(v1.PhotonPersistentDiskVolumeSource) + **out = **in + } + if in.Projected != nil { + in, out := &in.Projected, &out.Projected + *out = new(v1.ProjectedVolumeSource) + (*in).DeepCopyInto(*out) + } + if in.ScaleIO != nil { + in, out := &in.ScaleIO, &out.ScaleIO + *out = new(v1.ScaleIOVolumeSource) + (*in).DeepCopyInto(*out) + } + if in.StorageOS != nil { + in, out := &in.StorageOS, &out.StorageOS + *out = new(v1.StorageOSVolumeSource) + (*in).DeepCopyInto(*out) + } + if in.CSI != nil { + in, out := &in.CSI, &out.CSI + *out = new(v1.CSIVolumeSource) + (*in).DeepCopyInto(*out) + } + if in.Ephemeral != nil { + in, out := &in.Ephemeral, &out.Ephemeral + *out = new(v1.EphemeralVolumeSource) + (*in).DeepCopyInto(*out) + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new VolumeSource. +func (in *VolumeSource) DeepCopy() *VolumeSource { + if in == nil { + return nil + } + out := new(VolumeSource) + in.DeepCopyInto(out) + return out +} From 203160d2fceffa02b5b8fa08036409a11c4e2c84 Mon Sep 17 00:00:00 2001 From: Dan Prince Date: Tue, 15 Oct 2024 21:44:47 -0400 Subject: [PATCH 2/3] Drop more VolSource Probably not likely we'll be using these: PhotonPersistentDisk Projected Ephemeral --- modules/storage/storage.go | 13 ------------- modules/storage/zz_generated.deepcopy.go | 15 --------------- 2 files changed, 28 deletions(-) diff --git a/modules/storage/storage.go b/modules/storage/storage.go index e5ae8ba7..6e8f74ee 100644 --- a/modules/storage/storage.go +++ b/modules/storage/storage.go @@ -78,10 +78,6 @@ type VolumeSource struct { // configMap represents a configMap that should populate this volume // +optional ConfigMap *corev1.ConfigMapVolumeSource `json:"configMap,omitempty" protobuf:"bytes,19,opt,name=configMap"` - // photonPersistentDisk represents a PhotonController persistent disk attached and mounted on kubelets host machine - PhotonPersistentDisk *corev1.PhotonPersistentDiskVolumeSource `json:"photonPersistentDisk,omitempty" protobuf:"bytes,23,opt,name=photonPersistentDisk"` - // projected items for all in one resources secrets, configmaps, and downward API - Projected *corev1.ProjectedVolumeSource `json:"projected,omitempty" protobuf:"bytes,26,opt,name=projected"` // 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"` @@ -91,12 +87,6 @@ type VolumeSource struct { // 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"` - // ephemeral represents a volume that is handled by a cluster storage driver. - // The volume's lifecycle is tied to the pod that defines it - it will be created before the pod starts, - // and deleted when the pod is removed. - // - // +optional - Ephemeral *corev1.EphemeralVolumeSource `json:"ephemeral,omitempty" protobuf:"bytes,29,opt,name=ephemeral"` } // Volume our slimmed down version of Volume @@ -165,11 +155,8 @@ func ConvertVolumeSource(v *VolumeSource) corev1.VolumeSource { DownwardAPI: v.DownwardAPI, FC: v.FC, ConfigMap: v.ConfigMap, - PhotonPersistentDisk: v.PhotonPersistentDisk, - Projected: v.Projected, ScaleIO: v.ScaleIO, StorageOS: v.StorageOS, CSI: v.CSI, - Ephemeral: v.Ephemeral, } } diff --git a/modules/storage/zz_generated.deepcopy.go b/modules/storage/zz_generated.deepcopy.go index 06834f77..59603de0 100644 --- a/modules/storage/zz_generated.deepcopy.go +++ b/modules/storage/zz_generated.deepcopy.go @@ -128,16 +128,6 @@ func (in *VolumeSource) DeepCopyInto(out *VolumeSource) { *out = new(v1.ConfigMapVolumeSource) (*in).DeepCopyInto(*out) } - if in.PhotonPersistentDisk != nil { - in, out := &in.PhotonPersistentDisk, &out.PhotonPersistentDisk - *out = new(v1.PhotonPersistentDiskVolumeSource) - **out = **in - } - if in.Projected != nil { - in, out := &in.Projected, &out.Projected - *out = new(v1.ProjectedVolumeSource) - (*in).DeepCopyInto(*out) - } if in.ScaleIO != nil { in, out := &in.ScaleIO, &out.ScaleIO *out = new(v1.ScaleIOVolumeSource) @@ -153,11 +143,6 @@ func (in *VolumeSource) DeepCopyInto(out *VolumeSource) { *out = new(v1.CSIVolumeSource) (*in).DeepCopyInto(*out) } - if in.Ephemeral != nil { - in, out := &in.Ephemeral, &out.Ephemeral - *out = new(v1.EphemeralVolumeSource) - (*in).DeepCopyInto(*out) - } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new VolumeSource. From 061cc8a23ceb920fe939be82740e7f371757dbc1 Mon Sep 17 00:00:00 2001 From: Dan Prince Date: Fri, 25 Oct 2024 10:31:07 -0400 Subject: [PATCH 3/3] Change ConvertVolume to ToCoreVolumeSource --- modules/storage/storage.go | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/modules/storage/storage.go b/modules/storage/storage.go index 6e8f74ee..8ee12b5f 100644 --- a/modules/storage/storage.go +++ b/modules/storage/storage.go @@ -18,6 +18,9 @@ limitations under the License. package storage import ( + "encoding/json" + "fmt" + corev1 "k8s.io/api/core/v1" ) @@ -142,21 +145,19 @@ func (v *VolMounts) Propagate(svc []PropagationType) []VolMounts { return vl } -// ConvertVolumeSource function to convert from a VolumeSource to a corev1.VolumeSource -func ConvertVolumeSource(v *VolumeSource) corev1.VolumeSource { - return corev1.VolumeSource{ - HostPath: v.HostPath, - EmptyDir: v.EmptyDir, - Secret: v.Secret, - NFS: v.NFS, - ISCSI: v.ISCSI, - PersistentVolumeClaim: v.PersistentVolumeClaim, - CephFS: v.CephFS, - DownwardAPI: v.DownwardAPI, - FC: v.FC, - ConfigMap: v.ConfigMap, - ScaleIO: v.ScaleIO, - StorageOS: v.StorageOS, - CSI: v.CSI, +// 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 }