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

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