Skip to content

Commit

Permalink
Optimize Advanced DaemonSet internal new pod for imitating scheduling (
Browse files Browse the repository at this point in the history
…#1011)

Signed-off-by: FillZpp <[email protected]>
  • Loading branch information
FillZpp authored Jun 30, 2022
1 parent 924d7ab commit 69f33a1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
18 changes: 17 additions & 1 deletion pkg/controller/daemonset/daemonset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
ds := e.Object.(*appsv1alpha1.DaemonSet)
klog.V(4).Infof("Deleting DaemonSet %s/%s", ds.Namespace, ds.Name)
dsc.expectations.DeleteExpectations(keyFunc(ds))
newPodForDSCache.Delete(ds.UID)
return true
},
})
Expand Down Expand Up @@ -449,13 +450,28 @@ func isControlledByDaemonSet(p *corev1.Pod, uuid types.UID) bool {

// NewPod creates a new pod
func NewPod(ds *appsv1alpha1.DaemonSet, nodeName string) *corev1.Pod {
// firstly load the cache before lock
if pod := loadNewPodForDS(ds); pod != nil {
return pod
}

newPodForDSLock.Lock()
defer newPodForDSLock.Unlock()

// load the cache again after locked
if pod := loadNewPodForDS(ds); pod != nil {
return pod
}

newPod := &corev1.Pod{Spec: ds.Spec.Template.Spec, ObjectMeta: ds.Spec.Template.ObjectMeta}
newPod.Namespace = ds.Namespace
newPod.Spec.NodeName = nodeName
// no need to set nodeName
// newPod.Spec.NodeName = nodeName

// Added default tolerations for DaemonSet pods.
daemonsetutil.AddOrUpdateDaemonPodTolerations(&newPod.Spec)

newPodForDSCache.Store(ds.UID, &newPodForDS{generation: ds.Generation, pod: newPod})
return newPod
}

Expand Down
22 changes: 22 additions & 0 deletions pkg/controller/daemonset/daemonset_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package daemonset
import (
"fmt"
"sort"
"sync"

appspub "github.com/openkruise/kruise/apis/apps/pub"
appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"
Expand All @@ -37,6 +38,27 @@ import (
"k8s.io/utils/integer"
)

var (
// newPodForDSCache is a cache for NewPod, it is map[ds.UID]*newPodForDS
newPodForDSCache sync.Map
newPodForDSLock sync.Mutex
)

type newPodForDS struct {
generation int64
pod *corev1.Pod
}

func loadNewPodForDS(ds *appsv1alpha1.DaemonSet) *corev1.Pod {
if val, ok := newPodForDSCache.Load(ds.UID); ok {
newPodCache := val.(*newPodForDS)
if newPodCache.generation >= ds.Generation {
return newPodCache.pod
}
}
return nil
}

// nodeInSameCondition returns true if all effective types ("Status" is true) equals;
// otherwise, returns false.
func nodeInSameCondition(old []corev1.NodeCondition, cur []corev1.NodeCondition) bool {
Expand Down

0 comments on commit 69f33a1

Please sign in to comment.