github.com/openshift/installer@v1.4.17/pkg/asset/machines/ibmcloud/machinesets.go (about)

     1  package ibmcloud
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/pkg/errors"
     8  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     9  	"k8s.io/apimachinery/pkg/runtime"
    10  
    11  	machineapi "github.com/openshift/api/machine/v1beta1"
    12  	"github.com/openshift/installer/pkg/types"
    13  	"github.com/openshift/installer/pkg/types/ibmcloud"
    14  )
    15  
    16  // MachineSets returns a list of machinesets for a machinepool.
    17  func MachineSets(clusterID string, config *types.InstallConfig, subnets map[string]string, pool *types.MachinePool, role, userDataSecret string) ([]*machineapi.MachineSet, error) {
    18  	if configPlatform := config.Platform.Name(); configPlatform != ibmcloud.Name {
    19  		return nil, fmt.Errorf("non-IBMCloud configuration: %q", configPlatform)
    20  	}
    21  	if poolPlatform := pool.Platform.Name(); poolPlatform != ibmcloud.Name {
    22  		return nil, fmt.Errorf("non-IBMCloud machine-pool: %q", poolPlatform)
    23  	}
    24  	platform := config.Platform.IBMCloud
    25  	mpool := pool.Platform.IBMCloud
    26  	azs := mpool.Zones
    27  
    28  	total := int64(0)
    29  	if pool.Replicas != nil {
    30  		total = *pool.Replicas
    31  	}
    32  	numOfAZs := int64(len(azs))
    33  	var machinesets []*machineapi.MachineSet
    34  	for idx, az := range azs {
    35  		replicas := int32(total / numOfAZs)
    36  		if int64(idx) < total%numOfAZs {
    37  			replicas++
    38  		}
    39  
    40  		provider, err := provider(clusterID, platform, subnets, mpool, idx, role, userDataSecret)
    41  		if err != nil {
    42  			return nil, errors.Wrap(err, "failed to create provider")
    43  		}
    44  		name := fmt.Sprintf("%s-%s-%s", clusterID, pool.Name, strings.TrimPrefix(az, fmt.Sprintf("%s-", platform.Region)))
    45  		mset := &machineapi.MachineSet{
    46  			TypeMeta: metav1.TypeMeta{
    47  				APIVersion: "machine.openshift.io/v1beta1",
    48  				Kind:       "MachineSet",
    49  			},
    50  			ObjectMeta: metav1.ObjectMeta{
    51  				Namespace: "openshift-machine-api",
    52  				Name:      name,
    53  				Labels: map[string]string{
    54  					"machine.openshift.io/cluster-api-cluster": clusterID,
    55  				},
    56  			},
    57  			Spec: machineapi.MachineSetSpec{
    58  				Replicas: &replicas,
    59  				Selector: metav1.LabelSelector{
    60  					MatchLabels: map[string]string{
    61  						"machine.openshift.io/cluster-api-machineset": name,
    62  						"machine.openshift.io/cluster-api-cluster":    clusterID,
    63  					},
    64  				},
    65  				Template: machineapi.MachineTemplateSpec{
    66  					ObjectMeta: machineapi.ObjectMeta{
    67  						Labels: map[string]string{
    68  							"machine.openshift.io/cluster-api-machineset":   name,
    69  							"machine.openshift.io/cluster-api-cluster":      clusterID,
    70  							"machine.openshift.io/cluster-api-machine-role": role,
    71  							"machine.openshift.io/cluster-api-machine-type": role,
    72  						},
    73  					},
    74  					Spec: machineapi.MachineSpec{
    75  						ProviderSpec: machineapi.ProviderSpec{
    76  							Value: &runtime.RawExtension{Object: provider},
    77  						},
    78  					},
    79  				},
    80  			},
    81  		}
    82  		machinesets = append(machinesets, mset)
    83  	}
    84  	return machinesets, nil
    85  }