-
Notifications
You must be signed in to change notification settings - Fork 0
/
components.go
332 lines (288 loc) · 9.4 KB
/
components.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package main
import (
"fmt"
"github.com/pulumi/pulumi-gcp/sdk/v7/go/gcp/compute"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
type MachineSpec struct {
OS string
Size string
Count int
}
type FleetSpec struct {
Subnets []string
Machines []MachineSpec
}
type WebServerFleet struct {
pulumi.ResourceState
Name string
Subnets []pulumi.StringOutput
InstanceIps []pulumi.StringPtrOutput
}
var machineOS = map[string]string{
"ubuntu": "ubuntu-os-cloud/ubuntu-2204-lts",
"debian": "debian-cloud/debian-11",
}
var machineSize = map[string]string{
"small": "e2-micro",
"medium": "e2-small",
"large": "e2-medium",
}
var metadataStartupScripts = map[string]pulumi.String{
"ubuntu": `#!/bin/bash
sudo apt update
sudo apt install -y nginx
sudo ufw allow 'Nginx HTTP'`,
"debian": `#!/bin/bash
sudo apt update
sudo apt install -y nginx`,
}
func NewWebServerFleet(ctx *pulumi.Context, name string, network pulumi.IDOutput, instanceTag string, fleet FleetSpec, opts ...pulumi.ResourceOption) (*WebServerFleet, error) {
myFleet := &WebServerFleet{}
err := ctx.RegisterComponentResource("main:utils:WebFleet", name, myFleet, opts...)
if err != nil {
return nil, err
}
existing_subnets := map[string]struct{}{}
for k, v := range fleet.Subnets {
// Ensure that the subnet doesn't already exist.
if _, ok := existing_subnets[v]; ok {
continue
} else {
// Add our subnet to the map to keep tab.
existing_subnets[v] = struct{}{}
subnet_name := fmt.Sprintf("subnet-%v", k)
// Create a subnet on the network.
subnet, err := compute.NewSubnetwork(ctx, subnet_name, &compute.SubnetworkArgs{
Name: pulumi.String(v),
IpCidrRange: pulumi.Sprintf("10.%v.0.0/20", (k+1)%100),
Network: network,
}, pulumi.Parent(myFleet))
if err != nil {
return nil, err
}
myFleet.Subnets = append(myFleet.Subnets, subnet.Name)
}
}
for k, v := range fleet.Machines {
// Loop through and create all the machines using Instance Templates given the specs.
template_name := fmt.Sprintf("%s-fleet-template", v.OS)
instaceTemplate, err := compute.NewInstanceTemplate(ctx, template_name, &compute.InstanceTemplateArgs{
MachineType: pulumi.String(machineSize[v.Size]),
Disks: compute.InstanceTemplateDiskArray{
&compute.InstanceTemplateDiskArgs{
SourceImage: pulumi.String(machineOS[v.OS]),
AutoDelete: pulumi.Bool(true),
DiskSizeGb: pulumi.Int(10),
Boot: pulumi.Bool(true),
},
},
NetworkInterfaces: compute.InstanceTemplateNetworkInterfaceArray{
&compute.InstanceTemplateNetworkInterfaceArgs{
Network: network,
Subnetwork: pulumi.String(fleet.Subnets[k]),
AccessConfigs: compute.InstanceTemplateNetworkInterfaceAccessConfigArray{
compute.InstanceTemplateNetworkInterfaceAccessConfigArgs{
NatIp: nil,
// NetworkTier: nil,
},
},
},
},
ServiceAccount: compute.InstanceTemplateServiceAccountArgs{
Scopes: pulumi.ToStringArray([]string{
"https://www.googleapis.com/auth/cloud-platform",
}),
},
Metadata: pulumi.Map{
"foo": pulumi.Any("bar"),
},
MetadataStartupScript: metadataStartupScripts[v.OS],
CanIpForward: pulumi.Bool(false),
Tags: pulumi.ToStringArray([]string{
instanceTag,
}),
}, pulumi.Parent(myFleet))
if err != nil {
return nil, err
}
for i := 0; i < v.Count; i++ {
instance_name := fmt.Sprintf("%s-instance-%d", v.OS, i)
inst, err := compute.NewInstanceFromTemplate(ctx, instance_name, &compute.InstanceFromTemplateArgs{
Zone: pulumi.String("us-central1-a"),
SourceInstanceTemplate: instaceTemplate.SelfLinkUnique,
CanIpForward: pulumi.Bool(false),
Labels: pulumi.StringMap{
"my_key": pulumi.String("my_value"),
},
}, pulumi.Parent(myFleet))
if err != nil {
return nil, err
}
myFleet.InstanceIps = append(myFleet.InstanceIps, inst.NetworkInterfaces.Index(pulumi.Int(0)).AccessConfigs().Index(pulumi.Int(0)).NatIp())
}
}
myFleet.Name = name
return myFleet, nil
}
func NewWebServerFleet2(ctx *pulumi.Context, name string, network pulumi.IDOutput, instanceTag string, fleet FleetSpec, opts ...pulumi.ResourceOption) (*WebServerFleet, error) {
myFleet := &WebServerFleet{}
err := ctx.RegisterComponentResource("main:utils:WebFleet2", name, myFleet, opts...)
if err != nil {
return nil, err
}
existing_subnets := map[string]struct{}{}
for k, v := range fleet.Subnets {
// Ensure that the subnet doesn't already exist.
if _, ok := existing_subnets[v]; ok {
continue
} else {
// Add our subnet to the map to keep tab.
existing_subnets[v] = struct{}{}
subnet_name := fmt.Sprintf("subnet-%v", k)
// Create a subnet on the network.
_, err := compute.NewSubnetwork(ctx, subnet_name, &compute.SubnetworkArgs{
Name: pulumi.String(v),
IpCidrRange: pulumi.Sprintf("10.%v.0.0/20", (k+1)%100),
Network: network,
}, pulumi.Parent(myFleet))
if err != nil {
return nil, err
}
}
}
// Regional IP address for Regional External Forwarding Rule
regionalAddress, err := compute.NewAddress(ctx, "regional-ip-address", &compute.AddressArgs{
Name: pulumi.String("regional-ip-address"),
})
if err != nil {
return nil, err
}
// allow access from health check ranges
_, err = compute.NewFirewall(ctx, "health-check-fw-rule", &compute.FirewallArgs{
Name: pulumi.String("allow-health-checks"),
Direction: pulumi.String("INGRESS"),
Network: network,
SourceRanges: pulumi.StringArray{
pulumi.String("130.211.0.0/22"),
pulumi.String("35.191.0.0/16"),
},
Allows: compute.FirewallAllowArray{
&compute.FirewallAllowArgs{
Protocol: pulumi.String("tcp"),
Ports: pulumi.StringArray{
pulumi.String("80"),
},
},
},
TargetTags: pulumi.StringArray{
pulumi.String("allow-health-check"),
},
}, pulumi.Parent(myFleet))
if err != nil {
return nil, err
}
// Health Check for Instance Groups
health_check, err := compute.NewHealthCheck(ctx, "health-check", &compute.HealthCheckArgs{
CheckIntervalSec: pulumi.Int(5),
TimeoutSec: pulumi.Int(5),
HealthyThreshold: pulumi.Int(2),
UnhealthyThreshold: pulumi.Int(10),
HttpHealthCheck: &compute.HealthCheckHttpHealthCheckArgs{
Port: pulumi.Int(80),
},
}, pulumi.Parent(myFleet))
if err != nil {
return nil, err
}
// Target Pool for Intance Groups
instancePool, err := compute.NewTargetPool(ctx, "instance-pool", &compute.TargetPoolArgs{
Name: pulumi.String("instance-pool"),
})
if err != nil {
return nil, err
}
// Regional External Passthrough Network Load Balancer
_, err = compute.NewForwardingRule(ctx, "external-network-lb", &compute.ForwardingRuleArgs{
Name: pulumi.String("external-network-lb"),
IpAddress: regionalAddress.SelfLink,
IpProtocol: pulumi.String("TCP"),
LoadBalancingScheme: pulumi.String("EXTERNAL"),
PortRange: pulumi.String("80"),
Target: instancePool.SelfLink,
})
if err != nil {
return nil, err
}
for k, v := range fleet.Machines {
// Loop through and create all the machines using Managed Instance Groups given the specs.
template_name := fmt.Sprintf("%s-fleet-template", v.OS)
instaceTemplate, err := compute.NewInstanceTemplate(ctx, template_name, &compute.InstanceTemplateArgs{
MachineType: pulumi.String(machineSize[v.Size]),
Disks: compute.InstanceTemplateDiskArray{
&compute.InstanceTemplateDiskArgs{
SourceImage: pulumi.String(machineOS[v.OS]),
AutoDelete: pulumi.Bool(true),
DiskSizeGb: pulumi.Int(10),
Boot: pulumi.Bool(true),
},
},
NetworkInterfaces: compute.InstanceTemplateNetworkInterfaceArray{
&compute.InstanceTemplateNetworkInterfaceArgs{
Network: network,
Subnetwork: pulumi.String(fleet.Subnets[k]),
AccessConfigs: compute.InstanceTemplateNetworkInterfaceAccessConfigArray{
compute.InstanceTemplateNetworkInterfaceAccessConfigArgs{
NatIp: nil,
// NetworkTier: nil,
},
},
},
},
Metadata: pulumi.Map{
"foo": pulumi.Any("bar"),
},
MetadataStartupScript: metadataStartupScripts[v.OS],
CanIpForward: pulumi.Bool(false),
Tags: pulumi.ToStringArray([]string{
instanceTag, // Required for web servers
"allow-health-checks", // Required for health checks
}),
}, pulumi.Parent(myFleet))
if err != nil {
return nil, err
}
igm_name := fmt.Sprintf("%s-rigm-%v", v.OS, k)
// Managed Instance Group
_, err = compute.NewInstanceGroupManager(ctx, igm_name, &compute.InstanceGroupManagerArgs{
Name: pulumi.String(igm_name),
Zone: pulumi.String("us-central1-c"),
NamedPorts: compute.InstanceGroupManagerNamedPortArray{
&compute.InstanceGroupManagerNamedPortArgs{
Name: pulumi.String("tcp"),
Port: pulumi.Int(80),
},
},
Versions: compute.InstanceGroupManagerVersionArray{
&compute.InstanceGroupManagerVersionArgs{
InstanceTemplate: instaceTemplate.SelfLinkUnique,
Name: pulumi.String(v.OS),
},
},
BaseInstanceName: pulumi.String("fleet"),
TargetSize: pulumi.Int(v.Count),
TargetPools: pulumi.StringArray{
instancePool.SelfLink,
},
AutoHealingPolicies: &compute.InstanceGroupManagerAutoHealingPoliciesArgs{
HealthCheck: health_check.SelfLink,
InitialDelaySec: pulumi.Int(300),
},
}, pulumi.Parent(myFleet))
if err != nil {
return nil, err
}
}
myFleet.Name = name
return myFleet, nil
}