mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 02:41:25 +00:00
Merge pull request #94894 from gongguan/provider-id
use GetInstanceProviderID to get instance provider ID
This commit is contained in:
commit
b03a4ac626
@ -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",
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
})
|
||||
}
|
||||
}
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user