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

Commit c569a9d

Browse files
committed
Fix error-return for machine-cluster association
1 parent 67a0ea2 commit c569a9d

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

controllers/kubeadmconfig_controller.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,16 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
127127
// Lookup the cluster the machine is associated with
128128
cluster, err := util.GetClusterFromMetadata(ctx, r.Client, machine.ObjectMeta)
129129
if err != nil {
130-
log.Error(err, "could not get cluster by machine metadata")
131-
return ctrl.Result{}, err
130+
if errors.Cause(err) == util.ErrNoCluster {
131+
log.Info("Machine does not belong to a cluster yet, waiting until its part of a cluster")
132+
return ctrl.Result{}, nil
133+
} else if apierrors.IsNotFound(err) {
134+
log.Info("Cluster does not exist yet , waiting until it is created")
135+
return ctrl.Result{}, nil
136+
} else {
137+
log.Error(err, "could not get cluster by machine metadata")
138+
return ctrl.Result{}, err
139+
}
132140
}
133141

134142
// Wait patiently for the infrastructure to be ready

controllers/kubeadmconfig_controller_test.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,8 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnEarlyIfMachineHasBootstrapData(
152152
}
153153
}
154154

155-
// This returns an error since nothing can proceed without an associated cluster.
156-
// TODO: This should probably not return an error
157-
func TestKubeadmConfigReconciler_Reconcile_ReturnErrorIfMachineDoesNotHaveAssociatedCluster(t *testing.T) {
155+
// This does not expect an error, hoping the machine gets updated with a cluster
156+
func TestKubeadmConfigReconciler_Reconcile_ReturnNilIfMachineDoesNotHaveAssociatedCluster(t *testing.T) {
158157
machine := newMachine(nil, "machine") // intentionally omitting cluster
159158
config := newKubeadmConfig(machine, "cfg")
160159

@@ -176,14 +175,13 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnErrorIfMachineDoesNotHaveAssoci
176175
},
177176
}
178177
_, err := k.Reconcile(request)
179-
if err == nil {
180-
t.Fatal("Expected error, got nil")
178+
if err != nil {
179+
t.Fatal("Not Expecting error, got an error")
181180
}
182181
}
183182

184-
// If the associated cluster is not found then there is no way to proceed.
185-
// TODO: This should probably not be an error
186-
func TestKubeadmConfigReconciler_Reconcile_ReturnErrorIfAssociatedClusterIsNotFound(t *testing.T) {
183+
// This does not expect an error, hoping that the associated cluster will be created
184+
func TestKubeadmConfigReconciler_Reconcile_ReturnNilIfAssociatedClusterIsNotFound(t *testing.T) {
187185
cluster := newCluster("cluster")
188186
machine := newMachine(cluster, "machine")
189187
config := newKubeadmConfig(machine, "cfg")
@@ -207,8 +205,8 @@ func TestKubeadmConfigReconciler_Reconcile_ReturnErrorIfAssociatedClusterIsNotFo
207205
},
208206
}
209207
_, err := k.Reconcile(request)
210-
if err == nil {
211-
t.Fatal("Expected error, got nil")
208+
if err != nil {
209+
t.Fatal("Not Expecting error, got an error")
212210
}
213211
}
214212

0 commit comments

Comments
 (0)