Skip to content

Commit

Permalink
add v1alpha2 of linodeClusterTemplate (#425)
Browse files Browse the repository at this point in the history
  • Loading branch information
eljohnson92 authored Jul 25, 2024
1 parent 27f5949 commit a417bbc
Show file tree
Hide file tree
Showing 21 changed files with 1,706 additions and 13 deletions.
15 changes: 12 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ help: ## Display this help.

##@ Generate:
.PHONY: generate
generate: generate-manifests generate-code generate-mock
generate: generate-manifests generate-conversion generate-code generate-mock

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

.PHONY: generate-conversion
generate-conversion: conversion-gen
$(CONVERSION_GEN) ./api/v1alpha1 --go-header-file=./hack/boilerplate.go.txt --output-file=zz_generated.conversion.go

.PHONY: generate-mock
generate-mock: mockgen ## Generate mocks for the Linode API client.
$(MOCKGEN) -source=./clients/clients.go -destination ./mock/client.go -package mock
Expand Down Expand Up @@ -144,7 +148,7 @@ docs:
.PHONY: test
test: generate fmt vet envtest ## Run tests.
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
grep -v "zz_generated.deepcopy.go" cover.out.tmp > cover.out
grep -v "zz_generated.*" cover.out.tmp > cover.out
rm cover.out.tmp

.PHONY: e2etest
Expand Down Expand Up @@ -297,6 +301,7 @@ CTLPTL ?= $(LOCALBIN)/ctlptl
CLUSTERCTL ?= $(LOCALBIN)/clusterctl
KUBEBUILDER ?= $(LOCALBIN)/kubebuilder
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
CONVERSION_GEN ?= $(CACHE_BIN)/conversion-gen
TILT ?= $(LOCALBIN)/tilt
KIND ?= $(LOCALBIN)/kind
CHAINSAW ?= $(LOCALBIN)/chainsaw
Expand All @@ -323,7 +328,7 @@ MOCKGEN_VERSION ?= v0.4.0
GOWRAP_VERSION ?= latest

.PHONY: tools
tools: $(KUSTOMIZE) $(CTLPTL) $(CLUSTERCTL) $(CONTROLLER_GEN) $(TILT) $(KIND) $(CHAINSAW) $(ENVTEST) $(HUSKY) $(NILAWAY) $(GOVULNC) $(MOCKGEN) $(GOWRAP)
tools: $(KUSTOMIZE) $(CTLPTL) $(CLUSTERCTL) $(CONTROLLER_GEN) $(CONVERSION_GEN) $(TILT) $(KIND) $(CHAINSAW) $(ENVTEST) $(HUSKY) $(NILAWAY) $(GOVULNC) $(MOCKGEN) $(GOWRAP)


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

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

.PHONY: tilt
tilt: $(TILT) ## Download tilt locally if necessary.
Expand Down
11 changes: 11 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,15 @@ resources:
conversion: true
validation: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
domain: cluster.x-k8s.io
group: infrastructure
kind: LinodeClusterTemplate
path: github.com/linode/cluster-api-provider-linode/api/v1alpha2
version: v1alpha2
webhooks:
conversion: true
webhookVersion: v1
version: "3"
40 changes: 40 additions & 0 deletions api/v1alpha1/conversion.go
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
}
19 changes: 19 additions & 0 deletions api/v1alpha1/doc.go
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
2 changes: 2 additions & 0 deletions api/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ var (

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme

localSchemeBuilder = SchemeBuilder.SchemeBuilder
)
83 changes: 83 additions & 0 deletions api/v1alpha1/linodeclustertemplate_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 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)
}
165 changes: 165 additions & 0 deletions api/v1alpha1/linodeclustertemplate_conversion_test.go
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)
}
}),
),
),
)
}
Loading

0 comments on commit a417bbc

Please sign in to comment.