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#…
…1963) Signed-off-by: xulinfei.xlf <xulinfei.xlf@alibaba-inc.com> Co-authored-by: xulinfei.xlf <xulinfei.xlf@alibaba-inc.com>
- Loading branch information
Showing
6 changed files
with
483 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,69 @@ | ||
/* | ||
Copyright 2022 The Koordinator Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package deviceshare | ||
|
||
import ( | ||
"sync" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
|
||
schedulingv1alpha1 "github.com/koordinator-sh/koordinator/apis/scheduling/v1alpha1" | ||
) | ||
|
||
type nominator struct { | ||
lock 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.Lock() | ||
defer nominator.lock.Unlock() | ||
|
||
podNamespacedName := pod.Namespace + "/" + pod.Name | ||
nominator.nominateMap[podNamespacedName] = used | ||
} | ||
|
||
func (nominator *nominator) RemovePod(pod *corev1.Pod) { | ||
nominator.lock.Lock() | ||
defer nominator.lock.Unlock() | ||
|
||
podNamespacedName := pod.Namespace + "/" + pod.Name | ||
delete(nominator.nominateMap, podNamespacedName) | ||
} | ||
|
||
func (nominator *nominator) GetPodAllocated(pod *corev1.Pod) map[schedulingv1alpha1.DeviceType]deviceResources { | ||
nominator.lock.Lock() | ||
defer nominator.lock.Unlock() | ||
|
||
podNamespacedName := pod.Namespace + "/" + pod.Name | ||
return nominator.nominateMap[podNamespacedName] | ||
} | ||
|
||
func (nominator *nominator) IsPodExist(pod *corev1.Pod) bool { | ||
nominator.lock.Lock() | ||
defer nominator.lock.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,52 @@ | ||
/* | ||
Copyright 2022 The Koordinator Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package deviceshare | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
|
||
"github.com/koordinator-sh/koordinator/apis/scheduling/v1alpha1" | ||
) | ||
|
||
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[v1alpha1.DeviceType]deviceResources) | ||
used[v1alpha1.GPU] = make(deviceResources) | ||
used[v1alpha1.GPU][0] = corev1.ResourceList{ | ||
corev1.ResourceCPU: resource.MustParse("10"), | ||
} | ||
|
||
nominator.AddPod(pod, used) | ||
assert.Equal(t, 1, len(nominator.nominateMap)) | ||
used = nominator.GetPodAllocated(pod) | ||
usedCPU := used[v1alpha1.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.