-
Notifications
You must be signed in to change notification settings - Fork 75
/
actuator_reconcile.go
347 lines (284 loc) · 10.7 KB
/
actuator_reconcile.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package bastion
import (
"context"
"fmt"
"reflect"
"strings"
"time"
"github.com/gardener/gardener/extensions/pkg/controller"
"github.com/gardener/gardener/extensions/pkg/util"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
reconcilerutils "github.com/gardener/gardener/pkg/controllerutils/reconciler"
"github.com/go-logr/logr"
"google.golang.org/api/compute/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/gardener/gardener-extension-provider-gcp/pkg/apis/gcp/helper"
gcpclient "github.com/gardener/gardener-extension-provider-gcp/pkg/internal/client"
)
const (
osImage = "debian-cloud"
)
// bastionEndpoints collects the endpoints the bastion host provides; the
// private endpoint is important for opening a port on the worker node
// ingress firewall rule to allow SSH from that node, the public endpoint is where
// the end user connects to establish the SSH connection.
type bastionEndpoints struct {
private *corev1.LoadBalancerIngress
public *corev1.LoadBalancerIngress
}
// Ready returns true if both public and private interfaces each have either
// an IP or a hostname or both.
func (be *bastionEndpoints) Ready() bool {
return be != nil && IngressReady(be.private) && IngressReady(be.public)
}
func (a *actuator) Reconcile(ctx context.Context, log logr.Logger, bastion *extensionsv1alpha1.Bastion, cluster *controller.Cluster) error {
serviceAccount, err := getServiceAccount(ctx, a.client, bastion)
if err != nil {
return fmt.Errorf("failed to get service account: %w", err)
}
gcpClient, err := createGCPClient(ctx, serviceAccount)
if err != nil {
return util.DetermineError(fmt.Errorf("failed to create GCP client: %w", err), helper.KnownCodes)
}
infrastructureStatus, subnet, err := getInfrastructureStatus(ctx, a.client, cluster)
if err != nil {
return err
}
opt, err := DetermineOptions(bastion, cluster, serviceAccount.ProjectID, infrastructureStatus.Networks.VPC.Name, subnet)
if err != nil {
return fmt.Errorf("failed to determine Options: %w", err)
}
if opt.Zone == "" {
opt.Zone, err = getDefaultGCPZone(ctx, gcpClient, opt, cluster.Shoot.Spec.Region)
if err != nil {
return util.DetermineError(err, helper.KnownCodes)
}
}
bytes, err := marshalProviderStatus(opt.Zone)
if err != nil {
return err
}
patch := client.MergeFrom(bastion.DeepCopy())
bastion.Status.ProviderStatus = &runtime.RawExtension{Raw: bytes}
if err := a.client.Status().Patch(ctx, bastion, patch); err != nil {
return fmt.Errorf("failed to store status.providerStatus for zone: %s", opt.Zone)
}
err = ensureFirewallRules(ctx, log, gcpClient, bastion, opt)
if err != nil {
return util.DetermineError(fmt.Errorf("failed to ensure firewall rule: %w", err), helper.KnownCodes)
}
err = ensureDisk(ctx, log, gcpClient, opt)
if err != nil {
return util.DetermineError(err, helper.KnownCodes)
}
instance, err := ensureComputeInstance(ctx, log, bastion, gcpClient, opt)
if err != nil {
return util.DetermineError(err, helper.KnownCodes)
}
// check if the instance already exists and has an IP
endpoints, err := getInstanceEndpoints(instance)
if err != nil {
return err
}
if !endpoints.Ready() {
return &reconcilerutils.RequeueAfterError{
// requeue rather soon, so that the user (most likely gardenctl eventually)
// doesn't have to wait too long for the public endpoint to become available
RequeueAfter: 5 * time.Second,
Cause: fmt.Errorf("bastion instance has no public/private endpoints yet"),
}
}
// once a public endpoint is available, publish the endpoint on the
// Bastion resource to notify upstream about the ready instance
patch = client.MergeFrom(bastion.DeepCopy())
bastion.Status.Ingress = endpoints.public
return a.client.Status().Patch(ctx, bastion, patch)
}
func ensureFirewallRules(ctx context.Context, log logr.Logger, gcpclient gcpclient.Interface, bastion *extensionsv1alpha1.Bastion, opt *Options) error {
cidrs, err := ingressPermissions(bastion)
if err != nil {
return err
}
firewallList := []*compute.Firewall{IngressAllowSSH(opt, cidrs), EgressDenyAll(opt), EgressAllowOnly(opt)}
for _, item := range firewallList {
if err := createFirewallRuleIfNotExist(ctx, log, gcpclient, opt, item); err != nil {
return err
}
}
firewall, err := getFirewallRule(ctx, gcpclient, opt, IngressAllowSSH(opt, cidrs).Name)
if err != nil || firewall == nil {
return fmt.Errorf("could not get firewall rule: %w", err)
}
currentCIDRs := firewall.SourceRanges
wantedCIDRs := cidrs
if !reflect.DeepEqual(currentCIDRs, wantedCIDRs) {
return patchFirewallRule(ctx, gcpclient, opt, IngressAllowSSH(opt, cidrs).Name, cidrs)
}
return nil
}
func ensureComputeInstance(ctx context.Context, logger logr.Logger, bastion *extensionsv1alpha1.Bastion, gcpclient gcpclient.Interface, opt *Options) (*compute.Instance, error) {
instance, err := getBastionInstance(ctx, gcpclient, opt)
if instance != nil || err != nil {
return instance, err
}
logger.Info("Creating new bastion compute instance")
computeInstance := computeInstanceDefine(opt, bastion.Spec.UserData)
_, err = gcpclient.Instances().Insert(opt.ProjectID, opt.Zone, computeInstance).Context(ctx).Do()
if err != nil {
return nil, fmt.Errorf("failed to create bastion compute instance: %w", err)
}
instance, err = getBastionInstance(ctx, gcpclient, opt)
if instance != nil || err != nil {
return instance, err
}
return nil, fmt.Errorf("failed to get (create) bastion compute instance: %w", err)
}
func getInstanceEndpoints(instance *compute.Instance) (*bastionEndpoints, error) {
if instance == nil {
return nil, fmt.Errorf("compute instance can't be nil")
}
if instance.Status != "RUNNING" {
return nil, fmt.Errorf("instance not running, status: %s", instance.Status)
}
endpoints := &bastionEndpoints{}
networkInterfaces := instance.NetworkInterfaces
if len(networkInterfaces) == 0 {
return nil, fmt.Errorf("no network interfaces found: %s", instance.Name)
}
internalIP := &networkInterfaces[0].NetworkIP
if len(networkInterfaces[0].AccessConfigs) == 0 {
return nil, fmt.Errorf("no access config found for network interface: %s", instance.Name)
}
externalIP := &networkInterfaces[0].AccessConfigs[0].NatIP
if ingress := addressToIngress(&instance.Name, internalIP); ingress != nil {
endpoints.private = ingress
}
// GCP does not automatically assign a public dns name to the instance (in contrast to e.g. AWS).
// As we provide an externalIP to connect to the bastion, having a public dns name would just be an alternative way to connect to the bastion.
// Out of this reason, we spare the effort to create a PTR record (see https://cloud.google.com/compute/docs/instances/create-ptr-record#api) just for the sake of having it.
if ingress := addressToIngress(nil, externalIP); ingress != nil {
endpoints.public = ingress
}
return endpoints, nil
}
// IngressReady returns true if either an IP or a hostname or both are set.
func IngressReady(ingress *corev1.LoadBalancerIngress) bool {
return ingress != nil && (ingress.Hostname != "" || ingress.IP != "")
}
// addressToIngress converts the IP address into a
// corev1.LoadBalancerIngress resource. If both arguments are nil, then
// nil is returned.
func addressToIngress(dnsName *string, ipAddress *string) *corev1.LoadBalancerIngress {
var ingress *corev1.LoadBalancerIngress
if ipAddress != nil || dnsName != nil {
ingress = &corev1.LoadBalancerIngress{}
if dnsName != nil {
ingress.Hostname = *dnsName
}
if ipAddress != nil {
ingress.IP = *ipAddress
}
}
return ingress
}
func ensureDisk(ctx context.Context, log logr.Logger, gcpclient gcpclient.Interface, opt *Options) error {
disk, err := getDisk(ctx, gcpclient, opt)
if disk != nil || err != nil {
return err
}
log.Info("create new bastion compute instance disk")
osFamily, err := getOSImage(gcpclient)
if err != nil {
return err
}
disk = diskDefine(opt.Zone, opt.DiskName, osFamily)
_, err = gcpclient.Disks().Insert(opt.ProjectID, opt.Zone, disk).Context(ctx).Do()
if err != nil {
return fmt.Errorf("failed to create compute instance disk: %w", err)
}
disk, err = getDisk(ctx, gcpclient, opt)
if disk != nil || err != nil {
return err
}
return fmt.Errorf("failed to get (create) compute instance disk: %w", err)
}
func computeInstanceDefine(opt *Options, userData []byte) *compute.Instance {
return &compute.Instance{
Disks: disksDefine(opt),
DeletionProtection: false,
Description: "Bastion Instance",
Name: opt.BastionInstanceName,
Zone: opt.Zone,
MachineType: machineTypeDefine(opt),
NetworkInterfaces: networkInterfacesDefine(opt),
Tags: &compute.Tags{Items: []string{opt.BastionInstanceName}},
Metadata: &compute.Metadata{Items: metadataItemsDefine(userData)},
}
}
func metadataItemsDefine(userData []byte) []*compute.MetadataItems {
return []*compute.MetadataItems{
{
Key: "startup-script",
Value: ptr.To(string(userData)),
},
{
Key: "block-project-ssh-keys",
Value: ptr.To("TRUE"),
},
}
}
func machineTypeDefine(opt *Options) string {
return fmt.Sprintf("zones/%s/machineTypes/n1-standard-1", opt.Zone)
}
func networkInterfacesDefine(opt *Options) []*compute.NetworkInterface {
return []*compute.NetworkInterface{
{
Network: opt.Network,
Subnetwork: opt.Subnetwork,
AccessConfigs: []*compute.AccessConfig{{Name: "External NAT", Type: "ONE_TO_ONE_NAT"}},
},
}
}
func disksDefine(opt *Options) []*compute.AttachedDisk {
return []*compute.AttachedDisk{
{
AutoDelete: true,
Boot: true,
DiskSizeGb: 10,
Source: fmt.Sprintf("projects/%s/zones/%s/disks/%s", opt.ProjectID, opt.Zone, opt.DiskName),
Mode: "READ_WRITE",
},
}
}
func diskDefine(zone string, diskName, osFamily string) *compute.Disk {
return &compute.Disk{
Description: "Gardenctl Bastion disk",
Name: diskName,
SizeGb: 10,
SourceImage: fmt.Sprintf("projects/%s/global/images/family/%s", osImage, osFamily),
Zone: zone,
}
}
func getOSImage(gcpClient gcpclient.Interface) (string, error) {
resp, err := gcpClient.Images().List(osImage).OrderBy("creationTimestamp desc").Fields("items(name,creationTimestamp,family)").Do()
if err != nil {
return "", err
}
if resp == nil || len(resp.Items) == 0 {
return "", fmt.Errorf("no available os image find")
}
// looking for fist os x86 arch version sorted by creationTimestamp
for _, k := range resp.Items {
if strings.Contains(k.Family, "-arm64") {
continue
}
return k.Family, nil
}
return "", nil
}