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

     1  // Package openstack generates Machine objects for openstack.
     2  package openstack
     3  
     4  import (
     5  	"context"
     6  	"fmt"
     7  
     8  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     9  	"k8s.io/apimachinery/pkg/runtime"
    10  	"k8s.io/utils/ptr"
    11  
    12  	clusterapi "github.com/openshift/api/machine/v1beta1"
    13  	"github.com/openshift/installer/pkg/types"
    14  	"github.com/openshift/installer/pkg/types/openstack"
    15  )
    16  
    17  const maxInt32 int64 = int64(^uint32(0)) >> 1
    18  
    19  // MachineSets returns the MachineSets encoded by the given machine-pool. The
    20  // number of returned MachineSets, while being capped to the number of
    21  // replicas, depends on the variable-length fields in the machine-pool. Each
    22  // MachineSet generates a set of identical Machines; to encode for Machines
    23  // spread on, say, three availability zones, three MachineSets must be
    24  // produced. Note that for each variable-length field (currently: Compute
    25  // availability zones, Storage availability zones and Root volume types), when
    26  // more than one is specified, values of identical index are grouped in the
    27  // same MachineSet.
    28  func MachineSets(ctx context.Context, clusterID string, config *types.InstallConfig, pool *types.MachinePool, osImage, role, userDataSecret string) ([]*clusterapi.MachineSet, error) {
    29  	if configPlatform := config.Platform.Name(); configPlatform != openstack.Name {
    30  		return nil, fmt.Errorf("non-OpenStack configuration: %q", configPlatform)
    31  	}
    32  	if poolPlatform := pool.Platform.Name(); poolPlatform != openstack.Name {
    33  		return nil, fmt.Errorf("non-OpenStack machine-pool: %q", poolPlatform)
    34  	}
    35  	mpool := pool.Platform.OpenStack
    36  
    37  	// In installer CLI code paths, the replica number is set to 3 by default
    38  	// when install-config does not have any Compute machine-pool, or when the
    39  	// Compute machine-pool does not have the `replicas` property.
    40  	// However, external consumers of this func may not be so kind...
    41  	if pool.Replicas == nil {
    42  		pool.Replicas = ptr.To[int64](0)
    43  	}
    44  
    45  	failureDomains := failureDomainsFromSpec(*mpool)
    46  	numberOfFailureDomains := int64(len(failureDomains))
    47  
    48  	machinesets := make([]*clusterapi.MachineSet, len(failureDomains))
    49  	for idx := range machinesets {
    50  		var replicaNumber int32
    51  		{
    52  			replicas := *pool.Replicas / numberOfFailureDomains
    53  			if int64(idx) < *pool.Replicas%numberOfFailureDomains {
    54  				replicas++
    55  			}
    56  			if replicas > maxInt32 {
    57  				return nil, fmt.Errorf("the number of requested worker replicas (%d) is too high. Each MachineSet can hold %d replicas; the install-config encodes for %d MachineSets, which gives us a replica number of %d", *pool.Replicas, maxInt32, numberOfFailureDomains, replicas)
    58  			}
    59  			replicaNumber = int32(replicas)
    60  		}
    61  
    62  		providerSpec, err := generateProviderSpec(
    63  			ctx,
    64  			clusterID,
    65  			config.Platform.OpenStack,
    66  			mpool,
    67  			osImage,
    68  			role,
    69  			userDataSecret,
    70  			failureDomains[idx],
    71  		)
    72  		if err != nil {
    73  			return nil, err
    74  		}
    75  
    76  		// Set unique name for the machineset
    77  		name := fmt.Sprintf("%s-%s-%d", clusterID, pool.Name, idx)
    78  
    79  		machinesets[idx] = &clusterapi.MachineSet{
    80  			TypeMeta: metav1.TypeMeta{
    81  				APIVersion: "machine.openshift.io/v1beta1",
    82  				Kind:       "MachineSet",
    83  			},
    84  			ObjectMeta: metav1.ObjectMeta{
    85  				Namespace: "openshift-machine-api",
    86  				Name:      name,
    87  				Labels: map[string]string{
    88  					"machine.openshift.io/cluster-api-cluster":      clusterID,
    89  					"machine.openshift.io/cluster-api-machine-role": role,
    90  					"machine.openshift.io/cluster-api-machine-type": role,
    91  				},
    92  			},
    93  			Spec: clusterapi.MachineSetSpec{
    94  				Replicas: &replicaNumber,
    95  				Selector: metav1.LabelSelector{
    96  					MatchLabels: map[string]string{
    97  						"machine.openshift.io/cluster-api-machineset": name,
    98  						"machine.openshift.io/cluster-api-cluster":    clusterID,
    99  					},
   100  				},
   101  				Template: clusterapi.MachineTemplateSpec{
   102  					ObjectMeta: clusterapi.ObjectMeta{
   103  						Labels: map[string]string{
   104  							"machine.openshift.io/cluster-api-machineset":   name,
   105  							"machine.openshift.io/cluster-api-cluster":      clusterID,
   106  							"machine.openshift.io/cluster-api-machine-role": role,
   107  							"machine.openshift.io/cluster-api-machine-type": role,
   108  						},
   109  					},
   110  					Spec: clusterapi.MachineSpec{
   111  						ProviderSpec: clusterapi.ProviderSpec{
   112  							Value: &runtime.RawExtension{Object: providerSpec},
   113  						},
   114  						// we don't need to set Versions, because we control those via cluster operators.
   115  					},
   116  				},
   117  			},
   118  		}
   119  	}
   120  
   121  	return machinesets, nil
   122  }