Skip to content

Commit b03a4ac

Browse files
authored
Merge pull request kubernetes#94894 from gongguan/provider-id
use GetInstanceProviderID to get instance provider ID
2 parents 51ffb49 + 7380a58 commit b03a4ac

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ go_test(
9797
"gce_annotations_test.go",
9898
"gce_disks_test.go",
9999
"gce_healthchecks_test.go",
100+
"gce_instances_test.go",
100101
"gce_loadbalancer_external_test.go",
101102
"gce_loadbalancer_internal_test.go",
102103
"gce_loadbalancer_metrics_test.go",

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,10 @@ func (g *Cloud) InstanceExists(ctx context.Context, node *v1.Node) (bool, error)
264264
providerID := node.Spec.ProviderID
265265
if providerID == "" {
266266
var err error
267-
if providerID, err = g.InstanceID(ctx, types.NodeName(node.Name)); err != nil {
267+
if providerID, err = cloudprovider.GetInstanceProviderID(ctx, g, types.NodeName(node.Name)); err != nil {
268+
if err == cloudprovider.InstanceNotFound {
269+
return false, nil
270+
}
268271
return false, err
269272
}
270273
}
@@ -279,7 +282,7 @@ func (g *Cloud) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloudprov
279282
providerID := node.Spec.ProviderID
280283
if providerID == "" {
281284
var err error
282-
if providerID, err = g.InstanceID(ctx, types.NodeName(node.Name)); err != nil {
285+
if providerID, err = cloudprovider.GetInstanceProviderID(ctx, g, types.NodeName(node.Name)); err != nil {
283286
return nil, err
284287
}
285288
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// +build !providerless
2+
3+
/*
4+
Copyright 2020 The Kubernetes Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package gce
20+
21+
import (
22+
"context"
23+
"fmt"
24+
"testing"
25+
26+
"github.com/stretchr/testify/assert"
27+
"github.com/stretchr/testify/require"
28+
29+
"k8s.io/api/core/v1"
30+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31+
)
32+
33+
func TestInstanceExists(t *testing.T) {
34+
gce, err := fakeGCECloud(DefaultTestClusterValues())
35+
require.NoError(t, err)
36+
37+
nodeNames := []string{"test-node-1"}
38+
_, err = createAndInsertNodes(gce, nodeNames, vals.ZoneName)
39+
require.NoError(t, err)
40+
41+
testcases := []struct {
42+
name string
43+
nodeName string
44+
exist bool
45+
expectedErr error
46+
}{
47+
{
48+
name: "node exist",
49+
nodeName: "test-node-1",
50+
exist: true,
51+
expectedErr: nil,
52+
},
53+
{
54+
name: "node not exist",
55+
nodeName: "test-node-2",
56+
exist: false,
57+
expectedErr: fmt.Errorf("failed to get instance ID from cloud provider: instance not found"),
58+
},
59+
}
60+
61+
for _, test := range testcases {
62+
t.Run(test.name, func(t *testing.T) {
63+
node := &v1.Node{ObjectMeta: metav1.ObjectMeta{Name: test.nodeName}}
64+
exist, err := gce.InstanceExists(context.TODO(), node)
65+
assert.Equal(t, test.expectedErr, err, test.name)
66+
assert.Equal(t, test.exist, exist, test.name)
67+
})
68+
}
69+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ func createAndInsertNodes(gce *Cloud, nodeNames []string, zoneName string) ([]*v
9494
Tags: &compute.Tags{
9595
Items: []string{name},
9696
},
97+
// add Instance.Zone, otherwise InstanceID() won't return a right instanceID.
98+
Zone: zoneName,
9799
},
98100
)
99101
if err != nil {

0 commit comments

Comments
 (0)