Skip to content

Commit

Permalink
bump version for linodecluster to v1alpha2
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulait committed Jun 5, 2024
1 parent 30e714e commit 40cda9f
Show file tree
Hide file tree
Showing 51 changed files with 1,494 additions and 226 deletions.
11 changes: 11 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,15 @@ resources:
webhooks:
validation: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
domain: cluster.x-k8s.io
group: infrastructure
kind: LinodeCluster
path: github.com/linode/cluster-api-provider-linode/api/v1alpha2
version: v1alpha2
webhooks:
conversion: true
webhookVersion: v1
version: "3"
85 changes: 85 additions & 0 deletions api/v1alpha1/linodecluster_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
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"

"sigs.k8s.io/controller-runtime/pkg/conversion"

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

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

Check warning on line 31 in api/v1alpha1/linodecluster_conversion.go

View check run for this annotation

Codecov / codecov/patch

api/v1alpha1/linodecluster_conversion.go#L31

Added line #L31 was not covered by tests
}

// ObjectMeta
dst.ObjectMeta = src.ObjectMeta

// Spec
dst.Spec.Network = infrastructurev1alpha2.NetworkSpec{
LoadBalancerType: src.Spec.Network.LoadBalancerType,
ApiserverLoadBalancerPort: src.Spec.Network.LoadBalancerPort,
NodeBalancerID: src.Spec.Network.NodeBalancerID,
ApiserverNodeBalancerConfigID: src.Spec.Network.NodeBalancerConfigID,
}
dst.Spec.ControlPlaneEndpoint = src.Spec.ControlPlaneEndpoint
dst.Spec.Region = src.Spec.Region
dst.Spec.VPCRef = src.Spec.VPCRef
dst.Spec.CredentialsRef = src.Spec.CredentialsRef

// Status
dst.Status.Ready = src.Status.Ready
dst.Status.Conditions = src.Status.Conditions
dst.Status.FailureMessage = src.Status.FailureMessage
dst.Status.FailureReason = src.Status.FailureReason

return nil
}

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

Check warning on line 62 in api/v1alpha1/linodecluster_conversion.go

View check run for this annotation

Codecov / codecov/patch

api/v1alpha1/linodecluster_conversion.go#L62

Added line #L62 was not covered by tests
}

// ObjectMeta
dst.ObjectMeta = src.ObjectMeta

// Spec
dst.Spec.Network.LoadBalancerPort = src.Spec.Network.ApiserverLoadBalancerPort
dst.Spec.Network.LoadBalancerType = src.Spec.Network.LoadBalancerType
dst.Spec.Network.NodeBalancerID = src.Spec.Network.NodeBalancerID
dst.Spec.Network.NodeBalancerConfigID = src.Spec.Network.ApiserverNodeBalancerConfigID
dst.Spec.ControlPlaneEndpoint = src.Spec.ControlPlaneEndpoint
dst.Spec.Region = src.Spec.Region
dst.Spec.VPCRef = src.Spec.VPCRef
dst.Spec.CredentialsRef = src.Spec.CredentialsRef

// Status
dst.Status.Ready = src.Status.Ready
dst.Status.Conditions = src.Status.Conditions
dst.Status.FailureMessage = src.Status.FailureMessage
dst.Status.FailureReason = src.Status.FailureReason

return nil
}
140 changes: 140 additions & 0 deletions api/v1alpha1/linodecluster_conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
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"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"

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 TestConvertTo(t *testing.T) {
t.Parallel()

src := &LinodeCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cluster",
},
Spec: LinodeClusterSpec{
Network: NetworkSpec{
LoadBalancerType: "test-type",
LoadBalancerPort: 12345,
NodeBalancerID: ptr.To(1234),
NodeBalancerConfigID: ptr.To(2345),
},
ControlPlaneEndpoint: clusterv1.APIEndpoint{Host: "1.2.3.4"},
Region: "test-region",
},
}
expectedDst := &infrav1alpha2.LinodeCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cluster",
},
Spec: infrav1alpha2.LinodeClusterSpec{
Network: infrav1alpha2.NetworkSpec{
LoadBalancerType: "test-type",
NodeBalancerID: ptr.To(1234),
ApiserverLoadBalancerPort: 12345,
ApiserverNodeBalancerConfigID: ptr.To(2345),
},
ControlPlaneEndpoint: clusterv1.APIEndpoint{Host: "1.2.3.4"},
Region: "test-region",
},
}
dst := &infrav1alpha2.LinodeCluster{}

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 TestConvertFrom(t *testing.T) {
t.Parallel()

src := &infrav1alpha2.LinodeCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cluster",
},
Spec: infrav1alpha2.LinodeClusterSpec{
Network: infrav1alpha2.NetworkSpec{
LoadBalancerType: "test-type",
NodeBalancerID: ptr.To(1234),
ApiserverLoadBalancerPort: 12345,
ApiserverNodeBalancerConfigID: ptr.To(2345),
},
ControlPlaneEndpoint: clusterv1.APIEndpoint{Host: "1.2.3.4"},
Region: "test-region",
},
}
expectedDst := &LinodeCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cluster",
},
Spec: LinodeClusterSpec{
Network: NetworkSpec{
LoadBalancerType: "test-type",
LoadBalancerPort: 12345,
NodeBalancerID: ptr.To(1234),
NodeBalancerConfigID: ptr.To(2345),
},
ControlPlaneEndpoint: clusterv1.APIEndpoint{Host: "1.2.3.4"},
Region: "test-region",
},
}
dst := &LinodeCluster{}

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)
}
}),
),
),
)
}
36 changes: 36 additions & 0 deletions api/v1alpha2/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
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 v1alpha2 contains API Schema definitions for the infrastructure v1alpha2 API group
// +kubebuilder:object:generate=true
// +groupName=infrastructure.cluster.x-k8s.io
package v1alpha2

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "infrastructure.cluster.x-k8s.io", Version: "v1alpha2"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)
20 changes: 20 additions & 0 deletions api/v1alpha2/linodecluster_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
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 v1alpha2

// Hub marks LinodeCluster as a conversion hub.
func (*LinodeCluster) Hub() {}

Check warning on line 20 in api/v1alpha2/linodecluster_conversion.go

View check run for this annotation

Codecov / codecov/patch

api/v1alpha2/linodecluster_conversion.go#L20

Added line #L20 was not covered by tests
Loading

0 comments on commit 40cda9f

Please sign in to comment.