|
| 1 | +/* |
| 2 | +Copyright 2024. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package v1alpha1 |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "log" |
| 22 | + |
| 23 | + "sigs.k8s.io/controller-runtime/pkg/conversion" |
| 24 | + |
| 25 | + csiv1beta1 "github.com/ceph/ceph-csi-operator/api/csi/v1beta1" |
| 26 | +) |
| 27 | + |
| 28 | +func convertVolumeSpec[Src, Dest any](elems []Src, convertor func(Src) Dest) []Dest { |
| 29 | + if elems == nil { |
| 30 | + return nil |
| 31 | + } |
| 32 | + |
| 33 | + dst := make([]Dest, len(elems)) |
| 34 | + for i, v := range elems { |
| 35 | + dst[i] = convertor(v) |
| 36 | + } |
| 37 | + |
| 38 | + return dst |
| 39 | +} |
| 40 | + |
| 41 | +func ConvertAlphaDriverToBeta(spec DriverSpec) csiv1beta1.DriverSpec { |
| 42 | + ret := csiv1beta1.DriverSpec{} |
| 43 | + |
| 44 | + ret.AttachRequired = spec.AttachRequired |
| 45 | + ret.CephFsClientType = csiv1beta1.CephFsClientType(spec.CephFsClientType) |
| 46 | + ret.ClusterName = spec.ClusterName |
| 47 | + ret.ControllerPlugin = &csiv1beta1.ControllerPluginSpec{ |
| 48 | + HostNetwork: spec.ControllerPlugin.HostNetwork, |
| 49 | + PodCommonSpec: csiv1beta1.PodCommonSpec{ |
| 50 | + ServiceAccountName: spec.NodePlugin.PodCommonSpec.ServiceAccountName, |
| 51 | + PrioritylClassName: spec.NodePlugin.PodCommonSpec.PrioritylClassName, |
| 52 | + Labels: spec.NodePlugin.PodCommonSpec.Labels, |
| 53 | + Annotations: spec.NodePlugin.PodCommonSpec.Annotations, |
| 54 | + Affinity: spec.NodePlugin.PodCommonSpec.Affinity, |
| 55 | + Tolerations: spec.NodePlugin.PodCommonSpec.Tolerations, |
| 56 | + Volumes: convertVolumeSpec(spec.NodePlugin.PodCommonSpec.Volumes, func(v VolumeSpec) csiv1beta1.VolumeSpec { |
| 57 | + return csiv1beta1.VolumeSpec{ |
| 58 | + Volume: v.Volume, |
| 59 | + Mount: v.Mount, |
| 60 | + } |
| 61 | + }), |
| 62 | + ImagePullPolicy: spec.NodePlugin.PodCommonSpec.ImagePullPolicy, |
| 63 | + }, |
| 64 | + DeploymentStrategy: spec.ControllerPlugin.DeploymentStrategy, |
| 65 | + Replicas: spec.ControllerPlugin.Replicas, |
| 66 | + Resources: csiv1beta1.ControllerPluginResourcesSpec(spec.ControllerPlugin.Resources), |
| 67 | + Privileged: spec.ControllerPlugin.Privileged, |
| 68 | + } |
| 69 | + ret.DeployCsiAddons = spec.DeployCsiAddons |
| 70 | + ret.EnableMetadata = spec.EnableMetadata |
| 71 | + ret.Encryption = (*csiv1beta1.EncryptionSpec)(spec.Encryption) |
| 72 | + ret.FsGroupPolicy = spec.FsGroupPolicy |
| 73 | + ret.FuseMountOptions = spec.FuseMountOptions |
| 74 | + ret.GRpcTimeout = spec.GRpcTimeout |
| 75 | + ret.GenerateOMapInfo = spec.GenerateOMapInfo |
| 76 | + ret.ImageSet = spec.ImageSet |
| 77 | + ret.KernelMountOptions = spec.KernelMountOptions |
| 78 | + ret.LeaderElection = (*csiv1beta1.LeaderElectionSpec)(spec.LeaderElection) |
| 79 | + ret.Liveness = (*csiv1beta1.LivenessSpec)(spec.Liveness) |
| 80 | + ret.Log = &csiv1beta1.LogSpec{ |
| 81 | + Verbosity: spec.Log.Verbosity, |
| 82 | + Rotation: &csiv1beta1.LogRotationSpec{ |
| 83 | + MaxFiles: spec.Log.Rotation.MaxFiles, |
| 84 | + MaxLogSize: spec.Log.Rotation.MaxLogSize, |
| 85 | + Periodicity: csiv1beta1.PeriodicityType(spec.Log.Rotation.Periodicity), |
| 86 | + LogHostPath: spec.Log.Rotation.LogHostPath, |
| 87 | + }, |
| 88 | + } |
| 89 | + ret.NodePlugin = &csiv1beta1.NodePluginSpec{ |
| 90 | + PodCommonSpec: csiv1beta1.PodCommonSpec{ |
| 91 | + ServiceAccountName: spec.NodePlugin.PodCommonSpec.ServiceAccountName, |
| 92 | + PrioritylClassName: spec.NodePlugin.PodCommonSpec.PrioritylClassName, |
| 93 | + Labels: spec.NodePlugin.PodCommonSpec.Labels, |
| 94 | + Annotations: spec.NodePlugin.PodCommonSpec.Annotations, |
| 95 | + Affinity: spec.NodePlugin.PodCommonSpec.Affinity, |
| 96 | + Tolerations: spec.NodePlugin.PodCommonSpec.Tolerations, |
| 97 | + Volumes: convertVolumeSpec(spec.NodePlugin.PodCommonSpec.Volumes, func(v VolumeSpec) csiv1beta1.VolumeSpec { |
| 98 | + return csiv1beta1.VolumeSpec{ |
| 99 | + Volume: v.Volume, |
| 100 | + Mount: v.Mount, |
| 101 | + } |
| 102 | + }), |
| 103 | + ImagePullPolicy: spec.NodePlugin.PodCommonSpec.ImagePullPolicy, |
| 104 | + }, |
| 105 | + } |
| 106 | + ret.SnapshotPolicy = csiv1beta1.SnapshotPolicyType(spec.SnapshotPolicy) |
| 107 | + |
| 108 | + return ret |
| 109 | +} |
| 110 | + |
| 111 | +func ConvertBetaDriverToAlpha(spec csiv1beta1.DriverSpec) DriverSpec { |
| 112 | + ret := DriverSpec{} |
| 113 | + |
| 114 | + ret.AttachRequired = spec.AttachRequired |
| 115 | + ret.CephFsClientType = CephFsClientType(spec.CephFsClientType) |
| 116 | + ret.ClusterName = spec.ClusterName |
| 117 | + ret.ControllerPlugin = &ControllerPluginSpec{ |
| 118 | + HostNetwork: spec.ControllerPlugin.HostNetwork, |
| 119 | + PodCommonSpec: PodCommonSpec{ |
| 120 | + ServiceAccountName: spec.NodePlugin.PodCommonSpec.ServiceAccountName, |
| 121 | + PrioritylClassName: spec.NodePlugin.PodCommonSpec.PrioritylClassName, |
| 122 | + Labels: spec.NodePlugin.PodCommonSpec.Labels, |
| 123 | + Annotations: spec.NodePlugin.PodCommonSpec.Annotations, |
| 124 | + Affinity: spec.NodePlugin.PodCommonSpec.Affinity, |
| 125 | + Tolerations: spec.NodePlugin.PodCommonSpec.Tolerations, |
| 126 | + Volumes: convertVolumeSpec(spec.NodePlugin.PodCommonSpec.Volumes, func(v csiv1beta1.VolumeSpec) VolumeSpec { |
| 127 | + return VolumeSpec{ |
| 128 | + Volume: v.Volume, |
| 129 | + Mount: v.Mount, |
| 130 | + } |
| 131 | + }), |
| 132 | + ImagePullPolicy: spec.NodePlugin.PodCommonSpec.ImagePullPolicy, |
| 133 | + }, |
| 134 | + DeploymentStrategy: spec.ControllerPlugin.DeploymentStrategy, |
| 135 | + Replicas: spec.ControllerPlugin.Replicas, |
| 136 | + Resources: ControllerPluginResourcesSpec(spec.ControllerPlugin.Resources), |
| 137 | + Privileged: spec.ControllerPlugin.Privileged, |
| 138 | + } |
| 139 | + ret.DeployCsiAddons = spec.DeployCsiAddons |
| 140 | + ret.EnableMetadata = spec.EnableMetadata |
| 141 | + ret.Encryption = (*EncryptionSpec)(spec.Encryption) |
| 142 | + ret.FsGroupPolicy = spec.FsGroupPolicy |
| 143 | + ret.FuseMountOptions = spec.FuseMountOptions |
| 144 | + ret.GRpcTimeout = spec.GRpcTimeout |
| 145 | + ret.GenerateOMapInfo = spec.GenerateOMapInfo |
| 146 | + ret.ImageSet = spec.ImageSet |
| 147 | + ret.KernelMountOptions = spec.KernelMountOptions |
| 148 | + ret.LeaderElection = (*LeaderElectionSpec)(spec.LeaderElection) |
| 149 | + ret.Liveness = (*LivenessSpec)(spec.Liveness) |
| 150 | + ret.Log = &LogSpec{ |
| 151 | + Verbosity: spec.Log.Verbosity, |
| 152 | + Rotation: &LogRotationSpec{ |
| 153 | + MaxFiles: spec.Log.Rotation.MaxFiles, |
| 154 | + MaxLogSize: spec.Log.Rotation.MaxLogSize, |
| 155 | + Periodicity: PeriodicityType(spec.Log.Rotation.Periodicity), |
| 156 | + LogHostPath: spec.Log.Rotation.LogHostPath, |
| 157 | + }, |
| 158 | + } |
| 159 | + ret.NodePlugin = &NodePluginSpec{ |
| 160 | + PodCommonSpec: PodCommonSpec{ |
| 161 | + ServiceAccountName: spec.NodePlugin.PodCommonSpec.ServiceAccountName, |
| 162 | + PrioritylClassName: spec.NodePlugin.PodCommonSpec.PrioritylClassName, |
| 163 | + Labels: spec.NodePlugin.PodCommonSpec.Labels, |
| 164 | + Annotations: spec.NodePlugin.PodCommonSpec.Annotations, |
| 165 | + Affinity: spec.NodePlugin.PodCommonSpec.Affinity, |
| 166 | + Tolerations: spec.NodePlugin.PodCommonSpec.Tolerations, |
| 167 | + Volumes: convertVolumeSpec(spec.NodePlugin.PodCommonSpec.Volumes, func(v csiv1beta1.VolumeSpec) VolumeSpec { |
| 168 | + return VolumeSpec{ |
| 169 | + Volume: v.Volume, |
| 170 | + Mount: v.Mount, |
| 171 | + } |
| 172 | + }), |
| 173 | + ImagePullPolicy: spec.NodePlugin.PodCommonSpec.ImagePullPolicy, |
| 174 | + }, |
| 175 | + } |
| 176 | + ret.SnapshotPolicy = SnapshotPolicyType(spec.SnapshotPolicy) |
| 177 | + |
| 178 | + return ret |
| 179 | +} |
| 180 | + |
| 181 | +// ConvertTo converts this Driver (v1alpha1) to the Hub version (v1beta1). |
| 182 | +func (src *Driver) ConvertTo(dstRaw conversion.Hub) error { |
| 183 | + dst, ok := dstRaw.(*csiv1beta1.Driver) |
| 184 | + if !ok { |
| 185 | + return errors.New("convertto: failed to cast to v1beta1 Driver") |
| 186 | + } |
| 187 | + |
| 188 | + log.Printf("ConvertTo: Converting Driver from Spoke version v1alpha1 to Hub version v1beta1;"+ |
| 189 | + "source: %s/%s, target: %s/%s", src.Namespace, src.Name, dst.Namespace, dst.Name) |
| 190 | + |
| 191 | + dst.ObjectMeta = src.ObjectMeta |
| 192 | + dst.Spec = ConvertAlphaDriverToBeta(src.Spec) |
| 193 | + |
| 194 | + return nil |
| 195 | +} |
| 196 | + |
| 197 | +// ConvertFrom converts the Hub version (v1beta1) to this Driver (v1alpha1). |
| 198 | +func (dst *Driver) ConvertFrom(srcRaw conversion.Hub) error { |
| 199 | + src, ok := srcRaw.(*csiv1beta1.Driver) |
| 200 | + if !ok { |
| 201 | + return errors.New("convertfrom: failed to cast to v1beta1 Driver") |
| 202 | + } |
| 203 | + |
| 204 | + log.Printf("ConvertFrom: Converting Driver from Hub version v1beta1 to Spoke version v1alpha1;"+ |
| 205 | + "source: %s/%s, target: %s/%s", src.Namespace, src.Name, dst.Namespace, dst.Name) |
| 206 | + |
| 207 | + dst.ObjectMeta = src.ObjectMeta |
| 208 | + dst.Spec = ConvertBetaDriverToAlpha(src.Spec) |
| 209 | + |
| 210 | + return nil |
| 211 | +} |
0 commit comments