-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
koord-manager: refactor gpu device resource plugin
Signed-off-by: saintube <saintube@foxmail.com>
- Loading branch information
Showing
9 changed files
with
492 additions
and
18 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
70 changes: 70 additions & 0 deletions
70
pkg/slo-controller/noderesource/plugins/gpudeviceresource/device_event_handler.go
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,70 @@ | ||
/* | ||
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 gpudeviceresource | ||
|
||
import ( | ||
"reflect" | ||
|
||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/util/workqueue" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
||
schedulingv1alpha1 "github.com/koordinator-sh/koordinator/apis/scheduling/v1alpha1" | ||
) | ||
|
||
var _ handler.EventHandler = &DeviceHandler{} | ||
|
||
type DeviceHandler struct{} | ||
|
||
func (d *DeviceHandler) Create(e event.CreateEvent, q workqueue.RateLimitingInterface) { | ||
device := e.Object.(*schedulingv1alpha1.Device) | ||
q.Add(reconcile.Request{ | ||
NamespacedName: types.NamespacedName{ | ||
Name: device.Name, | ||
}, | ||
}) | ||
} | ||
|
||
func (d *DeviceHandler) Update(e event.UpdateEvent, q workqueue.RateLimitingInterface) { | ||
newDevice := e.ObjectNew.(*schedulingv1alpha1.Device) | ||
oldDevice := e.ObjectOld.(*schedulingv1alpha1.Device) | ||
if reflect.DeepEqual(newDevice.Spec, oldDevice.Spec) { | ||
return | ||
} | ||
q.Add(reconcile.Request{ | ||
NamespacedName: types.NamespacedName{ | ||
Name: newDevice.Name, | ||
}, | ||
}) | ||
} | ||
|
||
func (d *DeviceHandler) Delete(e event.DeleteEvent, q workqueue.RateLimitingInterface) { | ||
device, ok := e.Object.(*schedulingv1alpha1.Device) | ||
if !ok { | ||
return | ||
} | ||
q.Add(reconcile.Request{ | ||
NamespacedName: types.NamespacedName{ | ||
Name: device.Name, | ||
}, | ||
}) | ||
} | ||
|
||
func (d *DeviceHandler) Generic(e event.GenericEvent, q workqueue.RateLimitingInterface) { | ||
} |
165 changes: 165 additions & 0 deletions
165
pkg/slo-controller/noderesource/plugins/gpudeviceresource/device_event_handler_test.go
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,165 @@ | ||
/* | ||
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 gpudeviceresource | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/util/workqueue" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
||
schedulingv1alpha1 "github.com/koordinator-sh/koordinator/apis/scheduling/v1alpha1" | ||
) | ||
|
||
func Test_EnqueueRequestForNodeMetricMetric(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
fn func(handler handler.EventHandler, q workqueue.RateLimitingInterface) | ||
hasEvent bool | ||
eventName string | ||
}{ | ||
{ | ||
name: "create device event", | ||
fn: func(handler handler.EventHandler, q workqueue.RateLimitingInterface) { | ||
handler.Create(event.CreateEvent{ | ||
Object: &schedulingv1alpha1.Device{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "node1", | ||
}, | ||
}, | ||
}, q) | ||
}, | ||
hasEvent: true, | ||
eventName: "node1", | ||
}, | ||
{ | ||
name: "delete device event", | ||
fn: func(handler handler.EventHandler, q workqueue.RateLimitingInterface) { | ||
handler.Delete(event.DeleteEvent{ | ||
Object: &schedulingv1alpha1.Device{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "node1", | ||
}, | ||
}, | ||
}, q) | ||
}, | ||
hasEvent: true, | ||
eventName: "node1", | ||
}, | ||
{ | ||
name: "delete event not device", | ||
fn: func(handler handler.EventHandler, q workqueue.RateLimitingInterface) { | ||
handler.Delete(event.DeleteEvent{ | ||
Object: &corev1.Node{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "node1", | ||
}, | ||
}, | ||
}, q) | ||
}, | ||
hasEvent: false, | ||
}, | ||
{ | ||
name: "generic event ignore", | ||
fn: func(handler handler.EventHandler, q workqueue.RateLimitingInterface) { | ||
handler.Generic(event.GenericEvent{}, q) | ||
}, | ||
hasEvent: false, | ||
}, | ||
{ | ||
name: "update device event", | ||
fn: func(handler handler.EventHandler, q workqueue.RateLimitingInterface) { | ||
handler.Update(event.UpdateEvent{ | ||
ObjectOld: &schedulingv1alpha1.Device{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "node1", | ||
ResourceVersion: "100", | ||
}, | ||
Spec: schedulingv1alpha1.DeviceSpec{ | ||
Devices: []schedulingv1alpha1.DeviceInfo{ | ||
{}, | ||
}, | ||
}, | ||
}, | ||
ObjectNew: &schedulingv1alpha1.Device{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "node1", | ||
ResourceVersion: "101", | ||
}, | ||
Spec: schedulingv1alpha1.DeviceSpec{ | ||
Devices: []schedulingv1alpha1.DeviceInfo{ | ||
{}, | ||
{}, | ||
}, | ||
}, | ||
}, | ||
}, q) | ||
}, | ||
hasEvent: true, | ||
eventName: "node1", | ||
}, | ||
{ | ||
name: "update device event ignore", | ||
fn: func(handler handler.EventHandler, q workqueue.RateLimitingInterface) { | ||
handler.Update(event.UpdateEvent{ | ||
ObjectOld: &schedulingv1alpha1.Device{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "node1", | ||
ResourceVersion: "100", | ||
}, | ||
Spec: schedulingv1alpha1.DeviceSpec{ | ||
Devices: []schedulingv1alpha1.DeviceInfo{ | ||
{}, | ||
}, | ||
}, | ||
}, | ||
ObjectNew: &schedulingv1alpha1.Device{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "node1", | ||
ResourceVersion: "100", | ||
}, | ||
Spec: schedulingv1alpha1.DeviceSpec{ | ||
Devices: []schedulingv1alpha1.DeviceInfo{ | ||
{}, | ||
}, | ||
}, | ||
}, | ||
}, q) | ||
}, | ||
hasEvent: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
queue := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()) | ||
h := &DeviceHandler{} | ||
tt.fn(h, queue) | ||
assert.Equal(t, tt.hasEvent, queue.Len() > 0, "unexpected event") | ||
if tt.hasEvent { | ||
assert.True(t, queue.Len() >= 0, "expected event") | ||
e, _ := queue.Get() | ||
assert.Equal(t, tt.eventName, e.(reconcile.Request).Name) | ||
} | ||
}) | ||
} | ||
|
||
} |
Oops, something went wrong.