Skip to content

Commit 0e67f05

Browse files
v1alpha4 webhook support (#18)
* initial work * make test * make build * implement hub and conversion * test e2e with clusterctl * use go 1.16 * typo changes rename MAASMachine MinMemory and MinCPU change from optional to mendatory * webhook tests * make fmt * debug with sleep * correct output base * remove sleep * use autoconvert * add template * template e2e test * remove omitempty Co-authored-by: Roshani Rathi <[email protected]>
1 parent 4938e93 commit 0e67f05

File tree

70 files changed

+4744
-650
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+4744
-650
lines changed

Makefile

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,20 @@ MANIFEST_DIR=_build/manifests
1111
BUILD_DIR :=_build
1212
RELEASE_DIR := _build/release
1313
DEV_DIR := _build/dev
14+
REPO_ROOT := $(shell git rev-parse --show-toplevel)
1415

1516
# Image URL to use all building/pushing image targets
1617
IMAGE_NAME := cluster-api-provider-maas-controller
1718
IMG_URL ?= gcr.io/$(shell gcloud config get-value project)/${USER}
1819
IMG_TAG ?= latest
1920
IMG ?= ${IMG_URL}/cluster-api-provider-maas:${IMG_TAG}
2021

22+
# Set --output-base for conversion-gen if we are not within GOPATH
23+
ifneq ($(abspath $(REPO_ROOT)),$(shell go env GOPATH)/src/github.com/spectrocloud/cluster-api-provider-maas)
24+
GEN_OUTPUT_BASE := --output-base=$(REPO_ROOT)
25+
else
26+
export GOPATH := $(shell go env GOPATH)
27+
endif
2128

2229
# Release images
2330
# Release docker variables
@@ -93,6 +100,7 @@ endif
93100
release-manifests: test
94101
$(MAKE) manifests STAGE=release MANIFEST_DIR=$(RELEASE_DIR) PULL_POLICY=IfNotPresent IMAGE=$(RELEASE_CONTROLLER_IMG):$(VERSION)
95102
cp metadata.yaml $(RELEASE_DIR)/metadata.yaml
103+
$(MAKE) templates OUTPUT_DIR=$(RELEASE_DIR)
96104

97105
.PHONY: release-overrides
98106
release-overrides:
@@ -102,6 +110,7 @@ release-overrides:
102110
dev-manifests:
103111
$(MAKE) manifests STAGE=dev MANIFEST_DIR=$(DEV_DIR) PULL_POLICY=Always IMAGE=$(IMG)
104112
cp metadata.yaml $(DEV_DIR)/metadata.yaml
113+
$(MAKE) templates OUTPUT_DIR=$(DEV_DIR)
105114

106115
# Generate manifests e.g. CRD, RBAC etc.
107116
manifests: $(CONTROLLER_GEN) $(MANIFEST_DIR) $(KUSTOMIZE) $(BUILD_DIR) ## Generate manifests e.g. CRD, RBAC etc.
@@ -120,13 +129,20 @@ vet: ## Run go vet against code
120129
go vet ./...
121130

122131
# Generate code
123-
generate: $(CONTROLLER_GEN)
132+
generate: $(CONTROLLER_GEN) $(CONVERSION_GEN)
124133
$(MAKE) generate-go
125134
$(MAKE) generate-manifests
126135

127136
generate-go:
128137
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
129138

139+
$(CONVERSION_GEN) \
140+
--input-dirs=./api/v1alpha3 \
141+
--extra-peer-dirs=github.com/spectrocloud/cluster-api-provider-maas/api/v1alpha3 \
142+
--build-tag=ignore_autogenerated_core_v1alpha3 \
143+
--output-file-base=zz_generated.conversion $(GEN_OUTPUT_BASE) \
144+
--go-header-file=./hack/boilerplate.go.txt
145+
130146
generate-manifests: ## Generate manifests
131147
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
132148

@@ -152,5 +168,9 @@ release: release-manifests
152168
$(MAKE) docker-build IMG=$(RELEASE_CONTROLLER_IMG):$(VERSION)
153169
$(MAKE) docker-push IMG=$(RELEASE_CONTROLLER_IMG):$(VERSION)
154170

171+
.PHONY: templates
172+
templates: ## Generate release templates
173+
cp templates/cluster-template*.yaml $(OUTPUT_DIR)/
174+
155175
version: ## Prints version of current make
156176
@echo $(VERSION)

PROJECT

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,13 @@ resources:
1010
- group: infrastructure
1111
kind: MaasMachineTemplate
1212
version: v1alpha3
13+
- group: infrastructure
14+
kind: MaasMachine
15+
version: v1alpha4
16+
- group: infrastructure
17+
kind: MaasCluster
18+
version: v1alpha4
19+
- group: infrastructure
20+
kind: MaasMachineTemplate
21+
version: v1alpha4
1322
version: "2"

api/v1alpha3/condition_consts.go

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package v1alpha3
1818

1919
import clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
2020

21-
// Conditions and condition Reasons for the DockerMachine object
21+
// Conditions and condition Reasons for the MAAS Machine object
2222

2323
const (
2424
// MachineDeployedCondition documents the status of the deployment of a machine
@@ -55,28 +55,6 @@ const (
5555
MachineDeployStartedReason = "MachineDeployStartedReason"
5656
)
5757

58-
//const (
59-
// // BootstrapExecSucceededCondition provides an observation of the DockerMachine bootstrap process.
60-
// // It is set based on successful execution of bootstrap commands and on the existence of
61-
// // the /run/cluster-api/bootstrap-success.complete file.
62-
// // The condition gets generated after ContainerProvisionedCondition is True.
63-
// //
64-
// // NOTE as a difference from other providers, container provisioning and bootstrap are directly managed
65-
// // by the DockerMachine controller (not by cloud-init).
66-
// BootstrapExecSucceededCondition clusterv1.ConditionType = "BootstrapExecSucceeded"
67-
//
68-
// // BootstrappingReason documents (Severity=Info) a DockerMachine currently executing the bootstrap
69-
// // script that creates the Kubernetes node on the newly provisioned machine infrastructure.
70-
// BootstrappingReason = "Bootstrapping"
71-
//
72-
// // BootstrapFailedReason documents (Severity=Warning) a DockerMachine controller detecting an error while
73-
// // bootstrapping the Kubernetes node on the machine just provisioned; those kind of errors are usually
74-
// // transient and failed bootstrap are automatically re-tried by the controller.
75-
// BootstrapFailedReason = "BootstrapFailed"
76-
//)
77-
//
78-
// Conditions and condition Reasons for the DockerCluster object
79-
8058
const (
8159
// Only applicable to control plane machines. DNSAttachedCondition will report true when a control plane is successfully registered with an DNS
8260
// When set to false, severity can be an Error if the subnet is not found or unavailable in the instance's AZ
@@ -90,12 +68,10 @@ const (
9068

9169
const (
9270
// DNSReadyCondition documents the availability of the container that implements the cluster DNS.
93-
//
9471
DNSReadyCondition clusterv1.ConditionType = "LoadBalancerReady"
9572

96-
// LoadBalancerProvisioningFailedReason (Severity=Warning) documents a DockerCluster controller detecting
97-
// an error while provisioning the container that provides the cluster load balancer.; those kind of
98-
// errors are usually transient and failed provisioning are automatically re-tried by the controller.
73+
// LoadBalancerProvisioningFailedReason (Severity=Warning) documents a MAASCluster controller detecting
74+
// dns reconcile failure will be retried
9975
DNSFailedReason = "LoadBalancerFailed"
10076

10177
WaitForDNSNameReason = "WaitForDNSName"

api/v1alpha3/conversion.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
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 v1alpha3
18+
19+
import (
20+
apiconversion "k8s.io/apimachinery/pkg/conversion"
21+
"sigs.k8s.io/controller-runtime/pkg/conversion"
22+
"unsafe"
23+
24+
"github.com/spectrocloud/cluster-api-provider-maas/api/v1alpha4"
25+
)
26+
27+
func (in *MaasCluster) ConvertTo(dstRaw conversion.Hub) error {
28+
dst := dstRaw.(*v1alpha4.MaasCluster)
29+
30+
return Convert_v1alpha3_MaasCluster_To_v1alpha4_MaasCluster(in, dst, nil)
31+
}
32+
33+
func (in *MaasCluster) ConvertFrom(srcRaw conversion.Hub) error {
34+
src := srcRaw.(*v1alpha4.MaasCluster)
35+
36+
return Convert_v1alpha4_MaasCluster_To_v1alpha3_MaasCluster(src, in, nil)
37+
}
38+
39+
func (in *MaasClusterList) ConvertTo(dstRaw conversion.Hub) error {
40+
dst := dstRaw.(*v1alpha4.MaasClusterList)
41+
42+
return Convert_v1alpha3_MaasClusterList_To_v1alpha4_MaasClusterList(in, dst, nil)
43+
}
44+
45+
func (in *MaasClusterList) ConvertFrom(srcRaw conversion.Hub) error {
46+
src := srcRaw.(*v1alpha4.MaasClusterList)
47+
48+
return Convert_v1alpha4_MaasClusterList_To_v1alpha3_MaasClusterList(src, in, nil)
49+
}
50+
51+
func (in *MaasMachine) ConvertTo(dstRaw conversion.Hub) error {
52+
dst := dstRaw.(*v1alpha4.MaasMachine)
53+
54+
return Convert_v1alpha3_MaasMachine_To_v1alpha4_MaasMachine(in, dst, nil)
55+
}
56+
57+
func (in *MaasMachine) ConvertFrom(srcRaw conversion.Hub) error {
58+
src := srcRaw.(*v1alpha4.MaasMachine)
59+
60+
return Convert_v1alpha4_MaasMachine_To_v1alpha3_MaasMachine(src, in, nil)
61+
}
62+
63+
func (in *MaasMachineList) ConvertTo(dstRaw conversion.Hub) error {
64+
dst := dstRaw.(*v1alpha4.MaasMachineList)
65+
66+
return Convert_v1alpha3_MaasMachineList_To_v1alpha4_MaasMachineList(in, dst, nil)
67+
}
68+
69+
func (in *MaasMachineList) ConvertFrom(srcRaw conversion.Hub) error {
70+
src := srcRaw.(*v1alpha4.MaasMachineList)
71+
72+
return Convert_v1alpha4_MaasMachineList_To_v1alpha3_MaasMachineList(src, in, nil)
73+
}
74+
75+
func (in *MaasMachineTemplate) ConvertTo(dstRaw conversion.Hub) error {
76+
dst := dstRaw.(*v1alpha4.MaasMachineTemplate)
77+
78+
return Convert_v1alpha3_MaasMachineTemplate_To_v1alpha4_MaasMachineTemplate(in, dst, nil)
79+
}
80+
81+
func (in *MaasMachineTemplate) ConvertFrom(srcRaw conversion.Hub) error {
82+
src := srcRaw.(*v1alpha4.MaasMachineTemplate)
83+
84+
return Convert_v1alpha4_MaasMachineTemplate_To_v1alpha3_MaasMachineTemplate(src, in, nil)
85+
}
86+
87+
func (in *MaasMachineTemplateList) ConvertTo(dstRaw conversion.Hub) error {
88+
dst := dstRaw.(*v1alpha4.MaasMachineTemplateList)
89+
90+
return Convert_v1alpha3_MaasMachineTemplateList_To_v1alpha4_MaasMachineTemplateList(in, dst, nil)
91+
}
92+
93+
func (in *MaasMachineTemplateList) ConvertFrom(srcRaw conversion.Hub) error {
94+
src := srcRaw.(*v1alpha4.MaasMachineTemplateList)
95+
96+
return Convert_v1alpha4_MaasMachineTemplateList_To_v1alpha3_MaasMachineTemplateList(src, in, nil)
97+
}
98+
99+
func Convert_v1alpha4_MaasMachineSpec_To_v1alpha3_MaasMachineSpec(in *v1alpha4.MaasMachineSpec, out *MaasMachineSpec, s apiconversion.Scope) error {
100+
if err := autoConvert_v1alpha4_MaasMachineSpec_To_v1alpha3_MaasMachineSpec(in, out, s); err != nil {
101+
return err
102+
}
103+
out.MinMemory = (*int)(unsafe.Pointer(in.MinMemoryInMB))
104+
return nil
105+
}
106+
107+
func Convert_v1alpha3_MaasMachineSpec_To_v1alpha4_MaasMachineSpec(in *MaasMachineSpec, out *v1alpha4.MaasMachineSpec, s apiconversion.Scope) error {
108+
if err := autoConvert_v1alpha3_MaasMachineSpec_To_v1alpha4_MaasMachineSpec(in, out, s); err != nil {
109+
return err
110+
}
111+
out.MinMemoryInMB = (*int)(unsafe.Pointer(in.MinMemory))
112+
return nil
113+
}

api/v1alpha3/conversion_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
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 v1alpha3
18+
19+
import (
20+
"testing"
21+
22+
. "github.com/onsi/gomega"
23+
"k8s.io/apimachinery/pkg/runtime"
24+
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
25+
26+
"github.com/spectrocloud/cluster-api-provider-maas/api/v1alpha4"
27+
)
28+
29+
func TestFuzzyConversion(t *testing.T) {
30+
g := NewWithT(t)
31+
scheme := runtime.NewScheme()
32+
g.Expect(AddToScheme(scheme)).To(Succeed())
33+
g.Expect(v1alpha4.AddToScheme(scheme)).To(Succeed())
34+
35+
t.Run("for MaasCluster", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
36+
Scheme: scheme,
37+
Hub: &v1alpha4.MaasCluster{},
38+
Spoke: &MaasCluster{},
39+
}))
40+
41+
t.Run("for MaasMachine", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
42+
Scheme: scheme,
43+
Hub: &v1alpha4.MaasMachine{},
44+
Spoke: &MaasMachine{},
45+
}))
46+
47+
t.Run("for MaasMachineTemplate", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
48+
Scheme: scheme,
49+
Hub: &v1alpha4.MaasMachineTemplate{},
50+
Spoke: &MaasMachineTemplate{},
51+
}))
52+
}

