Skip to content

Commit

Permalink
optimize the pv and pvc code
Browse files Browse the repository at this point in the history
Signed-off-by: duanmengkk <[email protected]>
  • Loading branch information
duanmengkk committed Jan 24, 2024
1 parent cdb1b21 commit 48f20e1
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pod

import (
"context"
"time"

"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
Expand All @@ -25,7 +24,6 @@ import (

const (
LeafPodControllerName = "leaf-pod-controller"
LeafPodRequeueTime = 10 * time.Second
)

type LeafPodReconciler struct {
Expand All @@ -40,12 +38,12 @@ func (r *LeafPodReconciler) Reconcile(ctx context.Context, request reconcile.Req
if apierrors.IsNotFound(err) {
// delete pod in root
if err := DeletePodInRootCluster(ctx, request.NamespacedName, r.RootClient); err != nil {
return reconcile.Result{RequeueAfter: LeafPodRequeueTime}, nil
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}
return reconcile.Result{}, nil
}
klog.Errorf("get %s error: %v", request.NamespacedName, err)
return reconcile.Result{RequeueAfter: LeafPodRequeueTime}, nil
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}

podCopy := pod.DeepCopy()
Expand All @@ -59,7 +57,7 @@ func (r *LeafPodReconciler) Reconcile(ctx context.Context, request reconcile.Req
podCopy.ResourceVersion = "0"
if err := r.RootClient.Status().Update(ctx, podCopy); err != nil && !apierrors.IsNotFound(err) {
klog.V(4).Info(errors.Wrap(err, "error while updating pod status in kubernetes"))
return reconcile.Result{RequeueAfter: LeafPodRequeueTime}, nil
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}
}
return reconcile.Result{}, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (

const (
RootPodControllerName = "root-pod-controller"
RootPodRequeueTime = 10 * time.Second
)

type RootPodReconciler struct {
Expand Down Expand Up @@ -128,17 +127,17 @@ func (r *RootPodReconciler) Reconcile(ctx context.Context, request reconcile.Req
lr, err := r.GlobalLeafManager.GetLeafResourceByNodeName(nodeName)
if err != nil {
// wait for leaf resource init
return reconcile.Result{RequeueAfter: RootPodRequeueTime}, nil
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}
if err := r.DeletePodInLeafCluster(ctx, lr, request.NamespacedName, false); err != nil {
klog.Errorf("delete pod in leaf error[1]: %v, %s", err, request.NamespacedName)
return reconcile.Result{RequeueAfter: RootPodRequeueTime}, nil
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}
}
return reconcile.Result{}, nil
}
klog.Errorf("get %s error: %v", request.NamespacedName, err)
return reconcile.Result{RequeueAfter: RootPodRequeueTime}, nil
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}

rootpod := *(cachepod.DeepCopy())
Expand All @@ -154,7 +153,7 @@ func (r *RootPodReconciler) Reconcile(ctx context.Context, request reconcile.Req

targetNode := &corev1.Node{}
if err := r.RootClient.Get(ctx, nn, targetNode); err != nil {
return reconcile.Result{RequeueAfter: RootPodRequeueTime}, nil
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}

if targetNode.Annotations == nil {
Expand All @@ -171,13 +170,13 @@ func (r *RootPodReconciler) Reconcile(ctx context.Context, request reconcile.Req
// TODO: GlobalLeafResourceManager may not inited....
// belongs to the current node
if !r.GlobalLeafManager.HasNode(rootpod.Spec.NodeName) {
return reconcile.Result{RequeueAfter: RootPodRequeueTime}, nil
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}

lr, err := r.GlobalLeafManager.GetLeafResourceByNodeName(rootpod.Spec.NodeName)
if err != nil {
// wait for leaf resource init
return reconcile.Result{RequeueAfter: RootPodRequeueTime}, nil
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}

// skip namespace
Expand All @@ -189,7 +188,7 @@ func (r *RootPodReconciler) Reconcile(ctx context.Context, request reconcile.Req
if !rootpod.GetDeletionTimestamp().IsZero() {
if err := r.DeletePodInLeafCluster(ctx, lr, request.NamespacedName, true); err != nil {
klog.Errorf("delete pod in leaf error[1]: %v, %s", err, request.NamespacedName)
return reconcile.Result{RequeueAfter: RootPodRequeueTime}, nil
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}
return reconcile.Result{}, nil
}
Expand All @@ -202,20 +201,20 @@ func (r *RootPodReconciler) Reconcile(ctx context.Context, request reconcile.Req
if errors.IsNotFound(err) {
if err := r.CreatePodInLeafCluster(ctx, lr, &rootpod, r.GlobalLeafManager.GetClusterNode(rootpod.Spec.NodeName).LeafNodeSelector); err != nil {
klog.Errorf("create pod inleaf error, err: %s", err)
return reconcile.Result{RequeueAfter: RootPodRequeueTime}, nil
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
} else {
return reconcile.Result{}, nil
}
} else {
klog.Errorf("get pod in leaf error[3]: %v, %s", err, request.NamespacedName)
return reconcile.Result{RequeueAfter: RootPodRequeueTime}, nil
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}
}

// update pod in leaf
if podutils.ShouldEnqueue(leafPod, &rootpod) {
if err := r.UpdatePodInLeafCluster(ctx, lr, &rootpod, leafPod, r.GlobalLeafManager.GetClusterNode(rootpod.Spec.NodeName).LeafNodeSelector); err != nil {
return reconcile.Result{RequeueAfter: RootPodRequeueTime}, nil
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pv

import (
"context"
"time"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -26,7 +25,6 @@ import (

const (
LeafPVControllerName = "leaf-pv-controller"
LeafPVRequeueTime = 10 * time.Second
)

type LeafPVController struct {
Expand All @@ -44,7 +42,7 @@ func (l *LeafPVController) Reconcile(ctx context.Context, request reconcile.Requ
if err != nil {
if !errors.IsNotFound(err) {
klog.Errorf("get pv from leaf cluster failed, error: %v", err)
return reconcile.Result{RequeueAfter: LeafPVRequeueTime}, nil
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}
pvNeedDelete = true
}
Expand All @@ -55,7 +53,7 @@ func (l *LeafPVController) Reconcile(ctx context.Context, request reconcile.Requ
if err != nil {
if !errors.IsNotFound(err) {
klog.Errorf("get root pv failed, error: %v", err)
return reconcile.Result{RequeueAfter: LeafPVRequeueTime}, nil
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}

if pvNeedDelete || pv.DeletionTimestamp != nil {
Expand All @@ -72,7 +70,7 @@ func (l *LeafPVController) Reconcile(ctx context.Context, request reconcile.Requ
if err != nil {
if !errors.IsNotFound(err) {
klog.Errorf("get tmp pvc failed, error: %v", err)
return reconcile.Result{RequeueAfter: LeafPVRequeueTime}, nil
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}
klog.Warningf("tmp pvc not exist, error: %v", err)
return reconcile.Result{}, nil
Expand Down Expand Up @@ -108,7 +106,7 @@ func (l *LeafPVController) Reconcile(ctx context.Context, request reconcile.Requ
rootPV, err = l.RootClientSet.CoreV1().PersistentVolumes().Create(ctx, rootPV, metav1.CreateOptions{})
if err != nil || rootPV == nil {
klog.Errorf("create pv in root cluster failed, error: %v", err)
return reconcile.Result{RequeueAfter: LeafPVRequeueTime}, nil
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}

return reconcile.Result{}, nil
Expand All @@ -123,7 +121,7 @@ func (l *LeafPVController) Reconcile(ctx context.Context, request reconcile.Requ
if err = l.RootClientSet.CoreV1().PersistentVolumes().Delete(ctx, request.NamespacedName.Name, metav1.DeleteOptions{}); err != nil {
if !errors.IsNotFound(err) {
klog.Errorf("delete root pv failed, error: %v", err)
return reconcile.Result{RequeueAfter: LeafPVRequeueTime}, nil
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}
}
klog.V(4).Infof("root pv name: %q deleted", request.NamespacedName.Name)
Expand Down Expand Up @@ -167,7 +165,7 @@ func (l *LeafPVController) Reconcile(ctx context.Context, request reconcile.Requ
if err != nil {
klog.Errorf("patch pv namespace: %q, name: %q to root cluster failed, error: %v",
request.NamespacedName.Namespace, request.NamespacedName.Name, err)
return reconcile.Result{RequeueAfter: LeafPVRequeueTime}, nil
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}
return reconcile.Result{}, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ type RootPVController struct {
}

func (r *RootPVController) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
pv := &v1.PersistentVolume{}
shouldDelete := false
err := r.RootClient.Get(ctx, request.NamespacedName, pv)
if err != nil {
if !errors.IsNotFound(err) {
klog.Warningf("get pv from root cluster failed, error: %v", err)
return reconcile.Result{RequeueAfter: requeueTime}, nil
}
shouldDelete = true
pv.Namespace = request.Namespace
pv.Name = request.Name
}

if !pv.DeletionTimestamp.IsZero() || shouldDelete {
return r.cleanupPv(pv)
}
return reconcile.Result{}, nil
}

Expand Down Expand Up @@ -85,3 +101,26 @@ func (r *RootPVController) SetupWithManager(mgr manager.Manager) error {
})).
Complete(r)
}

func (r *RootPVController) cleanupPv(pv *v1.PersistentVolume) (reconcile.Result, error) {
clusters := utils.ListResourceClusters(pv.Annotations)
if len(clusters) == 0 {
klog.Warningf("pv leaf %q doesn't existed", pv.GetName())
return reconcile.Result{}, nil
}

lr, err := r.GlobalLeafManager.GetLeafResource(clusters[0])
if err != nil {
klog.Warningf("pv leaf %q doesn't existed in LeafResources", pv.GetName())
return reconcile.Result{}, nil
}

if err = lr.Clientset.CoreV1().PersistentVolumes().Delete(context.TODO(), pv.GetName(),
metav1.DeleteOptions{}); err != nil {
if !errors.IsNotFound(err) {
klog.Errorf("delete pv from leaf cluster failed, %q, error: %v", pv.GetName(), err)
return reconcile.Result{RequeueAfter: requeueTime}, nil
}
}
return reconcile.Result{}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (

const (
LeafPVCControllerName = "leaf-pvc-controller"
LeafPVCRequeueTime = 10 * time.Second
)

type LeafPVCController struct {
Expand All @@ -44,7 +43,7 @@ func (l *LeafPVCController) Reconcile(ctx context.Context, request reconcile.Req
if err != nil {
if !errors.IsNotFound(err) {
klog.Errorf("get pvc from leaf cluster failed, error: %v", err)
return reconcile.Result{RequeueAfter: LeafPVCRequeueTime}, nil
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}
klog.V(4).Infof("leaf pvc namespace: %q, name: %q deleted", request.NamespacedName.Namespace,
request.NamespacedName.Name)
Expand All @@ -55,7 +54,7 @@ func (l *LeafPVCController) Reconcile(ctx context.Context, request reconcile.Req
err = l.RootClient.Get(ctx, request.NamespacedName, rootPVC)
if err != nil {
if !errors.IsNotFound(err) {
return reconcile.Result{RequeueAfter: LeafPVCRequeueTime}, nil
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}
klog.Warningf("pvc namespace: %q, name: %q has been deleted from root cluster", request.NamespacedName.Namespace,
request.NamespacedName.Name)
Expand Down Expand Up @@ -83,7 +82,7 @@ func (l *LeafPVCController) Reconcile(ctx context.Context, request reconcile.Req
})
if err != nil {
if !errors.IsNotFound(err) {
return reconcile.Result{RequeueAfter: LeafPVCRequeueTime}, nil
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}
return reconcile.Result{}, nil
}
Expand All @@ -110,7 +109,7 @@ func (l *LeafPVCController) Reconcile(ctx context.Context, request reconcile.Req
if err != nil {
klog.Errorf("patch pvc namespace: %q, name: %q to root cluster failed, error: %v",
request.NamespacedName.Namespace, request.NamespacedName.Name, err)
return reconcile.Result{RequeueAfter: RootPVCRequeueTime}, nil
return reconcile.Result{RequeueAfter: utils.DefaultRequeueTime}, nil
}

return reconcile.Result{}, nil
Expand Down
Loading

0 comments on commit 48f20e1

Please sign in to comment.