Skip to content

Commit

Permalink
fix: incorrect node resources
Browse files Browse the repository at this point in the history
Signed-off-by: wangyizhi1 <[email protected]>
  • Loading branch information
wangyizhi1 committed Nov 21, 2023
1 parent 000fb35 commit 58b05ec
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 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 @@ -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
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 58b05ec

Please sign in to comment.