Skip to content

Commit 148b0a0

Browse files
api: add v1beta1 conversion and types for driver
Signed-off-by: Niraj Yadav <niryadav@redhat.com>
1 parent 066edb7 commit 148b0a0

File tree

19 files changed

+9402
-0
lines changed

19 files changed

+9402
-0
lines changed

PROJECT

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,17 @@ resources:
9191
spoke:
9292
- v1alpha1
9393
webhookVersion: v1
94+
- api:
95+
crdVersion: v1
96+
namespaced: true
97+
domain: ceph.io
98+
group: csi
99+
kind: Driver
100+
path: github.com/ceph/ceph-csi-operator/api/csi/v1beta1
101+
version: v1beta1
102+
webhooks:
103+
conversion: true
104+
spoke:
105+
- v1alpha1
106+
webhookVersion: v1
94107
version: "3"
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
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+
if spec.ControllerPlugin != nil {
48+
ret.ControllerPlugin = &csiv1beta1.ControllerPluginSpec{
49+
HostNetwork: spec.ControllerPlugin.HostNetwork,
50+
PodCommonSpec: csiv1beta1.PodCommonSpec{
51+
ServiceAccountName: spec.ControllerPlugin.PodCommonSpec.ServiceAccountName,
52+
PrioritylClassName: spec.ControllerPlugin.PodCommonSpec.PrioritylClassName,
53+
Labels: spec.ControllerPlugin.PodCommonSpec.Labels,
54+
Annotations: spec.ControllerPlugin.PodCommonSpec.Annotations,
55+
Affinity: spec.ControllerPlugin.PodCommonSpec.Affinity,
56+
Tolerations: spec.ControllerPlugin.PodCommonSpec.Tolerations,
57+
Volumes: convertVolumeSpec(spec.ControllerPlugin.PodCommonSpec.Volumes, func(v VolumeSpec) csiv1beta1.VolumeSpec {
58+
return csiv1beta1.VolumeSpec{
59+
Volume: v.Volume,
60+
Mount: v.Mount,
61+
}
62+
}),
63+
ImagePullPolicy: spec.ControllerPlugin.PodCommonSpec.ImagePullPolicy,
64+
},
65+
DeploymentStrategy: spec.ControllerPlugin.DeploymentStrategy,
66+
Replicas: spec.ControllerPlugin.Replicas,
67+
Resources: csiv1beta1.ControllerPluginResourcesSpec(spec.ControllerPlugin.Resources),
68+
Privileged: spec.ControllerPlugin.Privileged,
69+
}
70+
}
71+
ret.DeployCsiAddons = spec.DeployCsiAddons
72+
ret.EnableMetadata = spec.EnableMetadata
73+
if spec.Encryption != nil {
74+
ret.Encryption = (*csiv1beta1.EncryptionSpec)(spec.Encryption)
75+
}
76+
ret.FsGroupPolicy = spec.FsGroupPolicy
77+
ret.FuseMountOptions = spec.FuseMountOptions
78+
ret.GRpcTimeout = spec.GRpcTimeout
79+
ret.GenerateOMapInfo = spec.GenerateOMapInfo
80+
ret.ImageSet = spec.ImageSet
81+
ret.KernelMountOptions = spec.KernelMountOptions
82+
if spec.LeaderElection != nil {
83+
ret.LeaderElection = (*csiv1beta1.LeaderElectionSpec)(spec.LeaderElection)
84+
}
85+
if spec.Liveness != nil {
86+
ret.Liveness = (*csiv1beta1.LivenessSpec)(spec.Liveness)
87+
}
88+
if spec.Log != nil {
89+
ret.Log = &csiv1beta1.LogSpec{
90+
Verbosity: spec.Log.Verbosity,
91+
}
92+
if spec.Log.Rotation != nil {
93+
ret.Log.Rotation = &csiv1beta1.LogRotationSpec{
94+
MaxFiles: spec.Log.Rotation.MaxFiles,
95+
MaxLogSize: spec.Log.Rotation.MaxLogSize,
96+
Periodicity: csiv1beta1.PeriodicityType(spec.Log.Rotation.Periodicity),
97+
LogHostPath: spec.Log.Rotation.LogHostPath,
98+
}
99+
}
100+
}
101+
if spec.NodePlugin != nil {
102+
ret.NodePlugin = &csiv1beta1.NodePluginSpec{
103+
PodCommonSpec: csiv1beta1.PodCommonSpec{
104+
ServiceAccountName: spec.NodePlugin.PodCommonSpec.ServiceAccountName,
105+
PrioritylClassName: spec.NodePlugin.PodCommonSpec.PrioritylClassName,
106+
Labels: spec.NodePlugin.PodCommonSpec.Labels,
107+
Annotations: spec.NodePlugin.PodCommonSpec.Annotations,
108+
Affinity: spec.NodePlugin.PodCommonSpec.Affinity,
109+
Tolerations: spec.NodePlugin.PodCommonSpec.Tolerations,
110+
Volumes: convertVolumeSpec(spec.NodePlugin.PodCommonSpec.Volumes, func(v VolumeSpec) csiv1beta1.VolumeSpec {
111+
return csiv1beta1.VolumeSpec{
112+
Volume: v.Volume,
113+
Mount: v.Mount,
114+
}
115+
}),
116+
ImagePullPolicy: spec.NodePlugin.PodCommonSpec.ImagePullPolicy,
117+
},
118+
}
119+
}
120+
ret.SnapshotPolicy = csiv1beta1.SnapshotPolicyType(spec.SnapshotPolicy)
121+
122+
return ret
123+
}
124+
125+
func ConvertBetaDriverToAlpha(spec csiv1beta1.DriverSpec) DriverSpec {
126+
ret := DriverSpec{}
127+
128+
ret.AttachRequired = spec.AttachRequired
129+
ret.CephFsClientType = CephFsClientType(spec.CephFsClientType)
130+
ret.ClusterName = spec.ClusterName
131+
if spec.ControllerPlugin != nil {
132+
ret.ControllerPlugin = &ControllerPluginSpec{
133+
HostNetwork: spec.ControllerPlugin.HostNetwork,
134+
PodCommonSpec: PodCommonSpec{
135+
ServiceAccountName: spec.NodePlugin.PodCommonSpec.ServiceAccountName,
136+
PrioritylClassName: spec.NodePlugin.PodCommonSpec.PrioritylClassName,
137+
Labels: spec.NodePlugin.PodCommonSpec.Labels,
138+
Annotations: spec.NodePlugin.PodCommonSpec.Annotations,
139+
Affinity: spec.NodePlugin.PodCommonSpec.Affinity,
140+
Tolerations: spec.NodePlugin.PodCommonSpec.Tolerations,
141+
Volumes: convertVolumeSpec(spec.NodePlugin.PodCommonSpec.Volumes, func(v csiv1beta1.VolumeSpec) VolumeSpec {
142+
return VolumeSpec{
143+
Volume: v.Volume,
144+
Mount: v.Mount,
145+
}
146+
}),
147+
ImagePullPolicy: spec.NodePlugin.PodCommonSpec.ImagePullPolicy,
148+
},
149+
DeploymentStrategy: spec.ControllerPlugin.DeploymentStrategy,
150+
Replicas: spec.ControllerPlugin.Replicas,
151+
Resources: ControllerPluginResourcesSpec(spec.ControllerPlugin.Resources),
152+
Privileged: spec.ControllerPlugin.Privileged,
153+
}
154+
}
155+
ret.DeployCsiAddons = spec.DeployCsiAddons
156+
ret.EnableMetadata = spec.EnableMetadata
157+
if spec.Encryption != nil {
158+
ret.Encryption = (*EncryptionSpec)(spec.Encryption)
159+
}
160+
ret.FsGroupPolicy = spec.FsGroupPolicy
161+
ret.FuseMountOptions = spec.FuseMountOptions
162+
ret.GRpcTimeout = spec.GRpcTimeout
163+
ret.GenerateOMapInfo = spec.GenerateOMapInfo
164+
ret.ImageSet = spec.ImageSet
165+
ret.KernelMountOptions = spec.KernelMountOptions
166+
if spec.LeaderElection != nil {
167+
ret.LeaderElection = (*LeaderElectionSpec)(spec.LeaderElection)
168+
}
169+
if spec.Liveness != nil {
170+
ret.Liveness = (*LivenessSpec)(spec.Liveness)
171+
}
172+
if spec.Log != nil {
173+
ret.Log = &LogSpec{
174+
Verbosity: spec.Log.Verbosity,
175+
}
176+
if spec.Log.Rotation != nil {
177+
ret.Log.Rotation = &LogRotationSpec{
178+
MaxFiles: spec.Log.Rotation.MaxFiles,
179+
MaxLogSize: spec.Log.Rotation.MaxLogSize,
180+
Periodicity: PeriodicityType(spec.Log.Rotation.Periodicity),
181+
LogHostPath: spec.Log.Rotation.LogHostPath,
182+
}
183+
}
184+
}
185+
if spec.NodePlugin != nil {
186+
ret.NodePlugin = &NodePluginSpec{
187+
PodCommonSpec: PodCommonSpec{
188+
ServiceAccountName: spec.NodePlugin.PodCommonSpec.ServiceAccountName,
189+
PrioritylClassName: spec.NodePlugin.PodCommonSpec.PrioritylClassName,
190+
Labels: spec.NodePlugin.PodCommonSpec.Labels,
191+
Annotations: spec.NodePlugin.PodCommonSpec.Annotations,
192+
Affinity: spec.NodePlugin.PodCommonSpec.Affinity,
193+
Tolerations: spec.NodePlugin.PodCommonSpec.Tolerations,
194+
Volumes: convertVolumeSpec(spec.NodePlugin.PodCommonSpec.Volumes, func(v csiv1beta1.VolumeSpec) VolumeSpec {
195+
return VolumeSpec{
196+
Volume: v.Volume,
197+
Mount: v.Mount,
198+
}
199+
}),
200+
ImagePullPolicy: spec.NodePlugin.PodCommonSpec.ImagePullPolicy,
201+
},
202+
}
203+
}
204+
ret.SnapshotPolicy = SnapshotPolicyType(spec.SnapshotPolicy)
205+
206+
return ret
207+
}
208+
209+
// ConvertTo converts this Driver (v1alpha1) to the Hub version (v1beta1).
210+
func (src *Driver) ConvertTo(dstRaw conversion.Hub) error {
211+
dst, ok := dstRaw.(*csiv1beta1.Driver)
212+
if !ok {
213+
return errors.New("convertto: failed to cast to v1beta1 Driver")
214+
}
215+
216+
log.Printf("ConvertTo: Converting Driver from Spoke version v1alpha1 to Hub version v1beta1;"+
217+
"source: %s/%s, target: %s/%s", src.Namespace, src.Name, dst.Namespace, dst.Name)
218+
219+
dst.ObjectMeta = src.ObjectMeta
220+
dst.Spec = ConvertAlphaDriverToBeta(src.Spec)
221+
222+
return nil
223+
}
224+
225+
// ConvertFrom converts the Hub version (v1beta1) to this Driver (v1alpha1).
226+
func (dst *Driver) ConvertFrom(srcRaw conversion.Hub) error {
227+
src, ok := srcRaw.(*csiv1beta1.Driver)
228+
if !ok {
229+
return errors.New("convertfrom: failed to cast to v1beta1 Driver")
230+
}
231+
232+
log.Printf("ConvertFrom: Converting Driver from Hub version v1beta1 to Spoke version v1alpha1;"+
233+
"source: %s/%s, target: %s/%s", src.Namespace, src.Name, dst.Namespace, dst.Name)
234+
235+
dst.ObjectMeta = src.ObjectMeta
236+
dst.Spec = ConvertBetaDriverToAlpha(src.Spec)
237+
238+
return nil
239+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 v1beta1
18+
19+
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
20+
21+
// Hub marks this type as a conversion hub.
22+
func (*Driver) Hub() {}

0 commit comments

Comments
 (0)