Skip to content

Commit 066edb7

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

19 files changed

+852
-0
lines changed

PROJECT

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,17 @@ resources:
7878
spoke:
7979
- v1alpha1
8080
webhookVersion: v1
81+
- api:
82+
crdVersion: v1
83+
namespaced: true
84+
domain: ceph.io
85+
group: csi
86+
kind: ClientProfileMapping
87+
path: github.com/ceph/ceph-csi-operator/api/csi/v1beta1
88+
version: v1beta1
89+
webhooks:
90+
conversion: true
91+
spoke:
92+
- v1alpha1
93+
webhookVersion: v1
8194
version: "3"
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
"slices"
23+
24+
"sigs.k8s.io/controller-runtime/pkg/conversion"
25+
26+
csiv1beta1 "github.com/ceph/ceph-csi-operator/api/csi/v1beta1"
27+
)
28+
29+
// convertBlockPoolIdPair is a generic convertor function to convert between
30+
// different versions of BlockPoolIdPair types
31+
func convertBlockPoolIdPair[From ~[]string, To ~[]string](mappings []From) []To {
32+
ret := make([]To, len(mappings))
33+
34+
for i, pair := range mappings {
35+
ret[i] = To(slices.Clone(pair))
36+
}
37+
38+
return ret
39+
}
40+
41+
// ConvertTo converts this ClientProfileMapping (v1alpha1) to the Hub version (v1beta1).
42+
func (src *ClientProfileMapping) ConvertTo(dstRaw conversion.Hub) error {
43+
dst, ok := dstRaw.(*csiv1beta1.ClientProfileMapping)
44+
if !ok {
45+
return errors.New("convertto: failed to cast to v1beta1 ClientProfileMapping")
46+
}
47+
48+
log.Printf("ConvertTo: Converting ClientProfileMapping from Spoke version v1alpha1 to Hub version v1beta1;"+
49+
"source: %s/%s, target: %s/%s", src.Namespace, src.Name, dst.Namespace, dst.Name)
50+
51+
dst.ObjectMeta = src.ObjectMeta
52+
53+
if len(src.Spec.Mappings) > 0 {
54+
dst.Spec.Mappings = make([]csiv1beta1.MappingsSpec, len(src.Spec.Mappings))
55+
56+
for i, mapping := range src.Spec.Mappings {
57+
dst.Spec.Mappings[i] = csiv1beta1.MappingsSpec{
58+
LocalClientProfile: mapping.LocalClientProfile,
59+
RemoteClientProfile: mapping.RemoteClientProfile,
60+
BlockPoolIdMapping: convertBlockPoolIdPair[BlockPoolIdPair, csiv1beta1.BlockPoolIdPair](mapping.BlockPoolIdMapping),
61+
}
62+
}
63+
}
64+
65+
return nil
66+
}
67+
68+
// ConvertFrom converts the Hub version (v1beta1) to this ClientProfileMapping (v1alpha1).
69+
func (dst *ClientProfileMapping) ConvertFrom(srcRaw conversion.Hub) error {
70+
src, ok := srcRaw.(*csiv1beta1.ClientProfileMapping)
71+
if !ok {
72+
return errors.New("convertfrom: failed to cast to v1beta1 ClientProfileMapping")
73+
}
74+
75+
log.Printf("ConvertFrom: Converting ClientProfileMapping from Hub version v1beta1 to Spoke version v1alpha1;"+
76+
"source: %s/%s, target: %s/%s", src.Namespace, src.Name, dst.Namespace, dst.Name)
77+
78+
dst.ObjectMeta = src.ObjectMeta
79+
80+
if len(src.Spec.Mappings) > 0 {
81+
dst.Spec.Mappings = make([]MappingsSpec, len(src.Spec.Mappings))
82+
83+
for i, mapping := range src.Spec.Mappings {
84+
dst.Spec.Mappings[i] = MappingsSpec{
85+
LocalClientProfile: mapping.LocalClientProfile,
86+
RemoteClientProfile: mapping.RemoteClientProfile,
87+
BlockPoolIdMapping: convertBlockPoolIdPair[csiv1beta1.BlockPoolIdPair, BlockPoolIdPair](mapping.BlockPoolIdMapping),
88+
}
89+
}
90+
}
91+
92+
return nil
93+
}
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 (*ClientProfileMapping) Hub() {}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
)
22+
23+
// +kubebuilder:validation:MinItems:=2
24+
// +kubebuilder:validation:MaxItems:=2
25+
type BlockPoolIdPair []string
26+
27+
// MappingsSpec define a mapping between a local and remote profiles
28+
type MappingsSpec struct {
29+
// +kubebuilder:validation:Required
30+
LocalClientProfile string `json:"localClientProfile,omitempty"`
31+
32+
// +kubebuilder:validation:Required
33+
RemoteClientProfile string `json:"remoteClientProfile,omitempty"`
34+
35+
// +kubebuilder:validation:Optional
36+
BlockPoolIdMapping []BlockPoolIdPair `json:"blockPoolIdMapping,omitempty"`
37+
}
38+
39+
// ClientProfileMappingSpec defines the desired state of ClientProfileMapping
40+
type ClientProfileMappingSpec struct {
41+
// +kubebuilder:validation:Required
42+
Mappings []MappingsSpec `json:"mappings,omitempty"`
43+
}
44+
45+
// ClientProfileMappingStatus defines the observed state of ClientProfileMapping
46+
type ClientProfileMappingStatus struct {
47+
}
48+
49+
// +kubebuilder:object:root=true
50+
// +kubebuilder:storageversion
51+
// +kubebuilder:conversion:hub
52+
// +kubebuilder:subresource:status
53+
54+
// ClientProfileMapping is the Schema for the clientprofilemappings API
55+
type ClientProfileMapping struct {
56+
metav1.TypeMeta `json:",inline"`
57+
metav1.ObjectMeta `json:"metadata,omitempty"`
58+
59+
Spec ClientProfileMappingSpec `json:"spec,omitempty"`
60+
Status ClientProfileMappingStatus `json:"status,omitempty"`
61+
}
62+
63+
// +kubebuilder:object:root=true
64+
65+
// ClientProfileMappingList contains a list of ClientProfileMapping
66+
type ClientProfileMappingList struct {
67+
metav1.TypeMeta `json:",inline"`
68+
metav1.ListMeta `json:"metadata,omitempty"`
69+
Items []ClientProfileMapping `json:"items"`
70+
}

api/csi/v1beta1/groupversion_info.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func addKnownTypes(scheme *runtime.Scheme) error {
4242
scheme.AddKnownTypes(GroupVersion,
4343
&CephConnection{}, &CephConnectionList{},
4444
&ClientProfile{}, &ClientProfileList{},
45+
&ClientProfileMapping{}, &ClientProfileMappingList{},
4546
)
4647
metav1.AddToGroupVersion(scheme, GroupVersion)
4748
return nil

api/csi/v1beta1/zz_generated.deepcopy.go

Lines changed: 141 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,13 @@ func main() {
260260
os.Exit(1)
261261
}
262262
}
263+
// nolint:goconst
264+
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
265+
if err = webhookcsiv1beta1.SetupClientProfileMappingWebhookWithManager(mgr); err != nil {
266+
setupLog.Error(err, "unable to create webhook", "webhook", "ClientProfileMapping")
267+
os.Exit(1)
268+
}
269+
}
263270
//+kubebuilder:scaffold:builder
264271

265272
if metricsCertWatcher != nil {

0 commit comments

Comments
 (0)