mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 04:06:03 +00:00
Merge pull request #94846 from gongguan/gce-instancev2
implement and enable gce instancev2
This commit is contained in:
commit
467f6f0983
@ -660,9 +660,9 @@ func (g *Cloud) Instances() (cloudprovider.Instances, bool) {
|
||||
}
|
||||
|
||||
// InstancesV2 returns an implementation of InstancesV2 for Google Compute Engine.
|
||||
// TODO: implement ONLY for external cloud provider
|
||||
// Implement ONLY for external cloud provider
|
||||
func (g *Cloud) InstancesV2() (cloudprovider.InstancesV2, bool) {
|
||||
return nil, false
|
||||
return g, true
|
||||
}
|
||||
|
||||
// Zones returns an implementation of Zones for Google Compute Engine.
|
||||
|
@ -210,6 +210,11 @@ func (g *Cloud) InstanceShutdownByProviderID(ctx context.Context, providerID str
|
||||
return false, cloudprovider.NotImplemented
|
||||
}
|
||||
|
||||
// InstanceShutdown returns true if the instance is in safe state to detach volumes
|
||||
func (g *Cloud) InstanceShutdown(ctx context.Context, node *v1.Node) (bool, error) {
|
||||
return false, cloudprovider.NotImplemented
|
||||
}
|
||||
|
||||
func nodeAddressesFromInstance(instance *compute.Instance) ([]v1.NodeAddress, error) {
|
||||
if len(instance.NetworkInterfaces) < 1 {
|
||||
return nil, fmt.Errorf("could not find network interfaces for instanceID %q", instance.Id)
|
||||
@ -253,6 +258,61 @@ func (g *Cloud) InstanceExistsByProviderID(ctx context.Context, providerID strin
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// InstanceExists returns true if the instance with the given provider id still exists and is running.
|
||||
// If false is returned with no error, the instance will be immediately deleted by the cloud controller manager.
|
||||
func (g *Cloud) InstanceExists(ctx context.Context, node *v1.Node) (bool, error) {
|
||||
providerID := node.Spec.ProviderID
|
||||
if providerID == "" {
|
||||
var err error
|
||||
if providerID, err = g.InstanceID(ctx, types.NodeName(node.Name)); err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
return g.InstanceExistsByProviderID(ctx, providerID)
|
||||
}
|
||||
|
||||
// InstanceMetadata returns metadata of the specified instance.
|
||||
func (g *Cloud) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloudprovider.InstanceMetadata, error) {
|
||||
timeoutCtx, cancel := context.WithTimeout(ctx, 1*time.Hour)
|
||||
defer cancel()
|
||||
|
||||
providerID := node.Spec.ProviderID
|
||||
if providerID == "" {
|
||||
var err error
|
||||
if providerID, err = g.InstanceID(ctx, types.NodeName(node.Name)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
_, zone, name, err := splitProviderID(providerID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
region, err := GetGCERegion(zone)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
instance, err := g.c.Instances().Get(timeoutCtx, meta.ZonalKey(canonicalizeInstanceName(name), zone))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error while querying for providerID %q: %v", providerID, err)
|
||||
}
|
||||
|
||||
addresses, err := nodeAddressesFromInstance(instance)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &cloudprovider.InstanceMetadata{
|
||||
ProviderID: providerID,
|
||||
InstanceType: lastComponent(instance.MachineType),
|
||||
NodeAddresses: addresses,
|
||||
Zone: zone,
|
||||
Region: region,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// InstanceID returns the cloud provider ID of the node with the specified NodeName.
|
||||
func (g *Cloud) InstanceID(ctx context.Context, nodeName types.NodeName) (string, error) {
|
||||
instanceName := mapNodeNameToInstanceName(nodeName)
|
||||
|
Loading…
Reference in New Issue
Block a user