Skip to content

Commit 7098da7

Browse files
committed
test: migrate networkaware testing to ctrl runtime
1 parent 9096af8 commit 7098da7

File tree

9 files changed

+225
-213
lines changed

9 files changed

+225
-213
lines changed

pkg/networkaware/networkoverhead/networkoverhead.go

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"k8s.io/apimachinery/pkg/runtime"
2828
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2929
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
30+
corelisters "k8s.io/client-go/listers/core/v1"
3031
"k8s.io/klog/v2"
3132
"k8s.io/kubernetes/pkg/scheduler/framework"
3233

@@ -35,8 +36,8 @@ import (
3536
pluginconfig "sigs.k8s.io/scheduler-plugins/apis/config"
3637
networkawareutil "sigs.k8s.io/scheduler-plugins/pkg/networkaware/util"
3738

38-
appgroupv1a1 "github.com/diktyo-io/appgroup-api/pkg/apis/appgroup/v1alpha1"
39-
nettopov1a1 "github.com/diktyo-io/networktopology-api/pkg/apis/networktopology/v1alpha1"
39+
agv1alpha1 "github.com/diktyo-io/appgroup-api/pkg/apis/appgroup/v1alpha1"
40+
ntv1alpha1 "github.com/diktyo-io/networktopology-api/pkg/apis/networktopology/v1alpha1"
4041
)
4142

