@@ -14,6 +14,7 @@ import (
1414 "github.com/tidwall/gjson"
1515 "github.com/tidwall/sjson"
1616 "k8s.io/apimachinery/pkg/util/wait"
17+ e2e "k8s.io/kubernetes/test/e2e/framework"
1718)
1819
1920// MachineSet struct to handle MachineSet resources
@@ -36,6 +37,10 @@ func NewMachineSetList(oc *exutil.CLI, namespace string) *MachineSetList {
3637 return & MachineSetList {* NewNamespacedResourceList (oc , MachineSetFullName , namespace )}
3738}
3839
40+ func (ms MachineSet ) String () string {
41+ return ms .GetName ()
42+ }
43+
3944// ScaleTo scales the MachineSet to the exact given value
4045func (ms MachineSet ) ScaleTo (scale int ) error {
4146 return ms .Patch ("merge" , fmt .Sprintf (`{"spec": {"replicas": %d}}` , scale ))
@@ -250,24 +255,86 @@ func (ms MachineSet) GetArchitectureOrFail() architecture.Architecture {
250255 return arch
251256}
252257
258+ // SetArchitecture sets the architecture annotation for this MachineSet
259+ func (ms MachineSet ) SetArchitecture (arch string ) error {
260+ return ms .Patch ("json" ,
261+ fmt .Sprintf (`[{"op": "add", "path": "/metadata/annotations/capacity.cluster-autoscaler.kubernetes.io~1labels", "value": "kubernetes.io/arch=%s"}]` ,
262+ arch ))
263+ }
264+
265+ // GetUserDataSecretName returns the name of the user-data secret
266+ func (ms MachineSet ) GetUserDataSecretName () (string , error ) {
267+ return ms .Get (`{.spec.template.spec.providerSpec.value.userDataSecret.name}` )
268+ }
269+
270+ // GetUserDataSecret returns the secret used for user-data
271+ func (ms MachineSet ) GetUserDataSecret () (* Secret , error ) {
272+ secretName , err := ms .GetUserDataSecretName ()
273+ if err != nil {
274+ return nil , err
275+ }
276+ return NewSecret (ms .GetOC (), MachineAPINamespace , secretName ), nil
277+ }
278+
253279// SetUserDataSecret configures the machineset to use the provided user-data secret in the machine-config-api namespace
254280func (ms MachineSet ) SetUserDataSecret (userDataSecretName string ) error {
255281 return ms .Patch ("json" , `[{ "op": "replace", "path": "/spec/template/spec/providerSpec/value/userDataSecret/name", "value": "` + userDataSecretName + `" }]` )
256282}
257283
258- // GetAll returns a []*MachineSet list with all existing MachineSets
284+ // GetCoreOsBootImage returns the configured coreOsBootImage in this machineset
285+ func (ms MachineSet ) GetCoreOsBootImage () (string , error ) {
286+ // the coreOs boot image is stored differently in the machineset spec depending on the platform
287+ // currently we only support testing the coresOs boot image in GCP platform.
288+ coreOsBootImagePath := ""
289+ switch p := exutil .CheckPlatform (ms .oc ); p {
290+ case AWSPlatform :
291+ coreOsBootImagePath = `{.spec.template.spec.providerSpec.value.ami.id}`
292+ case GCPPlatform :
293+ // For GCP, dynamically find the boot disk index
294+ bootDiskIndex , err := GCPGetMachineSetBootDiskIndex (ms )
295+ if err != nil {
296+ return "" , err
297+ }
298+ coreOsBootImagePath = fmt .Sprintf (`{.spec.template.spec.providerSpec.value.disks[%d].image}` , bootDiskIndex )
299+ case VspherePlatform :
300+ coreOsBootImagePath = `{.spec.template.spec.providerSpec.value.template}`
301+ case AzurePlatform :
302+ coreOsBootImagePath = `{.spec.template.spec.providerSpec.value.image}`
303+ default :
304+ e2e .Failf ("Machineset.GetCoreOsBootImage method is only supported for GCP and AWS infrastructure" )
305+ }
306+
307+ return ms .Get (coreOsBootImagePath )
308+ }
309+
310+ // GetCoreOsBootImageOrFail returns the configured coreOsBootImage in this machineset and fails the test case if any error happened
311+ func (ms MachineSet ) GetCoreOsBootImageOrFail () string {
312+ img , err := ms .GetCoreOsBootImage ()
313+ o .ExpectWithOffset (1 , err ).NotTo (o .HaveOccurred (), "Error getting the coreos boot image value in %s" , ms )
314+ return img
315+ }
316+
317+ // GetAll returns a []*MachineSet list with all existing machinesets
259318func (msl * MachineSetList ) GetAll () ([]* MachineSet , error ) {
260- allMachineSetResources , err := msl .ResourceList .GetAll ()
319+ allMSResources , err := msl .ResourceList .GetAll ()
261320 if err != nil {
262321 return nil , err
263322 }
264- allMachineSets := make ([]* MachineSet , 0 , len (allMachineSetResources ))
323+ allMS := make ([]* MachineSet , 0 , len (allMSResources ))
265324
266- for _ , msRes := range allMachineSetResources {
267- allMachineSets = append (allMachineSets , NewMachineSet (msl .oc , msRes .GetNamespace (), msRes .GetName ()))
325+ for _ , msRes := range allMSResources {
326+ allMS = append (allMS , NewMachineSet (msl .oc , msRes .GetNamespace (), msRes .GetName ()))
268327 }
269328
270- return allMachineSets , nil
329+ return allMS , nil
330+ }
331+
332+ // GetAllOrFail returns a []*Machineset list with all existing machinesets and fail the test if it is not possible
333+ func (msl * MachineSetList ) GetAllOrFail () []* MachineSet {
334+ allMs , err := msl .GetAll ()
335+ o .ExpectWithOffset (1 , err ).NotTo (o .HaveOccurred (), "Error getting the list of existing MachineSets" )
336+
337+ return allMs
271338}
272339
273340// WaitForRunningMachines waits for the specified number of running machines to be created from this MachineSet
@@ -511,3 +578,23 @@ func GCPGetMachineSetBootDiskIndex(ms MachineSet) (int, error) {
511578
512579 return - 1 , fmt .Errorf ("No boot disk found in disks array (no disk with boot: true)" )
513580}
581+
582+ // AllNodesUpdated returns true if all nodes in this machineset are updated
583+ func (ms MachineSet ) AllNodesUpdated () (bool , error ) {
584+ nodes , err := ms .GetNodes ()
585+ if err != nil {
586+ return false , err
587+ }
588+
589+ for _ , node := range nodes {
590+ updated , err := node .IsUpdated ()
591+ if err != nil {
592+ return false , err
593+ }
594+ if ! updated {
595+ return false , nil
596+ }
597+ }
598+
599+ return true , nil
600+ }
0 commit comments