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 2021 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
44 changes: 24 additions & 20 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 Down Expand Up @@ -322,12 +321,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 @@ -336,34 +335,34 @@ 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
}

// InstanceServiceAccountsSpec returns service-account spec.
func (m *MachineScope) InstanceServiceAccountsSpec() *compute.ServiceAccount {
func InstanceServiceAccountsSpec(spec *infrav1.ServiceAccount) *compute.ServiceAccount {
serviceAccount := &compute.ServiceAccount{
Email: "default",
Scopes: []string{
compute.CloudPlatformScope,
},
}

if m.GCPMachine.Spec.ServiceAccount != nil {
serviceAccount.Email = m.GCPMachine.Spec.ServiceAccount.Email
serviceAccount.Scopes = m.GCPMachine.Spec.ServiceAccount.Scopes
if spec != nil {
serviceAccount.Email = spec.Email
serviceAccount.Scopes = spec.Scopes
}

return serviceAccount
}

// 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 @@ -464,24 +463,29 @@ 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.ServiceAccounts = append(instance.ServiceAccounts, m.InstanceServiceAccountsSpec())
instance.NetworkInterfaces = append(instance.NetworkInterfaces, m.InstanceNetworkInterfaceSpec())
instance.Metadata = InstanceAdditionalMetadataSpec(m.GCPMachine.Spec.AdditionalMetadata)
instance.ServiceAccounts = append(instance.ServiceAccounts, InstanceServiceAccountsSpec(m.GCPMachine.Spec.ServiceAccount))
instance.NetworkInterfaces = append(instance.NetworkInterfaces, InstanceNetworkInterfaceSpec(m.ClusterGetter, m.GCPMachine.Spec.PublicIP, m.GCPMachine.Spec.Subnet))
return 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