Skip to content

Commit bd77f69

Browse files
unnatiaggUnnati Aggarwaluaggarwa
authored
[feat] add a v1alpha2 version of LinodeObjectStorageBucket (#427)
* v1alpha2 for linodeObjectStorageBucket * converting cluster ID into regionID * fixing lint changes * Using conversion-gen to convert Linode OBJ api conversions * adding lint suggestions * handling region conversion from v1alpha2 -> v1alpha1 * fixing lint issue * changing hostname condition statement --------- Co-authored-by: Unnati Aggarwal <uaggarwa@bos-mppxs.attlocal.net> Co-authored-by: Unnati Aggarwal <uaggarwa@akamai.com>
1 parent d0d96a7 commit bd77f69

File tree

42 files changed

+1271
-147
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1271
-147
lines changed

PROJECT

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,16 @@ resources:
141141
webhooks:
142142
conversion: true
143143
webhookVersion: v1
144+
- api:
145+
crdVersion: v1
146+
namespaced: true
147+
domain: cluster.x-k8s.io
148+
group: infrastructure
149+
kind: LinodeObjectStorageBucket
150+
path: github.com/linode/cluster-api-provider-linode/api/v1alpha2
151+
version: v1alpha2
152+
webhooks:
153+
conversion: true
154+
validation: true
155+
webhookVersion: v1
144156
version: "3"

api/v1alpha1/conversion.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package v1alpha1
1818

1919
import (
20+
"strings"
21+
2022
"k8s.io/apimachinery/pkg/conversion"
2123

2224
infrastructurev1alpha2 "github.com/linode/cluster-api-provider-linode/api/v1alpha2"
@@ -43,3 +45,30 @@ func Convert_v1alpha2_LinodeMachineSpec_To_v1alpha1_LinodeMachineSpec(in *infras
4345
// Ok to use the auto-generated conversion function, it simply drops the PlacementGroupRef, and copies everything else
4446
return autoConvert_v1alpha2_LinodeMachineSpec_To_v1alpha1_LinodeMachineSpec(in, out, s)
4547
}
48+
49+
func Convert_v1alpha1_LinodeObjectStorageBucketSpec_To_v1alpha2_LinodeObjectStorageBucketSpec(in *LinodeObjectStorageBucketSpec, out *infrastructurev1alpha2.LinodeObjectStorageBucketSpec, s conversion.Scope) error {
50+
// WARNING: in.Cluster requires manual conversion: does not exist in peer-type
51+
out.Region = in.Cluster
52+
out.CredentialsRef = in.CredentialsRef
53+
out.KeyGeneration = in.KeyGeneration
54+
out.SecretType = in.SecretType
55+
return nil
56+
}
57+
58+
func Convert_v1alpha2_LinodeObjectStorageBucketSpec_To_v1alpha1_LinodeObjectStorageBucketSpec(in *infrastructurev1alpha2.LinodeObjectStorageBucketSpec, out *LinodeObjectStorageBucketSpec, s conversion.Scope) error {
59+
// WARNING: in.Region requires manual conversion: does not exist in peer-type
60+
out.Cluster = in.Region
61+
out.CredentialsRef = in.CredentialsRef
62+
out.KeyGeneration = in.KeyGeneration
63+
out.SecretType = in.SecretType
64+
return nil
65+
}
66+
67+
func Convert_v1alpha2_LinodeObjectStorageBucket_To_v1alpha1_LinodeObjectStorageBucket(in *infrastructurev1alpha2.LinodeObjectStorageBucket, out *LinodeObjectStorageBucket, scope conversion.Scope) error {
68+
if in.Status.Hostname != nil && *in.Status.Hostname != "" {
69+
in.Spec.Region = strings.Split(*in.Status.Hostname, ".")[1]
70+
} else {
71+
in.Spec.Region += "-1"
72+
}
73+
return autoConvert_v1alpha2_LinodeObjectStorageBucket_To_v1alpha1_LinodeObjectStorageBucket(in, out, scope)
74+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Copyright 2023 Akamai Technologies, Inc.
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+
22+
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
23+
"sigs.k8s.io/controller-runtime/pkg/conversion"
24+
25+
infrastructurev1alpha2 "github.com/linode/cluster-api-provider-linode/api/v1alpha2"
26+
)
27+
28+
// ConvertTo converts this LinodeObjectStorageBucket to the Hub version (v1alpha2).
29+
func (src *LinodeObjectStorageBucket) ConvertTo(dstRaw conversion.Hub) error {
30+
dst, ok := dstRaw.(*infrastructurev1alpha2.LinodeObjectStorageBucket)
31+
if !ok {
32+
return errors.New("failed to convert LinodeObjectStorageBucket version from v1alpha1 to v1alpha2")
33+
}
34+
35+
if err := Convert_v1alpha1_LinodeObjectStorageBucket_To_v1alpha2_LinodeObjectStorageBucket(src, dst, nil); err != nil {
36+
return err
37+
}
38+
39+
// Manually restore data from annotations
40+
restored := &LinodeObjectStorageBucket{}
41+
if ok, err := utilconversion.UnmarshalData(src, restored); err != nil || !ok {
42+
return err
43+
}
44+
45+
return nil
46+
}
47+
48+
// ConvertFrom converts from the Hub version (v1alpha2) to this version.
49+
func (dst *LinodeObjectStorageBucket) ConvertFrom(srcRaw conversion.Hub) error {
50+
src, ok := srcRaw.(*infrastructurev1alpha2.LinodeObjectStorageBucket)
51+
if !ok {
52+
return errors.New("failed to convert LinodeObjectStorageBucket version from v1alpha2 to v1alpha1")
53+
}
54+
55+
if err := Convert_v1alpha2_LinodeObjectStorageBucket_To_v1alpha1_LinodeObjectStorageBucket(src, dst, nil); err != nil {
56+
return err
57+
}
58+
59+
// Preserve Hub data on down-conversion.
60+
if err := utilconversion.MarshalData(src, dst); err != nil {
61+
return err
62+
}
63+
64+
return nil
65+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
Copyright 2023 Akamai Technologies, Inc.
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+
"context"
21+
"testing"
22+
23+
"github.com/google/go-cmp/cmp"
24+
corev1 "k8s.io/api/core/v1"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
"k8s.io/utils/ptr"
27+
28+
infrav1alpha2 "github.com/linode/cluster-api-provider-linode/api/v1alpha2"
29+
"github.com/linode/cluster-api-provider-linode/mock"
30+
31+
. "github.com/linode/cluster-api-provider-linode/mock/mocktest"
32+
)
33+
34+
const (
35+
ConversionDataAnnotation = "cluster.x-k8s.io/conversion-data"
36+
)
37+
38+
func TestLinodeObjectStorageBucketConvertTo(t *testing.T) {
39+
t.Parallel()
40+
41+
src := &LinodeObjectStorageBucket{
42+
ObjectMeta: metav1.ObjectMeta{Name: "test-bucket"},
43+
Spec: LinodeObjectStorageBucketSpec{
44+
Cluster: "us-mia-1",
45+
CredentialsRef: &corev1.SecretReference{
46+
Namespace: "default",
47+
Name: "cred-secret",
48+
},
49+
KeyGeneration: ptr.To(1),
50+
SecretType: "Opaque",
51+
},
52+
Status: LinodeObjectStorageBucketStatus{},
53+
}
54+
expectedDst := &infrav1alpha2.LinodeObjectStorageBucket{
55+
ObjectMeta: metav1.ObjectMeta{Name: "test-bucket"},
56+
Spec: infrav1alpha2.LinodeObjectStorageBucketSpec{
57+
Region: "us-mia-1",
58+
CredentialsRef: &corev1.SecretReference{
59+
Namespace: "default",
60+
Name: "cred-secret",
61+
},
62+
KeyGeneration: ptr.To(1),
63+
SecretType: "Opaque",
64+
},
65+
Status: infrav1alpha2.LinodeObjectStorageBucketStatus{},
66+
}
67+
dst := &infrav1alpha2.LinodeObjectStorageBucket{}
68+
69+
NewSuite(t, mock.MockLinodeClient{}).Run(
70+
OneOf(
71+
Path(
72+
Call("convert v1alpha1 to v1alpha2", func(ctx context.Context, mck Mock) {
73+
err := src.ConvertTo(dst)
74+
if err != nil {
75+
t.Fatalf("ConvertTo failed: %v", err)
76+
}
77+
}),
78+
Result("conversion succeeded", func(ctx context.Context, mck Mock) {
79+
if diff := cmp.Diff(expectedDst, dst); diff != "" {
80+
t.Errorf("ConvertTo() mismatch (-expected +got):\n%s", diff)
81+
}
82+
}),
83+
),
84+
),
85+
)
86+
}
87+
88+
func TestLinodeObjectStorageBucketFrom(t *testing.T) {
89+
t.Parallel()
90+
91+
src := &infrav1alpha2.LinodeObjectStorageBucket{
92+
ObjectMeta: metav1.ObjectMeta{Name: "test-bucket"},
93+
Spec: infrav1alpha2.LinodeObjectStorageBucketSpec{
94+
Region: "us-mia",
95+
CredentialsRef: &corev1.SecretReference{
96+
Namespace: "default",
97+
Name: "cred-secret",
98+
},
99+
KeyGeneration: ptr.To(1),
100+
SecretType: "Opaque",
101+
},
102+
Status: infrav1alpha2.LinodeObjectStorageBucketStatus{},
103+
}
104+
expectedDst := &LinodeObjectStorageBucket{
105+
ObjectMeta: metav1.ObjectMeta{
106+
Name: "test-bucket",
107+
Annotations: map[string]string{
108+
ConversionDataAnnotation: `{"spec":{"credentialsRef":{"name":"cred-secret","namespace":"default"},"keyGeneration":1,"region":"us-mia-1","secretType":"Opaque"},"status":{"ready":false}}`,
109+
},
110+
},
111+
Spec: LinodeObjectStorageBucketSpec{
112+
Cluster: "us-mia-1",
113+
CredentialsRef: &corev1.SecretReference{
114+
Namespace: "default",
115+
Name: "cred-secret",
116+
},
117+
KeyGeneration: ptr.To(1),
118+
SecretType: "Opaque",
119+
},
120+
Status: LinodeObjectStorageBucketStatus{},
121+
}
122+
dst := &LinodeObjectStorageBucket{}
123+
124+
NewSuite(t, mock.MockLinodeClient{}).Run(
125+
OneOf(
126+
Path(
127+
Call("convert v1alpha2 to v1alpha1", func(ctx context.Context, mck Mock) {
128+
err := dst.ConvertFrom(src)
129+
if err != nil {
130+
t.Fatalf("ConvertFrom failed: %v", err)
131+
}
132+
}),
133+
Result("conversion succeeded", func(ctx context.Context, mck Mock) {
134+
if diff := cmp.Diff(expectedDst, dst); diff != "" {
135+
t.Errorf("ConvertFrom() mismatch (-expected +got):\n%s", diff)
136+
}
137+
}),
138+
),
139+
),
140+
)
141+
}

0 commit comments

Comments
 (0)