Skip to content

Commit ee8ea85

Browse files
authored
Merge pull request #1831 from randomvariable/bastion-instance-type
✨ bastion: Make instance type selectable, with new defaults
2 parents f1e0891 + daf30e9 commit ee8ea85

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

api/v1alpha2/awscluster_conversion.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func (src *AWSCluster) ConvertTo(dstRaw conversion.Hub) error {
5555

5656
dst.Spec.Bastion.AllowedCIDRBlocks = restored.Spec.Bastion.AllowedCIDRBlocks
5757
dst.Spec.Bastion.DisableIngressRules = restored.Spec.Bastion.DisableIngressRules
58+
dst.Spec.Bastion.InstanceType = restored.Spec.Bastion.InstanceType
5859
dst.Spec.ImageLookupFormat = restored.Spec.ImageLookupFormat
5960
dst.Spec.ImageLookupOrg = restored.Spec.ImageLookupOrg
6061
dst.Spec.ImageLookupBaseOS = restored.Spec.ImageLookupBaseOS

api/v1alpha3/awscluster_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ type Bastion struct {
9999
// They are set as ingress rules for the Bastion host's Security Group (defaults to 0.0.0.0/0).
100100
// +optional
101101
AllowedCIDRBlocks []string `json:"allowedCIDRBlocks,omitempty"`
102+
103+
// InstanceType will use the specified instance type for the bastion. If not specified,
104+
// Cluster API Provider AWS will use t3.micro for all regions except us-east-1, where t2.micro
105+
// will be the default.
106+
InstanceType string `json:"instanceType,omitempty"`
102107
}
103108

104109
// AWSLoadBalancerSpec defines the desired state of an AWS load balancer

config/crd/bases/infrastructure.cluster.x-k8s.io_awsclusters.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,9 @@ spec:
414414
enabled:
415415
description: Enabled allows this provider to create a bastion host instance with a public ip to access the VPC private network.
416416
type: boolean
417+
instanceType:
418+
description: InstanceType will use the specified instance type for the bastion. If not specified, Cluster API Provider AWS will use t3.micro for all regions except us-east-1, where t2.micro will be the default.
419+
type: string
417420
type: object
418421
controlPlaneEndpoint:
419422
description: ControlPlaneEndpoint represents the endpoint used to communicate with the control plane.

pkg/cloud/services/ec2/bastion.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package ec2
1919
import (
2020
"encoding/base64"
2121
"fmt"
22+
"strings"
2223

2324
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
2425
"sigs.k8s.io/cluster-api/util/conditions"
@@ -37,6 +38,11 @@ const (
3738
defaultSSHKeyName = "default"
3839
)
3940

41+
var (
42+
fallbackBastionInstanceType = "t3.micro"
43+
fallbackBastionUsEast1InstanceType = "t2.micro"
44+
)
45+
4046
// ReconcileBastion ensures a bastion is created for the cluster
4147
func (s *Service) ReconcileBastion() error {
4248
if !s.scope.Bastion().Enabled {
@@ -61,8 +67,6 @@ func (s *Service) ReconcileBastion() error {
6167
return errors.New("failed to reconcile bastion host, no public subnets are available")
6268
}
6369

64-
spec := s.getDefaultBastion()
65-
6670
// Describe bastion instance, if any.
6771
instance, err := s.describeBastionInstance()
6872
if awserrors.IsNotFound(err) { // nolint:nestif
@@ -72,7 +76,7 @@ func (s *Service) ReconcileBastion() error {
7276
return errors.Wrap(err, "failed to patch conditions")
7377
}
7478
}
75-
instance, err = s.runInstance("bastion", spec)
79+
instance, err = s.runInstance("bastion", s.getDefaultBastion(s.scope.Bastion().InstanceType))
7680
if err != nil {
7781
record.Warnf(s.scope.InfraCluster(), "FailedCreateBastion", "Failed to create bastion instance: %v", err)
7882
return err
@@ -147,7 +151,7 @@ func (s *Service) describeBastionInstance() (*infrav1.Instance, error) {
147151
return nil, awserrors.NewNotFound(errors.New("bastion host not found"))
148152
}
149153

150-
func (s *Service) getDefaultBastion() *infrav1.Instance {
154+
func (s *Service) getDefaultBastion(instanceType string) *infrav1.Instance {
151155
name := fmt.Sprintf("%s-bastion", s.scope.Name())
152156
userData, _ := userdata.NewBastion(&userdata.BastionInput{})
153157

@@ -157,9 +161,19 @@ func (s *Service) getDefaultBastion() *infrav1.Instance {
157161
keyName = aws.String(defaultSSHKeyName)
158162
}
159163

164+
subnet := s.scope.Subnets().FilterPublic()[0]
165+
166+
if instanceType == "" {
167+
if strings.Contains(subnet.AvailabilityZone, "us-east-1") {
168+
instanceType = fallbackBastionUsEast1InstanceType
169+
} else {
170+
instanceType = fallbackBastionInstanceType
171+
}
172+
}
173+
160174
i := &infrav1.Instance{
161-
Type: "t2.micro",
162-
SubnetID: s.scope.Subnets().FilterPublic()[0].ID,
175+
Type: instanceType,
176+
SubnetID: subnet.ID,
163177
ImageID: s.defaultBastionAMILookup(s.scope.Region()),
164178
SSHKeyName: keyName,
165179
UserData: aws.String(base64.StdEncoding.EncodeToString([]byte(userData))),

0 commit comments

Comments
 (0)