Skip to content

Commit

Permalink
Merge pull request kubernetes#95499 from nicolehanjing/nicoleh-fix-fake
Browse files Browse the repository at this point in the history
cloud provider: fix the fake cloud provider
  • Loading branch information
k8s-ci-robot authored Oct 20, 2020
2 parents 6b7e807 + acee077 commit 4b59044
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,9 @@ func Test_syncNode(t *testing.T) {
ExtID: map[types.NodeName]string{
types.NodeName("node0"): "12345",
},
ProviderID: map[types.NodeName]string{
types.NodeName("node0"): "fake://12345",
},
Addresses: []v1.NodeAddress{
{
Type: v1.NodeHostName,
Expand Down Expand Up @@ -901,6 +904,9 @@ func Test_syncNode(t *testing.T) {
"topology.kubernetes.io/zone": "us-west-1a",
},
},
Spec: v1.NodeSpec{
ProviderID: "fake://12345",
},
Status: v1.NodeStatus{
Conditions: []v1.NodeCondition{
{
Expand Down Expand Up @@ -935,6 +941,9 @@ func Test_syncNode(t *testing.T) {
ExtID: map[types.NodeName]string{
types.NodeName("node0"): "12345",
},
ProviderID: map[types.NodeName]string{
types.NodeName("node0"): "fake://12345",
},
Addresses: []v1.NodeAddress{
{
Type: v1.NodeHostName,
Expand Down Expand Up @@ -996,6 +1005,7 @@ func Test_syncNode(t *testing.T) {
Effect: v1.TaintEffectNoSchedule,
},
},
ProviderID: "fake://12345",
},
Status: v1.NodeStatus{
Addresses: []v1.NodeAddress{
Expand Down Expand Up @@ -1276,6 +1286,69 @@ func Test_syncNode(t *testing.T) {
},
},
},
{
name: "[instanceV2] error getting InstanceMetadata",
fakeCloud: &fakecloud.Cloud{
EnableInstancesV2: true,
InstanceTypes: map[types.NodeName]string{},
Provider: "test",
ExtID: map[types.NodeName]string{},
ExtIDErr: map[types.NodeName]error{
types.NodeName("node0"): cloudprovider.NotImplemented,
},
MetadataErr: errors.New("metadata error"),
},
existingNode: &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node0",
CreationTimestamp: metav1.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
},
Status: v1.NodeStatus{
Conditions: []v1.NodeCondition{
{
Type: v1.NodeReady,
Status: v1.ConditionUnknown,
LastHeartbeatTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
LastTransitionTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
},
},
},
Spec: v1.NodeSpec{
Taints: []v1.Taint{
{
Key: cloudproviderapi.TaintExternalCloudProvider,
Value: "true",
Effect: v1.TaintEffectNoSchedule,
},
},
},
},
updatedNode: &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node0",
CreationTimestamp: metav1.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
},
Status: v1.NodeStatus{
Conditions: []v1.NodeCondition{
{
Type: v1.NodeReady,
Status: v1.ConditionUnknown,
LastHeartbeatTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
LastTransitionTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
},
},
},
Spec: v1.NodeSpec{
Taints: []v1.Taint{
{
Key: cloudproviderapi.TaintExternalCloudProvider,
Value: "true",
Effect: v1.TaintEffectNoSchedule,
},
},
},
},
},
}

for _, test := range tests {
Expand Down
15 changes: 12 additions & 3 deletions staging/src/k8s.io/cloud-provider/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"sync"
"time"

"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
cloudprovider "k8s.io/cloud-provider"
)
Expand Down Expand Up @@ -64,6 +64,7 @@ type Cloud struct {
ErrByProviderID error
NodeShutdown bool
ErrShutdownByProviderID error
MetadataErr error

Calls []string
Addresses []v1.NodeAddress
Expand All @@ -81,6 +82,7 @@ type Cloud struct {
RouteMap map[string]*Route
Lock sync.Mutex
Provider string
ProviderID map[types.NodeName]string
addCallLock sync.Mutex
cloudprovider.Zone
VolumeLabelMap map[string]map[string]string
Expand Down Expand Up @@ -321,13 +323,20 @@ func (f *Cloud) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloudprov
f.addCall("instance-metadata-by-provider-id")
f.addressesMux.Lock()
defer f.addressesMux.Unlock()

providerID := ""
id, ok := f.ProviderID[types.NodeName(node.Name)]
if ok {
providerID = id
}

return &cloudprovider.InstanceMetadata{
ProviderID: node.Spec.ProviderID,
ProviderID: providerID,
InstanceType: f.InstanceTypes[types.NodeName(node.Spec.ProviderID)],
NodeAddresses: f.Addresses,
Zone: f.Zone.FailureDomain,
Region: f.Zone.Region,
}, f.Err
}, f.MetadataErr
}

// List is a test-spy implementation of Instances.List.
Expand Down

0 comments on commit 4b59044

Please sign in to comment.