|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package vsphere |
| 15 | + |
| 16 | +// This file implements the InstancesV2 interface. |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + |
| 21 | + v1 "k8s.io/api/core/v1" |
| 22 | + "k8s.io/apimachinery/pkg/types" |
| 23 | + cloudprovider "k8s.io/cloud-provider" |
| 24 | + "k8s.io/klog/v2" |
| 25 | +) |
| 26 | + |
| 27 | +// AdditionalLabels contains additional labels to add to all nodes when generating the metadata information |
| 28 | +var AdditionalLabels map[string]string |
| 29 | + |
| 30 | +func newInstancesV2(instances cloudprovider.Instances, zones cloudprovider.Zones) cloudprovider.InstancesV2 { |
| 31 | + return &instancesV2{ |
| 32 | + instances: instances, |
| 33 | + zones: zones, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func (c *instancesV2) getProviderID(ctx context.Context, node *v1.Node) (string, error) { |
| 38 | + klog.V(4).Infof("instancesV2.getProviderID() called for node %s", node.Name) |
| 39 | + if node.Spec.ProviderID != "" { |
| 40 | + return node.Spec.ProviderID, nil |
| 41 | + } |
| 42 | + |
| 43 | + instanceID, err := c.instances.InstanceID(ctx, types.NodeName(node.Name)) |
| 44 | + if err != nil { |
| 45 | + return "", err |
| 46 | + } |
| 47 | + |
| 48 | + return ProviderName + "://" + instanceID, nil |
| 49 | +} |
| 50 | + |
| 51 | +// InstanceExists returns true if the instance for the given node exists according to the cloud provider. |
| 52 | +// Use the node.name or node.spec.providerID field to find the node in the cloud provider. |
| 53 | +func (c *instancesV2) InstanceExists(ctx context.Context, node *v1.Node) (bool, error) { |
| 54 | + klog.V(4).Infof("instancesV2.InstanceExists() called for node %s", node.Name) |
| 55 | + providerID, err := c.getProviderID(ctx, node) |
| 56 | + if err != nil { |
| 57 | + return false, err |
| 58 | + } |
| 59 | + |
| 60 | + return c.instances.InstanceExistsByProviderID(ctx, providerID) |
| 61 | +} |
| 62 | + |
| 63 | +// InstanceShutdown returns true if the instance is shutdown according to the cloud provider. |
| 64 | +// Use the node.name or node.spec.providerID field to find the node in the cloud provider. |
| 65 | +func (c *instancesV2) InstanceShutdown(ctx context.Context, node *v1.Node) (bool, error) { |
| 66 | + klog.V(4).Infof("instancesV2.InstanceShutdown() called for node %s", node.Name) |
| 67 | + providerID, err := c.getProviderID(ctx, node) |
| 68 | + if err != nil { |
| 69 | + return false, err |
| 70 | + } |
| 71 | + |
| 72 | + return c.instances.InstanceShutdownByProviderID(ctx, providerID) |
| 73 | +} |
| 74 | + |
| 75 | +func (c *instancesV2) getAdditionalLabels() (map[string]string, error) { |
| 76 | + return AdditionalLabels, nil |
| 77 | +} |
| 78 | + |
| 79 | +// InstanceMetadata returns the instance's metadata. The values returned in InstanceMetadata are |
| 80 | +// translated into specific fields and labels in the Node object on registration. |
| 81 | +// Implementations should always check node.spec.providerID first when trying to discover the instance |
| 82 | +// for a given node. In cases where node.spec.providerID is empty, implementations can use other |
| 83 | +// properties of the node like its name, labels and annotations. |
| 84 | +func (c *instancesV2) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloudprovider.InstanceMetadata, error) { |
| 85 | + klog.V(4).Infof("instancesV2.InstanceMetadata() called with node %s", node.Name) |
| 86 | + |
| 87 | + providerID, err := c.getProviderID(ctx, node) |
| 88 | + if err != nil { |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + klog.V(4).Infof("instancesV2.InstanceMetadata() got provider ID %s", providerID) |
| 92 | + |
| 93 | + instanceType, err := c.instances.InstanceTypeByProviderID(ctx, providerID) |
| 94 | + if err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + klog.V(4).Infof("instancesV2.InstanceMetadata() got instanceType %s", instanceType) |
| 98 | + |
| 99 | + zone, err := c.zones.GetZoneByProviderID(ctx, providerID) |
| 100 | + if err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + klog.V(4).InfoS("instancesV2.InstanceMetadata() got zone info", "zone", zone) |
| 104 | + |
| 105 | + nodeAddresses, err := c.instances.NodeAddressesByProviderID(ctx, providerID) |
| 106 | + if err != nil { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + klog.V(4).InfoS("instancesV2.InstanceMetadata() got nodeAddresses", "nodeAddresses", nodeAddresses) |
| 110 | + |
| 111 | + // Generate additionalLabels |
| 112 | + additionalLabels, err := c.getAdditionalLabels() |
| 113 | + if err != nil { |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + klog.V(4).InfoS("instancesV2.InstanceMetadata() got additionalLabels", "additionalLabels", additionalLabels) |
| 117 | + |
| 118 | + return &cloudprovider.InstanceMetadata{ |
| 119 | + ProviderID: providerID, |
| 120 | + InstanceType: instanceType, |
| 121 | + NodeAddresses: nodeAddresses, |
| 122 | + Zone: zone.FailureDomain, |
| 123 | + Region: zone.Region, |
| 124 | + AdditionalLabels: additionalLabels, |
| 125 | + }, nil |
| 126 | +} |
0 commit comments