Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#81 from chuckha/machine-ref-match
Browse files Browse the repository at this point in the history
Fix machine ref matching
  • Loading branch information
k8s-ci-robot authored Aug 6, 2019
2 parents 0c38ba3 + f18def0 commit 739894d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 65 deletions.
4 changes: 2 additions & 2 deletions cloudinit/controlplane_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ type ControlPlaneInput struct {
BaseUserData
Certificates

ClusterConfiguration []byte
InitConfiguration []byte
ClusterConfiguration string
InitConfiguration string
}

// NewInitControlPlane returns the user data string to be used on a controlplane instance.
Expand Down
2 changes: 1 addition & 1 deletion cloudinit/controlplane_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type ControlPlaneJoinInput struct {

BootstrapToken string
ControlPlaneAddress string
JoinConfiguration []byte
JoinConfiguration string
}

// NewJoinControlPlane returns the user data string to be used on a new control plane instance.
Expand Down
2 changes: 1 addition & 1 deletion cloudinit/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ runcmd:
type NodeInput struct {
BaseUserData

JoinConfiguration []byte
JoinConfiguration string
}

// NewNode returns the user data string to be used on a node instance.
Expand Down
10 changes: 5 additions & 5 deletions controllers/kubeadmconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
// Find the owner reference
var machineRef *v1.OwnerReference
for _, ref := range config.OwnerReferences {
if ref.Kind == machineKind.Kind && ref.APIVersion == machineKind.Version {
if ref.Kind == machineKind.Kind && ref.APIVersion == machineKind.GroupVersion().String() {
machineRef = &ref
break
}
Expand Down Expand Up @@ -137,8 +137,8 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
}

cloudInitData, err := cloudinit.NewInitControlPlane(&cloudinit.ControlPlaneInput{
InitConfiguration: initdata,
ClusterConfiguration: clusterdata,
InitConfiguration: string(initdata),
ClusterConfiguration: string(clusterdata),
})
if err != nil {
log.Error(err, "failed to generate cloud init for bootstrap control plane")
Expand Down Expand Up @@ -173,7 +173,7 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
joinData, err := cloudinit.NewJoinControlPlane(&cloudinit.ControlPlaneJoinInput{
// TODO do a len check or something here
ControlPlaneAddress: fmt.Sprintf("https://%s:%d", cluster.Status.APIEndpoints[0].Host, cluster.Status.APIEndpoints[0].Port),
JoinConfiguration: joinBytes,
JoinConfiguration: string(joinBytes),
})
if err != nil {
log.Error(err, "failed to create a control plane join configuration")
Expand All @@ -190,7 +190,7 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
}

joinData, err := cloudinit.NewNode(&cloudinit.NodeInput{
JoinConfiguration: joinBytes,
JoinConfiguration: string(joinBytes),
})
if err != nil {
log.Error(err, "failed to create a worker join configuration")
Expand Down
6 changes: 4 additions & 2 deletions controllers/kubeadmconfig_controller_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controllers

import (
"context"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -57,7 +58,8 @@ var _ = Describe("KubeadmConfigReconciler", func() {
Bootstrap: clusterv1alpha2.Bootstrap{
ConfigRef: &v1.ObjectReference{
Kind: "KubeadmConfig",
APIVersion: "v1alpha2",
APIVersion: v1alpha2.GroupVersion.String(),
Name: "my-config",
},
},
},
Expand Down Expand Up @@ -93,7 +95,7 @@ var _ = Describe("KubeadmConfigReconciler", func() {
})
Expect(err).To(Succeed())
Expect(result.Requeue).To(BeFalse())
Expect(result.RequeueAfter).To(BeZero())
Expect(result.RequeueAfter).To(Equal(30 * time.Second))
})
})
})
79 changes: 25 additions & 54 deletions controllers/kubeadmconfig_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ limitations under the License.
package controllers

