Skip to content

Commit cc1e0d4

Browse files
committed
cloud provider: remove provider ID references and improve documentation
Signed-off-by: Andrew Sy Kim <[email protected]>
1 parent ec560b9 commit cc1e0d4

File tree

3 files changed

+61
-27
lines changed

3 files changed

+61
-27
lines changed

staging/src/k8s.io/cloud-provider/cloud.go

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ type Interface interface {
4949
LoadBalancer() (LoadBalancer, bool)
5050
// Instances returns an instances interface. Also returns true if the interface is supported, false otherwise.
5151
Instances() (Instances, bool)
52-
// InstancesV2 is an implementation for instances only used by cloud node-controller now.
52+
// InstancesV2 is an implementation for instances and should only be implemented by external cloud providers.
53+
// Implementing InstancesV2 is behaviorally identical to Instances but is optimized to significantly reduce
54+
// API calls to the cloud provider when registering and syncing nodes.
5355
// Also returns true if the interface is supported, false otherwise.
56+
// WARNING: InstancesV2 is an experimental interface and is subject to change in v1.20.
5457
InstancesV2() (InstancesV2, bool)
5558
// Zones returns a zones interface. Also returns true if the interface is supported, false otherwise.
5659
Zones() (Zones, bool)
@@ -189,15 +192,20 @@ type Instances interface {
189192
InstanceShutdownByProviderID(ctx context.Context, providerID string) (bool, error)
190193
}
191194

192-
// InstancesV2 is an abstract, pluggable interface for sets of instances.
193-
// Unlike Instances, it is only used by cloud node-controller now.
195+
// InstancesV2 is an abstract, pluggable interface for cloud provider instances.
196+
// Unlike the Instances interface, it is designed for external cloud providers and should only be used by them.
197+
// WARNING: InstancesV2 is an experimental interface and is subject to change in v1.20.
194198
type InstancesV2 interface {
195-
// InstanceExistsByProviderID returns true if the instance for the given provider exists.
196-
InstanceExistsByProviderID(ctx context.Context, providerID string) (bool, error)
197-
// InstanceShutdownByProviderID returns true if the instance is shutdown in cloudprovider.
198-
InstanceShutdownByProviderID(ctx context.Context, providerID string) (bool, error)
199-
// InstanceMetadataByProviderID returns the instance's metadata.
200-
InstanceMetadataByProviderID(ctx context.Context, providerID string) (*InstanceMetadata, error)
199+
// InstanceExists returns true if the instance for the given node exists according to the cloud provider.
200+
// Use the node.name or node.spec.providerID field to find the node in the cloud provider.
201+
InstanceExists(ctx context.Context, node *v1.Node) (bool, error)
202+
// InstanceShutdown returns true if the instance is shutdown according to the cloud provider.
203+
// Use the node.name or node.spec.providerID field to find the node in the cloud provider.
204+
InstanceShutdown(ctx context.Context, node *v1.Node) (bool, error)
205+
// InstanceMetadata returns the instance's metadata. The values returned in InstanceMetadata are
206+
// translated into specific fields in the Node object on registration.
207+
// Use the node.name or node.spec.providerID field to find the node in the cloud provider.
208+
InstanceMetadata(ctx context.Context, node *v1.Node) (*InstanceMetadata, error)
201209
}
202210

203211
// Route is a representation of an advanced routing rule.
@@ -265,12 +273,25 @@ type PVLabeler interface {
265273
GetLabelsForVolume(ctx context.Context, pv *v1.PersistentVolume) (map[string]string, error)
266274
}
267275

268-
// InstanceMetadata contains metadata about the specific instance.
276+
// InstanceMetadata contains metadata about a specific instance.
277+
// Values returned in InstanceMetadata are translated into specific fields in Node.
269278
type InstanceMetadata struct {
270-
// ProviderID is provider's id that instance belongs to.
279+
// ProviderID is a unique ID used to idenfitify an instance on the cloud provider.
280+
// The ProviderID set here will be set on the node's spec.providerID field.
281+
// The provider ID format can be set by the cloud provider but providers should
282+
// ensure the format does not change in any incompatible way.
283+
//
284+
// The provider ID format used by existing cloud provider has been:
285+
// <provider-name>://<instance-id>
286+
// Existing providers setting this field should preserve the existing format
287+
// currently being set in node.spec.providerID.
271288
ProviderID string
272-
// Type is instance's type.
273-
Type string
289+
// InstanceType is the instance's type.
290+
// The InstanceType set here will be set using the following labels on the node object:
291+
// * node.kubernetes.io/instance-type=<instance-type>
292+
// * beta.kubernetes.io/instance-type=<instance-type> (DEPRECATED)
293+
InstanceType string
274294
// NodeAddress contains information for the instance's address.
295+
// The node addresses returned here will be set on the node's status.addresses field.
275296
NodeAddresses []v1.NodeAddress
276297
}

staging/src/k8s.io/cloud-provider/controllers/node/node_controller.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func (cnc *CloudNodeController) updateNodeAddress(ctx context.Context, node *v1.
225225

226226
instanceMetadataGetter := func(providerID string, node *v1.Node) (*cloudprovider.InstanceMetadata, error) {
227227
if instancesV2, ok := cnc.cloud.InstancesV2(); instancesV2 != nil && ok {
228-
return instancesV2.InstanceMetadataByProviderID(ctx, providerID)
228+
return instancesV2.InstanceMetadata(ctx, node)
229229
}
230230

231231
// If InstancesV2 not implement, use Instances.
@@ -435,9 +435,9 @@ func (cnc *CloudNodeController) getNodeModifiersFromCloudProvider(ctx context.Co
435435
providerID = node.Spec.ProviderID
436436
}
437437

438-
instanceMetadataGetter := func(providerID string, nodeName string) (*cloudprovider.InstanceMetadata, error) {
438+
instanceMetadataGetter := func(providerID string, nodeName string, node *v1.Node) (*cloudprovider.InstanceMetadata, error) {
439439
if instancesV2, ok := cnc.cloud.InstancesV2(); instancesV2 != nil && ok {
440-
return instancesV2.InstanceMetadataByProviderID(ctx, providerID)
440+
return instancesV2.InstanceMetadata(ctx, node)
441441
}
442442

443443
// If InstancesV2 not implement, use Instances.
@@ -454,12 +454,12 @@ func (cnc *CloudNodeController) getNodeModifiersFromCloudProvider(ctx context.Co
454454
return nil, err
455455
}
456456
return &cloudprovider.InstanceMetadata{
457-
Type: instanceType,
457+
InstanceType: instanceType,
458458
NodeAddresses: nodeAddresses,
459459
}, nil
460460
}
461461

462-
instanceMeta, err := instanceMetadataGetter(providerID, node.Name)
462+
instanceMeta, err := instanceMetadataGetter(providerID, node.Name, node)
463463
if err != nil {
464464
return nil, err
465465
}
@@ -470,15 +470,15 @@ func (cnc *CloudNodeController) getNodeModifiersFromCloudProvider(ctx context.Co
470470
return nil, errors.New("failed to find kubelet node IP from cloud provider")
471471
}
472472

473-
if instanceMeta.Type != "" {
474-
klog.V(2).Infof("Adding node label from cloud provider: %s=%s", v1.LabelInstanceType, instanceMeta.Type)
475-
klog.V(2).Infof("Adding node label from cloud provider: %s=%s", v1.LabelInstanceTypeStable, instanceMeta.Type)
473+
if instanceMeta.InstanceType != "" {
474+
klog.V(2).Infof("Adding node label from cloud provider: %s=%s", v1.LabelInstanceType, instanceMeta.InstanceType)
475+
klog.V(2).Infof("Adding node label from cloud provider: %s=%s", v1.LabelInstanceTypeStable, instanceMeta.InstanceType)
476476
nodeModifiers = append(nodeModifiers, func(n *v1.Node) {
477477
if n.Labels == nil {
478478
n.Labels = map[string]string{}
479479
}
480-
n.Labels[v1.LabelInstanceType] = instanceMeta.Type
481-
n.Labels[v1.LabelInstanceTypeStable] = instanceMeta.Type
480+
n.Labels[v1.LabelInstanceType] = instanceMeta.InstanceType
481+
n.Labels[v1.LabelInstanceTypeStable] = instanceMeta.InstanceType
482482
})
483483
}
484484

staging/src/k8s.io/cloud-provider/fake/fake.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,14 +303,27 @@ func (f *Cloud) InstanceShutdownByProviderID(ctx context.Context, providerID str
303303
return f.NodeShutdown, f.ErrShutdownByProviderID
304304
}
305305

306-
// InstanceMetadataByProviderID returns metadata of the specified instance.
307-
func (f *Cloud) InstanceMetadataByProviderID(ctx context.Context, providerID string) (*cloudprovider.InstanceMetadata, error) {
306+
// InstanceExists returns true if the instance corresponding to a node still exists and is running.
307+
// If false is returned with no error, the instance will be immediately deleted by the cloud controller manager.
308+
func (f *Cloud) InstanceExists(ctx context.Context, node *v1.Node) (bool, error) {
309+
f.addCall("instance-exists")
310+
return f.ExistsByProviderID, f.ErrByProviderID
311+
}
312+
313+
// InstanceShutdown returns true if the instances is in safe state to detach volumes
314+
func (f *Cloud) InstanceShutdown(ctx context.Context, node *v1.Node) (bool, error) {
315+
f.addCall("instance-shutdown")
316+
return f.NodeShutdown, f.ErrShutdownByProviderID
317+
}
318+
319+
// InstanceMetadata returns metadata of the specified instance.
320+
func (f *Cloud) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloudprovider.InstanceMetadata, error) {
308321
f.addCall("instance-metadata-by-provider-id")
309322
f.addressesMux.Lock()
310323
defer f.addressesMux.Unlock()
311324
return &cloudprovider.InstanceMetadata{
312-
ProviderID: providerID,
313-
Type: f.InstanceTypes[types.NodeName(providerID)],
325+
ProviderID: node.Spec.ProviderID,
326+
InstanceType: f.InstanceTypes[types.NodeName(node.Spec.ProviderID)],
314327
NodeAddresses: f.Addresses,
315328
}, f.Err
316329
}

0 commit comments

Comments
 (0)