Skip to content

Commit d259c3d

Browse files
committed
Refactor resize e2e: tighten CloudError assertion, pick next-larger master VM size
- Replace Expect(requestError).NotTo(HaveOccurred()) (struct, not error) with explicit Code/Message emptiness checks for clearer failures. - Extract nextLargerSupportedMasterVMSize helper that selects the smallest supported master SKU in the same family with a higher core count, replacing the hardcoded D<->E family swap.
1 parent ee78b49 commit d259c3d

1 file changed

Lines changed: 52 additions & 17 deletions

File tree

test/e2e/adminapi_resize_controlplane.go

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"encoding/json"
99
"fmt"
1010
"io"
11+
"math"
1112
"net/http"
1213
"net/url"
1314
"slices"
@@ -58,6 +59,39 @@ func getControlPlaneVMSize(ctx context.Context) string {
5859
return string(vms[0].HardwareProfile.VMSize)
5960
}
6061

62+
// nextLargerSupportedMasterVMSize returns the supported master VM size in the
63+
// same family as currentVMSize that has the smallest core count strictly
64+
// greater than currentVMSize's core count. It returns an error if currentVMSize
65+
// is not in the supported master list, or if no larger size exists in the same
66+
// family.
67+
func nextLargerSupportedMasterVMSize(currentVMSize string) (string, error) {
68+
supportedMasterSizes := validate.SupportedVMSizesByRole(validate.VMRoleMaster)
69+
currentInfo, ok := supportedMasterSizes[api.VMSize(currentVMSize)]
70+
if !ok {
71+
return "", fmt.Errorf("current VM size %q is not in the supported master list", currentVMSize)
72+
}
73+
74+
targetSku := ""
75+
targetCores := math.MaxInt
76+
for size, info := range supportedMasterSizes {
77+
if info.Family != currentInfo.Family {
78+
continue
79+
}
80+
if info.CoreCount <= currentInfo.CoreCount {
81+
continue
82+
}
83+
if info.CoreCount < targetCores {
84+
targetCores = info.CoreCount
85+
targetSku = string(size)
86+
}
87+
}
88+
89+
if targetSku == "" {
90+
return "", fmt.Errorf("no supported master VM size larger than %q (family %s, %d cores) is available", currentVMSize, currentInfo.Family, currentInfo.CoreCount)
91+
}
92+
return targetSku, nil
93+
}
94+
6195
// validateMasterVMSizeLabels makes sure that master machine and node Resources in the cluster have the correct vmsize labels. It verifies that the following are equal to the targetSku
6296
// - metadata.labels."machine.openshift.io/instance-type" for machine
6397
// - spec.ProviderSpec.value.vmSize for machine
@@ -81,8 +115,10 @@ func validateMasterVMSizeLabels(ctx context.Context, targetSku string) {
81115
Expect(json.Unmarshal(ma.Spec.ProviderSpec.Value.Raw, &machineProvSpec)).ToNot(HaveOccurred())
82116
Expect(machineProvSpec.VMSize).To(Equal(targetSku))
83117

118+
Expect(ma.Status.NodeRef).ToNot(BeNil())
119+
84120
var curNode corev1.Node
85-
err = clients.KubeClient.Get(ctx, types.NamespacedName{Name: ma.GetObjectMeta().GetName()}, &curNode)
121+
err = clients.KubeClient.Get(ctx, types.NamespacedName{Name: ma.Status.NodeRef.Name}, &curNode)
86122
Expect(err).ToNot(HaveOccurred())
87123

88124
nodeSizeLabelVal, ok := curNode.GetLabels()[nodeLabelInstanceType]
@@ -154,10 +190,10 @@ var _ = Describe("[Admin API] Resize control plane", func() {
154190
targetSku := ""
155191
for size, sizeInfo := range supportedSizes {
156192
for _, u := range usageRes {
157-
if u.Name == nil || u.Name.Value == nil || *u.Name.Value != sizeInfo.Family {
158-
continue
159-
}
160-
if u.Limit == nil {
193+
if u.Name == nil ||
194+
u.Name.Value == nil ||
195+
*u.Name.Value != sizeInfo.Family ||
196+
u.Limit == nil {
161197
continue
162198
}
163199

@@ -187,19 +223,17 @@ var _ = Describe("[Admin API] Resize control plane", func() {
187223
Expect(out.Details[0].Code).To(Equal("ResourceQuotaExceeded"))
188224
})
189225

190-
It("should do the resize when target size is different", Label(slow), Serial, func(ctx context.Context) {
226+
It("should do the resize when target size is different", Label(slow), FlakeAttempts(1), Serial, func(ctx context.Context) {
191227
By("Getting the current machine size")
192228
preResizeVMSize := getControlPlaneVMSize(ctx)
193229
Expect(preResizeVMSize).ToNot(BeZero())
194230

195-
// if we're on D, resize to same E series VM, and vice-versa
196-
targetSku := ""
197-
if strings.HasPrefix(preResizeVMSize, "Standard_D") {
198-
targetSku = strings.Replace(preResizeVMSize, "Standard_D", "Standard_E", 1)
199-
} else if strings.HasPrefix(preResizeVMSize, "Standard_E") {
200-
targetSku = strings.Replace(preResizeVMSize, "Standard_E", "Standard_D", 1)
201-
} else {
202-
Skip(fmt.Sprintf("Cowardly refusing to resize the cluster, only know how to handle E and D vms, this cluster has: %s", preResizeVMSize))
231+
// Pick the next-larger VM size within the same family from the
232+
// supported-master list. This keeps the resize on a well-tested size
233+
// while avoiding arbitrary family swaps.
234+
targetSku, err := nextLargerSupportedMasterVMSize(preResizeVMSize)
235+
if err != nil {
236+
Skip(err.Error())
203237
}
204238

205239
By(fmt.Sprintf("Resizing from %s to %s", preResizeVMSize, targetSku))
@@ -208,15 +242,16 @@ var _ = Describe("[Admin API] Resize control plane", func() {
208242
"vmSize": []string{targetSku},
209243
}
210244

211-
var requestError *api.CloudError
212-
resp, err := adminRequest(ctx, http.MethodPost, "/admin"+clusterResourceID+"/resizecontrolplane", params, true, nil, requestError)
245+
requestError := api.CloudError{}
246+
resp, err := adminRequest(ctx, http.MethodPost, "/admin"+clusterResourceID+"/resizecontrolplane", params, true, nil, &requestError)
213247
// err will be [io.EOF] when request is successful, as response body will
214248
// be empty. In case of error, response body will be parsed into an [api.CloudError]
215249
if err == io.EOF {
216250
err = nil
217251
}
218252
Expect(err).NotTo(HaveOccurred())
219-
Expect(requestError).NotTo(HaveOccurred())
253+
Expect(requestError.Code).To(BeEmpty(), "unexpected CloudError: %+v", requestError)
254+
Expect(requestError.Message).To(BeEmpty(), "unexpected CloudError: %+v", requestError)
220255
Expect(resp.StatusCode).To(Equal(http.StatusOK))
221256

222257
By("Validating vm size after resize")

0 commit comments

Comments
 (0)