import (
"context"
"errors"
"fmt"
"reflect"
"testing"
"time"

Expand All @@ -30,50 +27,22 @@ import (
kubeadmv1alpha2 "sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/api/v1alpha2"
"sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/runtime/log"
)

type myClient struct {
db map[string]runtime.Object
}

// Very basic implementation of copying pointers of interfaces around, stolen from controller runtime
func (c *myClient) Get(ctx context.Context, key client.ObjectKey, out runtime.Object) error {
obj, ok := c.db[key.String()]
if !ok {
return errors.New("object not found")
}
obj = obj.(runtime.Object).DeepCopyObject()
outVal := reflect.ValueOf(out)
objVal := reflect.ValueOf(obj)
reflect.Indirect(outVal).Set(reflect.Indirect(objVal))
return nil
}
func (c *myClient) List(ctx context.Context, list runtime.Object, opts ...client.ListOptionFunc) error {
return nil
}
func (c *myClient) Create(ctx context.Context, obj runtime.Object, opts ...client.CreateOptionFunc) error {
return nil
}
func (c *myClient) Delete(ctx context.Context, obj runtime.Object, opts ...client.DeleteOptionFunc) error {
return nil
}
func (c *myClient) Update(ctx context.Context, obj runtime.Object, opts ...client.UpdateOptionFunc) error {
return nil
}
func (c *myClient) Patch(ctx context.Context, obj runtime.Object, patch client.Patch, opts ...client.PatchOptionFunc) error {
return nil
}
func (c *myClient) Status() client.StatusWriter {
return c
func setupScheme() *runtime.Scheme {
scheme := runtime.NewScheme()
v1alpha2.AddToScheme(scheme)
kubeadmv1alpha2.AddToScheme(scheme)
return scheme
}

func TestSuccessfulReconcileShouldNotRequeue(t *testing.T) {
objects := map[string]runtime.Object{
"ns/cfg": &kubeadmv1alpha2.KubeadmConfig{
objects := []runtime.Object{
&kubeadmv1alpha2.KubeadmConfig{
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns",
Namespace: "default",
Name: "cfg",
OwnerReferences: []metav1.OwnerReference{
{
Expand All @@ -84,7 +53,7 @@ func TestSuccessfulReconcileShouldNotRequeue(t *testing.T) {
},
},
},
"ns/my-machine": &v1alpha2.Machine{
&v1alpha2.Machine{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "my-machine",
Expand All @@ -93,8 +62,13 @@ func TestSuccessfulReconcileShouldNotRequeue(t *testing.T) {
},
},
},
"ns/my-cluster": &v1alpha2.Cluster{
&v1alpha2.Cluster{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "my-cluster",
},
Status: v1alpha2.ClusterStatus{
InfrastructureReady: true,
APIEndpoints: []v1alpha2.APIEndpoint{
{
Host: "example.com",
Expand All @@ -104,9 +78,7 @@ func TestSuccessfulReconcileShouldNotRequeue(t *testing.T) {
},
},
}
myclient := &myClient{
db: objects,
}
myclient := fake.NewFakeClientWithScheme(setupScheme(), objects...)

k := &KubeadmConfigReconciler{
Log: log.ZapLogger(true),
Expand All @@ -115,7 +87,7 @@ func TestSuccessfulReconcileShouldNotRequeue(t *testing.T) {

request := ctrl.Request{
NamespacedName: types.NamespacedName{
Namespace: "ns",
Namespace: "default",
Name: "cfg",
},
}
Expand All @@ -132,19 +104,18 @@ func TestSuccessfulReconcileShouldNotRequeue(t *testing.T) {
}

func TestNoErrorIfNoMachineRefIsFound(t *testing.T) {
myclient := &myClient{
db: map[string]runtime.Object{
"ns/cfg": &kubeadmv1alpha2.KubeadmConfig{
ObjectMeta: metav1.ObjectMeta{
OwnerReferences: []metav1.OwnerReference{
{
Kind: "some non machine kind",
},
objects := []runtime.Object{
&kubeadmv1alpha2.KubeadmConfig{
ObjectMeta: metav1.ObjectMeta{
OwnerReferences: []metav1.OwnerReference{
{
Kind: "some non machine kind",
},
},
},
},
}
myclient := fake.NewFakeClientWithScheme(setupScheme(), objects...)

k := &KubeadmConfigReconciler{
Log: log.ZapLogger(true),
Expand Down

0 comments on commit 739894d

Please sign in to comment.