/
ipaddresses.go
327 lines (278 loc) · 11.8 KB
/
ipaddresses.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
package cluster
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
mgmtnetwork "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2020-08-01/network"
"github.com/Azure/go-autorest/autorest/to"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/Azure/ARO-RP/pkg/api"
"github.com/Azure/ARO-RP/pkg/database/cosmosdb"
"github.com/Azure/ARO-RP/pkg/env"
"github.com/Azure/ARO-RP/pkg/util/installer"
"github.com/Azure/ARO-RP/pkg/util/stringutils"
)
func (m *manager) updateClusterData(ctx context.Context) error {
resourceGroup := stringutils.LastTokenByte(m.doc.OpenShiftCluster.Properties.ClusterProfile.ResourceGroupID, '/')
account := "cluster" + m.doc.OpenShiftCluster.Properties.StorageSuffix
pg, err := m.graph.LoadPersisted(ctx, resourceGroup, account)
if err != nil {
return err
}
var installConfig *installer.InstallConfig
var kubeadminPassword *installer.KubeadminPasswordData
err = pg.GetByName(false, "*password.KubeadminPassword", &kubeadminPassword)
if err != nil {
return err
}
err = pg.GetByName(false, "*installconfig.InstallConfig", &installConfig)
if err != nil {
return err
}
// See aro-installer/pkg/installer/generateinstallconfig.go
domain := m.doc.OpenShiftCluster.Properties.ClusterProfile.Domain
if !strings.ContainsRune(domain, '.') {
domain += "." + m.env.Domain()
}
m.doc, err = m.db.PatchWithLease(ctx, m.doc.Key, func(doc *api.OpenShiftClusterDocument) error {
doc.OpenShiftCluster.Properties.APIServerProfile.URL = "https://api." + domain + ":6443/"
doc.OpenShiftCluster.Properties.ConsoleProfile.URL = "https://console-openshift-console.apps." + domain + "/"
doc.OpenShiftCluster.Properties.KubeadminPassword = api.SecureString(kubeadminPassword.Password)
doc.OpenShiftCluster.Properties.NetworkProfile.SoftwareDefinedNetwork = api.SoftwareDefinedNetwork(installConfig.Config.NetworkType)
return nil
})
return err
}
func (m *manager) createOrUpdateRouterIPFromCluster(ctx context.Context) error {
if !m.isIngressProfileAvailable() {
m.log.Error("skip createOrUpdateRouterIPFromCluster")
return nil
}
svc, err := m.kubernetescli.CoreV1().Services("openshift-ingress").Get(ctx, "router-default", metav1.GetOptions{})
// default ingress must be present in the cluster
if err != nil {
return err
}
// This must be present always. If not - we have an issue
if len(svc.Status.LoadBalancer.Ingress) == 0 {
return fmt.Errorf("routerIP not found")
}
ipAddress := svc.Status.LoadBalancer.Ingress[0].IP
err = m.dns.CreateOrUpdateRouter(ctx, m.doc.OpenShiftCluster, ipAddress)
if err != nil {
return err
}
m.doc, err = m.db.PatchWithLease(ctx, m.doc.Key, func(doc *api.OpenShiftClusterDocument) error {
doc.OpenShiftCluster.Properties.IngressProfiles[0].IP = ipAddress
return nil
})
return err
}
func (m *manager) createOrUpdateRouterIPEarly(ctx context.Context) error {
infraID := m.doc.OpenShiftCluster.Properties.InfraID
resourceGroup := stringutils.LastTokenByte(m.doc.OpenShiftCluster.Properties.ClusterProfile.ResourceGroupID, '/')
var ipAddress string
if m.doc.OpenShiftCluster.Properties.IngressProfiles[0].Visibility == api.VisibilityPublic {
ip, err := m.publicIPAddresses.Get(ctx, resourceGroup, infraID+"-default-v4", "")
if err != nil {
return err
}
ipAddress = *ip.IPAddress
} else {
// there's no way to reserve private IPs in Azure, so we pick the
// highest free address in the subnet (i.e., there's a race here). Azure
// specifically documents that dynamic allocation proceeds from the
// bottom of the subnet, so there's a good chance that we'll get away
// with this.
// https://docs.microsoft.com/en-us/azure/virtual-network/private-ip-addresses#allocation-method
var err error
workerProfiles, _ := api.GetEnrichedWorkerProfiles(m.doc.OpenShiftCluster.Properties)
workerSubnetId := workerProfiles[0].SubnetID
ipAddress, err = m.subnet.GetHighestFreeIP(ctx, workerSubnetId)
if err != nil {
return err
}
if ipAddress == "" {
return api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidLinkedVNet, "", "The subnet '%s' has no remaining IP addresses.", m.doc.OpenShiftCluster.Properties.MasterProfile.SubnetID)
}
}
err := m.dns.CreateOrUpdateRouter(ctx, m.doc.OpenShiftCluster, ipAddress)
if err != nil {
return err
}
m.doc, err = m.db.PatchWithLease(ctx, m.doc.Key, func(doc *api.OpenShiftClusterDocument) error {
doc.OpenShiftCluster.Properties.IngressProfiles[0].IP = ipAddress
return nil
})
return err
}
func (m *manager) populateDatabaseIntIP(ctx context.Context) error {
if m.doc.OpenShiftCluster.Properties.APIServerProfile.IntIP != "" {
return nil
}
infraID := m.doc.OpenShiftCluster.Properties.InfraID
if infraID == "" {
infraID = "aro"
}
resourceGroup := stringutils.LastTokenByte(m.doc.OpenShiftCluster.Properties.ClusterProfile.ResourceGroupID, '/')
var lbName string
switch m.doc.OpenShiftCluster.Properties.ArchitectureVersion {
case api.ArchitectureVersionV1:
lbName = infraID + "-internal-lb"
case api.ArchitectureVersionV2:
lbName = infraID + "-internal"
default:
return fmt.Errorf("unknown architecture version %d", m.doc.OpenShiftCluster.Properties.ArchitectureVersion)
}
lb, err := m.loadBalancers.Get(ctx, resourceGroup, lbName, "")
if err != nil {
return err
}
m.doc, err = m.db.PatchWithLease(ctx, m.doc.Key, func(doc *api.OpenShiftClusterDocument) error {
doc.OpenShiftCluster.Properties.APIServerProfile.IntIP = *((*lb.FrontendIPConfigurations)[0].PrivateIPAddress)
return nil
})
return err
}
// this function can only be called on create - not on update - because it
// refers to -pip-v4, which doesn't exist on pre-DNS change clusters.
func (m *manager) updateAPIIPEarly(ctx context.Context) error {
infraID := m.doc.OpenShiftCluster.Properties.InfraID
resourceGroup := stringutils.LastTokenByte(m.doc.OpenShiftCluster.Properties.ClusterProfile.ResourceGroupID, '/')
lb, err := m.loadBalancers.Get(ctx, resourceGroup, infraID+"-internal", "")
if err != nil {
return err
}
intIPAddress := *((*lb.FrontendIPConfigurations)[0].PrivateIPAddress)
ipAddress := intIPAddress
if m.doc.OpenShiftCluster.Properties.APIServerProfile.Visibility == api.VisibilityPublic {
ip, err := m.publicIPAddresses.Get(ctx, resourceGroup, infraID+"-pip-v4", "")
if err != nil {
return err
}
ipAddress = *ip.IPAddress
}
err = m.dns.Update(ctx, m.doc.OpenShiftCluster, ipAddress)
if err != nil {
return err
}
m.doc, err = m.db.PatchWithLease(ctx, m.doc.Key, func(doc *api.OpenShiftClusterDocument) error {
doc.OpenShiftCluster.Properties.APIServerProfile.IP = ipAddress
doc.OpenShiftCluster.Properties.APIServerProfile.IntIP = intIPAddress
return nil
})
return err
}
// ensureGatewayCreate approves the gateway PE/PLS connection, creates the
// gateway database record and updates the model with the private endpoint IP.
func (m *manager) ensureGatewayCreate(ctx context.Context) error {
if !m.doc.OpenShiftCluster.Properties.FeatureProfile.GatewayEnabled ||
m.doc.OpenShiftCluster.Properties.NetworkProfile.GatewayPrivateEndpointIP != "" {
return nil
}
infraID := m.doc.OpenShiftCluster.Properties.InfraID
resourceGroup := stringutils.LastTokenByte(m.doc.OpenShiftCluster.Properties.ClusterProfile.ResourceGroupID, '/')
pe, err := m.privateEndpoints.Get(ctx, resourceGroup, infraID+"-pe", "networkInterfaces")
if err != nil {
return err
}
pls, err := m.rpPrivateLinkServices.Get(ctx, m.env.GatewayResourceGroup(), "gateway-pls-001", "")
if err != nil {
return err
}
// this is O(N), which is not great, but this is only called once per
// cluster, and N < 1000. The portal handles this by making a kusto-style
// call to the resource graph service, but it's not worth the effort to do
// that here.
var linkIdentifier string
for _, conn := range *pls.PrivateEndpointConnections {
if !strings.EqualFold(*conn.PrivateEndpoint.ID, *pe.ID) {
continue
}
linkIdentifier = *conn.LinkIdentifier
if !strings.EqualFold(*conn.PrivateLinkServiceConnectionState.Status, "Approved") {
conn.PrivateLinkServiceConnectionState.Status = to.StringPtr("Approved")
conn.PrivateLinkServiceConnectionState.Description = to.StringPtr("Approved")
_, err = m.rpPrivateLinkServices.UpdatePrivateEndpointConnection(ctx, m.env.GatewayResourceGroup(), "gateway-pls-001", *conn.Name, conn)
if err != nil {
return err
}
}
break
}
if linkIdentifier == "" {
return errors.New("private endpoint connection not found")
}
_, err = m.dbGateway.Create(ctx, &api.GatewayDocument{
ID: linkIdentifier,
Gateway: &api.Gateway{
ID: m.doc.OpenShiftCluster.ID,
StorageSuffix: m.doc.OpenShiftCluster.Properties.StorageSuffix,
ImageRegistryStorageAccountName: m.doc.OpenShiftCluster.Properties.ImageRegistryStorageAccountName,
},
})
recordExists := err != nil && cosmosdb.IsErrorStatusCode(err, http.StatusConflict)
if err != nil && !recordExists /* already exists */ {
return err
}
// ensure the record is this clusters if it exists
if recordExists {
gwyDoc, err := m.dbGateway.Get(ctx, linkIdentifier)
if err != nil {
return err
}
if !strings.EqualFold(gwyDoc.Gateway.ID, m.doc.OpenShiftCluster.ID) ||
!strings.EqualFold(gwyDoc.ID, m.doc.OpenShiftCluster.Properties.NetworkProfile.GatewayPrivateLinkID) ||
!strings.EqualFold(gwyDoc.Gateway.ImageRegistryStorageAccountName, m.doc.OpenShiftCluster.Properties.ImageRegistryStorageAccountName) ||
!strings.EqualFold(gwyDoc.Gateway.StorageSuffix, m.doc.OpenShiftCluster.Properties.StorageSuffix) {
return fmt.Errorf("gateway record '%s' already exists for a different cluster '%s'", linkIdentifier, gwyDoc.Gateway.ID)
}
}
m.doc, err = m.db.PatchWithLease(ctx, m.doc.Key, func(doc *api.OpenShiftClusterDocument) error {
doc.OpenShiftCluster.Properties.NetworkProfile.GatewayPrivateEndpointIP = *(*(*pe.PrivateEndpointProperties.NetworkInterfaces)[0].IPConfigurations)[0].PrivateIPAddress
doc.OpenShiftCluster.Properties.NetworkProfile.GatewayPrivateLinkID = linkIdentifier
return nil
})
return err
}
func (m *manager) createAPIServerPrivateEndpoint(ctx context.Context) error {
infraID := m.doc.OpenShiftCluster.Properties.InfraID
if infraID == "" {
infraID = "aro"
}
err := m.fpPrivateEndpoints.CreateOrUpdateAndWait(ctx, m.env.ResourceGroup(), env.RPPrivateEndpointPrefix+m.doc.ID, mgmtnetwork.PrivateEndpoint{
PrivateEndpointProperties: &mgmtnetwork.PrivateEndpointProperties{
Subnet: &mgmtnetwork.Subnet{
// TODO: in the future we will need multiple vnets for our PEs.
// It will be necessary to decide the vnet for a cluster's PE
// somewhere around here.
ID: to.StringPtr("/subscriptions/" + m.env.SubscriptionID() + "/resourceGroups/" + m.env.ResourceGroup() + "/providers/Microsoft.Network/virtualNetworks/rp-pe-vnet-001/subnets/rp-pe-subnet"),
},
ManualPrivateLinkServiceConnections: &[]mgmtnetwork.PrivateLinkServiceConnection{
{
Name: to.StringPtr("rp-plsconnection"),
PrivateLinkServiceConnectionProperties: &mgmtnetwork.PrivateLinkServiceConnectionProperties{
PrivateLinkServiceID: to.StringPtr(m.doc.OpenShiftCluster.Properties.ClusterProfile.ResourceGroupID + "/providers/Microsoft.Network/privateLinkServices/" + infraID + "-pls"),
},
},
},
},
Location: &m.doc.OpenShiftCluster.Location,
})
if err != nil {
return err
}
pe, err := m.fpPrivateEndpoints.Get(ctx, m.env.ResourceGroup(), env.RPPrivateEndpointPrefix+m.doc.ID, "networkInterfaces")
if err != nil {
return err
}
m.doc, err = m.db.PatchWithLease(ctx, m.doc.Key, func(doc *api.OpenShiftClusterDocument) error {
doc.OpenShiftCluster.Properties.NetworkProfile.APIServerPrivateEndpointIP = *(*(*pe.PrivateEndpointProperties.NetworkInterfaces)[0].IPConfigurations)[0].PrivateIPAddress
return nil
})
return err
}