Skip to content

Commit cf3f2a8

Browse files
committed
Initial spike: GCPMachinePool
1 parent 221292a commit cf3f2a8

File tree

21 files changed

+1851
-23
lines changed

21 files changed

+1851
-23
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 2025 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: 30 additions & 18 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"
@@ -41,6 +40,13 @@ import (
4140
"sigs.k8s.io/controller-runtime/pkg/client"
4241
)
4342

43+
// Constants for GCP OnHostMaintenance values.
44+
// These are not exported, because they are not _our_ API, but they are used in multiple places.
45+
const (
46+
onHostMaintenanceTerminate = "TERMINATE"
47+
onHostMaintenanceMigrate = "MIGRATE"
48+
)
49+
4450
// MachineScopeParams defines the input parameters used to create a new MachineScope.
4551
type MachineScopeParams struct {
4652
Client client.Client
@@ -327,12 +333,12 @@ func instanceAdditionalDiskSpec(ctx context.Context, spec []infrav1.AttachedDisk
327333
}
328334

329335
// InstanceNetworkInterfaceSpec returns compute network interface spec.
330-
func (m *MachineScope) InstanceNetworkInterfaceSpec() *compute.NetworkInterface {
336+
func InstanceNetworkInterfaceSpec(cluster cloud.ClusterGetter, publicIP *bool, subnet *string) *compute.NetworkInterface {
331337
networkInterface := &compute.NetworkInterface{
332-
Network: path.Join("projects", m.ClusterGetter.NetworkProject(), "global", "networks", m.ClusterGetter.NetworkName()),
338+
Network: path.Join("projects", cluster.NetworkProject(), "global", "networks", cluster.NetworkName()),
333339
}
334340

335-
if m.GCPMachine.Spec.PublicIP != nil && *m.GCPMachine.Spec.PublicIP {
341+
if publicIP != nil && *publicIP {
336342
networkInterface.AccessConfigs = []*compute.AccessConfig{
337343
{
338344
Type: "ONE_TO_ONE_NAT",
@@ -341,8 +347,8 @@ func (m *MachineScope) InstanceNetworkInterfaceSpec() *compute.NetworkInterface
341347
}
342348
}
343349

344-
if m.GCPMachine.Spec.Subnet != nil {
345-
networkInterface.Subnetwork = path.Join("projects", m.ClusterGetter.NetworkProject(), "regions", m.ClusterGetter.Region(), "subnetworks", *m.GCPMachine.Spec.Subnet)
350+
if subnet != nil {
351+
networkInterface.Subnetwork = path.Join("projects", cluster.NetworkProject(), "regions", cluster.Region(), "subnetworks", *subnet)
346352
}
347353

348354
return networkInterface
@@ -366,9 +372,9 @@ func instanceServiceAccountsSpec(serviceAccount *infrav1.ServiceAccount) *comput
366372
}
367373

368374
// InstanceAdditionalMetadataSpec returns additional metadata spec.
369-
func (m *MachineScope) InstanceAdditionalMetadataSpec() *compute.Metadata {
375+
func InstanceAdditionalMetadataSpec(spec []infrav1.MetadataItem) *compute.Metadata {
370376
metadata := new(compute.Metadata)
371-
for _, additionalMetadata := range m.GCPMachine.Spec.AdditionalMetadata {
377+
for _, additionalMetadata := range spec {
372378
metadata.Items = append(metadata.Items, &compute.MetadataItems{
373379
Key: additionalMetadata.Key,
374380
Value: additionalMetadata.Value,
@@ -458,9 +464,9 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
458464
if m.GCPMachine.Spec.OnHostMaintenance != nil {
459465
switch *m.GCPMachine.Spec.OnHostMaintenance {
460466
case infrav1.HostMaintenancePolicyMigrate:
461-
instance.Scheduling.OnHostMaintenance = "MIGRATE"
467+
instance.Scheduling.OnHostMaintenance = onHostMaintenanceMigrate
462468
case infrav1.HostMaintenancePolicyTerminate:
463-
instance.Scheduling.OnHostMaintenance = "TERMINATE"
469+
instance.Scheduling.OnHostMaintenance = onHostMaintenanceTerminate
464470
default:
465471
log.Error(errors.New("Invalid value"), "Unknown OnHostMaintenance value", "Spec.OnHostMaintenance", *m.GCPMachine.Spec.OnHostMaintenance)
466472
}
@@ -485,12 +491,13 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
485491

486492
instance.Disks = append(instance.Disks, m.InstanceImageSpec())
487493
instance.Disks = append(instance.Disks, instanceAdditionalDiskSpec(ctx, m.GCPMachine.Spec.AdditionalDisks, m.GCPMachine.Spec.RootDiskEncryptionKey, m.Zone(), m.ResourceManagerTags())...)
488-
instance.Metadata = m.InstanceAdditionalMetadataSpec()
494+
495+
instance.Metadata = InstanceAdditionalMetadataSpec(m.GCPMachine.Spec.AdditionalMetadata)
489496
instance.ServiceAccounts = append(instance.ServiceAccounts, instanceServiceAccountsSpec(m.GCPMachine.Spec.ServiceAccount))
490-
instance.NetworkInterfaces = append(instance.NetworkInterfaces, m.InstanceNetworkInterfaceSpec())
497+
instance.NetworkInterfaces = append(instance.NetworkInterfaces, InstanceNetworkInterfaceSpec(m.ClusterGetter, m.GCPMachine.Spec.PublicIP, m.GCPMachine.Spec.Subnet))
491498
instance.GuestAccelerators = instanceGuestAcceleratorsSpec(m.GCPMachine.Spec.GuestAccelerators)
492499
if len(instance.GuestAccelerators) > 0 {
493-
instance.Scheduling.OnHostMaintenance = "TERMINATE"
500+
instance.Scheduling.OnHostMaintenance = onHostMaintenanceTerminate
494501
}
495502

496503
return instance
@@ -499,15 +506,20 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
499506
// ANCHOR_END: MachineInstanceSpec
500507

501508
// GetBootstrapData returns the bootstrap data from the secret in the Machine's bootstrap.dataSecretName.
502-
func (m *MachineScope) GetBootstrapData() (string, error) {
503-
if m.Machine.Spec.Bootstrap.DataSecretName == nil {
509+
func (m *MachineScope) GetBootstrapData(ctx context.Context) (string, error) {
510+
return GetBootstrapData(ctx, m.client, m.Machine, m.Machine.Spec.Bootstrap)
511+
}
512+
513+
// GetBootstrapData returns the bootstrap data from the secret in the Machine's bootstrap.dataSecretName.
514+
func GetBootstrapData(ctx context.Context, client client.Client, parent client.Object, bootstrap clusterv1.Bootstrap) (string, error) {
515+
if bootstrap.DataSecretName == nil {
504516
return "", errors.New("error retrieving bootstrap data: linked Machine's bootstrap.dataSecretName is nil")
505517
}
506518

507519
secret := &corev1.Secret{}
508-
key := types.NamespacedName{Namespace: m.Namespace(), Name: *m.Machine.Spec.Bootstrap.DataSecretName}
509-
if err := m.client.Get(context.TODO(), key, secret); err != nil {
510-
return "", errors.Wrapf(err, "failed to retrieve bootstrap data secret for GCPMachine %s/%s", m.Namespace(), m.Name())
520+
key := types.NamespacedName{Namespace: parent.GetNamespace(), Name: *bootstrap.DataSecretName}
521+
if err := client.Get(ctx, key, secret); err != nil {
522+
return "", errors.Wrapf(err, "failed to retrieve bootstrap data secret %s/%s", key.Namespace, key.Name)
511523
}
512524

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

0 commit comments

Comments
 (0)