func()

in operator/pkg/awsprovider/launchtemplate/reconciler.go [91:140]


func (c *Controller) createLaunchTemplate(ctx context.Context, dataplane *v1alpha1.DataPlane) error {
	// Currently, we get the same security group assigned to control plane instances
	// At some point, we will be creating dataplane specific security groups
	securityGroupID, err := securitygroup.New(c.ec2api, c.kubeclient).For(ctx, dataplane.Spec.ClusterName)
	if err != nil {
		return fmt.Errorf("getting security group for control plane nodes, %w", err)
	}
	clusterEndpoint, err := master.GetClusterEndpoint(ctx, c.kubeclient, types.NamespacedName{dataplane.Namespace, dataplane.Spec.ClusterName})
	if err != nil {
		return fmt.Errorf("getting cluster endpoint, %w", err)
	}
	caSecret, err := keypairs.Reconciler(c.kubeclient).GetSecretFromServer(ctx,
		object.NamespacedName(master.RootCASecretNameFor(dataplane.Spec.ClusterName), dataplane.Namespace))
	if err != nil {
		return fmt.Errorf("getting control plane ca certificate, %w", err)
	}
	_, clusterCA := secrets.Parse(caSecret)
	amiID, err := c.amiID(ctx, dataplane)
	if err != nil {
		return fmt.Errorf("getting ami id for worker nodes, %w", err)
	}
	input := &ec2.CreateLaunchTemplateInput{
		LaunchTemplateData: &ec2.RequestLaunchTemplateData{
			BlockDeviceMappings: []*ec2.LaunchTemplateBlockDeviceMappingRequest{{
				DeviceName: ptr.String("/dev/xvda"),
				Ebs: &ec2.LaunchTemplateEbsBlockDeviceRequest{
					DeleteOnTermination: ptr.Bool(true),
					Iops:                ptr.Int64(3000),
					VolumeSize:          ptr.Int64(20),
					VolumeType:          ptr.String("gp3"),
				}},
			},
			InstanceType: ptr.String("t2.xlarge"), // TODO get this from dataplane spec
			ImageId:      ptr.String(amiID),
			IamInstanceProfile: &ec2.LaunchTemplateIamInstanceProfileSpecificationRequest{
				Name: aws.String(iam.KitNodeInstanceProfileNameFor(dataplane.Spec.ClusterName)),
			},
			Monitoring:       &ec2.LaunchTemplatesMonitoringRequest{Enabled: ptr.Bool(true)},
			SecurityGroupIds: []*string{ptr.String(securityGroupID)},
			UserData: ptr.String(base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(userData,
				dataplane.Spec.ClusterName, v1alpha1.SchemeGroupVersion.Group, dnsClusterIP, base64.StdEncoding.EncodeToString(clusterCA), clusterEndpoint)))),
		},
		LaunchTemplateName: ptr.String(TemplateName(dataplane.Spec.ClusterName)),
		TagSpecifications:  generateEC2Tags("launch-template", dataplane.Spec.ClusterName),
	}
	if _, err := c.ec2api.CreateLaunchTemplate(input); err != nil {
		return fmt.Errorf("creating launch template, %w", err)
	}
	return nil
}