Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for issue-290 #343

Merged
merged 4 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions controllers/infrastructure/byomachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (

const (
ProviderIDPrefix = "byoh://"
providerIDSuffixLength = 6
ProviderIDSuffixLength = 6
RequeueForbyohost = 10 * time.Second
)

Expand Down Expand Up @@ -259,27 +259,25 @@ func (r *ByoMachineReconciler) reconcileNormal(ctx context.Context, machineScope
r.Recorder.Eventf(machineScope.ByoMachine, corev1.EventTypeNormal, "ByoHostAttachSucceeded", "Attached ByoHost %s", machineScope.ByoHost.Name)
}

if machineScope.ByoMachine.Spec.ProviderID == "" {
logger.Info("Updating Node with ProviderID")
remoteClient, err := r.getRemoteClient(ctx, machineScope.ByoMachine)
if err != nil {
logger.Error(err, "failed to get remote client")
return ctrl.Result{}, err
}

providerID, err := r.setNodeProviderID(ctx, remoteClient, machineScope.ByoHost)
if err != nil {
logger.Error(err, "failed to set node providerID")
r.Recorder.Eventf(machineScope.ByoMachine, corev1.EventTypeWarning, "SetNodeProviderFailed", "Node %s does not exist", machineScope.ByoHost.Name)
return ctrl.Result{}, err
}
logger.Info("Updating Node with ProviderID")
remoteClient, err := r.getRemoteClient(ctx, machineScope.ByoMachine)
anusha94 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
logger.Error(err, "failed to get remote client")
return ctrl.Result{}, err
}

machineScope.ByoMachine.Spec.ProviderID = providerID
machineScope.ByoMachine.Status.Ready = true
conditions.MarkTrue(machineScope.ByoMachine, infrav1.BYOHostReady)
r.Recorder.Eventf(machineScope.ByoMachine, corev1.EventTypeNormal, "NodeProvisionedSucceeded", "Provisioned Node %s", machineScope.ByoHost.Name)
providerID, err := r.setNodeProviderID(ctx, remoteClient, machineScope.ByoHost)
if err != nil {
logger.Error(err, "failed to set node providerID")
r.Recorder.Eventf(machineScope.ByoMachine, corev1.EventTypeWarning, "SetNodeProviderFailed", "Node %s does not exist", machineScope.ByoHost.Name)
return ctrl.Result{}, err
}

machineScope.ByoMachine.Spec.ProviderID = providerID
machineScope.ByoMachine.Status.Ready = true
conditions.MarkTrue(machineScope.ByoMachine, infrav1.BYOHostReady)
r.Recorder.Eventf(machineScope.ByoMachine, corev1.EventTypeNormal, "NodeProvisionedSucceeded", "Provisioned Node %s", machineScope.ByoHost.Name)

return ctrl.Result{}, nil
}

Expand Down Expand Up @@ -375,7 +373,7 @@ func (r *ByoMachineReconciler) setNodeProviderID(ctx context.Context, remoteClie
return "", err
}

node.Spec.ProviderID = fmt.Sprintf("%s%s/%s", ProviderIDPrefix, host.Name, util.RandomString(providerIDSuffixLength))
node.Spec.ProviderID = fmt.Sprintf("%s%s/%s", ProviderIDPrefix, host.Name, util.RandomString(ProviderIDSuffixLength))

return node.Spec.ProviderID, helper.Patch(ctx, node)
}
Expand Down
27 changes: 27 additions & 0 deletions controllers/infrastructure/byomachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
Expand Down Expand Up @@ -117,6 +118,32 @@ var _ = Describe("Controllers/ByomachineController", func() {
Expect(err).To(MatchError("nodes \"" + byoHost.Name + "\" not found"))
})

Context("When node.Spec.ProviderID is already set", func() {

BeforeEach(func() {
byoHost = builder.ByoHost(defaultNamespace, "test-node-providerid-host").Build()
Expect(k8sClientUncached.Create(ctx, byoHost)).Should(Succeed())
})

It("should not return error when node.Spec.ProviderID is with correct value", func() {
node := builder.Node(defaultNamespace, byoHost.Name).Build()
node.Spec.ProviderID = fmt.Sprintf("%s%s/%s", controllers.ProviderIDPrefix, byoHost.Name, util.RandomString(controllers.ProviderIDSuffixLength))
huchen2021 marked this conversation as resolved.
Show resolved Hide resolved
Expect(clientFake.Create(ctx, node)).Should(Succeed())
WaitForObjectsToBePopulatedInCache(byoHost)
huchen2021 marked this conversation as resolved.
Show resolved Hide resolved
_, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: byoMachineLookupKey})
Expect(err).ToNot(HaveOccurred())
})

It("should return error when node.Spec.ProviderID is without correct value", func() {
huchen2021 marked this conversation as resolved.
Show resolved Hide resolved
node := builder.Node(defaultNamespace, byoHost.Name).Build()
node.Spec.ProviderID = "invalid_format"
Expect(clientFake.Create(ctx, node)).Should(Succeed())
WaitForObjectsToBePopulatedInCache(byoHost)
_, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: byoMachineLookupKey})
Expect(err).To(MatchError("invalid format for node.Spec.ProviderID"))
})
})

Context("When BYO Hosts are not available", func() {
It("should mark BYOHostReady as False", func() {
_, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: byoMachineLookupKey})
Expand Down