Skip to content

Commit 160d9e6

Browse files
committed
Initial spike: GCPMachinePool
1 parent b504f4a commit 160d9e6

21 files changed

+1841
-27
lines changed

api/v1beta1/conditions_consts.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1beta1
18+
19+
const (
20+
// WaitingForClusterInfrastructureReason used when machine is waiting for cluster infrastructure to be ready before proceeding.
21+
WaitingForClusterInfrastructureReason = "WaitingForClusterInfrastructure"
22+
// WaitingForBootstrapDataReason used when machine is waiting for bootstrap data to be ready before proceeding.
23+
WaitingForBootstrapDataReason = "WaitingForBootstrapData"
24+
)

cloud/interfaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ type MachineGetter interface {
8989
ControlPlaneGroupName() string
9090
GetInstanceID() *string
9191
GetProviderID() string
92-
GetBootstrapData() (string, error)
92+
GetBootstrapData(ctx context.Context) (string, error)
9393
GetInstanceStatus() *infrav1.InstanceStatus
9494
}
9595

cloud/scope/machine.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"strings"
2626

2727
"github.com/go-logr/logr"
28-
2928
"github.com/pkg/errors"
3029
"golang.org/x/mod/semver"
3130
"google.golang.org/api/compute/v1"
@@ -322,12 +321,12 @@ func (m *MachineScope) InstanceAdditionalDiskSpec() []*compute.AttachedDisk {
322321
}
323322

324323
// InstanceNetworkInterfaceSpec returns compute network interface spec.
325-
func (m *MachineScope) InstanceNetworkInterfaceSpec() *compute.NetworkInterface {
324+
func InstanceNetworkInterfaceSpec(cluster cloud.ClusterGetter, publicIP *bool, subnet *string) *compute.NetworkInterface {
326325
networkInterface := &compute.NetworkInterface{
327-
Network: path.Join("projects", m.ClusterGetter.NetworkProject(), "global", "networks", m.ClusterGetter.NetworkName()),
326+
Network: path.Join("projects", cluster.NetworkProject(), "global", "networks", cluster.NetworkName()),
328327
}
329328

330-
if m.GCPMachine.Spec.PublicIP != nil && *m.GCPMachine.Spec.PublicIP {
329+
if publicIP != nil && *publicIP {
331330
networkInterface.AccessConfigs = []*compute.AccessConfig{
332331
{
333332
Type: "ONE_TO_ONE_NAT",
@@ -336,34 +335,34 @@ func (m *MachineScope) InstanceNetworkInterfaceSpec() *compute.NetworkInterface
336335
}
337336
}
338337

339-
if m.GCPMachine.Spec.Subnet != nil {
340-
networkInterface.Subnetwork = path.Join("projects", m.ClusterGetter.NetworkProject(), "regions", m.ClusterGetter.Region(), "subnetworks", *m.GCPMachine.Spec.Subnet)
338+
if subnet != nil {
339+
networkInterface.Subnetwork = path.Join("projects", cluster.NetworkProject(), "regions", cluster.Region(), "subnetworks", *subnet)
341340
}
342341

343342
return networkInterface
344343
}
345344

346345
// InstanceServiceAccountsSpec returns service-account spec.
347-
func (m *MachineScope) InstanceServiceAccountsSpec() *compute.ServiceAccount {
346+
func InstanceServiceAccountsSpec(spec *infrav1.ServiceAccount) *compute.ServiceAccount {
348347
serviceAccount := &compute.ServiceAccount{
349348
Email: "default",
350349
Scopes: []string{
351350
compute.CloudPlatformScope,
352351
},
353352
}
354353

355-
if m.GCPMachine.Spec.ServiceAccount != nil {
356-
serviceAccount.Email = m.GCPMachine.Spec.ServiceAccount.Email
357-
serviceAccount.Scopes = m.GCPMachine.Spec.ServiceAccount.Scopes
354+
if spec != nil {
355+
serviceAccount.Email = spec.Email
356+
serviceAccount.Scopes = spec.Scopes
358357
}
359358

360359
return serviceAccount
361360
}
362361

363362
// InstanceAdditionalMetadataSpec returns additional metadata spec.
364-
func (m *MachineScope) InstanceAdditionalMetadataSpec() *compute.Metadata {
363+
func InstanceAdditionalMetadataSpec(spec []infrav1.MetadataItem) *compute.Metadata {
365364
metadata := new(compute.Metadata)
366-
for _, additionalMetadata := range m.GCPMachine.Spec.AdditionalMetadata {
365+
for _, additionalMetadata := range spec {
367366
metadata.Items = append(metadata.Items, &compute.MetadataItems{
368367
Key: additionalMetadata.Key,
369368
Value: additionalMetadata.Value,
@@ -462,24 +461,28 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
462461

463462
instance.Disks = append(instance.Disks, m.InstanceImageSpec())
464463
instance.Disks = append(instance.Disks, m.InstanceAdditionalDiskSpec()...)
465-
instance.Metadata = m.InstanceAdditionalMetadataSpec()
466-
instance.ServiceAccounts = append(instance.ServiceAccounts, m.InstanceServiceAccountsSpec())
467-
instance.NetworkInterfaces = append(instance.NetworkInterfaces, m.InstanceNetworkInterfaceSpec())
464+
instance.Metadata = InstanceAdditionalMetadataSpec(m.GCPMachine.Spec.AdditionalMetadata)
465+
instance.ServiceAccounts = append(instance.ServiceAccounts, InstanceServiceAccountsSpec(m.GCPMachine.Spec.ServiceAccount))
466+
instance.NetworkInterfaces = append(instance.NetworkInterfaces, InstanceNetworkInterfaceSpec(m.ClusterGetter, m.GCPMachine.Spec.PublicIP, m.GCPMachine.Spec.Subnet))
468467
return instance
469468
}
470469

471470
// ANCHOR_END: MachineInstanceSpec
472471

472+
func (m *MachineScope) GetBootstrapData(ctx context.Context) (string, error) {
473+
return GetBootstrapData(ctx, m.client, m.Machine, m.Machine.Spec.Bootstrap)
474+
}
475+
473476
// GetBootstrapData returns the bootstrap data from the secret in the Machine's bootstrap.dataSecretName.
474-
func (m *MachineScope) GetBootstrapData() (string, error) {
475-
if m.Machine.Spec.Bootstrap.DataSecretName == nil {
477+
func GetBootstrapData(ctx context.Context, client client.Client, parent client.Object, bootstrap clusterv1.Bootstrap) (string, error) {
478+
if bootstrap.DataSecretName == nil {
476479
return "", errors.New("error retrieving bootstrap data: linked Machine's bootstrap.dataSecretName is nil")
477480
}
478481

479482
secret := &corev1.Secret{}
480-
key := types.NamespacedName{Namespace: m.Namespace(), Name: *m.Machine.Spec.Bootstrap.DataSecretName}
481-
if err := m.client.Get(context.TODO(), key, secret); err != nil {
482-
return "", errors.Wrapf(err, "failed to retrieve bootstrap data secret for GCPMachine %s/%s", m.Namespace(), m.Name())
483+
key := types.NamespacedName{Namespace: parent.GetNamespace(), Name: *bootstrap.DataSecretName}
484+
if err := client.Get(ctx, key, secret); err != nil {
485+
return "", errors.Wrapf(err, "failed to retrieve bootstrap data secret %s/%s", key.Namespace, key.Name)
483486
}
484487

485488
value, ok := secret.Data["value"]

0 commit comments

Comments
 (0)