Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cloud provider: fix the fake cloud provider #95499

Merged
merged 1 commit into from Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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 := ""
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrewsykim so I make this default empty providerID instead of inheriting from node.spec.providerID
PTAL :)

id, ok := f.ProviderID[types.NodeName(node.Name)]
if ok {
providerID = id
nicolehanjing marked this conversation as resolved.
Show resolved Hide resolved
}

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