use GetInstanceProviderID to get instance provider ID

This commit is contained in:
gongguan 2020-09-18 23:16:13 +08:00
parent 467f6f0983
commit 7380a58c19
4 changed files with 77 additions and 2 deletions

View File

@ -97,6 +97,7 @@ go_test(
"gce_annotations_test.go",
"gce_disks_test.go",
"gce_healthchecks_test.go",
"gce_instances_test.go",
"gce_loadbalancer_external_test.go",
"gce_loadbalancer_internal_test.go",
"gce_loadbalancer_metrics_test.go",

View File

@ -264,7 +264,10 @@ 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 {
if providerID, err = cloudprovider.GetInstanceProviderID(ctx, g, types.NodeName(node.Name)); err != nil {
if err == cloudprovider.InstanceNotFound {
return false, nil
}
return false, err
}
}
@ -279,7 +282,7 @@ func (g *Cloud) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloudprov
providerID := node.Spec.ProviderID
if providerID == "" {
var err error
if providerID, err = g.InstanceID(ctx, types.NodeName(node.Name)); err != nil {
if providerID, err = cloudprovider.GetInstanceProviderID(ctx, g, types.NodeName(node.Name)); err != nil {
return nil, err
}
}

View File

@ -0,0 +1,69 @@
// +build !providerless
/*
Copyright 2020 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 gce
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestInstanceExists(t *testing.T) {
gce, err := fakeGCECloud(DefaultTestClusterValues())
require.NoError(t, err)
nodeNames := []string{"test-node-1"}
_, err = createAndInsertNodes(gce, nodeNames, vals.ZoneName)
require.NoError(t, err)
testcases := []struct {
name string
nodeName string
exist bool
expectedErr error
}{
{
name: "node exist",
nodeName: "test-node-1",
exist: true,
expectedErr: nil,
},
{
name: "node not exist",
nodeName: "test-node-2",
exist: false,
expectedErr: fmt.Errorf("failed to get instance ID from cloud provider: instance not found"),
},
}
for _, test := range testcases {
t.Run(test.name, func(t *testing.T) {
node := &v1.Node{ObjectMeta: metav1.ObjectMeta{Name: test.nodeName}}
exist, err := gce.InstanceExists(context.TODO(), node)
assert.Equal(t, test.expectedErr, err, test.name)
assert.Equal(t, test.exist, exist, test.name)
})
}
}

View File

@ -94,6 +94,8 @@ func createAndInsertNodes(gce *Cloud, nodeNames []string, zoneName string) ([]*v
Tags: &compute.Tags{
Items: []string{name},
},
// add Instance.Zone, otherwise InstanceID() won't return a right instanceID.
Zone: zoneName,
},
)
if err != nil {