Skip to content

Commit a417bbc

Browse files
authored
add v1alpha2 of linodeClusterTemplate (#425)
1 parent 27f5949 commit a417bbc

21 files changed

+1706
-13
lines changed

Makefile

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ help: ## Display this help.
7878

7979
##@ Generate:
8080
.PHONY: generate
81-
generate: generate-manifests generate-code generate-mock
81+
generate: generate-manifests generate-conversion generate-code generate-mock
8282

8383
.PHONY: generate-manifests
8484
generate-manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
@@ -89,6 +89,10 @@ generate-code: controller-gen gowrap ## Generate code containing DeepCopy, DeepC
8989
go generate ./...
9090
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
9191

92+
.PHONY: generate-conversion
93+
generate-conversion: conversion-gen
94+
$(CONVERSION_GEN) ./api/v1alpha1 --go-header-file=./hack/boilerplate.go.txt --output-file=zz_generated.conversion.go
95+
9296
.PHONY: generate-mock
9397
generate-mock: mockgen ## Generate mocks for the Linode API client.
9498
$(MOCKGEN) -source=./clients/clients.go -destination ./mock/client.go -package mock
@@ -144,7 +148,7 @@ docs:
144148
.PHONY: test
145149
test: generate fmt vet envtest ## Run tests.
146150
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(CACHE_BIN) -p path)" go test -race -timeout 60s `go list ./... | grep -v ./mock$$` -coverprofile cover.out.tmp
147-
grep -v "zz_generated.deepcopy.go" cover.out.tmp > cover.out
151+
grep -v "zz_generated.*" cover.out.tmp > cover.out
148152
rm cover.out.tmp
149153

150154
.PHONY: e2etest
@@ -297,6 +301,7 @@ CTLPTL ?= $(LOCALBIN)/ctlptl
297301
CLUSTERCTL ?= $(LOCALBIN)/clusterctl
298302
KUBEBUILDER ?= $(LOCALBIN)/kubebuilder
299303
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
304+
CONVERSION_GEN ?= $(CACHE_BIN)/conversion-gen
300305
TILT ?= $(LOCALBIN)/tilt
301306
KIND ?= $(LOCALBIN)/kind
302307
CHAINSAW ?= $(LOCALBIN)/chainsaw
@@ -323,7 +328,7 @@ MOCKGEN_VERSION ?= v0.4.0
323328
GOWRAP_VERSION ?= latest
324329

325330
.PHONY: tools
326-
tools: $(KUSTOMIZE) $(CTLPTL) $(CLUSTERCTL) $(CONTROLLER_GEN) $(TILT) $(KIND) $(CHAINSAW) $(ENVTEST) $(HUSKY) $(NILAWAY) $(GOVULNC) $(MOCKGEN) $(GOWRAP)
331+
tools: $(KUSTOMIZE) $(CTLPTL) $(CLUSTERCTL) $(CONTROLLER_GEN) $(CONVERSION_GEN) $(TILT) $(KIND) $(CHAINSAW) $(ENVTEST) $(HUSKY) $(NILAWAY) $(GOVULNC) $(MOCKGEN) $(GOWRAP)
327332

328333

329334
.PHONY: kustomize
@@ -353,6 +358,10 @@ controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessar
353358
$(CONTROLLER_GEN): $(LOCALBIN)
354359
GOBIN=$(CACHE_BIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
355360

361+
.PHONY: conversion-gen
362+
conversion-gen: $(CONVERSION_GEN) ## Download conversion-gen locally if necessary.
363+
$(CONVERSION_GEN): $(LOCALBIN)
364+
GOBIN=$(CACHE_BIN) go install k8s.io/code-generator/cmd/conversion-gen@latest
356365

357366
.PHONY: tilt
358367
tilt: $(TILT) ## Download tilt locally if necessary.

PROJECT

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,15 @@ resources:
119119
conversion: true
120120
validation: true
121121
webhookVersion: v1
122+
- api:
123+
crdVersion: v1
124+
namespaced: true
125+
domain: cluster.x-k8s.io
126+
group: infrastructure
127+
kind: LinodeClusterTemplate
128+
path: github.com/linode/cluster-api-provider-linode/api/v1alpha2
129+
version: v1alpha2
130+
webhooks:
131+
conversion: true
132+
webhookVersion: v1
122133
version: "3"

api/v1alpha1/conversion.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
"k8s.io/apimachinery/pkg/conversion"
21+
22+
infrastructurev1alpha2 "github.com/linode/cluster-api-provider-linode/api/v1alpha2"
23+
)
24+
25+
func Convert_v1alpha1_NetworkSpec_To_v1alpha2_NetworkSpec(in *NetworkSpec, out *infrastructurev1alpha2.NetworkSpec, s conversion.Scope) error {
26+
out.ApiserverNodeBalancerConfigID = in.NodeBalancerConfigID
27+
out.ApiserverLoadBalancerPort = in.LoadBalancerPort
28+
out.LoadBalancerType = in.LoadBalancerType
29+
out.NodeBalancerID = in.NodeBalancerID
30+
out.AdditionalPorts = make([]infrastructurev1alpha2.LinodeNBPortConfig, 0)
31+
return nil
32+
}
33+
34+
func Convert_v1alpha2_NetworkSpec_To_v1alpha1_NetworkSpec(in *infrastructurev1alpha2.NetworkSpec, out *NetworkSpec, s conversion.Scope) error {
35+
out.NodeBalancerConfigID = in.ApiserverNodeBalancerConfigID
36+
out.LoadBalancerPort = in.ApiserverLoadBalancerPort
37+
out.LoadBalancerType = in.LoadBalancerType
38+
out.NodeBalancerID = in.NodeBalancerID
39+
return nil
40+
}

