Skip to content

Commit 020790d

Browse files
authored
add v1alpha2 of linodeMachineTemplate (#428)
1 parent a343c76 commit 020790d

39 files changed

+1029
-47
lines changed

.golangci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ issues:
207207
- gosec
208208
- exportloopref
209209
- unparam
210+
# conversion files rely on largely generated code so are very similar
211+
- path: _conversion\.go
212+
linters:
213+
- dupl
210214

211215
# Ease some gocritic warnings on test files.
212216
- path: _test\.go

PROJECT

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,15 @@ resources:
130130
webhooks:
131131
conversion: true
132132
webhookVersion: v1
133+
- api:
134+
crdVersion: v1
135+
namespaced: true
136+
domain: cluster.x-k8s.io
137+
group: infrastructure
138+
kind: LinodeMachineTemplate
139+
path: github.com/linode/cluster-api-provider-linode/api/v1alpha2
140+
version: v1alpha2
141+
webhooks:
142+
conversion: true
143+
webhookVersion: v1
133144
version: "3"

api/v1alpha1/linodeclustertemplate_conversion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (dst *LinodeClusterTemplate) ConvertFrom(srcRaw conversion.Hub) error {
6464
return nil
6565
}
6666

67-
// ConvertTo converts this DOClusterList to the Hub version (v1alpha2).
67+
// ConvertTo converts this LinodeClusterTemplateList to the Hub version (v1alpha2).
6868
func (src *LinodeClusterTemplateList) ConvertTo(dstRaw conversion.Hub) error {
6969
dst, ok := dstRaw.(*infrastructurev1alpha2.LinodeClusterTemplateList)
7070
if !ok {
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 LinodeMachineTemplate to the Hub version (v1alpha2).
29+
func (src *LinodeMachineTemplate) ConvertTo(dstRaw conversion.Hub) error {
30+
dst, ok := dstRaw.(*infrastructurev1alpha2.LinodeMachineTemplate)
31+
if !ok {
32+
return errors.New("failed to convert LinodeMachineTemplate version from v1alpha1 to v1alpha2")
33+
}
34+
35+
if err := Convert_v1alpha1_LinodeMachineTemplate_To_v1alpha2_LinodeMachineTemplate(src, dst, nil); err != nil {
36+
return err
37+
}
38+
39+
// Manually restore data from annotations
40+
restored := &LinodeMachineTemplate{}
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 *LinodeMachineTemplate) ConvertFrom(srcRaw conversion.Hub) error {
50+
src, ok := srcRaw.(*infrastructurev1alpha2.LinodeMachineTemplate)
51+
if !ok {
52+
return errors.New("failed to convert LinodeMachineTemplate version from v1alpha2 to v1alpha1")
53+
}
54+
55+
if err := Convert_v1alpha2_LinodeMachineTemplate_To_v1alpha1_LinodeMachineTemplate(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+
}
66+
67+
// ConvertTo converts this LinodeMachineTemplateList to the Hub version (v1alpha2).
68+
func (src *LinodeMachineTemplateList) ConvertTo(dstRaw conversion.Hub) error {
69+
dst, ok := dstRaw.(*infrastructurev1alpha2.LinodeMachineTemplateList)
70+
if !ok {
71+
return errors.New("failed to convert LinodeMachineTemplate version from v1alpha1 to v1alpha2")
72+
}
73+
return Convert_v1alpha1_LinodeMachineTemplateList_To_v1alpha2_LinodeMachineTemplateList(src, dst, nil)
74+
}
75+
76+
// ConvertFrom converts from the Hub version (v1alpha2) to this version.
77+
func (dst *LinodeMachineTemplateList) ConvertFrom(srcRaw conversion.Hub) error {
78+
src, ok := srcRaw.(*infrastructurev1alpha2.LinodeMachineTemplateList)
79+
if !ok {
80+
return errors.New("failed to convert LinodeMachineTemplate version from v1alpha2 to v1alpha1")
81+
}
82+
return Convert_v1alpha2_LinodeMachineTemplateList_To_v1alpha1_LinodeMachineTemplateList(src, dst, nil)
83+
}
Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
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+
"k8s.io/apimachinery/pkg/api/resource"
26+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/utils/ptr"
28+
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
29+
30+
infrav1alpha2 "github.com/linode/cluster-api-provider-linode/api/v1alpha2"
31+
"github.com/linode/cluster-api-provider-linode/mock"
32+
33+
. "github.com/linode/cluster-api-provider-linode/mock/mocktest"
34+
)
35+
36+
func TestLinodeMachineTemplateConvertTo(t *testing.T) {
37+
t.Parallel()
38+
39+
src := &LinodeMachineTemplate{
40+
ObjectMeta: metav1.ObjectMeta{Name: "test-machine"},
41+
Spec: LinodeMachineTemplateSpec{
42+
Template: LinodeMachineTemplateResource{
43+
Spec: LinodeMachineSpec{
44+
ProviderID: ptr.To("linode://1234"),
45+
InstanceID: ptr.To(1234),
46+
Region: "us-mia",
47+
Type: "g6-standard-2",
48+
Group: "",
49+
RootPass: "abc123",
50+
AuthorizedKeys: []string{"authorizedKey1"},
51+
AuthorizedUsers: []string{"authorizedUser1"},
52+
BackupID: 1234,
53+
Image: "linode/ubuntu24.04",
54+
Interfaces: []InstanceConfigInterfaceCreateOptions{{Primary: true}},
55+
BackupsEnabled: false,
56+
PrivateIP: ptr.To(true),
57+
Tags: []string{"test instance"},
58+
FirewallID: 123,
59+
OSDisk: ptr.To(InstanceDisk{
60+
DiskID: 0,
61+
Size: *resource.NewQuantity(12, resource.DecimalSI),
62+
Label: "main disk",
63+
Filesystem: "",
64+
}),
65+
DataDisks: map[string]*InstanceDisk{"sdb": {
66+
DiskID: 0,
67+
Size: *resource.NewQuantity(145, resource.DecimalSI),
68+
Label: "etcd disk",
69+
Filesystem: "",
70+
},
71+
"sdc": {
72+
DiskID: 0,
73+
Size: *resource.NewQuantity(543, resource.DecimalSI),
74+
Label: "another disk",
75+
Filesystem: "",
76+
}},
77+
CredentialsRef: &corev1.SecretReference{
78+
Namespace: "default",
79+
Name: "cred-secret",
80+
},
81+
},
82+
},
83+
},
84+
}
85+
expectedDst := &infrav1alpha2.LinodeMachineTemplate{
86+
ObjectMeta: metav1.ObjectMeta{Name: "test-machine"},
87+
Spec: infrav1alpha2.LinodeMachineTemplateSpec{
88+
Template: infrav1alpha2.LinodeMachineTemplateResource{
89+
Spec: infrav1alpha2.LinodeMachineSpec{
90+
ProviderID: ptr.To("linode://1234"),
91+
InstanceID: ptr.To(1234),
92+
Region: "us-mia",
93+
Type: "g6-standard-2",
94+
Group: "",
95+
RootPass: "abc123",
96+
AuthorizedKeys: []string{"authorizedKey1"},
97+
AuthorizedUsers: []string{"authorizedUser1"},
98+
BackupID: 1234,
99+
Image: "linode/ubuntu24.04",
100+
Interfaces: []infrav1alpha2.InstanceConfigInterfaceCreateOptions{{Primary: true}},
101+
BackupsEnabled: false,
102+
PrivateIP: ptr.To(true),
103+
Tags: []string{"test instance"},
104+
FirewallID: 123,
105+
OSDisk: ptr.To(infrav1alpha2.InstanceDisk{
106+
DiskID: 0,
107+
Size: *resource.NewQuantity(12, resource.DecimalSI),
108+
Label: "main disk",
109+
Filesystem: "",
110+
}),
111+
DataDisks: map[string]*infrav1alpha2.InstanceDisk{"sdb": {
112+
DiskID: 0,
113+
Size: *resource.NewQuantity(145, resource.DecimalSI),
114+
Label: "etcd disk",
115+
Filesystem: "",
116+
},
117+
"sdc": {
118+
DiskID: 0,
119+
Size: *resource.NewQuantity(543, resource.DecimalSI),
120+
Label: "another disk",
121+
Filesystem: "",
122+
}},
123+
CredentialsRef: &corev1.SecretReference{
124+
Namespace: "default",
125+
Name: "cred-secret",
126+
},
127+
},
128+
},
129+
},
130+
}
131+
dst := &infrav1alpha2.LinodeMachineTemplate{}
132+
133+
NewSuite(t, mock.MockLinodeClient{}).Run(
134+
OneOf(
135+
Path(
136+
Call("convert v1alpha1 to v1alpha2", func(ctx context.Context, mck Mock) {
137+
err := src.ConvertTo(dst)
138+
if err != nil {
139+
t.Fatalf("ConvertTo failed: %v", err)
140+
}
141+
}),
142+
Result("conversion succeeded", func(ctx context.Context, mck Mock) {
143+
if diff := cmp.Diff(expectedDst, dst); diff != "" {
144+
t.Errorf("ConvertTo() mismatch (-expected +got):\n%s", diff)
145+
}
146+
}),
147+
),
148+
),
149+
)
150+
}
151+
152+
func TestLinodeMachineTemplateConvertFrom(t *testing.T) {
153+
t.Parallel()
154+
155+
src := &infrav1alpha2.LinodeMachineTemplate{
156+
ObjectMeta: metav1.ObjectMeta{Name: "test-machine"},
157+
Spec: infrav1alpha2.LinodeMachineTemplateSpec{
158+
Template: infrav1alpha2.LinodeMachineTemplateResource{
159+
Spec: infrav1alpha2.LinodeMachineSpec{
160+
ProviderID: ptr.To("linode://1234"),
161+
InstanceID: ptr.To(1234),
162+
Region: "us-mia",
163+
Type: "g6-standard-2",
164+
Group: "",
165+
RootPass: "abc123",
166+
AuthorizedKeys: []string{"authorizedKey1"},
167+
AuthorizedUsers: []string{"authorizedUser1"},
168+
BackupID: 1234,
169+
Image: "linode/ubuntu24.04",
170+
Interfaces: []infrav1alpha2.InstanceConfigInterfaceCreateOptions{{Primary: true}},
171+
BackupsEnabled: false,
172+
PrivateIP: ptr.To(true),
173+
Tags: []string{"test instance"},
174+
FirewallID: 123,
175+
OSDisk: ptr.To(infrav1alpha2.InstanceDisk{
176+
DiskID: 0,
177+
Size: *resource.NewQuantity(12, resource.DecimalSI),
178+
Label: "main disk",
179+
Filesystem: "",
180+
}),
181+
DataDisks: map[string]*infrav1alpha2.InstanceDisk{"sdb": {
182+
DiskID: 0,
183+
Size: *resource.NewQuantity(145, resource.DecimalSI),
184+
Label: "etcd disk",
185+
Filesystem: "",
186+
},
187+
"sdc": {
188+
DiskID: 0,
189+
Size: *resource.NewQuantity(543, resource.DecimalSI),
190+
Label: "another disk",
191+
Filesystem: "",
192+
}},
193+
CredentialsRef: &corev1.SecretReference{
194+
Namespace: "default",
195+
Name: "cred-secret",
196+
},
197+
},
198+
},
199+
},
200+
}
201+
expectedDst := &LinodeMachineTemplate{
202+
ObjectMeta: metav1.ObjectMeta{Name: "test-machine"},
203+
Spec: LinodeMachineTemplateSpec{
204+
Template: LinodeMachineTemplateResource{
205+
Spec: LinodeMachineSpec{
206+
ProviderID: ptr.To("linode://1234"),
207+
InstanceID: ptr.To(1234),
208+
Region: "us-mia",
209+
Type: "g6-standard-2",
210+
Group: "",
211+
RootPass: "abc123",
212+
AuthorizedKeys: []string{"authorizedKey1"},
213+
AuthorizedUsers: []string{"authorizedUser1"},
214+
BackupID: 1234,
215+
Image: "linode/ubuntu24.04",
216+
Interfaces: []InstanceConfigInterfaceCreateOptions{{Primary: true}},
217+
BackupsEnabled: false,
218+
PrivateIP: ptr.To(true),
219+
Tags: []string{"test instance"},
220+
FirewallID: 123,
221+
OSDisk: ptr.To(InstanceDisk{
222+
DiskID: 0,
223+
Size: *resource.NewQuantity(12, resource.DecimalSI),
224+
Label: "main disk",
225+
Filesystem: "",
226+
}),
227+
DataDisks: map[string]*InstanceDisk{"sdb": {
228+
DiskID: 0,
229+
Size: *resource.NewQuantity(145, resource.DecimalSI),
230+
Label: "etcd disk",
231+
Filesystem: "",
232+
},
233+
"sdc": {
234+
DiskID: 0,
235+
Size: *resource.NewQuantity(543, resource.DecimalSI),
236+
Label: "another disk",
237+
Filesystem: "",
238+
}},
239+
CredentialsRef: &corev1.SecretReference{
240+
Namespace: "default",
241+
Name: "cred-secret",
242+
},
243+
},
244+
},
245+
},
246+
}
247+
if err := utilconversion.MarshalData(src, expectedDst); err != nil {
248+
t.Fatalf("ConvertFrom failed: %v", err)
249+
}
250+
dst := &LinodeMachineTemplate{}
251+
252+
NewSuite(t, mock.MockLinodeClient{}).Run(
253+
OneOf(
254+
Path(
255+
Call("convert v1alpha2 to v1alpha1", func(ctx context.Context, mck Mock) {
256+
err := dst.ConvertFrom(src)
257+
if err != nil {
258+
t.Fatalf("ConvertFrom failed: %v", err)
259+
}
260+
}),
261+
Result("conversion succeeded", func(ctx context.Context, mck Mock) {
262+
if diff := cmp.Diff(expectedDst, dst); diff != "" {
263+
t.Errorf("ConvertFrom() mismatch (-expected +got):\n%s", diff)
264+
}
265+
}),
266+
),
267+
),
268+
)
269+
}

0 commit comments

Comments
 (0)