github.com/giantswarm/apiextensions/v2@v2.6.2/pkg/apis/infrastructure/v1alpha2/nodepool.go (about)

     1  package v1alpha2
     2  
     3  import (
     4  	corev1 "k8s.io/api/core/v1"
     5  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     6  	apiv1alpha2 "sigs.k8s.io/cluster-api/api/v1alpha2"
     7  
     8  	"github.com/giantswarm/apiextensions/v2/pkg/annotation"
     9  	"github.com/giantswarm/apiextensions/v2/pkg/id"
    10  	"github.com/giantswarm/apiextensions/v2/pkg/label"
    11  )
    12  
    13  // +k8s:deepcopy-gen=false
    14  
    15  type NodePoolCRsConfig struct {
    16  	AvailabilityZones                   []string
    17  	AWSInstanceType                     string
    18  	ClusterID                           string
    19  	MachineDeploymentID                 string
    20  	Description                         string
    21  	NodesMax                            int
    22  	NodesMin                            int
    23  	OnDemandBaseCapacity                int
    24  	OnDemandPercentageAboveBaseCapacity int
    25  	Owner                               string
    26  	ReleaseComponents                   map[string]string
    27  	ReleaseVersion                      string
    28  	UseAlikeInstanceTypes               bool
    29  }
    30  
    31  // +k8s:deepcopy-gen=false
    32  
    33  type NodePoolCRs struct {
    34  	MachineDeployment    *apiv1alpha2.MachineDeployment
    35  	AWSMachineDeployment *AWSMachineDeployment
    36  }
    37  
    38  func NewNodePoolCRs(config NodePoolCRsConfig) (NodePoolCRs, error) {
    39  	// Default some essentials in case certain information are not given. E.g.
    40  	// the Tenant Cluster ID may be provided by the user.
    41  	{
    42  		if config.ClusterID == "" {
    43  			config.ClusterID = id.Generate()
    44  		}
    45  		if config.MachineDeploymentID == "" {
    46  			config.MachineDeploymentID = id.Generate()
    47  		}
    48  	}
    49  
    50  	awsMachineDeploymentCR := newAWSMachineDeploymentCR(config)
    51  	machineDeploymentCR := newMachineDeploymentCR(awsMachineDeploymentCR, config)
    52  
    53  	crs := NodePoolCRs{
    54  		MachineDeployment:    machineDeploymentCR,
    55  		AWSMachineDeployment: awsMachineDeploymentCR,
    56  	}
    57  
    58  	return crs, nil
    59  }
    60  
    61  func newAWSMachineDeploymentCR(c NodePoolCRsConfig) *AWSMachineDeployment {
    62  	return &AWSMachineDeployment{
    63  		TypeMeta: metav1.TypeMeta{
    64  			Kind:       kindAWSMachineDeployment,
    65  			APIVersion: SchemeGroupVersion.String(),
    66  		},
    67  		ObjectMeta: metav1.ObjectMeta{
    68  			Name:      c.MachineDeploymentID,
    69  			Namespace: metav1.NamespaceDefault,
    70  			Annotations: map[string]string{
    71  				annotation.Docs: "https://docs.giantswarm.io/reference/cp-k8s-api/awsmachinedeployments.infrastructure.giantswarm.io",
    72  			},
    73  			Labels: map[string]string{
    74  				label.AWSOperatorVersion: c.ReleaseComponents["aws-operator"],
    75  				label.Cluster:            c.ClusterID,
    76  				label.MachineDeployment:  c.MachineDeploymentID,
    77  				label.Organization:       c.Owner,
    78  				label.ReleaseVersion:     c.ReleaseVersion,
    79  			},
    80  		},
    81  		Spec: AWSMachineDeploymentSpec{
    82  			NodePool: AWSMachineDeploymentSpecNodePool{
    83  				Description: c.Description,
    84  				Machine: AWSMachineDeploymentSpecNodePoolMachine{
    85  					DockerVolumeSizeGB:  100,
    86  					KubeletVolumeSizeGB: 100,
    87  				},
    88  				Scaling: AWSMachineDeploymentSpecNodePoolScaling{
    89  					Max: c.NodesMax,
    90  					Min: c.NodesMin,
    91  				},
    92  			},
    93  			Provider: AWSMachineDeploymentSpecProvider{
    94  				AvailabilityZones: c.AvailabilityZones,
    95  				Worker: AWSMachineDeploymentSpecProviderWorker{
    96  					InstanceType:          c.AWSInstanceType,
    97  					UseAlikeInstanceTypes: c.UseAlikeInstanceTypes,
    98  				},
    99  				InstanceDistribution: AWSMachineDeploymentSpecInstanceDistribution{
   100  					OnDemandBaseCapacity:                c.OnDemandBaseCapacity,
   101  					OnDemandPercentageAboveBaseCapacity: &c.OnDemandPercentageAboveBaseCapacity,
   102  				},
   103  			},
   104  		},
   105  	}
   106  }
   107  
   108  func newMachineDeploymentCR(obj *AWSMachineDeployment, c NodePoolCRsConfig) *apiv1alpha2.MachineDeployment {
   109  	return &apiv1alpha2.MachineDeployment{
   110  		TypeMeta: metav1.TypeMeta{
   111  			Kind:       "MachineDeployment",
   112  			APIVersion: "cluster.x-k8s.io/v1alpha2",
   113  		},
   114  		ObjectMeta: metav1.ObjectMeta{
   115  			Name:      c.MachineDeploymentID,
   116  			Namespace: metav1.NamespaceDefault,
   117  			Annotations: map[string]string{
   118  				annotation.Docs: "https://docs.giantswarm.io/reference/cp-k8s-api/machinedeployments.cluster.x-k8s.io",
   119  			},
   120  			Labels: map[string]string{
   121  				label.Cluster:                c.ClusterID,
   122  				label.ClusterOperatorVersion: c.ReleaseComponents["cluster-operator"],
   123  				label.MachineDeployment:      c.MachineDeploymentID,
   124  				label.Organization:           c.Owner,
   125  				label.ReleaseVersion:         c.ReleaseVersion,
   126  			},
   127  		},
   128  		Spec: apiv1alpha2.MachineDeploymentSpec{
   129  			Template: apiv1alpha2.MachineTemplateSpec{
   130  				Spec: apiv1alpha2.MachineSpec{
   131  					InfrastructureRef: corev1.ObjectReference{
   132  						APIVersion: obj.TypeMeta.APIVersion,
   133  						Kind:       obj.TypeMeta.Kind,
   134  						Name:       obj.GetName(),
   135  						Namespace:  obj.GetNamespace(),
   136  					},
   137  				},
   138  			},
   139  		},
   140  	}
   141  }