/
provision.go
247 lines (207 loc) · 7.75 KB
/
provision.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
package infra
import (
"context"
"fmt"
"time"
"golang.org/x/sync/errgroup"
"github.com/Azure/aks-app-routing-operator/testing/e2e/clients"
"github.com/Azure/aks-app-routing-operator/testing/e2e/logger"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/keyvault/armkeyvault"
)
const (
// lenZones is the number of zones to provision
lenZones = 2
// lenPrivateZones is the number of private zones to provision
lenPrivateZones = 2
)
func (i *infra) Provision(ctx context.Context, tenantId, subscriptionId, applicationObjectId string) (Provisioned, *logger.LoggedError) {
lgr := logger.FromContext(ctx).With("infra", i.Name)
lgr.Info("provisioning infrastructure")
defer lgr.Info("finished provisioning infrastructure")
ret := Provisioned{
Name: i.Name,
SubscriptionId: subscriptionId,
TenantId: tenantId,
}
if i.AuthType == AuthTypeServicePrincipal {
if applicationObjectId == "" {
return ret, logger.Error(lgr, fmt.Errorf("application object id must be provided when provisioning infrastructure with service principal options"))
}
credName := i.Name + "-cred"
spOpt, err := clients.GetServicePrincipalOptions(ctx, applicationObjectId, credName)
if err != nil {
return ret, logger.Error(lgr, fmt.Errorf("getting app with credential: %w", err))
}
i.ServicePrincipal = spOpt
}
var err error
ret.ResourceGroup, err = clients.NewResourceGroup(ctx, subscriptionId, i.ResourceGroup, i.Location, clients.DeleteAfterOpt(2*time.Hour))
if err != nil {
return Provisioned{}, logger.Error(lgr, fmt.Errorf("creating resource group %s: %w", i.ResourceGroup, err))
}
// create resources
var resEg errgroup.Group
resEg.Go(func() error {
ret.ContainerRegistry, err = clients.NewAcr(ctx, subscriptionId, i.ResourceGroup, "registry"+i.Suffix, i.Location)
if err != nil {
return logger.Error(lgr, fmt.Errorf("creating container registry: %w", err))
}
resEg.Go(func() error {
e2eRepoAndTag := "e2e:" + i.Suffix
if err := ret.ContainerRegistry.BuildAndPush(ctx, e2eRepoAndTag, "../../", "../../Dockerfile.e2e"); err != nil {
return logger.Error(lgr, fmt.Errorf("building and pushing e2e image: %w", err))
}
ret.E2eImage = ret.ContainerRegistry.GetName() + ".azurecr.io/" + e2eRepoAndTag
return nil
})
resEg.Go(func() error {
operatorRepoAndTag := "operator:" + i.Suffix
if err := ret.ContainerRegistry.BuildAndPush(ctx, operatorRepoAndTag, "../../", "../../Dockerfile"); err != nil {
return logger.Error(lgr, fmt.Errorf("building and pushing operator image: %w", err))
}
ret.OperatorImage = ret.ContainerRegistry.GetName() + ".azurecr.io/" + operatorRepoAndTag
return nil
})
return nil
})
resEg.Go(func() error {
ret.Cluster, err = clients.NewAks(ctx, subscriptionId, i.ResourceGroup, "cluster"+i.Suffix, i.Location, i.ServicePrincipal, i.McOpts...)
if err != nil {
return logger.Error(lgr, fmt.Errorf("creating managed cluster: %w", err))
}
return nil
})
resEg.Go(func() error {
ret.KeyVault, err = clients.NewAkv(ctx, tenantId, subscriptionId, i.ResourceGroup, "keyvault"+i.Suffix, i.Location)
if err != nil {
return logger.Error(lgr, fmt.Errorf("creating key vault: %w", err))
}
ret.Cert, err = ret.KeyVault.CreateCertificate(ctx, "cert"+i.Suffix, []string{"" + i.Suffix})
if err != nil {
return logger.Error(lgr, fmt.Errorf("creating certificate: %w", err))
}
return nil
})
for idx := 0; idx < lenZones; idx++ {
// need to perform loop variable capture on the index.
// https://github.com/golang/go/wiki/LoopvarExperiment
// there is a proposed change for 1.21 https://tip.golang.org/blog/go1.21rc
// that will change the loop variable capture to be the standard way loops work.
func(idx int) {
resEg.Go(func() error {
zone, err := clients.NewZone(ctx, subscriptionId, i.ResourceGroup, fmt.Sprintf("zone-%d-%s", idx, i.Suffix))
if err != nil {
return logger.Error(lgr, fmt.Errorf("creating zone: %w", err))
}
ret.Zones = append(ret.Zones, zone)
return nil
})
}(idx)
}
for idx := 0; idx < lenPrivateZones; idx++ {
func(idx int) {
resEg.Go(func() error {
privateZone, err := clients.NewPrivateZone(ctx, subscriptionId, i.ResourceGroup, fmt.Sprintf("private-zone-%d-%s", idx, i.Suffix))
if err != nil {
return logger.Error(lgr, fmt.Errorf("creating private zone: %w", err))
}
ret.PrivateZones = append(ret.PrivateZones, privateZone)
return nil
})
}(idx)
}
if err := resEg.Wait(); err != nil {
return Provisioned{}, logger.Error(lgr, err)
}
// connect permissions
var permEg errgroup.Group
for _, pz := range ret.PrivateZones {
func(pz privateZone) {
permEg.Go(func() error {
dns, err := pz.GetDnsZone(ctx)
if err != nil {
return logger.Error(lgr, fmt.Errorf("getting dns: %w", err))
}
principalId := ret.Cluster.GetPrincipalId()
role := clients.PrivateDnsContributorRole
if _, err := clients.NewRoleAssignment(ctx, subscriptionId, *dns.ID, principalId, role); err != nil {
return logger.Error(lgr, fmt.Errorf("creating %s role assignment: %w", role.Name, err))
}
vnet, err := ret.Cluster.GetVnetId(ctx)
if err != nil {
return logger.Error(lgr, fmt.Errorf("getting vnet id: %w", err))
}
if err := pz.LinkVnet(ctx, fmt.Sprintf("link-%s-%s", pz.GetName(), i.Suffix), vnet); err != nil {
return logger.Error(lgr, fmt.Errorf("linking vnet: %w", err))
}
return nil
})
}(pz)
}
for _, z := range ret.Zones {
func(z zone) {
permEg.Go(func() error {
dns, err := z.GetDnsZone(ctx)
if err != nil {
return logger.Error(lgr, fmt.Errorf("getting dns: %w", err))
}
principalId := ret.Cluster.GetPrincipalId()
role := clients.DnsContributorRole
if _, err := clients.NewRoleAssignment(ctx, subscriptionId, *dns.ID, principalId, role); err != nil {
return logger.Error(lgr, fmt.Errorf("creating %s role assignment: %w", role.Name, err))
}
return nil
})
}(z)
}
permEg.Go(func() error {
principalId := ret.Cluster.GetPrincipalId()
role := clients.AcrPullRole
scope := ret.ContainerRegistry.GetId()
if _, err := clients.NewRoleAssignment(ctx, subscriptionId, scope, principalId, role); err != nil {
return logger.Error(lgr, fmt.Errorf("creating %s role assignment: %w", role.Name, err))
}
return nil
})
permEg.Go(func() error {
principalId := ret.Cluster.GetPrincipalId()
if err := ret.KeyVault.AddAccessPolicy(ctx, principalId, armkeyvault.Permissions{
Certificates: []*armkeyvault.CertificatePermissions{to.Ptr(armkeyvault.CertificatePermissionsGet)},
Secrets: []*armkeyvault.SecretPermissions{to.Ptr(armkeyvault.SecretPermissionsGet)},
}); err != nil {
return logger.Error(lgr, fmt.Errorf("adding access policy: %w", err))
}
return nil
})
if err := permEg.Wait(); err != nil {
return Provisioned{}, logger.Error(lgr, err)
}
return ret, nil
}
func (is infras) Provision(tenantId, subscriptionId, applicationObjectId string) ([]Provisioned, error) {
lgr := logger.FromContext(context.Background())
lgr.Info("starting to provision all infrastructure")
defer lgr.Info("finished provisioning all infrastructure")
var eg errgroup.Group
provisioned := make([]Provisioned, len(is))
for idx, inf := range is {
func(idx int, inf infra) {
eg.Go(func() error {
ctx := context.Background()
lgr := logger.FromContext(ctx)
ctx = logger.WithContext(ctx, lgr.With("infra", inf.Name))
provisionedInfra, err := inf.Provision(ctx, tenantId, subscriptionId, applicationObjectId)
if err != nil {
return fmt.Errorf("provisioning infrastructure %s: %w", inf.Name, err)
}
provisioned[idx] = provisionedInfra
return nil
})
}(idx, inf)
}
if err := eg.Wait(); err != nil {
return nil, err
}
return provisioned, nil
}