Skip to content

Commit

Permalink
Merge pull request #273 from wangyizhi1/node
Browse files Browse the repository at this point in the history
fix: incorrect node resources
  • Loading branch information
kosmos-robot authored Nov 21, 2023
2 parents ae17471 + 58b05ec commit 2aa0100
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,7 @@ func (c *NodeResourcesController) Reconcile(ctx context.Context, request reconci
}, err
}

clusterResources := utils.CalculateClusterResources(nodesInLeaf, pods)
clone := nodeInRoot.DeepCopy()
clone.Status.Allocatable = clusterResources
clone.Status.Capacity = clusterResources
clone.Status.Conditions = utils.NodeConditions()

// Node2Node mode should sync leaf node's labels and annotations to root nodeInRoot
Expand All @@ -144,8 +141,8 @@ func (c *NodeResourcesController) Reconcile(ctx context.Context, request reconci
}
node := getNode(nodesInLeaf)
if node != nil {
clone.Labels = mergeMap(clone.GetLabels(), node.GetLabels())
clone.Annotations = mergeMap(clone.GetAnnotations(), node.GetAnnotations())
clone.Labels = mergeMap(node.GetLabels(), clone.GetLabels())
clone.Annotations = mergeMap(node.GetAnnotations(), clone.GetAnnotations())
spec := corev1.NodeSpec{
Taints: rootNode.Spec.Taints,
}
Expand All @@ -156,6 +153,10 @@ func (c *NodeResourcesController) Reconcile(ctx context.Context, request reconci
}
}

clusterResources := utils.CalculateClusterResources(nodesInLeaf, pods)
clone.Status.Allocatable = clusterResources
clone.Status.Capacity = clusterResources

patch, err := utils.CreateMergePatch(nodeInRoot, clone)
if err != nil {
klog.Errorf("Could not CreateMergePatch,Error: %v", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ func (c *OnewayPVController) Reconcile(ctx context.Context, request reconcile.Re
vp, err := c.RootDynamic.Resource(VolumePathGVR).Get(ctx, request.Name, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
klog.Warningf("vp %s not found", request.Name)
return reconcile.Result{}, nil
klog.V(4).Infof("vp %s not found", request.Name)
return reconcile.Result{RequeueAfter: requeueTime}, nil
}
klog.Errorf("get volumePath %s error: %v", request.Name, err)
return reconcile.Result{RequeueAfter: requeueTime}, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ func (c *OnewayPVCController) Reconcile(ctx context.Context, request reconcile.R
vp, err := c.RootDynamic.Resource(VolumePathGVR).Get(ctx, request.Name, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
klog.Warningf("vp %s not found", request.Name)
return reconcile.Result{}, nil
klog.V(4).Infof("vp %s not found", request.Name)
return reconcile.Result{RequeueAfter: requeueTime}, nil
}
klog.Errorf("get volumePath %s error: %v", request.Name, err)
return reconcile.Result{RequeueAfter: requeueTime}, nil
Expand Down
3 changes: 3 additions & 0 deletions pkg/clustertree/cluster-manager/utils/leaf_model_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,11 @@ func (h DispersionModelHandler) UpdateNodeStatus(ctx context.Context, n []*corev
return fmt.Errorf("cannot get node in root cluster while update node status %s, err: %v", nodeCopy.Name, err)
}

rootCopy := nodeRoot.DeepCopy()
nodeRoot.Status = nodeInLeaf.Status
nodeRoot.Status.Addresses = GetAddress()
nodeRoot.Status.Allocatable = rootCopy.Status.Allocatable
nodeRoot.Status.Capacity = rootCopy.Status.Capacity

if node, err = h.RootClientset.CoreV1().Nodes().UpdateStatus(ctx, nodeRoot, metav1.UpdateOptions{}); err != nil {
return err
Expand Down
22 changes: 22 additions & 0 deletions pkg/utils/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ package utils

import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
v1resource "k8s.io/kubernetes/pkg/api/v1/resource"
)

const (
podResourceName corev1.ResourceName = "pods"
)

func CalculateClusterResources(nodes *corev1.NodeList, pods *corev1.PodList) corev1.ResourceList {
base := GetNodesTotalResources(nodes)
reqs, _ := GetPodsTotalRequestsAndLimits(pods)
podNums := GetUsedPodNums(pods)
SubResourceList(base, reqs)
SubResourceList(base, podNums)
return base
}

Expand Down Expand Up @@ -70,3 +77,18 @@ func GetPodsTotalRequestsAndLimits(podList *corev1.PodList) (reqs corev1.Resourc
}
return
}

func GetUsedPodNums(podList *corev1.PodList) (res corev1.ResourceList) {
podQuantity := resource.Quantity{}
res = corev1.ResourceList{}
for _, p := range podList.Items {
pod := p
if IsVirtualPod(&pod) {
continue
}
q := resource.MustParse("1")
podQuantity.Add(q)
}
res[podResourceName] = podQuantity
return
}

0 comments on commit 2aa0100

Please sign in to comment.