Skip to content
This repository was archived by the owner on Jul 30, 2021. It is now read-only.

Commit f18def0

Browse files
committed
Fix machine ref matching
Signed-off-by: Chuck Ha <[email protected]>
1 parent c0a12f5 commit f18def0

File tree

6 files changed

+38
-65
lines changed

6 files changed

+38
-65
lines changed

cloudinit/controlplane_init.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ type ControlPlaneInput struct {
3838
BaseUserData
3939
Certificates
4040

41-
ClusterConfiguration []byte
42-
InitConfiguration []byte
41+
ClusterConfiguration string
42+
InitConfiguration string
4343
}
4444

4545
// NewInitControlPlane returns the user data string to be used on a controlplane instance.

cloudinit/controlplane_join.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type ControlPlaneJoinInput struct {
4141

4242
BootstrapToken string
4343
ControlPlaneAddress string
44-
JoinConfiguration []byte
44+
JoinConfiguration string
4545
}
4646

4747
// NewJoinControlPlane returns the user data string to be used on a new control plane instance.

cloudinit/node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ runcmd:
3535
type NodeInput struct {
3636
BaseUserData
3737

38-
JoinConfiguration []byte
38+
JoinConfiguration string
3939
}
4040

4141
// NewNode returns the user data string to be used on a node instance.

controllers/kubeadmconfig_controller.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
7777
// Find the owner reference
7878
var machineRef *v1.OwnerReference
7979
for _, ref := range config.OwnerReferences {
80-
if ref.Kind == machineKind.Kind && ref.APIVersion == machineKind.Version {
80+
if ref.Kind == machineKind.Kind && ref.APIVersion == machineKind.GroupVersion().String() {
8181
machineRef = &ref
8282
break
8383
}
@@ -137,8 +137,8 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
137137
}
138138

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

192192
joinData, err := cloudinit.NewNode(&cloudinit.NodeInput{
193-
JoinConfiguration: joinBytes,
193+
JoinConfiguration: string(joinBytes),
194194
})
195195
if err != nil {
196196
log.Error(err, "failed to create a worker join configuration")

controllers/kubeadmconfig_controller_reconciler_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package controllers
1818

1919
import (
2020
"context"
21+
"time"
2122

2223
. "github.com/onsi/ginkgo"
2324
. "github.com/onsi/gomega"
@@ -57,7 +58,8 @@ var _ = Describe("KubeadmConfigReconciler", func() {
5758
Bootstrap: clusterv1alpha2.Bootstrap{
5859
ConfigRef: &v1.ObjectReference{
5960
Kind: "KubeadmConfig",
60-
APIVersion: "v1alpha2",
61+
APIVersion: v1alpha2.GroupVersion.String(),
62+
Name: "my-config",
6163
},
6264
},
6365
},
@@ -93,7 +95,7 @@ var _ = Describe("KubeadmConfigReconciler", func() {
9395
})
9496
Expect(err).To(Succeed())
9597
Expect(result.Requeue).To(BeFalse())
96-
Expect(result.RequeueAfter).To(BeZero())
98+
Expect(result.RequeueAfter).To(Equal(30 * time.Second))
9799
})
98100
})
99101
})

controllers/kubeadmconfig_controller_test.go

Lines changed: 25 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ limitations under the License.
1717
package controllers
1818

