forked from kubernetes-sigs/scheduler-plugins
-
Notifications
You must be signed in to change notification settings - Fork 4
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 #213 from openshift-kni/resync-20240624-4.16
[KNI][release-4.16][manual] resync 20240624
- Loading branch information
Showing
21 changed files
with
1,035 additions
and
289 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
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,99 @@ | ||
/* | ||
Copyright 2024 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 ( | ||
"context" | ||
|
||
"github.com/go-logr/logr" | ||
|
||
"k8s.io/apimachinery/pkg/watch" | ||
|
||
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
topologyv1alpha2 "github.com/k8stopologyawareschedwg/noderesourcetopology-api/pkg/apis/topology/v1alpha2" | ||
|
||
"sigs.k8s.io/scheduler-plugins/pkg/noderesourcetopology/logging" | ||
"sigs.k8s.io/scheduler-plugins/pkg/noderesourcetopology/nodeconfig" | ||
) | ||
|
||
type Watcher struct { | ||
lh logr.Logger | ||
nrts *nrtStore | ||
nodes counter | ||
} | ||
|
||
func (wt Watcher) NodeResourceTopologies(ctx context.Context, client ctrlclient.WithWatch) { | ||
done := false | ||
for !done { | ||
wt.lh.Info("start watching NRT objects") | ||
|
||
nrtObjs := topologyv1alpha2.NodeResourceTopologyList{} | ||
wa, err := client.Watch(ctx, &nrtObjs) | ||
if err != nil { | ||
wt.lh.Error(err, "cannot watch NRT objects") | ||
return | ||
} | ||
|
||
for !done { | ||
select { | ||
case ev := <-wa.ResultChan(): | ||
wt.ProcessEvent(ev) | ||
|
||
case <-ctx.Done(): | ||
wt.lh.Info("stop watching NRT objects") | ||
wa.Stop() | ||
done = true | ||
} | ||
} | ||
|
||
wt.lh.Info("done watching NRT objects") | ||
} | ||
} | ||
|
||
func (wt Watcher) ProcessEvent(ev watch.Event) bool { | ||
if ev.Type != watch.Modified { | ||
return false | ||
} | ||
|
||
nrtObj, ok := ev.Object.(*topologyv1alpha2.NodeResourceTopology) | ||
if !ok { | ||
wt.lh.Info("unexpected object %T", ev.Object) | ||
return false | ||
} | ||
|
||
nrtCur := wt.nrts.GetNRTCopyByNodeName(nrtObj.Name) | ||
if nrtCur == nil { | ||
wt.lh.Info("modified non-existent NRT", logging.KeyNode, nrtObj.Name) | ||
return false | ||
} | ||
|
||
if !areAttrsChanged(nrtCur, nrtObj) { | ||
return false | ||
} | ||
|
||
wt.lh.V(4).Info("attribute change", logging.KeyNode, nrtObj.Name) | ||
wt.nodes.Incr(nrtObj.Name) | ||
return true | ||
} | ||
|
||
func areAttrsChanged(oldNrt, newNrt *topologyv1alpha2.NodeResourceTopology) bool { | ||
lh := logr.Discard() // avoid spam in the logs | ||
oldConf := nodeconfig.TopologyManagerFromNodeResourceTopology(lh, oldNrt) | ||
newConf := nodeconfig.TopologyManagerFromNodeResourceTopology(lh, newNrt) | ||
return !oldConf.Equal(newConf) | ||
} |
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,153 @@ | ||
/* | ||
Copyright 2024 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 ( | ||
"reflect" | ||
"testing" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/watch" | ||
|
||
"k8s.io/klog/v2" | ||
|
||
topologyv1alpha2 "github.com/k8stopologyawareschedwg/noderesourcetopology-api/pkg/apis/topology/v1alpha2" | ||
) | ||
|
||
func TestWatcherProcessEvent(t *testing.T) { | ||
nrts := []topologyv1alpha2.NodeResourceTopology{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "node-0", | ||
}, | ||
Attributes: []topologyv1alpha2.AttributeInfo{ | ||
{ | ||
Name: "topologyManagerScope", | ||
Value: "pod", | ||
}, | ||
{ | ||
Name: "topologyManagerPolicy", | ||
Value: "restricted", | ||
}, | ||
}, | ||
}, | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "node-1", | ||
}, | ||
Attributes: []topologyv1alpha2.AttributeInfo{ | ||
{ | ||
Name: "topologyManagerScope", | ||
Value: "container", | ||
}, | ||
{ | ||
Name: "topologyManagerPolicy", | ||
Value: "single-numa-node", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
tcases := []struct { | ||
description string | ||
ev watch.Event | ||
expected []string | ||
}{ | ||
{ | ||
description: "irrelevant object", | ||
ev: watch.Event{ | ||
Type: watch.Added, | ||
Object: &topologyv1alpha2.NodeResourceTopology{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "node-7", | ||
}, | ||
Attributes: []topologyv1alpha2.AttributeInfo{ | ||
{ | ||
Name: "topologyManagerScope", | ||
Value: "container", | ||
}, | ||
{ | ||
Name: "topologyManagerPolicy", | ||
Value: "single-numa-node", | ||
}, | ||
}, | ||
}, | ||
}, | ||
expected: []string{}, | ||
}, | ||
{ | ||
description: "scope change", | ||
ev: watch.Event{ | ||
Type: watch.Modified, | ||
Object: &topologyv1alpha2.NodeResourceTopology{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "node-0", | ||
}, | ||
Attributes: []topologyv1alpha2.AttributeInfo{ | ||
{ | ||
Name: "topologyManagerScope", | ||
Value: "container", | ||
}, | ||
{ | ||
Name: "topologyManagerPolicy", | ||
Value: "restricted", | ||
}, | ||
}, | ||
}, | ||
}, | ||
expected: []string{"node-0"}, | ||
}, | ||
{ | ||
description: "all attr change", | ||
ev: watch.Event{ | ||
Type: watch.Modified, | ||
Object: &topologyv1alpha2.NodeResourceTopology{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "node-0", | ||
}, | ||
Attributes: []topologyv1alpha2.AttributeInfo{ | ||
{ | ||
Name: "topologyManagerScope", | ||
Value: "container", | ||
}, | ||
{ | ||
Name: "topologyManagerPolicy", | ||
Value: "single-numa-node", | ||
}, | ||
}, | ||
}, | ||
}, | ||
expected: []string{"node-0"}, | ||
}, | ||
} | ||
|
||
wt := Watcher{ | ||
lh: klog.Background(), | ||
nrts: newNrtStore(klog.Background(), nrts), | ||
nodes: newCounter(), | ||
} | ||
|
||
for _, tcase := range tcases { | ||
t.Run(tcase.description, func(t *testing.T) { | ||
wt.ProcessEvent(tcase.ev) | ||
got := wt.nodes.Keys() | ||
if !reflect.DeepEqual(got, tcase.expected) { | ||
t.Errorf("got=%+v expected=%+v", got, tcase.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
Oops, something went wrong.