-
Notifications
You must be signed in to change notification settings - Fork 12
/
flight.go
210 lines (179 loc) · 5.64 KB
/
flight.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
// Copyright 2018 Red Hat
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package azure
import (
"fmt"
"time"
"github.com/coreos/pkg/capnslog"
ctplatform "github.com/flatcar-linux/container-linux-config-transpiler/config/platform"
"github.com/flatcar-linux/mantle/platform"
"github.com/flatcar-linux/mantle/platform/api/azure"
)
const (
Platform platform.Name = "azure"
)
var (
plog = capnslog.NewPackageLogger("github.com/flatcar-linux/mantle", "platform/machine/azure")
)
type flight struct {
*platform.BaseFlight
api *azure.API
SSHKey string
FakeSSHKey string
ImageResourceGroup string
ImageStorageAccount string
Network azure.Network
}
// NewFlight creates an instance of a Flight suitable for spawning
// instances on the Azure platform. The flight creates a new Resource Group
// if an image needs to be created from a blob URL or a local image file.
// Clusters created in the Flight will reuse the image Resource Group or
// create their own Resource Group if no image Resource Group exists.
func NewFlight(opts *azure.Options) (platform.Flight, error) {
api, err := azure.New(opts)
if err != nil {
return nil, err
}
if err = api.SetupClients(); err != nil {
return nil, fmt.Errorf("setting up clients: %v", err)
}
bf, err := platform.NewBaseFlight(opts.Options, Platform, ctplatform.Azure)
if err != nil {
return nil, err
}
af := &flight{
BaseFlight: bf,
api: api,
}
keys, err := af.Keys()
if err != nil {
af.Destroy()
return nil, err
}
af.SSHKey = keys[0].String()
af.FakeSSHKey, err = platform.GenerateFakeKey()
if err != nil {
return nil, err
}
if opts.BlobURL != "" || opts.ImageFile != "" {
imageName := fmt.Sprintf("%v", time.Now().UnixNano())
blobName := imageName + ".vhd"
container := "temp"
af.ImageResourceGroup, err = af.api.CreateResourceGroup("kola-cluster-image")
if err != nil {
return nil, err
}
af.ImageStorageAccount, err = af.api.CreateStorageAccount(af.ImageResourceGroup)
if err != nil {
return nil, err
}
af.Network, err = af.api.PrepareNetworkResources(af.ImageResourceGroup)
if err != nil {
af.Destroy()
return nil, err
}
kr, err := af.api.GetStorageServiceKeysARM(af.ImageStorageAccount, af.ImageResourceGroup)
if err != nil {
return nil, fmt.Errorf("Fetching storage service keys failed: %v", err)
}
if kr.Keys == nil {
return nil, fmt.Errorf("No storage service keys found")
}
if opts.BlobURL != "" {
for _, k := range *kr.Keys {
plog.Infof("Copying blob")
if err := af.api.CopyBlob(af.ImageStorageAccount, *k.Value, container, blobName, opts.BlobURL); err != nil {
return nil, fmt.Errorf("Copying blob failed: %v", err)
}
plog.Infof("Blob copy done")
break
}
} else if opts.ImageFile != "" {
for _, k := range *kr.Keys {
if err := af.api.UploadBlob(af.ImageStorageAccount, *k.Value, opts.ImageFile, container, blobName, true); err != nil {
return nil, fmt.Errorf("Uploading blob failed: %v", err)
}
break
}
}
targetBlobURL := af.api.UrlOfBlob(af.ImageStorageAccount, container, blobName).String()
var imgID string
if opts.UseGallery {
imgID, err = af.api.CreateGalleryImage(imageName, af.ImageResourceGroup, af.ImageStorageAccount, targetBlobURL)
if err != nil {
return nil, fmt.Errorf("couldn't create gallery image: %w", err)
}
plog.Infof("Created gallery image: %v\n", imgID)
} else {
img, err := af.api.CreateImage(imageName, af.ImageResourceGroup, targetBlobURL)
if err != nil {
return nil, fmt.Errorf("couldn't create image: %w", err)
}
if img.ID == nil {
return nil, fmt.Errorf("received nil image")
}
imgID = *img.ID
}
opts.DiskURI = imgID
}
return af, nil
}
// NewCluster creates an instance of a Cluster suitable for spawning
// instances on the Azure platform. The cluster is created in the Flight's
// Resource Group if it has one. Otherwise the cluster is created in a new
// Resource Group that is deleted when the cluster is destroyed.
func (af *flight) NewCluster(rconf *platform.RuntimeConfig) (platform.Cluster, error) {
bc, err := platform.NewBaseCluster(af.BaseFlight, rconf)
if err != nil {
return nil, err
}
ac := &cluster{
BaseCluster: bc,
flight: af,
}
if !rconf.NoSSHKeyInMetadata {
ac.sshKey = af.SSHKey
} else {
ac.sshKey = af.FakeSSHKey
}
if af.ImageResourceGroup != "" && af.ImageStorageAccount != "" {
ac.ResourceGroup = af.ImageResourceGroup
ac.StorageAccount = af.ImageStorageAccount
ac.Network = af.Network
} else {
ac.ResourceGroup, err = af.api.CreateResourceGroup("kola-cluster")
if err != nil {
return nil, err
}
ac.StorageAccount, err = af.api.CreateStorageAccount(ac.ResourceGroup)
if err != nil {
return nil, err
}
ac.Network, err = af.api.PrepareNetworkResources(ac.ResourceGroup)
if err != nil {
ac.Destroy()
return nil, err
}
}
af.AddCluster(ac)
return ac, nil
}
func (af *flight) Destroy() {
af.BaseFlight.Destroy()
if af.ImageResourceGroup != "" {
if e := af.api.TerminateResourceGroup(af.ImageResourceGroup); e != nil {
plog.Errorf("Deleting image resource group %v: %v", af.ImageResourceGroup, e)
}
}
}