api/v1alpha1/doc.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
// +groupName=infrastructure.cluster.x-k8s.io
18+
// +k8s:conversion-gen=github.com/linode/cluster-api-provider-linode/api/v1alpha2
19+
package v1alpha1

api/v1alpha1/groupversion_info.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,6 @@ var (
3333

3434
// AddToScheme adds the types in this group-version to the given scheme.
3535
AddToScheme = SchemeBuilder.AddToScheme
36+
37+
localSchemeBuilder = SchemeBuilder.SchemeBuilder
3638
)
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 LinodeCluster to the Hub version (v1alpha2).
29+
func (src *LinodeClusterTemplate) ConvertTo(dstRaw conversion.Hub) error {
30+
dst, ok := dstRaw.(*infrastructurev1alpha2.LinodeClusterTemplate)
31+
if !ok {
32+
return errors.New("failed to convert LinodeClusterTemplate version from v1alpha1 to v1alpha2")
33+
}
34+
35+
if err := Convert_v1alpha1_LinodeClusterTemplate_To_v1alpha2_LinodeClusterTemplate(src, dst, nil); err != nil {
36+
return err
37+
}
38+
39+
// Manually restore data from annotations
40+
restored := &LinodeClusterTemplate{}
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 *LinodeClusterTemplate) ConvertFrom(srcRaw conversion.Hub) error {
50+
src, ok := srcRaw.(*infrastructurev1alpha2.LinodeClusterTemplate)
51+
if !ok {
52+
return errors.New("failed to convert LinodeClusterTemplate version from v1alpha2 to v1alpha1")
53+
}
54+
55+
if err := Convert_v1alpha2_LinodeClusterTemplate_To_v1alpha1_LinodeClusterTemplate(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 DOClusterList to the Hub version (v1alpha2).
68+
func (src *LinodeClusterTemplateList) ConvertTo(dstRaw conversion.Hub) error {
69+
dst, ok := dstRaw.(*infrastructurev1alpha2.LinodeClusterTemplateList)
70+
if !ok {
71+
return errors.New("failed to convert LinodeClusterTemplate version from v1alpha1 to v1alpha2")
72+
}
73+
return Convert_v1alpha1_LinodeClusterTemplateList_To_v1alpha2_LinodeClusterTemplateList(src, dst, nil)
74+
}
75+
76+
// ConvertFrom converts from the Hub version (v1alpha2) to this version.
77+
func (dst *LinodeClusterTemplateList) ConvertFrom(srcRaw conversion.Hub) error {
78+
src, ok := srcRaw.(*infrastructurev1alpha2.LinodeClusterTemplateList)
79+
if !ok {
80+
return errors.New("failed to convert LinodeClusterTemplate version from v1alpha2 to v1alpha1")
81+
}
82+
return Convert_v1alpha2_LinodeClusterTemplateList_To_v1alpha1_LinodeClusterTemplateList(src, dst, nil)
83+
}
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"k8s.io/utils/ptr"
26+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
27+
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
28+
29+
infrav1alpha2 "github.com/linode/cluster-api-provider-linode/api/v1alpha2"
30+
"github.com/linode/cluster-api-provider-linode/mock"
31+
32+
. "github.com/linode/cluster-api-provider-linode/mock/mocktest"
33+
)
34+
35+
func TestLinodeClusterTemplateConvertTo(t *testing.T) {
36+
t.Parallel()
37+
38+
src := &LinodeClusterTemplate{
39+
ObjectMeta: metav1.ObjectMeta{
40+
Name: "test-cluster",
41+
},
42+
Spec: LinodeClusterTemplateSpec{
43+
Template: LinodeClusterTemplateResource{
44+
Spec: LinodeClusterSpec{
45+
Network: NetworkSpec{
46+
LoadBalancerType: "test-type",
47+
LoadBalancerPort: 12345,
48+
NodeBalancerID: ptr.To(1234),
49+
NodeBalancerConfigID: ptr.To(2345),
50+
},
51+
ControlPlaneEndpoint: clusterv1.APIEndpoint{Host: "1.2.3.4"},
52+
Region: "test-region",
53+
},
54+
},
55+
},
56+
}
57+
expectedDst := &infrav1alpha2.LinodeClusterTemplate{
58+
ObjectMeta: metav1.ObjectMeta{
59+
Name: "test-cluster",
60+
},
61+
Spec: infrav1alpha2.LinodeClusterTemplateSpec{
62+
Template: infrav1alpha2.LinodeClusterTemplateResource{
63+
Spec: infrav1alpha2.LinodeClusterSpec{
64+
Network: infrav1alpha2.NetworkSpec{
65+
LoadBalancerType: "test-type",
66+
ApiserverLoadBalancerPort: 12345,
67+
NodeBalancerID: ptr.To(1234),
68+
ApiserverNodeBalancerConfigID: ptr.To(2345),
69+
AdditionalPorts: []infrav1alpha2.LinodeNBPortConfig{},
70+
},
71+
ControlPlaneEndpoint: clusterv1.APIEndpoint{Host: "1.2.3.4"},
72+
Region: "test-region",
73+
},
74+
},
75+
},
76+
}
77+
dst := &infrav1alpha2.LinodeClusterTemplate{}
78+
79+
NewSuite(t, mock.MockLinodeClient{}).Run(
80+
OneOf(
81+
Path(
82+
Call("convert v1alpha1 to v1alpha2", func(ctx context.Context, mck Mock) {
83+
err := src.ConvertTo(dst)
84+
if err != nil {
85+
t.Fatalf("ConvertTo failed: %v", err)
86+
}
87+
}),
88+
Result("conversion succeeded", func(ctx context.Context, mck Mock) {
89+
if diff := cmp.Diff(expectedDst, dst); diff != "" {
90+
t.Errorf("ConvertTo() mismatch (-expected +got):\n%s", diff)
91+
}
92+
}),
93+
),
94+
),
95+
)
96+
}
97+
98+
func TestLinodeClusterTemplateConvertFrom(t *testing.T) {
99+
t.Parallel()
100+
101+
src := &infrav1alpha2.LinodeClusterTemplate{
102+
ObjectMeta: metav1.ObjectMeta{
103+
Name: "test-cluster",
104+
},
105+
Spec: infrav1alpha2.LinodeClusterTemplateSpec{
106+
Template: infrav1alpha2.LinodeClusterTemplateResource{
107+
Spec: infrav1alpha2.LinodeClusterSpec{
108+
Network: infrav1alpha2.NetworkSpec{
109+
LoadBalancerType: "test-type",
110+
ApiserverLoadBalancerPort: 12345,
111+
NodeBalancerID: ptr.To(1234),
112+
ApiserverNodeBalancerConfigID: ptr.To(2345),
113+
AdditionalPorts: []infrav1alpha2.LinodeNBPortConfig{{
114+
Port: 6443,
115+
NodeBalancerConfigID: ptr.To(12345),
116+
}},
117+
},
118+
ControlPlaneEndpoint: clusterv1.APIEndpoint{Host: "1.2.3.4"},
119+
Region: "test-region",
120+
},
121+
},
122+
},
123+
}
124+
expectedDst := &LinodeClusterTemplate{
125+
ObjectMeta: metav1.ObjectMeta{
126+
Name: "test-cluster",
127+
},
128+
Spec: LinodeClusterTemplateSpec{
129+
Template: LinodeClusterTemplateResource{
130+
Spec: LinodeClusterSpec{
131+
Network: NetworkSpec{
132+
LoadBalancerType: "test-type",
133+
LoadBalancerPort: 12345,
134+
NodeBalancerID: ptr.To(1234),
135+
NodeBalancerConfigID: ptr.To(2345),
136+
},
137+
ControlPlaneEndpoint: clusterv1.APIEndpoint{Host: "1.2.3.4"},
138+
Region: "test-region",
139+
},
140+
},
141+
},
142+
}
143+
if err := utilconversion.MarshalData(src, expectedDst); err != nil {
144+
t.Fatalf("ConvertFrom failed: %v", err)
145+
}
146+
dst := &LinodeClusterTemplate{}
147+
148+
NewSuite(t, mock.MockLinodeClient{}).Run(
149+
OneOf(
150+
Path(
151+
Call("convert v1alpha2 to v1alpha1", func(ctx context.Context, mck Mock) {
152+
err := dst.ConvertFrom(src)
153+
if err != nil {
154+
t.Fatalf("ConvertFrom failed: %v", err)
155+
}
156+
}),
157+
Result("conversion succeeded", func(ctx context.Context, mck Mock) {
158+
if diff := cmp.Diff(expectedDst, dst); diff != "" {
159+
t.Errorf("ConvertFrom() mismatch (-expected +got):\n%s", diff)
160+
}
161+
}),
162+
),
163+
),
164+
)
165+
}

0 commit comments

Comments
 (0)