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

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