Skip to content

Commit

Permalink
Add MachinePool conversion
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Bueringer <[email protected]>
  • Loading branch information
sbueringer committed Sep 22, 2021
1 parent 2ebdffb commit b276512
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 15 deletions.
2 changes: 2 additions & 0 deletions config/crd/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ patchesStrategicMerge:
# patches here are for enabling the conversion webhook for each CRD
- patches/webhook_in_clusterclasses.yaml
- patches/webhook_in_clusters.yaml
- patches/webhook_in_machinepools.yaml
- patches/webhook_in_machines.yaml
- patches/webhook_in_machinesets.yaml
- patches/webhook_in_machinedeployments.yaml
Expand All @@ -28,6 +29,7 @@ patchesStrategicMerge:
# patches here are for enabling the CA injection for each CRD
- patches/cainjection_in_clusterclasses.yaml
- patches/cainjection_in_clusters.yaml
- patches/cainjection_in_machinepools.yaml
- patches/cainjection_in_machines.yaml
- patches/cainjection_in_machinesets.yaml
- patches/cainjection_in_machinedeployments.yaml
Expand Down
8 changes: 8 additions & 0 deletions config/crd/patches/cainjection_in_machinepools.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# The following patch adds a directive for certmanager to inject CA into the CRD
# CRD conversion requires k8s 1.13 or later.
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
cert-manager.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME)
name: machinepools.cluster.x-k8s.io
19 changes: 19 additions & 0 deletions config/crd/patches/webhook_in_machinepools.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# The following patch enables conversion webhook for CRD
# CRD conversion requires k8s 1.13 or later.
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: machinepools.cluster.x-k8s.io
spec:
conversion:
strategy: Webhook
webhook:
conversionReviewVersions: ["v1", "v1beta1"]
clientConfig:
# this is "\n" used as a placeholder, otherwise it will be rejected by the apiserver for being blank,
# but we're going to set it later using the cert-manager (or potentially a patch if not using cert-manager)
caBundle: Cg==
service:
namespace: system
name: webhook-service
path: /convert
49 changes: 44 additions & 5 deletions exp/api/v1alpha3/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,65 @@ limitations under the License.
package v1alpha3

import (
"k8s.io/apimachinery/pkg/conversion"
v1beta1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
apimachineryconversion "k8s.io/apimachinery/pkg/conversion"
"sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)

// Convert_v1alpha3_MachinePoolSpec_To_v1beta1_MachinePoolSpec is an autogenerated conversion function.
func Convert_v1alpha3_MachinePoolSpec_To_v1beta1_MachinePoolSpec(in *MachinePoolSpec, out *v1beta1.MachinePoolSpec, s conversion.Scope) error {
func Convert_v1alpha3_MachinePoolSpec_To_v1beta1_MachinePoolSpec(in *MachinePoolSpec, out *v1beta1.MachinePoolSpec, s apimachineryconversion.Scope) error {
return autoConvert_v1alpha3_MachinePoolSpec_To_v1beta1_MachinePoolSpec(in, out, s)
}

func Convert_v1alpha3_MachinePool_To_v1beta1_MachinePool(in *MachinePool, out *v1beta1.MachinePool, s conversion.Scope) error {
func Convert_v1alpha3_MachinePool_To_v1beta1_MachinePool(in *MachinePool, out *v1beta1.MachinePool, s apimachineryconversion.Scope) error {
if err := autoConvert_v1alpha3_MachinePool_To_v1beta1_MachinePool(in, out, s); err != nil {
return err
}

// Replace v1alpha3 finalizer to allow old MachinePools to get deleted.
if !controllerutil.ContainsFinalizer(out, MachinePoolFinalizer) {
if controllerutil.ContainsFinalizer(out, MachinePoolFinalizer) {
controllerutil.RemoveFinalizer(out, MachinePoolFinalizer)
controllerutil.AddFinalizer(out, v1beta1.MachinePoolFinalizer)
}

return nil
}

func Convert_v1beta1_MachinePool_To_v1alpha3_MachinePool(in *v1beta1.MachinePool, out *MachinePool, s apimachineryconversion.Scope) error {
if err := autoConvert_v1beta1_MachinePool_To_v1alpha3_MachinePool(in, out, s); err != nil {
return err
}

// Replace v1beta1 finalizer to allow old MachinePools to get deleted.
if controllerutil.ContainsFinalizer(out, v1beta1.MachinePoolFinalizer) {
controllerutil.RemoveFinalizer(out, v1beta1.MachinePoolFinalizer)
controllerutil.AddFinalizer(out, MachinePoolFinalizer)
}

return nil
}

func (src *MachinePool) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*v1beta1.MachinePool)

return Convert_v1alpha3_MachinePool_To_v1beta1_MachinePool(src, dst, nil)
}

func (dst *MachinePool) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*v1beta1.MachinePool)

return Convert_v1beta1_MachinePool_To_v1alpha3_MachinePool(src, dst, nil)
}

