func (r *rangeAllocator) updateCIDRsAllocation(data nodeReservedCIDRs) error {
	var err error
	var node *v1.Node
	defer r.removeNodeFromProcessing(data.nodeName)
	cidrsString := cidrsAsString(data.allocatedCIDRs)
	node, err = r.nodeLister.Get(data.nodeName)
	if err != nil {
		klog.Errorf("Failed while getting node %v for updating Node.Spec.PodCIDRs: %v", data.nodeName, err)
		return err
	}

	// if cidr list matches the proposed.
	// then we possibly updated this node
	// and just failed to ack the success.
	if len(node.Spec.PodCIDRs) == len(data.allocatedCIDRs) {
		match := true
		for idx, cidr := range cidrsString {
			if node.Spec.PodCIDRs[idx] != cidr {
				match = false
				break
			}
		}
		if match {
			klog.V(4).Infof("Node %v already has allocated CIDR %v. It matches the proposed one.", node.Name, data.allocatedCIDRs)
			return nil
		}
	}

	// node has cidrs, release the reserved
	if len(node.Spec.PodCIDRs) != 0 {
		klog.Errorf("Node %v already has a CIDR allocated %v. Releasing the new one.", node.Name, node.Spec.PodCIDRs)
		for idx, cidr := range data.allocatedCIDRs {
			if releaseErr := r.cidrSets[idx].Release(cidr); releaseErr != nil {
				klog.Errorf("Error when releasing CIDR idx:%v value: %v err:%v", idx, cidr, releaseErr)
			}
		}
		return nil
	}

	// If we reached here, it means that the node has no CIDR currently assigned. So we set it.
	for i := 0; i < cidrUpdateRetries; i++ {
		if err = utilnode.PatchNodeCIDRs(r.client, types.NodeName(node.Name), cidrsString); err == nil {
			klog.Infof("Set node %v PodCIDR to %v", node.Name, cidrsString)
			return nil
		}
	}
	// failed release back to the pool
	klog.Errorf("Failed to update node %v PodCIDR to %v after multiple attempts: %v", node.Name, cidrsString, err)
	nodeutil.RecordNodeStatusChange(r.recorder, node, "CIDRAssignmentFailed")
	// We accept the fact that we may leak CIDRs here. This is safer than releasing
	// them in case when we don't know if request went through.
	// NodeController restart will return all falsely allocated CIDRs to the pool.
	if !apierrors.IsServerTimeout(err) {
		klog.Errorf("CIDR assignment for node %v failed: %v. Releasing allocated CIDR", node.Name, err)
		for idx, cidr := range data.allocatedCIDRs {
			if releaseErr := r.cidrSets[idx].Release(cidr); releaseErr != nil {
				klog.Errorf("Error releasing allocated CIDR for node %v: %v", node.Name, releaseErr)
			}
		}
	}
	return err
}
// updateCIDRsAllocation assigns CIDR to Node and sends an update to the API server.
updateCIDRsAllocation assigns CIDR to Node and sends an update to the API server.
// updateCIDRsAllocation assigns CIDR to Node and sends an update to the API server .
func (r *rangeAllocator) updateCIDRsAllocation(data nodeReservedCIDRs) error {
	var err error
	var node *v1.Node
	defer r.removeNodeFromProcessing(data.nodeName)
	cidrsString := cidrsAsString(data.allocatedCIDRs)
	node, err = r.nodeLister.Get(data.nodeName)
	if err != nil {
		klog.Errorf("Failed while getting node %v for updating Node.Spec.PodCIDRs: %v", data.nodeName, err)
		return err
	}
	if len(node.Spec.PodCIDRs) == len(data.allocatedCIDRs) {
		match := true
		for idx, cidr := range cidrsString {
			if node.Spec.PodCIDRs[idx] != cidr {
				match = false
				break
			}
		}
		if match {
			klog.V(4).Infof("Node %v already has allocated CIDR %v. It matches the proposed one.", node.Name, data.allocatedCIDRs)
			return nil
		}
	}
	if len(node.Spec.PodCIDRs) != 0 {
		klog.Errorf("Node %v already has a CIDR allocated %v. Releasing the new one.", node.Name, node.Spec.PodCIDRs)
		for idx, cidr := range data.allocatedCIDRs {
			if releaseErr := r.cidrSets[idx].Release(cidr); releaseErr != nil {
				klog.Errorf("Error when releasing CIDR idx:%v value: %v err:%v", idx, cidr, releaseErr)
			}
		}
		return nil
	}
	for i := 0; i < cidrUpdateRetries; i++ {
		if err = utilnode.PatchNodeCIDRs(r.client, types.NodeName(node.Name), cidrsString); err == nil {
			klog.Infof("Set node %v PodCIDR to %v", node.Name, cidrsString)
			return nil
		}
	}
	klog.Errorf("Failed to update node %v PodCIDR to %v after multiple attempts: %v", node.Name, cidrsString, err)
	nodeutil.RecordNodeStatusChange(r.recorder, node, "CIDRAssignmentFailed")
	if !apierrors.IsServerTimeout(err) {
		klog.Errorf("CIDR assignment for node %v failed: %v. Releasing allocated CIDR", node.Name, err)
		for idx, cidr := range data.allocatedCIDRs {
			if releaseErr := r.cidrSets[idx].Release(cidr); releaseErr != nil {
				klog.Errorf("Error releasing allocated CIDR for node %v: %v", node.Name, releaseErr)
			}
		}
	}
	return err
}
func ( r * rangeAllocator ) updateCIDRsAllocation ( data nodeReservedCIDRs ) error { var err error var node * v1 . Node defer r . removeNodeFromProcessing ( data . nodeName ) cidrsString := cidrsAsString ( data . allocatedCIDRs ) node , err = r . nodeLister . Get ( data . nodeName ) if err != nil { klog . Errorf ( " " , data . nodeName , err ) return err } if len ( node . Spec . PodCIDRs ) == len ( data . allocatedCIDRs ) { match := true for idx , cidr := range cidrsString { if node . Spec . PodCIDRs [ idx ] != cidr { match = false break } } if match { klog . V ( 4 ) . Infof ( " " , node . Name , data . allocatedCIDRs ) return nil } } if len ( node . Spec . PodCIDRs ) != 0 { klog . Errorf ( " " , node . Name , node . Spec . PodCIDRs ) for idx , cidr := range data . allocatedCIDRs { if releaseErr := r . cidrSets [ idx ] . Release ( cidr ) ; releaseErr != nil { klog . Errorf ( " " , idx , cidr , releaseErr ) } } return nil } for i := 0 ; i < cidrUpdateRetries ; i ++ { if err = utilnode . PatchNodeCIDRs ( r . client , types . NodeName ( node . Name ) , cidrsString ) ; err == nil { klog . Infof ( " " , node . Name , cidrsString ) return nil } } klog . Errorf ( " " , node . Name , cidrsString , err ) nodeutil . RecordNodeStatusChange ( r . recorder , node , " " ) if ! apierrors . IsServerTimeout ( err ) { klog . Errorf ( " " , node . Name , err ) for idx , cidr := range data . allocatedCIDRs { if releaseErr := r . cidrSets [ idx ] . Release ( cidr ) ; releaseErr != nil { klog . Errorf ( " " , node . Name , releaseErr ) } } } return err }
updateCIDRsAllocation assigns CIDR to Node and sends an update to the API server.
updateCIDRsAllocation assigns CIDR to Node and sends an update to the API server .
// if cidr list matches the proposed. // then we possibly updated this node // and just failed to ack the success. // node has cidrs, release the reserved // If we reached here, it means that the node has no CIDR currently assigned. So we set it. // failed release back to the pool // We accept the fact that we may leak CIDRs here. This is safer than releasing // them in case when we don't know if request went through. // NodeController restart will return all falsely allocated CIDRs to the pool.