-
Notifications
You must be signed in to change notification settings - Fork 33
/
compute.go
200 lines (173 loc) · 5.05 KB
/
compute.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
// compute.go holds functions specific to Azure compute resources
package main
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"net/http"
"os"
"github.com/pulumi/pulumi-azure/sdk/v5/go/azure/compute"
"github.com/pulumi/pulumi-azure/sdk/v5/go/azure/storage"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func (ri *ResourceInfo) createImage(ctx *pulumi.Context) error {
// TODO: be intelligent about whether we actually dl this
err := downloadVHD()
if err != nil {
return err
}
blob, err := storage.NewBlob(
ctx,
"talos-"+TalosVersion+".vhd",
&storage.BlobArgs{
StorageAccountName: ri.StorageAcct.Name,
StorageContainerName: ri.StorageContainer.Name,
Type: pulumi.String("Page"),
Source: pulumi.NewFileAsset(LocalVHDName),
Name: pulumi.String("talos-" + TalosVersion + ".vhd"),
},
)
if err != nil {
return err
}
img, err := compute.NewImage(
ctx,
"talos-"+TalosVersion,
&compute.ImageArgs{
ResourceGroupName: ri.ResourceGroup.Name,
OsDisk: &compute.ImageOsDiskArgs{
OsType: pulumi.String("Linux"),
OsState: pulumi.String("Generalized"),
BlobUri: blob.Url,
SizeGb: pulumi.Int(10),
},
Name: pulumi.String("talos-" + TalosVersion),
})
if err != nil {
return err
}
ri.Image = img
return nil
}
func (ri *ResourceInfo) createCPVMs(ctx *pulumi.Context) error {
for i := 0; i < ControlPlaneNodesCount; i++ {
_, err := compute.NewVirtualMachine(
ctx,
fmt.Sprintf("%s-cp-%d", ClusterName, i),
&compute.VirtualMachineArgs{
NetworkInterfaceIds: pulumi.StringArray{
ri.CPNics[fmt.Sprintf("%s-cp-nic-%d", ClusterName, i)].ID(),
},
ResourceGroupName: ri.ResourceGroup.Name,
VmSize: pulumi.String("Standard_DS1_v2"),
StorageImageReference: &compute.VirtualMachineStorageImageReferenceArgs{
Id: ri.Image.ID(),
},
StorageOsDisk: &compute.VirtualMachineStorageOsDiskArgs{
Name: pulumi.String(fmt.Sprintf("%s-cp-disk-%d", ClusterName, i)),
Caching: pulumi.String("ReadWrite"),
CreateOption: pulumi.String("FromImage"),
ManagedDiskType: pulumi.String("Standard_LRS"),
},
OsProfile: &compute.VirtualMachineOsProfileArgs{
ComputerName: pulumi.String(fmt.Sprintf("%s-cp-%d", ClusterName, i)),
AdminUsername: pulumi.String("testadmin"),
AdminPassword: pulumi.String("Password1234!"),
CustomData: ri.TalosClusterConfig.ControlplaneConfig,
},
OsProfileLinuxConfig: &compute.VirtualMachineOsProfileLinuxConfigArgs{
DisablePasswordAuthentication: pulumi.Bool(false),
},
},
)
if err != nil {
return err
}
}
return nil
}
func (ri *ResourceInfo) createWorkerVMs(ctx *pulumi.Context) error {
for i := 0; i < WorkerNodesCount; i++ {
_, err := compute.NewVirtualMachine(
ctx,
fmt.Sprintf("%s-worker-%d", ClusterName, i),
&compute.VirtualMachineArgs{
NetworkInterfaceIds: pulumi.StringArray{
ri.WorkerNics[fmt.Sprintf("%s-worker-nic-%d", ClusterName, i)].ID(),
},
ResourceGroupName: ri.ResourceGroup.Name,
VmSize: pulumi.String("Standard_DS1_v2"),
StorageImageReference: &compute.VirtualMachineStorageImageReferenceArgs{
Id: ri.Image.ID(),
},
StorageOsDisk: &compute.VirtualMachineStorageOsDiskArgs{
Name: pulumi.String(fmt.Sprintf("%s-worker-disk-%d", ClusterName, i)),
Caching: pulumi.String("ReadWrite"),
CreateOption: pulumi.String("FromImage"),
ManagedDiskType: pulumi.String("Standard_LRS"),
},
OsProfile: &compute.VirtualMachineOsProfileArgs{
ComputerName: pulumi.String(fmt.Sprintf("%s-worker-%d", ClusterName, i)),
AdminUsername: pulumi.String("testadmin"),
AdminPassword: pulumi.String("Password1234!"),
CustomData: ri.TalosClusterConfig.WorkerConfig,
},
OsProfileLinuxConfig: &compute.VirtualMachineOsProfileLinuxConfigArgs{
DisablePasswordAuthentication: pulumi.Bool(false),
},
},
)
if err != nil {
return err
}
}
return nil
}
func downloadVHD() error {
// Download VHD tar.gz from our releases
out, err := os.Create("azure-amd64.tar.gz")
if err != nil {
return err
}
//TODO: strings.Join() this or something better
resp, err := http.Get("https://github.com/siderolabs/talos/releases/download/" + TalosVersion + "/azure-amd64.tar.gz")
if err != nil {
return err
}
defer resp.Body.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
out.Close()
// Extract file
file, err := os.Open("azure-amd64.tar.gz")
if err != nil {
return err
}
defer file.Close()
var fileReader io.ReadCloser = file
fileReader, err = gzip.NewReader(file)
if err != nil {
return err
}
defer fileReader.Close()
tarBallReader := tar.NewReader(fileReader)
header, err := tarBallReader.Next()
if err != nil {
return err
}
writer, err := os.Create(LocalVHDName)
if err != nil {
return err
}
defer writer.Close()
io.Copy(writer, tarBallReader)
err = os.Chmod(LocalVHDName, os.FileMode(header.Mode))
if err != nil {
return err
}
return nil
}