Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions api/v1beta1/conditions_consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Copyright 2025 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1

const (
// WaitingForClusterInfrastructureReason used when machine is waiting for cluster infrastructure to be ready before proceeding.
WaitingForClusterInfrastructureReason = "WaitingForClusterInfrastructure"
// WaitingForBootstrapDataReason used when machine is waiting for bootstrap data to be ready before proceeding.
WaitingForBootstrapDataReason = "WaitingForBootstrapData"
)
2 changes: 1 addition & 1 deletion cloud/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type MachineGetter interface {
ControlPlaneGroupName() string
GetInstanceID() *string
GetProviderID() string
GetBootstrapData() (string, error)
GetBootstrapData(ctx context.Context) (string, error)
GetInstanceStatus() *infrav1.InstanceStatus
}

Expand Down
48 changes: 30 additions & 18 deletions cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"strings"

"github.com/go-logr/logr"

"github.com/pkg/errors"
"golang.org/x/mod/semver"
"google.golang.org/api/compute/v1"
Expand All @@ -41,6 +40,13 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

// Constants for GCP OnHostMaintenance values.
// These are not exported, because they are not _our_ API, but they are used in multiple places.
const (
onHostMaintenanceTerminate = "TERMINATE"
onHostMaintenanceMigrate = "MIGRATE"
)

// MachineScopeParams defines the input parameters used to create a new MachineScope.
type MachineScopeParams struct {
Client client.Client
Expand Down Expand Up @@ -327,12 +333,12 @@ func instanceAdditionalDiskSpec(ctx context.Context, spec []infrav1.AttachedDisk
}

// InstanceNetworkInterfaceSpec returns compute network interface spec.
func (m *MachineScope) InstanceNetworkInterfaceSpec() *compute.NetworkInterface {
func InstanceNetworkInterfaceSpec(cluster cloud.ClusterGetter, publicIP *bool, subnet *string) *compute.NetworkInterface {
networkInterface := &compute.NetworkInterface{
Network: path.Join("projects", m.ClusterGetter.NetworkProject(), "global", "networks", m.ClusterGetter.NetworkName()),
Network: path.Join("projects", cluster.NetworkProject(), "global", "networks", cluster.NetworkName()),
}

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

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

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

// InstanceAdditionalMetadataSpec returns additional metadata spec.
func (m *MachineScope) InstanceAdditionalMetadataSpec() *compute.Metadata {
func InstanceAdditionalMetadataSpec(spec []infrav1.MetadataItem) *compute.Metadata {
metadata := new(compute.Metadata)
for _, additionalMetadata := range m.GCPMachine.Spec.AdditionalMetadata {
for _, additionalMetadata := range spec {
metadata.Items = append(metadata.Items, &compute.MetadataItems{
Key: additionalMetadata.Key,
Value: additionalMetadata.Value,
Expand Down Expand Up @@ -458,9 +464,9 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
if m.GCPMachine.Spec.OnHostMaintenance != nil {
switch *m.GCPMachine.Spec.OnHostMaintenance {
case infrav1.HostMaintenancePolicyMigrate:
instance.Scheduling.OnHostMaintenance = "MIGRATE"
instance.Scheduling.OnHostMaintenance = onHostMaintenanceMigrate
case infrav1.HostMaintenancePolicyTerminate:
instance.Scheduling.OnHostMaintenance = "TERMINATE"
instance.Scheduling.OnHostMaintenance = onHostMaintenanceTerminate
default:
log.Error(errors.New("Invalid value"), "Unknown OnHostMaintenance value", "Spec.OnHostMaintenance", *m.GCPMachine.Spec.OnHostMaintenance)
}
Expand All @@ -485,12 +491,13 @@ func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {

instance.Disks = append(instance.Disks, m.InstanceImageSpec())
instance.Disks = append(instance.Disks, instanceAdditionalDiskSpec(ctx, m.GCPMachine.Spec.AdditionalDisks, m.GCPMachine.Spec.RootDiskEncryptionKey, m.Zone(), m.ResourceManagerTags())...)
instance.Metadata = m.InstanceAdditionalMetadataSpec()

instance.Metadata = InstanceAdditionalMetadataSpec(m.GCPMachine.Spec.AdditionalMetadata)
instance.ServiceAccounts = append(instance.ServiceAccounts, instanceServiceAccountsSpec(m.GCPMachine.Spec.ServiceAccount))
instance.NetworkInterfaces = append(instance.NetworkInterfaces, m.InstanceNetworkInterfaceSpec())
instance.NetworkInterfaces = append(instance.NetworkInterfaces, InstanceNetworkInterfaceSpec(m.ClusterGetter, m.GCPMachine.Spec.PublicIP, m.GCPMachine.Spec.Subnet))
instance.GuestAccelerators = instanceGuestAcceleratorsSpec(m.GCPMachine.Spec.GuestAccelerators)
if len(instance.GuestAccelerators) > 0 {
instance.Scheduling.OnHostMaintenance = "TERMINATE"
instance.Scheduling.OnHostMaintenance = onHostMaintenanceTerminate
}

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

// GetBootstrapData returns the bootstrap data from the secret in the Machine's bootstrap.dataSecretName.
func (m *MachineScope) GetBootstrapData() (string, error) {
if m.Machine.Spec.Bootstrap.DataSecretName == nil {
func (m *MachineScope) GetBootstrapData(ctx context.Context) (string, error) {
return GetBootstrapData(ctx, m.client, m.Machine, m.Machine.Spec.Bootstrap)
}

// GetBootstrapData returns the bootstrap data from the secret in the Machine's bootstrap.dataSecretName.
func GetBootstrapData(ctx context.Context, client client.Client, parent client.Object, bootstrap clusterv1.Bootstrap) (string, error) {
if bootstrap.DataSecretName == nil {
return "", errors.New("error retrieving bootstrap data: linked Machine's bootstrap.dataSecretName is nil")
}

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

value, ok := secret.Data["value"]
Expand Down
Loading