-
Notifications
You must be signed in to change notification settings - Fork 0
/
single_node.go
206 lines (167 loc) · 7.03 KB
/
single_node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package taloscdk
import (
"fmt"
"github.com/aws/aws-cdk-go/awscdk"
"github.com/aws/aws-cdk-go/awscdk/awsec2"
"github.com/aws/aws-cdk-go/awscdk/awsiam"
"github.com/aws/constructs-go/constructs/v3"
"github.com/aws/jsii-runtime-go"
)
type SingleNodeProps struct {
// ClusterName is used for tagging all resources with kubernetes.io/cluster/<name>=owned
// Default: talos
ClusterName *string
// NodeName is used for naming your EC2 instances
// Default: jsii.String("talos")
NodeName *string
// MachineImageName is used for searching AMI by name and supports * wildcard.
// Be sure to select an arch that matches your instance type.
// It's typically easiest to use a wildcard for the region so that it works cross-region.
// Format: talos-<Version>-<AWSRegion>-<arch>
// Default: talos-v0.11.2-*-amd64
MachineImageName *string
// MachineImageAMI is used to get the image from an AMI.
// Talos AMIs can be found in the docs: https://www.talos.dev/docs/v0.11/cloud-platforms/aws/ (sub v0.11 for current version)
// Example: {"us-east-1": jsii.String("ami-0fdb2f5cb915076a3")} (us-east-1 amd64 v0.11 image)
// Defaults to using MachineImageName
MachineImageAMI *map[string]*string
// TalosNodeConfig is a *string of the controlplane.yaml or join.yaml you've generated with
// `talosctl gen config <clusterName> <endpoint>`
// To load a node config use taloscdk.LoadConfig("<yourConfig>")
//
// Example:
// config, err := taloscdk.LoadConfig("cluster-config/controlplane.yaml")
// if err != nil {
// panic("Could not load talos config")
// }
// TalosNodeConfig is required
TalosNodeConfig *string
// TransformConfig sets whether or not to change the endpoint in our TalosNodeConfig to
// the OverwriteValue
// Default: jsii.Bool(true)
TransformConfig *bool
// EndpointToOverwrite is the <endpoint> you used when running `talosctl gen config <clusterName> https://<endpoint>:6443`
// This will overwrite the <endpoint> in your config, while keeping https:// and the port (:6443).
// For example: in https://talos.cluster:6443, if you overwrite "talos.cluster", it would become https://YourOverwriteValue:6443
// By default, the OverwriteValue does not include protocl or port.
EndpointToOverwrite *string
// OverwriteValue to replace EndpointToOverwrite
// Default: EIP. Can use GetEIPAddress() to get from another node.
OverwriteValue *string
// InstanceType is used to determine the size/arch of the instance.
// Default: t3.small (amd64). Meets min specs: https://www.talos.dev/docs/v0.11/introduction/system-requirements/
InstanceType awsec2.InstanceType
// SecurityGroup for the instance.
// To create a security group to use with multiple images, you can use:
// taloscdk.NewSecutiyGroup()
// Default: Generates a new security group, opening ports: 6443, 50000, 50001 to the any peer
SecurityGroup awsec2.SecurityGroup
// Vpc selects the AWS VPC to deploy your instance into.
// Default for NewSingleNode(): Default VPC
Vpc awsec2.IVpc
// Subnets to allow the instance to be deployed into
// Default: &awsec2.SubnetSelection{SubnetType: awsec2.SubnetType_PUBLIC}
SubnetSelection *awsec2.SubnetSelection
// CreateEIP enables an ElasticIP to be created and allocated to your instance.
// This is generally used as the cluster endpoint in a single node cluster.
// Default: jsii.Bool("true")
CreateEIP *bool
// IAMRole used when launching the instance.
// If planning to create AWS load balancers, it's best to use
// taloscdk.NewControlPlaneIAMRole() or taloscdk.NewWorkerIAMRole()
// Default: NewControlPlaneIAMRole()
IAMRole awsiam.Role
}
type SingleNode struct {
constructs.Construct
// SecurityGroup used or created by NewSingleNode()
SecurityGroup awsec2.SecurityGroup
// VPC of the node
Vpc awsec2.IVpc
// EIP (if allocated/assigned)
EIP awsec2.CfnEIP
}
func (s *SingleNode) GetEIPAddress() *string {
return s.EIP.Ref()
}
// NewSingleNode creates a new EC2 instance that runs Talos.
// Required SingleNodeProps:
// TalosNodeConfig, EndpointToOverwrite (if TransformConfig==true)
func NewSingleNode(scope constructs.Construct, id *string, props *SingleNodeProps) SingleNode {
construct := awscdk.NewConstruct(scope, jsii.String(*id))
if props.ClusterName == nil {
props.ClusterName = jsii.String("talos")
}
if props.NodeName == nil {
props.NodeName = jsii.String("talos-node")
}
if props.Vpc == nil {
props.Vpc = awsec2.Vpc_FromLookup(construct, jsii.String("VPC"), &awsec2.VpcLookupOptions{
IsDefault: jsii.Bool(true),
})
}
if props.TalosNodeConfig == nil {
panic("TalosNodeConfig cannot be nil. taloscdk.LoadConfig() can be used to load the needed file.")
}
if props.SecurityGroup == nil {
props.SecurityGroup = NewSecurityGroup(construct, jsii.String("SG"), &SecurityGroupProps{
Vpc: props.Vpc,
})
}
if props.InstanceType == nil {
props.InstanceType = awsec2.InstanceType_Of(awsec2.InstanceClass_BURSTABLE3, awsec2.InstanceSize_SMALL)
}
if props.SubnetSelection == nil {
props.SubnetSelection = &awsec2.SubnetSelection{SubnetType: awsec2.SubnetType_PUBLIC}
}
if props.MachineImageName == nil && props.MachineImageAMI == nil {
props.MachineImageName = jsii.String("talos-v0.11.2-*-amd64")
}
if props.CreateEIP == nil {
props.CreateEIP = jsii.Bool(true)
}
var eip awsec2.CfnEIP
if *props.CreateEIP {
eip = awsec2.NewCfnEIP(construct, jsii.String("EIP"), &awsec2.CfnEIPProps{})
eip.ApplyRemovalPolicy(awscdk.RemovalPolicy_DESTROY, nil)
}
if props.OverwriteValue == nil {
props.OverwriteValue = eip.Ref()
}
if props.EndpointToOverwrite == nil && props.TransformConfig != nil {
panic("Requested config transform but missing EndpointToOverwrite.")
}
if *props.TransformConfig {
props.TalosNodeConfig = TransformConfig(props.TalosNodeConfig, *props.EndpointToOverwrite, *props.OverwriteValue)
}
if props.IAMRole == nil {
props.IAMRole = NewControlPlaneIAMRole(construct, jsii.String("Role"))
}
var image awsec2.IMachineImage
if props.MachineImageAMI != nil {
image = awsec2.NewGenericLinuxImage(props.MachineImageAMI, &awsec2.GenericLinuxImageProps{
UserData: awsec2.UserData_Custom(props.TalosNodeConfig),
})
} else {
image = awsec2.NewLookupMachineImage(&awsec2.LookupMachineImageProps{
Name: props.MachineImageName,
Owners: jsii.Strings("540036508848"),
UserData: awsec2.UserData_Custom(props.TalosNodeConfig),
})
}
instance := awsec2.NewInstance(construct, jsii.String("Instance"), &awsec2.InstanceProps{
InstanceName: props.NodeName,
InstanceType: props.InstanceType,
MachineImage: image,
Vpc: props.Vpc,
SecurityGroup: props.SecurityGroup,
VpcSubnets: props.SubnetSelection,
Role: props.IAMRole,
})
if *props.CreateEIP {
awsec2.NewCfnEIPAssociation(construct, jsii.String("EIPAssoc"), &awsec2.CfnEIPAssociationProps{InstanceId: instance.InstanceId(), Eip: eip.Ref()})
}
awscdk.Tags_Of(construct).Add(jsii.String(fmt.Sprintf("kubernetes.io/cluster/%s", *props.ClusterName)), jsii.String("owned"), nil)
TagSubnets(props.Vpc)
return SingleNode{Construct: construct, SecurityGroup: props.SecurityGroup, Vpc: props.Vpc, EIP: eip}
}