1919
import (
20-
"context"
21-
"errors"
2220
"fmt"
23-
"reflect"
2421
"testing"
2522
"time"
2623

@@ -30,50 +27,22 @@ import (
3027
kubeadmv1alpha2 "sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/api/v1alpha2"
3128
"sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha2"
3229
ctrl "sigs.k8s.io/controller-runtime"
33-
"sigs.k8s.io/controller-runtime/pkg/client"
30+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
3431
"sigs.k8s.io/controller-runtime/pkg/runtime/log"
3532
)
3633

37-
type myClient struct {
38-
db map[string]runtime.Object
39-
}
40-
41-
// Very basic implementation of copying pointers of interfaces around, stolen from controller runtime
42-
func (c *myClient) Get(ctx context.Context, key client.ObjectKey, out runtime.Object) error {
43-
obj, ok := c.db[key.String()]
44-
if !ok {
45-
return errors.New("object not found")
46-
}
47-
obj = obj.(runtime.Object).DeepCopyObject()
48-
outVal := reflect.ValueOf(out)
49-
objVal := reflect.ValueOf(obj)
50-
reflect.Indirect(outVal).Set(reflect.Indirect(objVal))
51-
return nil
52-
}
53-
func (c *myClient) List(ctx context.Context, list runtime.Object, opts ...client.ListOptionFunc) error {
54-
return nil
55-
}
56-
func (c *myClient) Create(ctx context.Context, obj runtime.Object, opts ...client.CreateOptionFunc) error {
57-
return nil
58-
}
59-
func (c *myClient) Delete(ctx context.Context, obj runtime.Object, opts ...client.DeleteOptionFunc) error {
60-
return nil
61-
}
62-
func (c *myClient) Update(ctx context.Context, obj runtime.Object, opts ...client.UpdateOptionFunc) error {
63-
return nil
64-
}
65-
func (c *myClient) Patch(ctx context.Context, obj runtime.Object, patch client.Patch, opts ...client.PatchOptionFunc) error {
66-
return nil
67-
}
68-
func (c *myClient) Status() client.StatusWriter {
69-
return c
34+
func setupScheme() *runtime.Scheme {
35+
scheme := runtime.NewScheme()
36+
v1alpha2.AddToScheme(scheme)
37+
kubeadmv1alpha2.AddToScheme(scheme)
38+
return scheme
7039
}
7140

7241
func TestSuccessfulReconcileShouldNotRequeue(t *testing.T) {
73-
objects := map[string]runtime.Object{
74-
"ns/cfg": &kubeadmv1alpha2.KubeadmConfig{
42+
objects := []runtime.Object{
43+
&kubeadmv1alpha2.KubeadmConfig{
7544
ObjectMeta: metav1.ObjectMeta{
76-
Namespace: "ns",
45+
Namespace: "default",
7746
Name: "cfg",
7847
OwnerReferences: []metav1.OwnerReference{
7948
{
@@ -84,7 +53,7 @@ func TestSuccessfulReconcileShouldNotRequeue(t *testing.T) {
8453
},
8554
},
8655
},
87-
"ns/my-machine": &v1alpha2.Machine{
56+
&v1alpha2.Machine{
8857
ObjectMeta: metav1.ObjectMeta{
8958
Namespace: "default",
9059
Name: "my-machine",
@@ -93,8 +62,13 @@ func TestSuccessfulReconcileShouldNotRequeue(t *testing.T) {
9362
},
9463
},
9564
},
96-
"ns/my-cluster": &v1alpha2.Cluster{
65+
&v1alpha2.Cluster{
66+
ObjectMeta: metav1.ObjectMeta{
67+
Namespace: "default",
68+
Name: "my-cluster",
69+
},
9770
Status: v1alpha2.ClusterStatus{
71+
InfrastructureReady: true,
9872
APIEndpoints: []v1alpha2.APIEndpoint{
9973
{
10074
Host: "example.com",
@@ -104,9 +78,7 @@ func TestSuccessfulReconcileShouldNotRequeue(t *testing.T) {
10478
},
10579
},
10680
}
107-
myclient := &myClient{
108-
db: objects,
109-
}
81+
myclient := fake.NewFakeClientWithScheme(setupScheme(), objects...)
11082

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

11688
request := ctrl.Request{
11789
NamespacedName: types.NamespacedName{
118-
Namespace: "ns",
90+
Namespace: "default",
11991
Name: "cfg",
12092
},
12193
}
@@ -132,19 +104,18 @@ func TestSuccessfulReconcileShouldNotRequeue(t *testing.T) {
132104
}
133105

134106
func TestNoErrorIfNoMachineRefIsFound(t *testing.T) {
135-
myclient := &myClient{
136-
db: map[string]runtime.Object{
137-
"ns/cfg": &kubeadmv1alpha2.KubeadmConfig{
138-
ObjectMeta: metav1.ObjectMeta{
139-
OwnerReferences: []metav1.OwnerReference{
140-
{
141-
Kind: "some non machine kind",
142-
},
107+
objects := []runtime.Object{
108+
&kubeadmv1alpha2.KubeadmConfig{
109+
ObjectMeta: metav1.ObjectMeta{
110+
OwnerReferences: []metav1.OwnerReference{
111+
{
112+
Kind: "some non machine kind",
143113
},
144114
},
145115
},
146116
},
147117
}
118+
myclient := fake.NewFakeClientWithScheme(setupScheme(), objects...)
148119

149120
k := &KubeadmConfigReconciler{
150121
Log: log.ZapLogger(true),

0 commit comments

Comments
 (0)