Skip to content

Commit 467f6f0

Browse files
authored
Merge pull request kubernetes#94846 from gongguan/gce-instancev2
implement and enable gce instancev2
2 parents 8d30a5f + 16cc4ef commit 467f6f0

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

staging/src/k8s.io/legacy-cloud-providers/gce/gce.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,9 +660,9 @@ func (g *Cloud) Instances() (cloudprovider.Instances, bool) {
660660
}
661661

662662
// InstancesV2 returns an implementation of InstancesV2 for Google Compute Engine.
663-
// TODO: implement ONLY for external cloud provider
663+
// Implement ONLY for external cloud provider
664664
func (g *Cloud) InstancesV2() (cloudprovider.InstancesV2, bool) {
665-
return nil, false
665+
return g, true
666666
}
667667

668668
// Zones returns an implementation of Zones for Google Compute Engine.

staging/src/k8s.io/legacy-cloud-providers/gce/gce_instances.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ func (g *Cloud) InstanceShutdownByProviderID(ctx context.Context, providerID str
210210
return false, cloudprovider.NotImplemented
211211
}
212212

213+
// InstanceShutdown returns true if the instance is in safe state to detach volumes
214+
func (g *Cloud) InstanceShutdown(ctx context.Context, node *v1.Node) (bool, error) {
215+
return false, cloudprovider.NotImplemented
216+
}
217+
213218
func nodeAddressesFromInstance(instance *compute.Instance) ([]v1.NodeAddress, error) {
214219
if len(instance.NetworkInterfaces) < 1 {
215220
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
253258
return true, nil
254259
}
255260

261+
// InstanceExists returns true if the instance with the given provider id still exists and is running.
262+
// If false is returned with no error, the instance will be immediately deleted by the cloud controller manager.
263+
func (g *Cloud) InstanceExists(ctx context.Context, node *v1.Node) (bool, error) {
264+
providerID := node.Spec.ProviderID
265+
if providerID == "" {
266+
var err error
267+
if providerID, err = g.InstanceID(ctx, types.NodeName(node.Name)); err != nil {
268+
return false, err
269+
}
270+
}
271+
return g.InstanceExistsByProviderID(ctx, providerID)
272+
}
273+
274+
// InstanceMetadata returns metadata of the specified instance.
275+
func (g *Cloud) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloudprovider.InstanceMetadata, error) {
276+
timeoutCtx, cancel := context.WithTimeout(ctx, 1*time.Hour)
277+
defer cancel()
278+
279+
providerID := node.Spec.ProviderID
280+
if providerID == "" {
281+
var err error
282+
if providerID, err = g.InstanceID(ctx, types.NodeName(node.Name)); err != nil {
283+
return nil, err
284+
}
285+
}
286+
287+
_, zone, name, err := splitProviderID(providerID)
288+
if err != nil {
289+
return nil, err
290+
}
291+
292+
region, err := GetGCERegion(zone)
293+
if err != nil {
294+
return nil, err
295+
}
296+
297+
instance, err := g.c.Instances().Get(timeoutCtx, meta.ZonalKey(canonicalizeInstanceName(name), zone))
298+
if err != nil {
299+
return nil, fmt.Errorf("error while querying for providerID %q: %v", providerID, err)
300+
}
301+
302+
addresses, err := nodeAddressesFromInstance(instance)
303+
if err != nil {
304+
return nil, err
305+
}
306+
307+
return &cloudprovider.InstanceMetadata{
308+
ProviderID: providerID,
309+
InstanceType: lastComponent(instance.MachineType),
310+
NodeAddresses: addresses,
311+
Zone: zone,
312+
Region: region,
313+
}, nil
314+
}
315+
256316
// InstanceID returns the cloud provider ID of the node with the specified NodeName.
257317
func (g *Cloud) InstanceID(ctx context.Context, nodeName types.NodeName) (string, error) {
258318
instanceName := mapNodeNameToInstanceName(nodeName)

0 commit comments

Comments
 (0)