generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 542
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #439 from fromanirh/nrt-reserve-impl-enh
Add local cache for noderesourcetopology using the reserve plugin [3/3] - enhancements
- Loading branch information
Showing
16 changed files
with
505 additions
and
41 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
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,81 @@ | ||
/* | ||
Copyright 2022 The Kubernetes 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 cache | ||
|
||
import ( | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
k8scache "k8s.io/client-go/tools/cache" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
// The nodeIndexer and the go client facilities are global objects, so we need this to be global as well. | ||
// Each profile must register its name in order to support foreign pods detection. Foreign pods are pods | ||
// which are scheduled to nodes without the caching machinery knowning. | ||
// Note this is NOT lock-protected because the scheduling framework calls New() sequentially, | ||
// and plugin instances are meant to register their name using SetupForeignPodsDetector inside their New() | ||
var ( | ||
schedProfileNames = sets.String{} | ||
) | ||
|
||
func SetupForeignPodsDetector(schedProfileName string, podInformer k8scache.SharedInformer, cc Interface) { | ||
foreignCache := func(obj interface{}) { | ||
pod, ok := obj.(*corev1.Pod) | ||
if !ok { | ||
klog.V(3).InfoS("nrtcache: foreign: unsupported object %T", obj) | ||
return | ||
} | ||
if !IsForeignPod(pod) { | ||
return | ||
} | ||
|
||
cc.NodeHasForeignPods(pod.Spec.NodeName, pod) | ||
klog.V(6).InfoS("nrtcache: has foreign pods", "logID", klog.KObj(pod), "node", pod.Spec.NodeName, "podUID", pod.UID) | ||
} | ||
|
||
podInformer.AddEventHandler(k8scache.ResourceEventHandlerFuncs{ | ||
AddFunc: foreignCache, | ||
UpdateFunc: func(oldObj, newObj interface{}) { | ||
foreignCache(newObj) | ||
}, | ||
DeleteFunc: foreignCache, | ||
}) | ||
} | ||
|
||
func RegisterSchedulerProfileName(schedProfileName string) { | ||
klog.InfoS("nrtcache: setting up foreign pod detection", "profile", schedProfileName) | ||
schedProfileNames.Insert(schedProfileName) | ||
|
||
klog.V(5).InfoS("nrtcache: registered scheduler profiles", "names", schedProfileNames.List()) | ||
} | ||
|
||
func IsForeignPod(pod *corev1.Pod) bool { | ||
if pod.Spec.NodeName == "" { | ||
// nothing to do yet | ||
return false | ||
} | ||
if schedProfileNames.Has(pod.Spec.SchedulerName) { | ||
// nothing to do here - we know already about this pod | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
// for testing only; NOT thread safe | ||
func CleanRegisteredSchedulerProfileNames() { | ||
schedProfileNames = sets.String{} | ||
} |
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,109 @@ | ||
/* | ||
Copyright 2022 The Kubernetes 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 cache | ||
|
||
import ( | ||
"testing" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestIsForeignPod(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
profileNames []string | ||
pod *corev1.Pod | ||
expected bool | ||
}{ | ||
{ | ||
name: "empty", | ||
pod: &corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "pod", | ||
Namespace: "default", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "no-node-no-profile", | ||
profileNames: []string{"secondary-scheduler"}, | ||
pod: &corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "pod", | ||
Namespace: "default", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "node-no-profile", | ||
profileNames: []string{"secondary-scheduler"}, | ||
pod: &corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "pod", | ||
Namespace: "default", | ||
}, | ||
Spec: corev1.PodSpec{ | ||
NodeName: "random-node", | ||
}, | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "node-profile", | ||
profileNames: []string{"secondary-scheduler"}, | ||
pod: &corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "pod", | ||
Namespace: "default", | ||
}, | ||
Spec: corev1.PodSpec{ | ||
NodeName: "random-node", | ||
SchedulerName: "secondary-scheduler", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "node-multi-profile", | ||
profileNames: []string{"secondary-scheduler-A", "secondary-scheduler-B", "fancy-scheduler"}, | ||
pod: &corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "pod", | ||
Namespace: "default", | ||
}, | ||
Spec: corev1.PodSpec{ | ||
NodeName: "random-node", | ||
SchedulerName: "secondary-scheduler-B", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
for _, profileName := range tt.profileNames { | ||
RegisterSchedulerProfileName(profileName) | ||
} | ||
defer CleanRegisteredSchedulerProfileNames() | ||
|
||
got := IsForeignPod(tt.pod) | ||
if got != tt.expected { | ||
t.Errorf("pod %s foreign status got %v expected %v", tt.pod.Name, got, tt.expected) | ||
} | ||
}) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -162,7 +162,6 @@ func TestNodeNameIndexerByIndexAdd(t *testing.T) { | |
} | ||
}) | ||
} | ||
|
||
} | ||
|
||
const ( | ||
|
Oops, something went wrong.