forked from koordinator-sh/koordinator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
scheduler : fix deviceshare plugin of add\remove pod (koordinator-sh#…
- Loading branch information
xingbao.zy
committed
Mar 18, 2024
1 parent
f3bc42a
commit b169d49
Showing
6 changed files
with
453 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package deviceshare | ||
|
||
import ( | ||
"sync" | ||
|
||
schedulingv1alpha1 "github.com/koordinator-sh/koordinator/apis/scheduling/v1alpha1" | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
type Nominator struct { | ||
sync.Mutex | ||
nominateMap map[string]map[schedulingv1alpha1.DeviceType]deviceResources | ||
} | ||
|
||
func NewNominator() *Nominator { | ||
return &Nominator{ | ||
nominateMap: make(map[string]map[schedulingv1alpha1.DeviceType]deviceResources), | ||
} | ||
} | ||
|
||
func (nominator *Nominator) AddPod(pod *corev1.Pod, used map[schedulingv1alpha1.DeviceType]deviceResources) { | ||
nominator.Lock() | ||
defer nominator.Unlock() | ||
|
||
podNamespacedName := pod.Namespace + "/" + pod.Name | ||
nominator.nominateMap[podNamespacedName] = used | ||
} | ||
|
||
func (nominator *Nominator) RemovePod(pod *corev1.Pod) { | ||
nominator.Lock() | ||
defer nominator.Unlock() | ||
|
||
podNamespacedName := pod.Namespace + "/" + pod.Name | ||
delete(nominator.nominateMap, podNamespacedName) | ||
} | ||
|
||
func (nominator *Nominator) GetPodAllocate(pod *corev1.Pod) map[schedulingv1alpha1.DeviceType]deviceResources { | ||
nominator.Lock() | ||
defer nominator.Unlock() | ||
|
||
podNamespacedName := pod.Namespace + "/" + pod.Name | ||
return nominator.nominateMap[podNamespacedName] | ||
} | ||
|
||
func (nominator *Nominator) IsPodExist(pod *corev1.Pod) bool { | ||
nominator.Lock() | ||
defer nominator.Unlock() | ||
|
||
podNamespacedName := pod.Namespace + "/" + pod.Name | ||
_, exist := nominator.nominateMap[podNamespacedName] | ||
return exist | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package deviceshare | ||
|
||
import ( | ||
"testing" | ||
|
||
schedulingv1alpha1 "github.com/koordinator-sh/koordinator/apis/scheduling/v1alpha1" | ||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
) | ||
|
||
func TestNominator(t *testing.T) { | ||
nominator := NewNominator() | ||
assert.Equal(t, 0, len(nominator.nominateMap)) | ||
|
||
pod := &corev1.Pod{} | ||
pod.Namespace = "test" | ||
pod.Name = "job1" | ||
|
||
used := make(map[schedulingv1alpha1.DeviceType]deviceResources) | ||
used[schedulingv1alpha1.GPU] = make(deviceResources) | ||
used[schedulingv1alpha1.GPU][0] = corev1.ResourceList{ | ||
corev1.ResourceCPU: resource.MustParse("10"), | ||
} | ||
|
||
nominator.AddPod(pod, used) | ||
assert.Equal(t, 1, len(nominator.nominateMap)) | ||
used = nominator.GetPodAllocate(pod) | ||
usedCPU := used[schedulingv1alpha1.GPU][0][corev1.ResourceCPU] | ||
assert.Equal(t, usedCPU.String(), "10") | ||
assert.Equal(t, true, nominator.IsPodExist(pod)) | ||
|
||
nominator.RemovePod(pod) | ||
assert.Equal(t, 0, len(nominator.nominateMap)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.