func (src *MachinePoolList) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*v1beta1.MachinePoolList)

return Convert_v1alpha3_MachinePoolList_To_v1beta1_MachinePoolList(src, dst, nil)
}

func (dst *MachinePoolList) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*v1beta1.MachinePoolList)

return Convert_v1beta1_MachinePoolList_To_v1alpha3_MachinePoolList(src, dst, nil)
}
70 changes: 70 additions & 0 deletions exp/api/v1alpha3/conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Copyright 2021 The Kubernetes Authors.
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 v1alpha3

import (
"testing"

fuzz "github.com/google/gofuzz"
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
clusterv1exp "sigs.k8s.io/cluster-api/exp/api/v1beta1"
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
)

func TestFuzzyConversion(t *testing.T) {
t.Run("for MachinePool", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &clusterv1exp.MachinePool{},
Spoke: &MachinePool{},
FuzzerFuncs: []fuzzer.FuzzerFuncs{fuzzFuncs},
}))
}

func fuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} {
return []interface{}{
BootstrapFuzzer,
MachinePoolSpecFuzzer,
ObjectMetaFuzzer,
}
}

func BootstrapFuzzer(obj *clusterv1.Bootstrap, c fuzz.Continue) {
c.FuzzNoCustom(obj)

// Bootstrap.Data has been removed in v1alpha4, so setting it to nil in order to avoid v1alpha3 --> <hub> --> v1alpha3 round trip errors.
obj.Data = nil
}

func ObjectMetaFuzzer(in *clusterv1.ObjectMeta, c fuzz.Continue) {
c.FuzzNoCustom(in)

// These fields have been removed in v1beta1
// data is going to be lost, so we're forcing zero values here.
in.Name = ""
in.GenerateName = ""
in.Namespace = ""
in.OwnerReferences = nil
}

func MachinePoolSpecFuzzer(in *MachinePoolSpec, c fuzz.Continue) {
c.Fuzz(in)

// These fields have been removed in v1beta1
// data is going to be lost, so we're forcing zero values here.
in.Strategy = nil
}
15 changes: 5 additions & 10 deletions exp/api/v1alpha3/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions exp/api/v1alpha4/conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright 2021 The Kubernetes Authors.
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 v1alpha4

import (
v1beta1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)

func (src *MachinePool) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*v1beta1.MachinePool)

return Convert_v1alpha4_MachinePool_To_v1beta1_MachinePool(src, dst, nil)
}

func (dst *MachinePool) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*v1beta1.MachinePool)

return Convert_v1beta1_MachinePool_To_v1alpha4_MachinePool(src, dst, nil)
}

func (src *MachinePoolList) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*v1beta1.MachinePoolList)

return Convert_v1alpha4_MachinePoolList_To_v1beta1_MachinePoolList(src, dst, nil)
}

func (dst *MachinePoolList) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*v1beta1.MachinePoolList)

return Convert_v1beta1_MachinePoolList_To_v1alpha4_MachinePoolList(src, dst, nil)
}
33 changes: 33 additions & 0 deletions exp/api/v1alpha4/conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright 2021 The Kubernetes Authors.
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 v1alpha4

import (
"testing"

"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
clusterv1exp "sigs.k8s.io/cluster-api/exp/api/v1beta1"
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
)

func TestFuzzyConversion(t *testing.T) {
t.Run("for MachinePool", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &clusterv1exp.MachinePool{},
Spoke: &MachinePool{},
FuzzerFuncs: []fuzzer.FuzzerFuncs{},
}))
}
20 changes: 20 additions & 0 deletions exp/api/v1beta1/conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Copyright 2021 The Kubernetes Authors.
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 v1beta1

func (*MachinePool) Hub() {}
func (*MachinePoolList) Hub() {}

0 comments on commit b276512

Please sign in to comment.