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

🏃[e2e] Add machine deployment scaling helper function #3027

Merged
merged 1 commit into from
May 11, 2020
Merged
Changes from all commits
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
57 changes: 57 additions & 0 deletions test/framework/machinedeployment_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/pointer"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/patch"
Expand Down Expand Up @@ -289,3 +290,59 @@ func machineDeploymentOptions(deployment clusterv1.MachineDeployment) []client.L
client.MatchingLabels(deployment.Spec.Selector.MatchLabels),
}
}

// ScaleAndWaitMachineDeploymentInput is the input for ScaleAndWaitMachineDeployment.
type ScaleAndWaitMachineDeploymentInput struct {
ClusterProxy ClusterProxy
Cluster *clusterv1.Cluster
MachineDeployment *clusterv1.MachineDeployment
Replicas int32
WaitForMachineDeployments []interface{}
}

// ScaleAndWaitMachineDeployment scales MachineDeployment and waits until all machines have node ref and equal to Replicas.
func ScaleAndWaitMachineDeployment(ctx context.Context, input ScaleAndWaitMachineDeploymentInput) {
Expect(ctx).NotTo(BeNil(), "ctx is required for ScaleAndWaitMachineDeployment")
Expect(input.ClusterProxy).ToNot(BeNil(), "Invalid argument. input.ClusterProxy can't be nil when calling ScaleAndWaitMachineDeployment")
Expect(input.Cluster).ToNot(BeNil(), "Invalid argument. input.Cluster can't be nil when calling ScaleAndWaitMachineDeployment")

fmt.Fprintf(GinkgoWriter, "Scaling machine deployment %s/%s from %v to %v replicas\n", input.MachineDeployment.Namespace, input.MachineDeployment.Name, input.MachineDeployment.Spec.Replicas, input.Replicas)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Ginkgo's By might be cleaner here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In all new helpers, we log like this. If you think By should be used in all helpers, we should track it in a separate issue.

patchHelper, err := patch.NewHelper(input.MachineDeployment, input.ClusterProxy.GetClient())
Expect(err).ToNot(HaveOccurred())
input.MachineDeployment.Spec.Replicas = pointer.Int32Ptr(input.Replicas)
Expect(patchHelper.Patch(ctx, input.MachineDeployment)).To(Succeed())

fmt.Fprintf(GinkgoWriter, "Waiting for correct number of replicas to exist\n")
Eventually(func() (int, error) {
selectorMap, err := metav1.LabelSelectorAsMap(&input.MachineDeployment.Spec.Selector)
if err != nil {
return -1, err
}
ms := &clusterv1.MachineSetList{}
if err := input.ClusterProxy.GetClient().List(ctx, ms, client.InNamespace(input.Cluster.Namespace), client.MatchingLabels(selectorMap)); err != nil {
return -1, err
}
if len(ms.Items) == 0 {
return -1, errors.New("no machinesets were found")
}
machineSet := ms.Items[0]
selectorMap, err = metav1.LabelSelectorAsMap(&machineSet.Spec.Selector)
if err != nil {
return -1, err
}
machines := &clusterv1.MachineList{}
if err := input.ClusterProxy.GetClient().List(ctx, machines, client.InNamespace(machineSet.Namespace), client.MatchingLabels(selectorMap)); err != nil {
return -1, err
}
nodeRefCount := 0
for _, machine := range machines.Items {
if machine.Status.NodeRef != nil {
nodeRefCount++
}
}
if len(machines.Items) != nodeRefCount {
return -1, errors.New("Machine count does not match existing nodes count")
}
return nodeRefCount, nil
}, input.WaitForMachineDeployments...).Should(Equal(int(*input.MachineDeployment.Spec.Replicas)))
}