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

     1  // Package aws generates Machine objects for aws.
     2  package aws
     3  
     4  import (
     5  	"fmt"
     6  
     7  	"github.com/pkg/errors"
     8  	corev1 "k8s.io/api/core/v1"
     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  	icaws "github.com/openshift/installer/pkg/asset/installconfig/aws"
    14  	"github.com/openshift/installer/pkg/types"
    15  	"github.com/openshift/installer/pkg/types/aws"
    16  )
    17  
    18  // MachineSetInput holds the input arguments required to MachineSets for a machinepool.
    19  type MachineSetInput struct {
    20  	ClusterID                string
    21  	InstallConfigPlatformAWS *aws.Platform
    22  	Subnets                  icaws.Subnets
    23  	Zones                    icaws.Zones
    24  	Pool                     *types.MachinePool
    25  	Role                     string
    26  	UserDataSecret           string
    27  }
    28  
    29  // MachineSets returns a list of machinesets for a machinepool.
    30  func MachineSets(in *MachineSetInput) ([]*machineapi.MachineSet, error) {
    31  	if poolPlatform := in.Pool.Platform.Name(); poolPlatform != aws.Name {
    32  		return nil, fmt.Errorf("non-AWS machine-pool: %q", poolPlatform)
    33  	}
    34  	mpool := in.Pool.Platform.AWS
    35  	azs := mpool.Zones
    36  
    37  	total := int64(0)
    38  	if in.Pool.Replicas != nil {
    39  		total = *in.Pool.Replicas
    40  	}
    41  	numOfAZs := int64(len(azs))
    42  	var machinesets []*machineapi.MachineSet
    43  	for idx, az := range mpool.Zones {
    44  		replicas := int32(total / numOfAZs)
    45  		if int64(idx) < total%numOfAZs {
    46  			replicas++
    47  		}
    48  
    49  		nodeLabels := make(map[string]string, 3)
    50  		nodeTaints := []corev1.Taint{}
    51  		instanceType := mpool.InstanceType
    52  		publicSubnet := false
    53  		subnetID := ""
    54  		if len(in.Subnets) > 0 {
    55  			subnet, ok := in.Subnets[az]
    56  			if !ok {
    57  				return nil, errors.Errorf("no subnet for zone %s", az)
    58  			}
    59  			publicSubnet = subnet.Public
    60  			subnetID = subnet.ID
    61  		}
    62  
    63  		if in.Pool.Name == types.MachinePoolEdgeRoleName {
    64  			// edge pools not share same instance type and regular cluster workloads.
    65  			// The instance type is selected based in the offerings for the location.
    66  			// The labels and taints are set to prevent regular workloads.
    67  			// https://github.com/openshift/enhancements/blob/master/enhancements/installer/aws-custom-edge-machineset-local-zones.md
    68  			zone := in.Zones[az]
    69  			if zone.PreferredInstanceType != "" {
    70  				instanceType = zone.PreferredInstanceType
    71  			}
    72  			nodeLabels = map[string]string{
    73  				"node-role.kubernetes.io/edge":          "",
    74  				"machine.openshift.io/zone-type":        zone.Type,
    75  				"machine.openshift.io/zone-group":       zone.GroupName,
    76  				"machine.openshift.io/parent-zone-name": zone.ParentZoneName,
    77  			}
    78  			nodeTaints = append(nodeTaints, corev1.Taint{
    79  				Key:    "node-role.kubernetes.io/edge",
    80  				Effect: "NoSchedule",
    81  			})
    82  		}
    83  
    84  		instanceProfile := mpool.IAMProfile
    85  		if len(instanceProfile) == 0 {
    86  			instanceProfile = fmt.Sprintf("%s-worker-profile", in.ClusterID)
    87  		}
    88  
    89  		provider, err := provider(&machineProviderInput{
    90  			clusterID:        in.ClusterID,
    91  			region:           in.InstallConfigPlatformAWS.Region,
    92  			subnet:           subnetID,
    93  			instanceType:     instanceType,
    94  			osImage:          mpool.AMIID,
    95  			zone:             az,
    96  			role:             "worker",
    97  			userDataSecret:   in.UserDataSecret,
    98  			instanceProfile:  instanceProfile,
    99  			root:             &mpool.EC2RootVolume,
   100  			imds:             mpool.EC2Metadata,
   101  			userTags:         in.InstallConfigPlatformAWS.UserTags,
   102  			publicSubnet:     publicSubnet,
   103  			securityGroupIDs: in.Pool.Platform.AWS.AdditionalSecurityGroupIDs,
   104  		})
   105  		if err != nil {
   106  			return nil, errors.Wrap(err, "failed to create provider")
   107  		}
   108  		name := fmt.Sprintf("%s-%s-%s", in.ClusterID, in.Pool.Name, az)
   109  		spec := machineapi.MachineSpec{
   110  			ProviderSpec: machineapi.ProviderSpec{
   111  				Value: &runtime.RawExtension{Object: provider},
   112  			},
   113  			ObjectMeta: machineapi.ObjectMeta{
   114  				Labels: nodeLabels,
   115  			},
   116  			Taints: nodeTaints,
   117  		}
   118  
   119  		mset := &machineapi.MachineSet{
   120  			TypeMeta: metav1.TypeMeta{
   121  				APIVersion: "machine.openshift.io/v1beta1",
   122  				Kind:       "MachineSet",
   123  			},
   124  			ObjectMeta: metav1.ObjectMeta{
   125  				Namespace: "openshift-machine-api",
   126  				Name:      name,
   127  				Labels: map[string]string{
   128  					"machine.openshift.io/cluster-api-cluster": in.ClusterID,
   129  				},
   130  			},
   131  			Spec: machineapi.MachineSetSpec{
   132  				Replicas: &replicas,
   133  				Selector: metav1.LabelSelector{
   134  					MatchLabels: map[string]string{
   135  						"machine.openshift.io/cluster-api-machineset": name,
   136  						"machine.openshift.io/cluster-api-cluster":    in.ClusterID,
   137  					},
   138  				},
   139  				Template: machineapi.MachineTemplateSpec{
   140  					ObjectMeta: machineapi.ObjectMeta{
   141  						Labels: map[string]string{
   142  							"machine.openshift.io/cluster-api-machineset":   name,
   143  							"machine.openshift.io/cluster-api-cluster":      in.ClusterID,
   144  							"machine.openshift.io/cluster-api-machine-role": in.Role,
   145  							"machine.openshift.io/cluster-api-machine-type": in.Role,
   146  						},
   147  					},
   148  					Spec: spec,
   149  					// we don't need to set Versions, because we control those via cluster operators.
   150  				},
   151  			},
   152  		}
   153  		machinesets = append(machinesets, mset)
   154  	}
   155  
   156  	return machinesets, nil
   157  }