Skip to content

Commit

Permalink
Stronger link between Machine* <-> Cluster
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <vincepri@vmware.com>
  • Loading branch information
vincepri committed Feb 5, 2019
1 parent 14b40f3 commit 2f4f2a6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
11 changes: 8 additions & 3 deletions pkg/controller/machine/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog"
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
Expand Down Expand Up @@ -117,8 +118,7 @@ func (r *ReconcileMachine) Reconcile(request reconcile.Request) (reconcile.Resul
// for machine management.
cluster, err := r.getCluster(ctx, m)
if err != nil {
// Just log the error here.
klog.V(4).Infof("Cluster not found, machine actuation might fail: %v", err)
return reconcile.Result{}, err
}
// If object hasn't been deleted and doesn't have a finalizer, add one
// Add a finalizer to newly created objects.
Expand Down Expand Up @@ -194,9 +194,14 @@ func (r *ReconcileMachine) Reconcile(request reconcile.Request) (reconcile.Resul
}

func (r *ReconcileMachine) getCluster(ctx context.Context, machine *clusterv1.Machine) (*clusterv1.Cluster, error) {
if machine.Labels["cluster"] == "" {
return nil, errors.New("missing `cluster` label in Machine spec")
}

clusterList := clusterv1.ClusterList{}
listOptions := &client.ListOptions{
Namespace: machine.Namespace,
FieldSelector: fields.OneTermEqualSelector("metadata.name", machine.Labels["cluster"]),
Namespace: machine.Namespace,
// This is set so the fake client can be used for unit test. See:
// https://github.com/kubernetes-sigs/controller-runtime/issues/168
Raw: &metav1.ListOptions{
Expand Down
20 changes: 19 additions & 1 deletion pkg/controller/machine/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ func TestReconcileRequest(t *testing.T) {
Name: "create",
Namespace: "default",
Finalizers: []string{v1alpha1.MachineFinalizer},
Labels: map[string]string{
"cluster": "testcluster",
},
},
}
machine2 := v1alpha1.Machine{
Expand All @@ -47,6 +50,9 @@ func TestReconcileRequest(t *testing.T) {
Name: "update",
Namespace: "default",
Finalizers: []string{v1alpha1.MachineFinalizer},
Labels: map[string]string{
"cluster": "testcluster",
},
},
}
time := metav1.Now()
Expand All @@ -59,6 +65,9 @@ func TestReconcileRequest(t *testing.T) {
Namespace: "default",
Finalizers: []string{v1alpha1.MachineFinalizer},
DeletionTimestamp: &time,
Labels: map[string]string{
"cluster": "testcluster",
},
},
}
clusterList := v1alpha1.ClusterList{
Expand All @@ -71,10 +80,19 @@ func TestReconcileRequest(t *testing.T) {
Kind: "Cluster",
},
ObjectMeta: metav1.ObjectMeta{
Name: "cluster",
Name: "testcluster",
Namespace: "default",
},
},
{
TypeMeta: metav1.TypeMeta{
Kind: "Cluster",
},
ObjectMeta: metav1.ObjectMeta{
Name: "rainbow",
Namespace: "foo",
},
},
},
}

Expand Down
1 change: 0 additions & 1 deletion pkg/controller/machinedeployment/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,6 @@ func TestMaxUnavailable(t *testing.T) {
}

for _, test := range tests {
t.Log(test.name)
t.Run(test.name, func(t *testing.T) {
maxUnavailable := MaxUnavailable(test.deployment)
if test.expected != maxUnavailable {
Expand Down

0 comments on commit 2f4f2a6

Please sign in to comment.