4243
var _ framework.PreFilterPlugin = &NetworkOverhead{}
@@ -64,6 +65,7 @@ const (
6465
type NetworkOverhead struct {
6566
client.Client
6667

68+
podLister corelisters.PodLister
6769
handle framework.Handle
6870
namespaces []string
6971
weightsName string
@@ -79,13 +81,13 @@ type PreFilterState struct {
7981
agName string
8082

8183
// AppGroup CR
82-
appGroup *appgroupv1a1.AppGroup
84+
appGroup *agv1alpha1.AppGroup
8385

8486
// NetworkTopology CR
85-
networkTopology *nettopov1a1.NetworkTopology
87+
networkTopology *ntv1alpha1.NetworkTopology
8688

8789
// Dependency List of the given pod
88-
dependencyList []appgroupv1a1.DependenciesInfo
90+
dependencyList []agv1alpha1.DependenciesInfo
8991

9092
// Pods already scheduled based on the dependency list
9193
scheduledList networkawareutil.ScheduledList
@@ -140,8 +142,8 @@ func New(obj runtime.Object, handle framework.Handle) (framework.Plugin, error)
140142

141143
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
142144

143-
utilruntime.Must(appgroupv1a1.AddToScheme(scheme))
144-
utilruntime.Must(nettopov1a1.AddToScheme(scheme))
145+
utilruntime.Must(agv1alpha1.AddToScheme(scheme))
146+
utilruntime.Must(ntv1alpha1.AddToScheme(scheme))
145147

146148
client, err := client.New(handle.KubeConfig(), client.Options{
147149
Scheme: scheme,
@@ -151,7 +153,9 @@ func New(obj runtime.Object, handle framework.Handle) (framework.Plugin, error)
151153
}
152154

153155
no := &NetworkOverhead{
154-
Client: client,
156+
Client: client,
157+
158+
podLister: handle.SharedInformerFactory().Core().V1().Pods().Lister(),
155159
handle: handle,
156160
namespaces: args.Namespaces,
157161
weightsName: args.WeightsName,
@@ -199,28 +203,22 @@ func (no *NetworkOverhead) PreFilter(ctx context.Context, state *framework.Cycle
199203
return nil, framework.NewStatus(framework.Success, "Pod has no dependencies, return")
200204
}
201205

202-
podList := &corev1.PodList{}
203-
if err := no.List(ctx, podList,
204-
client.MatchingLabelsSelector{
205-
Selector: labels.Set(map[string]string{
206-
appgroupv1a1.AppGroupLabel: agName,
207-
}).AsSelector(),
208-
}); err != nil {
209-
klog.ErrorS(err, "List pods for group failed")
210-
return nil, framework.NewStatus(
211-
framework.Success, "Error while returning pods from appGroup, return")
206+
// Get pods from lister
207+
selector := labels.Set(map[string]string{agv1alpha1.AppGroupLabel: agName}).AsSelector()
208+
pods, err := no.podLister.List(selector)
209+
if err != nil {
210+
return nil, framework.NewStatus(framework.Success, "Error while returning pods from appGroup, return")
212211
}
213-
pods := podList.Items
214212

215213
// Return if pods are not yet allocated for the AppGroup...
216-
if pods == nil || len(pods) == 0 {
214+
if len(pods) == 0 {
217215
return nil, framework.NewStatus(framework.Success, "No pods yet allocated, return")
218216
}
219217

220218
// Pods already scheduled: Get Scheduled List (Deployment name, replicaID, hostname)
221219
scheduledList := networkawareutil.GetScheduledList(pods)
222220
// Check if scheduledList is empty...
223-
if scheduledList == nil || len(scheduledList) == 0 {
221+
if len(scheduledList) == 0 {
224222
klog.ErrorS(nil, "Scheduled list is empty, return")
225223
return nil, framework.NewStatus(framework.Success, "Scheduled list is empty, return")
226224
}
@@ -432,8 +430,8 @@ func getMinMaxScores(scores framework.NodeScoreList) (int64, int64) {
432430
}
433431

434432
// sortNetworkTopologyCosts : sort costs if manual weights were selected
435-
func (no *NetworkOverhead) sortNetworkTopologyCosts(networkTopology *nettopov1a1.NetworkTopology) {
436-
if no.weightsName != nettopov1a1.NetworkTopologyNetperfCosts { // Manual weights were selected
433+
func (no *NetworkOverhead) sortNetworkTopologyCosts(networkTopology *ntv1alpha1.NetworkTopology) {
434+
if no.weightsName != ntv1alpha1.NetworkTopologyNetperfCosts { // Manual weights were selected
437435
for _, w := range networkTopology.Spec.Weights {
438436
// Sort Costs by TopologyKey, might not be sorted since were manually defined
439437
sort.Sort(networkawareutil.ByTopologyKey(w.TopologyList))
@@ -444,7 +442,7 @@ func (no *NetworkOverhead) sortNetworkTopologyCosts(networkTopology *nettopov1a1
444442
// populateCostMap : Populates costMap based on the node being filtered/scored
445443
func (no *NetworkOverhead) populateCostMap(
446444
costMap map[networkawareutil.CostKey]int64,
447-
networkTopology *nettopov1a1.NetworkTopology,
445+
networkTopology *ntv1alpha1.NetworkTopology,
448446
region string,
449447
zone string) {
450448
for _, w := range networkTopology.Spec.Weights { // Check the weights List
@@ -454,9 +452,9 @@ func (no *NetworkOverhead) populateCostMap(
454452

455453
if region != "" { // Add Region Costs
456454
// Binary search through CostList: find the Topology Key for region
457-
topologyList := networkawareutil.FindTopologyKey(w.TopologyList, nettopov1a1.NetworkTopologyRegion)
455+
topologyList := networkawareutil.FindTopologyKey(w.TopologyList, ntv1alpha1.NetworkTopologyRegion)
458456

459-
if no.weightsName != nettopov1a1.NetworkTopologyNetperfCosts {
457+
if no.weightsName != ntv1alpha1.NetworkTopologyNetperfCosts {
460458
// Sort Costs by origin, might not be sorted since were manually defined
461459
sort.Sort(networkawareutil.ByOrigin(topologyList))
462460
}
@@ -473,9 +471,9 @@ func (no *NetworkOverhead) populateCostMap(
473471
}
474472
if zone != "" { // Add Zone Costs
475473
// Binary search through CostList: find the Topology Key for zone
476-
topologyList := networkawareutil.FindTopologyKey(w.TopologyList, nettopov1a1.NetworkTopologyZone)
474+
topologyList := networkawareutil.FindTopologyKey(w.TopologyList, ntv1alpha1.NetworkTopologyZone)
477475

478-
if no.weightsName != nettopov1a1.NetworkTopologyNetperfCosts {
476+
if no.weightsName != ntv1alpha1.NetworkTopologyNetperfCosts {
479477
// Sort Costs by origin, might not be sorted since were manually defined
480478
sort.Sort(networkawareutil.ByOrigin(topologyList))
481479
}
@@ -496,7 +494,7 @@ func (no *NetworkOverhead) populateCostMap(
496494
// checkMaxNetworkCostRequirements : verifies the number of met and unmet dependencies based on the pod being filtered
497495
func checkMaxNetworkCostRequirements(
498496
scheduledList networkawareutil.ScheduledList,
499-
dependencyList []appgroupv1a1.DependenciesInfo,
497+
dependencyList []agv1alpha1.DependenciesInfo,
500498
nodeInfo *framework.NodeInfo,
501499
region string,
502500
zone string,
@@ -571,7 +569,7 @@ func checkMaxNetworkCostRequirements(
571569
// getAccumulatedCost : calculate the accumulated cost based on the Pod's dependencies
572570
func (no *NetworkOverhead) getAccumulatedCost(
573571
scheduledList networkawareutil.ScheduledList,
574-
dependencyList []appgroupv1a1.DependenciesInfo,
572+
dependencyList []agv1alpha1.DependenciesInfo,
575573
nodeName string,
576574
region string,
577575
zone string,
@@ -647,12 +645,12 @@ func getPreFilterState(cycleState *framework.CycleState) (*PreFilterState, error
647645
return state, nil
648646
}
649647

650-
func (no *NetworkOverhead) findAppGroupNetworkOverhead(agName string) *appgroupv1a1.AppGroup {
648+
func (no *NetworkOverhead) findAppGroupNetworkOverhead(agName string) *agv1alpha1.AppGroup {
651649
klog.V(6).InfoS("namespaces: %s", no.namespaces)
652650
for _, namespace := range no.namespaces {
653651
klog.V(6).InfoS("appGroup CR", "namespace", namespace, "name", agName)
654652
// AppGroup could not be placed in several namespaces simultaneously
655-
appGroup := &appgroupv1a1.AppGroup{}
653+
appGroup := &agv1alpha1.AppGroup{}
656654
err := no.Get(context.TODO(), client.ObjectKey{
657655
Namespace: namespace,
658656
Name: agName,
@@ -668,12 +666,12 @@ func (no *NetworkOverhead) findAppGroupNetworkOverhead(agName string) *appgroupv
668666
return nil
669667
}
670668

671-
func (no *NetworkOverhead) findNetworkTopologyNetworkOverhead() *nettopov1a1.NetworkTopology {
669+
func (no *NetworkOverhead) findNetworkTopologyNetworkOverhead() *ntv1alpha1.NetworkTopology {
672670
klog.V(6).InfoS("namespaces: %s", no.namespaces)
673671
for _, namespace := range no.namespaces {
674672
klog.V(6).InfoS("networkTopology CR:", "namespace", namespace, "name", no.ntName)
675673
// NetworkTopology could not be placed in several namespaces simultaneously
676-
networkTopology := &nettopov1a1.NetworkTopology{}
674+
networkTopology := &ntv1alpha1.NetworkTopology{}
677675
err := no.Get(context.TODO(), client.ObjectKey{
678676
Namespace: namespace,
679677
Name: no.ntName,

0 commit comments

Comments
 (0)