-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add v1alpha2 of linodeClusterTemplate (#425)
- Loading branch information
1 parent
27f5949
commit a417bbc
Showing
21 changed files
with
1,706 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
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 ( | ||
"k8s.io/apimachinery/pkg/conversion" | ||
|
||
infrastructurev1alpha2 "github.com/linode/cluster-api-provider-linode/api/v1alpha2" | ||
) | ||
|
||
func Convert_v1alpha1_NetworkSpec_To_v1alpha2_NetworkSpec(in *NetworkSpec, out *infrastructurev1alpha2.NetworkSpec, s conversion.Scope) error { | ||
out.ApiserverNodeBalancerConfigID = in.NodeBalancerConfigID | ||
out.ApiserverLoadBalancerPort = in.LoadBalancerPort | ||
out.LoadBalancerType = in.LoadBalancerType | ||
out.NodeBalancerID = in.NodeBalancerID | ||
out.AdditionalPorts = make([]infrastructurev1alpha2.LinodeNBPortConfig, 0) | ||
return nil | ||
} | ||
|
||
func Convert_v1alpha2_NetworkSpec_To_v1alpha1_NetworkSpec(in *infrastructurev1alpha2.NetworkSpec, out *NetworkSpec, s conversion.Scope) error { | ||
out.NodeBalancerConfigID = in.ApiserverNodeBalancerConfigID | ||
out.LoadBalancerPort = in.ApiserverLoadBalancerPort | ||
out.LoadBalancerType = in.LoadBalancerType | ||
out.NodeBalancerID = in.NodeBalancerID | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
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. | ||
*/ | ||
|
||
// +groupName=infrastructure.cluster.x-k8s.io | ||
// +k8s:conversion-gen=github.com/linode/cluster-api-provider-linode/api/v1alpha2 | ||
package v1alpha1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 LinodeCluster to the Hub version (v1alpha2). | ||
func (src *LinodeClusterTemplate) ConvertTo(dstRaw conversion.Hub) error { | ||
dst, ok := dstRaw.(*infrastructurev1alpha2.LinodeClusterTemplate) | ||
if !ok { | ||
return errors.New("failed to convert LinodeClusterTemplate version from v1alpha1 to v1alpha2") | ||
} | ||
|
||
if err := Convert_v1alpha1_LinodeClusterTemplate_To_v1alpha2_LinodeClusterTemplate(src, dst, nil); err != nil { | ||
return err | ||
} | ||
|
||
// Manually restore data from annotations | ||
restored := &LinodeClusterTemplate{} | ||
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 *LinodeClusterTemplate) ConvertFrom(srcRaw conversion.Hub) error { | ||
src, ok := srcRaw.(*infrastructurev1alpha2.LinodeClusterTemplate) | ||
if !ok { | ||
return errors.New("failed to convert LinodeClusterTemplate version from v1alpha2 to v1alpha1") | ||
} | ||
|
||
if err := Convert_v1alpha2_LinodeClusterTemplate_To_v1alpha1_LinodeClusterTemplate(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 DOClusterList to the Hub version (v1alpha2). | ||
func (src *LinodeClusterTemplateList) ConvertTo(dstRaw conversion.Hub) error { | ||
dst, ok := dstRaw.(*infrastructurev1alpha2.LinodeClusterTemplateList) | ||
if !ok { | ||
return errors.New("failed to convert LinodeClusterTemplate version from v1alpha1 to v1alpha2") | ||
} | ||
return Convert_v1alpha1_LinodeClusterTemplateList_To_v1alpha2_LinodeClusterTemplateList(src, dst, nil) | ||
} | ||
|
||
// ConvertFrom converts from the Hub version (v1alpha2) to this version. | ||
func (dst *LinodeClusterTemplateList) ConvertFrom(srcRaw conversion.Hub) error { | ||
src, ok := srcRaw.(*infrastructurev1alpha2.LinodeClusterTemplateList) | ||
if !ok { | ||
return errors.New("failed to convert LinodeClusterTemplate version from v1alpha2 to v1alpha1") | ||
} | ||
return Convert_v1alpha2_LinodeClusterTemplateList_To_v1alpha1_LinodeClusterTemplateList(src, dst, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/* | ||
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" | ||
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 TestLinodeClusterTemplateConvertTo(t *testing.T) { | ||
t.Parallel() | ||
|
||
src := &LinodeClusterTemplate{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-cluster", | ||
}, | ||
Spec: LinodeClusterTemplateSpec{ | ||
Template: LinodeClusterTemplateResource{ | ||
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.LinodeClusterTemplate{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-cluster", | ||
}, | ||
Spec: infrav1alpha2.LinodeClusterTemplateSpec{ | ||
Template: infrav1alpha2.LinodeClusterTemplateResource{ | ||
Spec: infrav1alpha2.LinodeClusterSpec{ | ||
Network: infrav1alpha2.NetworkSpec{ | ||
LoadBalancerType: "test-type", | ||
ApiserverLoadBalancerPort: 12345, | ||
NodeBalancerID: ptr.To(1234), | ||
ApiserverNodeBalancerConfigID: ptr.To(2345), | ||
AdditionalPorts: []infrav1alpha2.LinodeNBPortConfig{}, | ||
}, | ||
ControlPlaneEndpoint: clusterv1.APIEndpoint{Host: "1.2.3.4"}, | ||
Region: "test-region", | ||
}, | ||
}, | ||
}, | ||
} | ||
dst := &infrav1alpha2.LinodeClusterTemplate{} | ||
|
||
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 TestLinodeClusterTemplateConvertFrom(t *testing.T) { | ||
t.Parallel() | ||
|
||
src := &infrav1alpha2.LinodeClusterTemplate{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-cluster", | ||
}, | ||
Spec: infrav1alpha2.LinodeClusterTemplateSpec{ | ||
Template: infrav1alpha2.LinodeClusterTemplateResource{ | ||
Spec: infrav1alpha2.LinodeClusterSpec{ | ||
Network: infrav1alpha2.NetworkSpec{ | ||
LoadBalancerType: "test-type", | ||
ApiserverLoadBalancerPort: 12345, | ||
NodeBalancerID: ptr.To(1234), | ||
ApiserverNodeBalancerConfigID: ptr.To(2345), | ||
AdditionalPorts: []infrav1alpha2.LinodeNBPortConfig{{ | ||
Port: 6443, | ||
NodeBalancerConfigID: ptr.To(12345), | ||
}}, | ||
}, | ||
ControlPlaneEndpoint: clusterv1.APIEndpoint{Host: "1.2.3.4"}, | ||
Region: "test-region", | ||
}, | ||
}, | ||
}, | ||
} | ||
expectedDst := &LinodeClusterTemplate{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-cluster", | ||
}, | ||
Spec: LinodeClusterTemplateSpec{ | ||
Template: LinodeClusterTemplateResource{ | ||
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", | ||
}, | ||
}, | ||
}, | ||
} | ||
if err := utilconversion.MarshalData(src, expectedDst); err != nil { | ||
t.Fatalf("ConvertFrom failed: %v", err) | ||
} | ||
dst := &LinodeClusterTemplate{} | ||
|
||
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) | ||
} | ||
}), | ||
), | ||
), | ||
) | ||
} |
Oops, something went wrong.