api/v1alpha3/doc.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
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+
// +k8s:conversion-gen=github.com/spectrocloud/cluster-api-provider-maas/api/v1alpha4
18+
package v1alpha3

api/v1alpha3/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
)

api/v1alpha3/maascluster_types.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ func (in APIEndpoint) IsZero() bool {
8383

8484
// +kubebuilder:resource:path=maasclusters,scope=Namespaced,categories=cluster-api
8585
// +kubebuilder:subresource:status
86-
// +kubebuilder:storageversion
8786
// +kubebuilder:object:root=true
8887

8988
// MaasCluster is the Schema for the maasclusters API

api/v1alpha3/maasmachine_types.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type MaasMachineSpec struct {
4444
// +optional
4545
ProviderID *string `json:"providerID,omitempty"`
4646

47-
// ResourcePool will be the MaaS machine ID
47+
// ResourcePool will be the MAAS Machine resourcepool
4848
// +optional
4949
ResourcePool *string `json:"resourcePool,omitempty"`
5050

@@ -67,8 +67,8 @@ type MaasMachineStatus struct {
6767
// +kubebuilder:default=false
6868
Ready bool `json:"ready"`
6969

70-
// InstanceState is the state of the AWS instance for this machine.
71-
MachineState *MachineState `json:"instanceState,omitempty"`
70+
// MachineState is the state of this MAAS machine.
71+
MachineState *MachineState `json:"machineState,omitempty"`
7272

7373
// MachinePowered is if the machine is "Powered" on
7474
MachinePowered bool `json:"machinePowered,omitempty"`
@@ -98,7 +98,6 @@ type MaasMachineStatus struct {
9898

9999
// +kubebuilder:resource:path=maasmachines,scope=Namespaced,categories=cluster-api
100100
// +kubebuilder:object:root=true
101-
// +kubebuilder:storageversion
102101
// +kubebuilder:subresource:status
103102

104103
// MaasMachine is the Schema for the maasmachines API

api/v1alpha3/maasmachinetemplate_types.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ type MaasMachineTemplateSpec struct {
2727

2828
// +kubebuilder:object:root=true
2929
// +kubebuilder:resource:path=maasmachinetemplates,scope=Namespaced,categories=cluster-api
30-
// +kubebuilder:storageversion
3130

3231
// MaasMachineTemplate is the Schema for the maasmachinetemplates API
3332
type MaasMachineTemplate struct {

0 commit comments

Comments
 (0)