Skip to content

Commit

Permalink
add node condition for node initialization by cloud provider
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Sy Kim <kim.andrewsy@gmail.com>
  • Loading branch information
andrewsykim committed Aug 18, 2021
1 parent 58617e1 commit ae695ff
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
27 changes: 27 additions & 0 deletions staging/src/k8s.io/cloud-provider/api/conditions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright 2021 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 api

import (
v1 "k8s.io/api/core/v1"
)

const (
// NodeInitializedByCloudProvider means the node has been successfully initialized
// by an external cloud provider.
NodeInitializedByCloudProviderCondition v1.NodeConditionType = "NodeInitializedByCloudProvider"
)
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,16 @@ func (cnc *CloudNodeController) syncNode(ctx context.Context, nodeName string) e

klog.Infof("Initializing node %s with cloud provider", nodeName)

if err := cloudnodeutil.SetNodeCondition(cnc.kubeClient, types.NodeName(nodeName), v1.NodeCondition{
Type: cloudproviderapi.NodeInitializedByCloudProviderCondition,
Status: v1.ConditionFalse,
Reason: "NodeInitializing",
Message: fmt.Sprintf("Node %s is being initialized", nodeName),
LastTransitionTime: metav1.Now(),
}); err != nil {
return err
}

copyNode := curNode.DeepCopy()
providerID, err := cnc.getProviderID(ctx, copyNode)
if err != nil {
Expand All @@ -397,11 +407,31 @@ func (cnc *CloudNodeController) syncNode(ctx context.Context, nodeName string) e

instanceMetadata, err := cnc.getInstanceMetadata(ctx, providerID, copyNode)
if err != nil {
if err := cloudnodeutil.SetNodeCondition(cnc.kubeClient, types.NodeName(nodeName), v1.NodeCondition{
Type: cloudproviderapi.NodeInitializedByCloudProviderCondition,
Status: v1.ConditionFalse,
Reason: "NodeInitializationFailed",
Message: fmt.Sprintf("Node %s failed to initialize with error: %v", nodeName, err),
LastTransitionTime: metav1.Now(),
}); err != nil {
klog.ErrorS(err, "failed to set node condition", "condition", cloudproviderapi.NodeInitializedByCloudProviderCondition, "node", nodeName)
}

return fmt.Errorf("failed to get instance metadata for node %s: %v", nodeName, err)
}

nodeModifiers, err := cnc.getNodeModifiersFromCloudProvider(ctx, providerID, copyNode, instanceMetadata)
if err != nil {
if err := cloudnodeutil.SetNodeCondition(cnc.kubeClient, types.NodeName(nodeName), v1.NodeCondition{
Type: cloudproviderapi.NodeInitializedByCloudProviderCondition,
Status: v1.ConditionFalse,
Reason: "NodeInitializationFailed",
Message: fmt.Sprintf("Node %s failed to initialize with error: %v", nodeName, err),
LastTransitionTime: metav1.Now(),
}); err != nil {
klog.ErrorS(err, "failed to set node condition", "condition", cloudproviderapi.NodeInitializedByCloudProviderCondition, "node", nodeName)
}

return fmt.Errorf("failed to get node modifiers from cloud provider: %v", err)
}

Expand Down Expand Up @@ -447,6 +477,16 @@ func (cnc *CloudNodeController) syncNode(ctx context.Context, nodeName string) e
return err
}

if err := cloudnodeutil.SetNodeCondition(cnc.kubeClient, types.NodeName(nodeName), v1.NodeCondition{
Type: cloudproviderapi.NodeInitializedByCloudProviderCondition,
Status: v1.ConditionTrue,
Reason: "NodeInitialized",
Message: fmt.Sprintf("Node %s was successfully initialized", nodeName),
LastTransitionTime: metav1.Now(),
}); err != nil {
return err
}

// After adding, call UpdateNodeAddress to set the CloudProvider provided IPAddresses
// So that users do not see any significant delay in IP addresses being filled into the node
cnc.updateNodeAddress(ctx, newNode, instanceMetadata)
Expand Down

0 comments on commit ae695ff

Please sign in to comment.