Skip to content

Commit

Permalink
kcp: convert infrastructure machine ref API contract
Browse files Browse the repository at this point in the history
  • Loading branch information
sbueringer committed Aug 25, 2021
1 parent 766dfe0 commit 2b14b18
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 8 deletions.
7 changes: 7 additions & 0 deletions controlplane/kubeadm/config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ metadata:
creationTimestamp: null
name: manager-role
rules:
- apiGroups:
- apiextensions.k8s.io
resources:
- customresourcedefinitions
verbs:
- get
- list
- apiGroups:
- bootstrap.cluster.x-k8s.io
- controlplane.cluster.x-k8s.io
Expand Down
4 changes: 4 additions & 0 deletions controlplane/kubeadm/controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/record"
"k8s.io/utils/pointer"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
Expand Down Expand Up @@ -57,12 +58,14 @@ import (
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io;bootstrap.cluster.x-k8s.io;controlplane.cluster.x-k8s.io,resources=*,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=clusters;clusters/status,verbs=get;list;watch
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=machines;machines/status,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=apiextensions.k8s.io,resources=customresourcedefinitions,verbs=get;list

// KubeadmControlPlaneReconciler reconciles a KubeadmControlPlane object.
type KubeadmControlPlaneReconciler struct {
Client client.Client
controller controller.Controller
recorder record.EventRecorder
restConfig *rest.Config
Tracker *remote.ClusterCacheTracker
WatchFilterValue string

Expand Down Expand Up @@ -95,6 +98,7 @@ func (r *KubeadmControlPlaneReconciler) SetupWithManager(ctx context.Context, mg

r.controller = c
r.recorder = mgr.GetEventRecorderFor("kubeadm-control-plane-controller")
r.restConfig = mgr.GetConfig()

if r.managementCluster == nil {
if r.Tracker == nil {
Expand Down
9 changes: 5 additions & 4 deletions controlplane/kubeadm/controllers/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ func TestKubeadmControlPlaneReconciler_adoption(t *testing.T) {
Machines: collections.Machines{},
Workload: fakeWorkloadCluster{},
}
objs := []client.Object{cluster.DeepCopy(), kcp.DeepCopy(), tmpl.DeepCopy()}
objs := []client.Object{fakeGenericMachineTemplateCRD, cluster.DeepCopy(), kcp.DeepCopy(), tmpl.DeepCopy()}
for i := 0; i < 3; i++ {
name := fmt.Sprintf("test-%d", i)
m := &clusterv1.Machine{
Expand Down Expand Up @@ -532,7 +532,7 @@ func TestKubeadmControlPlaneReconciler_adoption(t *testing.T) {
Machines: collections.Machines{},
Workload: fakeWorkloadCluster{},
}
objs := []client.Object{cluster.DeepCopy(), kcp.DeepCopy(), tmpl.DeepCopy()}
objs := []client.Object{fakeGenericMachineTemplateCRD, cluster.DeepCopy(), kcp.DeepCopy(), tmpl.DeepCopy()}
for i := 0; i < 3; i++ {
name := fmt.Sprintf("test-%d", i)
m := &clusterv1.Machine{
Expand Down Expand Up @@ -640,7 +640,7 @@ func TestKubeadmControlPlaneReconciler_adoption(t *testing.T) {
Machines: collections.Machines{},
Workload: fakeWorkloadCluster{},
}
objs := []client.Object{cluster.DeepCopy(), kcp.DeepCopy(), tmpl.DeepCopy()}
objs := []client.Object{fakeGenericMachineTemplateCRD, cluster.DeepCopy(), kcp.DeepCopy(), tmpl.DeepCopy()}
for i := 0; i < 3; i++ {
name := fmt.Sprintf("test-%d", i)
m := &clusterv1.Machine{
Expand Down Expand Up @@ -720,7 +720,7 @@ func TestKubeadmControlPlaneReconciler_adoption(t *testing.T) {
Workload: fakeWorkloadCluster{},
}

fakeClient := newFakeClient(cluster.DeepCopy(), kcp.DeepCopy(), tmpl.DeepCopy(), fmc.Machines["test0"].DeepCopy())
fakeClient := newFakeClient(fakeGenericMachineTemplateCRD, cluster.DeepCopy(), kcp.DeepCopy(), tmpl.DeepCopy(), fmc.Machines["test0"].DeepCopy())
fmc.Reader = fakeClient
recorder := record.NewFakeRecorder(32)
r := &KubeadmControlPlaneReconciler{
Expand Down Expand Up @@ -847,6 +847,7 @@ kubernetesVersion: metav1.16.1`,
}

fakeClient := newFakeClient(
fakeGenericMachineTemplateCRD,
kcp.DeepCopy(),
cluster.DeepCopy(),
genericMachineTemplate.DeepCopy(),
Expand Down
5 changes: 5 additions & 0 deletions controlplane/kubeadm/controllers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/certs"
"sigs.k8s.io/cluster-api/util/conditions"
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
"sigs.k8s.io/cluster-api/util/kubeconfig"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/cluster-api/util/secret"
Expand Down Expand Up @@ -125,6 +126,10 @@ func (r *KubeadmControlPlaneReconciler) reconcileExternalReference(ctx context.C
return nil
}

if err := utilconversion.ConvertReferenceAPIContract(ctx, r.Client, r.restConfig, ref); err != nil {
return err
}

obj, err := external.Get(ctx, r.Client, ref, cluster.Namespace)
if err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion controlplane/kubeadm/controllers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"testing"

. "github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down
2 changes: 1 addition & 1 deletion controlplane/kubeadm/controllers/scale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestKubeadmControlPlaneReconciler_scaleUpControlPlane(t *testing.T) {
})
t.Run("does not create a control plane Machine if preflight checks fail", func(t *testing.T) {
cluster, kcp, genericMachineTemplate := createClusterWithControlPlane(metav1.NamespaceDefault)
initObjs := []client.Object{cluster.DeepCopy(), kcp.DeepCopy(), genericMachineTemplate.DeepCopy()}
initObjs := []client.Object{fakeGenericMachineTemplateCRD, cluster.DeepCopy(), kcp.DeepCopy(), genericMachineTemplate.DeepCopy()}
cluster.Spec.ControlPlaneEndpoint.Host = "nodomain.example.com"
cluster.Spec.ControlPlaneEndpoint.Port = 6443

Expand Down
21 changes: 21 additions & 0 deletions controlplane/kubeadm/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"os"
"testing"

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/cluster-api/internal/envtest"
ctrl "sigs.k8s.io/controller-runtime"
// +kubebuilder:scaffold:imports
Expand All @@ -28,6 +30,25 @@ import (
var (
env *envtest.Environment
ctx = ctrl.SetupSignalHandler()
// TODO(sbueringer): move under internal/testtypes (or refactor it in a way that we don't need it anymore).
fakeGenericMachineTemplateCRD = &apiextensionsv1.CustomResourceDefinition{
TypeMeta: metav1.TypeMeta{
APIVersion: apiextensionsv1.SchemeGroupVersion.String(),
Kind: "CustomResourceDefinition",
},
ObjectMeta: metav1.ObjectMeta{
Name: "genericmachinetemplate.generic.io",
Labels: map[string]string{
"cluster.x-k8s.io/v1alpha4": "v1",
},
},
Spec: apiextensionsv1.CustomResourceDefinitionSpec{
Group: "generic.io",
Names: apiextensionsv1.CustomResourceDefinitionNames{
Kind: "GenericMachineTemplate",
},
},
}
)

func TestMain(m *testing.M) {
Expand Down
4 changes: 2 additions & 2 deletions controlplane/kubeadm/controllers/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestKubeadmControlPlaneReconciler_RolloutStrategy_ScaleUp(t *testing.T) {
kcp.Spec.Replicas = pointer.Int32Ptr(1)
setKCPHealthy(kcp)

fakeClient := newFakeClient(cluster.DeepCopy(), kcp.DeepCopy(), genericMachineTemplate.DeepCopy())
fakeClient := newFakeClient(fakeGenericMachineTemplateCRD, cluster.DeepCopy(), kcp.DeepCopy(), genericMachineTemplate.DeepCopy())

r := &KubeadmControlPlaneReconciler{
Client: fakeClient,
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestKubeadmControlPlaneReconciler_RolloutStrategy_ScaleDown(t *testing.T) {
Status: internal.ClusterStatus{Nodes: 3},
},
}
objs := []client.Object{cluster.DeepCopy(), kcp.DeepCopy(), tmpl.DeepCopy()}
objs := []client.Object{fakeGenericMachineTemplateCRD, cluster.DeepCopy(), kcp.DeepCopy(), tmpl.DeepCopy()}
for i := 0; i < 3; i++ {
name := fmt.Sprintf("test-%d", i)
m := &clusterv1.Machine{
Expand Down
2 changes: 2 additions & 0 deletions controlplane/kubeadm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/spf13/pflag"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
cliflag "k8s.io/component-base/cli/flag"
Expand Down Expand Up @@ -61,6 +62,7 @@ func init() {
_ = kubeadmcontrolplanev1old.AddToScheme(scheme)
_ = kubeadmcontrolplanev1.AddToScheme(scheme)
_ = kubeadmbootstrapv1.AddToScheme(scheme)
_ = apiextensionsv1.AddToScheme(scheme)
// +kubebuilder:scaffold:scheme
}

Expand Down

0 comments on commit 2b14b18

Please sign in to comment.