Skip to content

Commit

Permalink
add v1alpha2 of linodeMachineTemplate (#428)
Browse files Browse the repository at this point in the history
  • Loading branch information
eljohnson92 authored Jul 26, 2024
1 parent a343c76 commit 020790d
Show file tree
Hide file tree
Showing 39 changed files with 1,029 additions and 47 deletions.
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ issues:
- gosec
- exportloopref
- unparam
# conversion files rely on largely generated code so are very similar
- path: _conversion\.go
linters:
- dupl

# Ease some gocritic warnings on test files.
- path: _test\.go
Expand Down
11 changes: 11 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,15 @@ resources:
webhooks:
conversion: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
domain: cluster.x-k8s.io
group: infrastructure
kind: LinodeMachineTemplate
path: github.com/linode/cluster-api-provider-linode/api/v1alpha2
version: v1alpha2
webhooks:
conversion: true
webhookVersion: v1
version: "3"
2 changes: 1 addition & 1 deletion api/v1alpha1/linodeclustertemplate_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (dst *LinodeClusterTemplate) ConvertFrom(srcRaw conversion.Hub) error {
return nil
}

// ConvertTo converts this DOClusterList to the Hub version (v1alpha2).
// ConvertTo converts this LinodeClusterTemplateList to the Hub version (v1alpha2).
func (src *LinodeClusterTemplateList) ConvertTo(dstRaw conversion.Hub) error {
dst, ok := dstRaw.(*infrastructurev1alpha2.LinodeClusterTemplateList)
if !ok {
Expand Down
83 changes: 83 additions & 0 deletions api/v1alpha1/linodemachinetemplate_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Copyright 2023 Akamai Technologies, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"errors"

utilconversion "sigs.k8s.io/cluster-api/util/conversion"
"sigs.k8s.io/controller-runtime/pkg/conversion"

infrastructurev1alpha2 "github.com/linode/cluster-api-provider-linode/api/v1alpha2"
)

// ConvertTo converts this LinodeMachineTemplate to the Hub version (v1alpha2).
func (src *LinodeMachineTemplate) ConvertTo(dstRaw conversion.Hub) error {
dst, ok := dstRaw.(*infrastructurev1alpha2.LinodeMachineTemplate)
if !ok {
return errors.New("failed to convert LinodeMachineTemplate version from v1alpha1 to v1alpha2")
}

if err := Convert_v1alpha1_LinodeMachineTemplate_To_v1alpha2_LinodeMachineTemplate(src, dst, nil); err != nil {
return err
}

// Manually restore data from annotations
restored := &LinodeMachineTemplate{}
if ok, err := utilconversion.UnmarshalData(src, restored); err != nil || !ok {
return err
}

return nil
}

// ConvertFrom converts from the Hub version (v1alpha2) to this version.
func (dst *LinodeMachineTemplate) ConvertFrom(srcRaw conversion.Hub) error {
src, ok := srcRaw.(*infrastructurev1alpha2.LinodeMachineTemplate)
if !ok {
return errors.New("failed to convert LinodeMachineTemplate version from v1alpha2 to v1alpha1")
}

if err := Convert_v1alpha2_LinodeMachineTemplate_To_v1alpha1_LinodeMachineTemplate(src, dst, nil); err != nil {
return err
}

// Preserve Hub data on down-conversion.
if err := utilconversion.MarshalData(src, dst); err != nil {
return err
}

return nil
}

// ConvertTo converts this LinodeMachineTemplateList to the Hub version (v1alpha2).
func (src *LinodeMachineTemplateList) ConvertTo(dstRaw conversion.Hub) error {
dst, ok := dstRaw.(*infrastructurev1alpha2.LinodeMachineTemplateList)
if !ok {
return errors.New("failed to convert LinodeMachineTemplate version from v1alpha1 to v1alpha2")
}
return Convert_v1alpha1_LinodeMachineTemplateList_To_v1alpha2_LinodeMachineTemplateList(src, dst, nil)
}

// ConvertFrom converts from the Hub version (v1alpha2) to this version.
func (dst *LinodeMachineTemplateList) ConvertFrom(srcRaw conversion.Hub) error {
src, ok := srcRaw.(*infrastructurev1alpha2.LinodeMachineTemplateList)
if !ok {
return errors.New("failed to convert LinodeMachineTemplate version from v1alpha2 to v1alpha1")
}
return Convert_v1alpha2_LinodeMachineTemplateList_To_v1alpha1_LinodeMachineTemplateList(src, dst, nil)
}
269 changes: 269 additions & 0 deletions api/v1alpha1/linodemachinetemplate_conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
/*
Copyright 2023 Akamai Technologies, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
utilconversion "sigs.k8s.io/cluster-api/util/conversion"

infrav1alpha2 "github.com/linode/cluster-api-provider-linode/api/v1alpha2"
"github.com/linode/cluster-api-provider-linode/mock"

. "github.com/linode/cluster-api-provider-linode/mock/mocktest"
)

func TestLinodeMachineTemplateConvertTo(t *testing.T) {
t.Parallel()

src := &LinodeMachineTemplate{
ObjectMeta: metav1.ObjectMeta{Name: "test-machine"},
Spec: LinodeMachineTemplateSpec{
Template: LinodeMachineTemplateResource{
Spec: LinodeMachineSpec{
ProviderID: ptr.To("linode://1234"),
InstanceID: ptr.To(1234),
Region: "us-mia",
Type: "g6-standard-2",
Group: "",
RootPass: "abc123",
AuthorizedKeys: []string{"authorizedKey1"},
AuthorizedUsers: []string{"authorizedUser1"},
BackupID: 1234,
Image: "linode/ubuntu24.04",
Interfaces: []InstanceConfigInterfaceCreateOptions{{Primary: true}},
BackupsEnabled: false,
PrivateIP: ptr.To(true),
Tags: []string{"test instance"},
FirewallID: 123,
OSDisk: ptr.To(InstanceDisk{
DiskID: 0,
Size: *resource.NewQuantity(12, resource.DecimalSI),
Label: "main disk",
Filesystem: "",
}),
DataDisks: map[string]*InstanceDisk{"sdb": {
DiskID: 0,
Size: *resource.NewQuantity(145, resource.DecimalSI),
Label: "etcd disk",
Filesystem: "",
},
"sdc": {
DiskID: 0,
Size: *resource.NewQuantity(543, resource.DecimalSI),
Label: "another disk",
Filesystem: "",
}},
CredentialsRef: &corev1.SecretReference{
Namespace: "default",
Name: "cred-secret",
},
},
},
},
}
expectedDst := &infrav1alpha2.LinodeMachineTemplate{
ObjectMeta: metav1.ObjectMeta{Name: "test-machine"},
Spec: infrav1alpha2.LinodeMachineTemplateSpec{
Template: infrav1alpha2.LinodeMachineTemplateResource{
Spec: infrav1alpha2.LinodeMachineSpec{
ProviderID: ptr.To("linode://1234"),
InstanceID: ptr.To(1234),
Region: "us-mia",
Type: "g6-standard-2",
Group: "",
RootPass: "abc123",
AuthorizedKeys: []string{"authorizedKey1"},
AuthorizedUsers: []string{"authorizedUser1"},
BackupID: 1234,
Image: "linode/ubuntu24.04",
Interfaces: []infrav1alpha2.InstanceConfigInterfaceCreateOptions{{Primary: true}},
BackupsEnabled: false,
PrivateIP: ptr.To(true),
Tags: []string{"test instance"},
FirewallID: 123,
OSDisk: ptr.To(infrav1alpha2.InstanceDisk{
DiskID: 0,
Size: *resource.NewQuantity(12, resource.DecimalSI),
Label: "main disk",
Filesystem: "",
}),
DataDisks: map[string]*infrav1alpha2.InstanceDisk{"sdb": {
DiskID: 0,
Size: *resource.NewQuantity(145, resource.DecimalSI),
Label: "etcd disk",
Filesystem: "",
},
"sdc": {
DiskID: 0,
Size: *resource.NewQuantity(543, resource.DecimalSI),
Label: "another disk",
Filesystem: "",
}},
CredentialsRef: &corev1.SecretReference{
Namespace: "default",
Name: "cred-secret",
},
},
},
},
}
dst := &infrav1alpha2.LinodeMachineTemplate{}

NewSuite(t, mock.MockLinodeClient{}).Run(
OneOf(
Path(
Call("convert v1alpha1 to v1alpha2", func(ctx context.Context, mck Mock) {
err := src.ConvertTo(dst)
if err != nil {
t.Fatalf("ConvertTo failed: %v", err)
}
}),
Result("conversion succeeded", func(ctx context.Context, mck Mock) {
if diff := cmp.Diff(expectedDst, dst); diff != "" {
t.Errorf("ConvertTo() mismatch (-expected +got):\n%s", diff)
}
}),
),
),
)
}

func TestLinodeMachineTemplateConvertFrom(t *testing.T) {
t.Parallel()

src := &infrav1alpha2.LinodeMachineTemplate{
ObjectMeta: metav1.ObjectMeta{Name: "test-machine"},
Spec: infrav1alpha2.LinodeMachineTemplateSpec{
Template: infrav1alpha2.LinodeMachineTemplateResource{
Spec: infrav1alpha2.LinodeMachineSpec{
ProviderID: ptr.To("linode://1234"),
InstanceID: ptr.To(1234),
Region: "us-mia",
Type: "g6-standard-2",
Group: "",
RootPass: "abc123",
AuthorizedKeys: []string{"authorizedKey1"},
AuthorizedUsers: []string{"authorizedUser1"},
BackupID: 1234,
Image: "linode/ubuntu24.04",
Interfaces: []infrav1alpha2.InstanceConfigInterfaceCreateOptions{{Primary: true}},
BackupsEnabled: false,
PrivateIP: ptr.To(true),
Tags: []string{"test instance"},
FirewallID: 123,
OSDisk: ptr.To(infrav1alpha2.InstanceDisk{
DiskID: 0,
Size: *resource.NewQuantity(12, resource.DecimalSI),
Label: "main disk",
Filesystem: "",
}),
DataDisks: map[string]*infrav1alpha2.InstanceDisk{"sdb": {
DiskID: 0,
Size: *resource.NewQuantity(145, resource.DecimalSI),
Label: "etcd disk",
Filesystem: "",
},
"sdc": {
DiskID: 0,
Size: *resource.NewQuantity(543, resource.DecimalSI),
Label: "another disk",
Filesystem: "",
}},
CredentialsRef: &corev1.SecretReference{
Namespace: "default",
Name: "cred-secret",
},
},
},
},
}
expectedDst := &LinodeMachineTemplate{
ObjectMeta: metav1.ObjectMeta{Name: "test-machine"},
Spec: LinodeMachineTemplateSpec{
Template: LinodeMachineTemplateResource{
Spec: LinodeMachineSpec{
ProviderID: ptr.To("linode://1234"),
InstanceID: ptr.To(1234),
Region: "us-mia",
Type: "g6-standard-2",
Group: "",
RootPass: "abc123",
AuthorizedKeys: []string{"authorizedKey1"},
AuthorizedUsers: []string{"authorizedUser1"},
BackupID: 1234,
Image: "linode/ubuntu24.04",
Interfaces: []InstanceConfigInterfaceCreateOptions{{Primary: true}},
BackupsEnabled: false,
PrivateIP: ptr.To(true),
Tags: []string{"test instance"},
FirewallID: 123,
OSDisk: ptr.To(InstanceDisk{
DiskID: 0,
Size: *resource.NewQuantity(12, resource.DecimalSI),
Label: "main disk",
Filesystem: "",
}),
DataDisks: map[string]*InstanceDisk{"sdb": {
DiskID: 0,
Size: *resource.NewQuantity(145, resource.DecimalSI),
Label: "etcd disk",
Filesystem: "",
},
"sdc": {
DiskID: 0,
Size: *resource.NewQuantity(543, resource.DecimalSI),
Label: "another disk",
Filesystem: "",
}},
CredentialsRef: &corev1.SecretReference{
Namespace: "default",
Name: "cred-secret",
},
},
},
},
}
if err := utilconversion.MarshalData(src, expectedDst); err != nil {
t.Fatalf("ConvertFrom failed: %v", err)
}
dst := &LinodeMachineTemplate{}

NewSuite(t, mock.MockLinodeClient{}).Run(
OneOf(
Path(
Call("convert v1alpha2 to v1alpha1", func(ctx context.Context, mck Mock) {
err := dst.ConvertFrom(src)
if err != nil {
t.Fatalf("ConvertFrom failed: %v", err)
}
}),
Result("conversion succeeded", func(ctx context.Context, mck Mock) {
if diff := cmp.Diff(expectedDst, dst); diff != "" {
t.Errorf("ConvertFrom() mismatch (-expected +got):\n%s", diff)
}
}),
),
),
)
}
Loading

0 comments on commit 020790d

Please sign in to comment.