mirror of
https://github.com/kubernetes-sigs/node-feature-discovery.git
synced 2025-03-15 04:57:56 +00:00
topology-updater: Refactor Scan to expand response
We are gonna add new data to Scan response so better introduce a new ScanResponse struct as Scan return value to make it easier.
This commit is contained in:
parent
69440d7820
commit
1a687cb286
4 changed files with 39 additions and 35 deletions
|
@ -154,13 +154,13 @@ func (w *nfdTopologyUpdater) Run() error {
|
||||||
select {
|
select {
|
||||||
case <-crTrigger.C:
|
case <-crTrigger.C:
|
||||||
klog.Infof("Scanning")
|
klog.Infof("Scanning")
|
||||||
podResources, err := resScan.Scan()
|
scanResponse, err := resScan.Scan()
|
||||||
utils.KlogDump(1, "podResources are", " ", podResources)
|
utils.KlogDump(1, "podResources are", " ", scanResponse.PodResources)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Warningf("Scan failed: %v", err)
|
klog.Warningf("Scan failed: %v", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
zones = resAggr.Aggregate(podResources)
|
zones = resAggr.Aggregate(scanResponse.PodResources)
|
||||||
utils.KlogDump(1, "After aggregating resources identified zones are", " ", zones)
|
utils.KlogDump(1, "After aggregating resources identified zones are", " ", zones)
|
||||||
if !w.args.NoPublish {
|
if !w.args.NoPublish {
|
||||||
if err = w.updateNodeResourceTopology(zones); err != nil {
|
if err = w.updateNodeResourceTopology(zones); err != nil {
|
||||||
|
|
|
@ -113,14 +113,14 @@ func hasIntegralCPUs(pod *corev1.Pod, container *corev1.Container) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scan gathers all the PodResources from the system, using the podresources API client.
|
// Scan gathers all the PodResources from the system, using the podresources API client.
|
||||||
func (resMon *PodResourcesScanner) Scan() ([]PodResources, error) {
|
func (resMon *PodResourcesScanner) Scan() (ScanResponse, error) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), defaultPodResourcesTimeout)
|
ctx, cancel := context.WithTimeout(context.Background(), defaultPodResourcesTimeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
// Pod Resource API client
|
// Pod Resource API client
|
||||||
resp, err := resMon.podResourceClient.List(ctx, &podresourcesapi.ListPodResourcesRequest{})
|
resp, err := resMon.podResourceClient.List(ctx, &podresourcesapi.ListPodResourcesRequest{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("can't receive response: %v.Get(_) = _, %w", resMon.podResourceClient, err)
|
return ScanResponse{}, fmt.Errorf("can't receive response: %v.Get(_) = _, %w", resMon.podResourceClient, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var podResData []PodResources
|
var podResData []PodResources
|
||||||
|
@ -130,7 +130,7 @@ func (resMon *PodResourcesScanner) Scan() ([]PodResources, error) {
|
||||||
hasDevice := hasDevice(podResource)
|
hasDevice := hasDevice(podResource)
|
||||||
isWatchable, isIntegralGuaranteed, err := resMon.isWatchable(podResource.GetNamespace(), podResource.GetName(), hasDevice)
|
isWatchable, isIntegralGuaranteed, err := resMon.isWatchable(podResource.GetNamespace(), podResource.GetName(), hasDevice)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("checking if pod in a namespace is watchable, namespace:%v, pod name %v: %v", podResource.GetNamespace(), podResource.GetName(), err)
|
return ScanResponse{}, fmt.Errorf("checking if pod in a namespace is watchable, namespace:%v, pod name %v: %v", podResource.GetNamespace(), podResource.GetName(), err)
|
||||||
}
|
}
|
||||||
if !isWatchable {
|
if !isWatchable {
|
||||||
continue
|
continue
|
||||||
|
@ -198,7 +198,7 @@ func (resMon *PodResourcesScanner) Scan() ([]PodResources, error) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return podResData, nil
|
return ScanResponse{PodResources: podResData}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func hasDevice(podResource *podresourcesapi.PodResources) bool {
|
func hasDevice(podResource *podresourcesapi.PodResources) bool {
|
||||||
|
|
|
@ -58,7 +58,7 @@ func TestPodScanner(t *testing.T) {
|
||||||
So(err, ShouldNotBeNil)
|
So(err, ShouldNotBeNil)
|
||||||
})
|
})
|
||||||
Convey("Return PodResources should be nil", func() {
|
Convey("Return PodResources should be nil", func() {
|
||||||
So(res, ShouldBeNil)
|
So(res.PodResources, ShouldBeNil)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -70,7 +70,7 @@ func TestPodScanner(t *testing.T) {
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
})
|
})
|
||||||
Convey("Return PodResources should be zero", func() {
|
Convey("Return PodResources should be zero", func() {
|
||||||
So(len(res), ShouldEqual, 0)
|
So(len(res.PodResources), ShouldEqual, 0)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -160,7 +160,7 @@ func TestPodScanner(t *testing.T) {
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
})
|
})
|
||||||
Convey("Return PodResources should have values", func() {
|
Convey("Return PodResources should have values", func() {
|
||||||
So(len(res), ShouldBeGreaterThan, 0)
|
So(len(res.PodResources), ShouldBeGreaterThan, 0)
|
||||||
|
|
||||||
expected := []PodResources{
|
expected := []PodResources{
|
||||||
{
|
{
|
||||||
|
@ -194,14 +194,14 @@ func TestPodScanner(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, podresource := range res {
|
for _, podresource := range res.PodResources {
|
||||||
for _, container := range podresource.Containers {
|
for _, container := range podresource.Containers {
|
||||||
sort.Slice(res, func(i, j int) bool {
|
sort.Slice(res.PodResources, func(i, j int) bool {
|
||||||
return container.Resources[i].Name < container.Resources[j].Name
|
return container.Resources[i].Name < container.Resources[j].Name
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
So(reflect.DeepEqual(res, expected), ShouldBeTrue)
|
So(reflect.DeepEqual(res.PodResources, expected), ShouldBeTrue)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -266,7 +266,7 @@ func TestPodScanner(t *testing.T) {
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
})
|
})
|
||||||
Convey("Return PodResources should have values", func() {
|
Convey("Return PodResources should have values", func() {
|
||||||
So(len(res), ShouldBeGreaterThan, 0)
|
So(len(res.PodResources), ShouldBeGreaterThan, 0)
|
||||||
|
|
||||||
expected := []PodResources{
|
expected := []PodResources{
|
||||||
{
|
{
|
||||||
|
@ -290,7 +290,7 @@ func TestPodScanner(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
So(reflect.DeepEqual(res, expected), ShouldBeTrue)
|
So(reflect.DeepEqual(res.PodResources, expected), ShouldBeTrue)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -345,7 +345,7 @@ func TestPodScanner(t *testing.T) {
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
})
|
})
|
||||||
Convey("Return PodResources should have values", func() {
|
Convey("Return PodResources should have values", func() {
|
||||||
So(len(res), ShouldBeGreaterThan, 0)
|
So(len(res.PodResources), ShouldBeGreaterThan, 0)
|
||||||
|
|
||||||
expected := []PodResources{
|
expected := []PodResources{
|
||||||
{
|
{
|
||||||
|
@ -365,7 +365,7 @@ func TestPodScanner(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
So(reflect.DeepEqual(res, expected), ShouldBeTrue)
|
So(reflect.DeepEqual(res.PodResources, expected), ShouldBeTrue)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -427,7 +427,7 @@ func TestPodScanner(t *testing.T) {
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
})
|
})
|
||||||
Convey("Return PodResources should have values", func() {
|
Convey("Return PodResources should have values", func() {
|
||||||
So(len(res), ShouldBeGreaterThan, 0)
|
So(len(res.PodResources), ShouldBeGreaterThan, 0)
|
||||||
|
|
||||||
expected := []PodResources{
|
expected := []PodResources{
|
||||||
{
|
{
|
||||||
|
@ -447,7 +447,7 @@ func TestPodScanner(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
So(reflect.DeepEqual(res, expected), ShouldBeTrue)
|
So(reflect.DeepEqual(res.PodResources, expected), ShouldBeTrue)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -505,7 +505,7 @@ func TestPodScanner(t *testing.T) {
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
})
|
})
|
||||||
Convey("Return PodResources should have values", func() {
|
Convey("Return PodResources should have values", func() {
|
||||||
So(len(res), ShouldBeGreaterThan, 0)
|
So(len(res.PodResources), ShouldBeGreaterThan, 0)
|
||||||
})
|
})
|
||||||
|
|
||||||
expected := []PodResources{
|
expected := []PodResources{
|
||||||
|
@ -526,7 +526,7 @@ func TestPodScanner(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
So(reflect.DeepEqual(res, expected), ShouldBeTrue)
|
So(reflect.DeepEqual(res.PodResources, expected), ShouldBeTrue)
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("When I successfully get valid response for (non-guaranteed) pods with devices with cpus", func() {
|
Convey("When I successfully get valid response for (non-guaranteed) pods with devices with cpus", func() {
|
||||||
|
@ -589,7 +589,7 @@ func TestPodScanner(t *testing.T) {
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
})
|
})
|
||||||
Convey("Return PodResources should have values", func() {
|
Convey("Return PodResources should have values", func() {
|
||||||
So(len(res), ShouldBeGreaterThan, 0)
|
So(len(res.PodResources), ShouldBeGreaterThan, 0)
|
||||||
})
|
})
|
||||||
|
|
||||||
expected := []PodResources{
|
expected := []PodResources{
|
||||||
|
@ -609,7 +609,7 @@ func TestPodScanner(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
So(reflect.DeepEqual(res, expected), ShouldBeTrue)
|
So(reflect.DeepEqual(res.PodResources, expected), ShouldBeTrue)
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
@ -632,7 +632,7 @@ func TestPodScanner(t *testing.T) {
|
||||||
So(err, ShouldNotBeNil)
|
So(err, ShouldNotBeNil)
|
||||||
})
|
})
|
||||||
Convey("Return PodResources should be nil", func() {
|
Convey("Return PodResources should be nil", func() {
|
||||||
So(res, ShouldBeNil)
|
So(res.PodResources, ShouldBeNil)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -644,7 +644,7 @@ func TestPodScanner(t *testing.T) {
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
})
|
})
|
||||||
Convey("Return PodResources should be zero", func() {
|
Convey("Return PodResources should be zero", func() {
|
||||||
So(len(res), ShouldEqual, 0)
|
So(len(res.PodResources), ShouldEqual, 0)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -712,7 +712,7 @@ func TestPodScanner(t *testing.T) {
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
})
|
})
|
||||||
Convey("Return PodResources should be zero", func() {
|
Convey("Return PodResources should be zero", func() {
|
||||||
So(len(res), ShouldEqual, 0)
|
So(len(res.PodResources), ShouldEqual, 0)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -778,7 +778,7 @@ func TestPodScanner(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("Return PodResources should have values", func() {
|
Convey("Return PodResources should have values", func() {
|
||||||
So(len(res), ShouldBeGreaterThan, 0)
|
So(len(res.PodResources), ShouldBeGreaterThan, 0)
|
||||||
|
|
||||||
expected := []PodResources{
|
expected := []PodResources{
|
||||||
{
|
{
|
||||||
|
@ -802,7 +802,7 @@ func TestPodScanner(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
So(reflect.DeepEqual(res, expected), ShouldBeTrue)
|
So(reflect.DeepEqual(res.PodResources, expected), ShouldBeTrue)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -858,7 +858,7 @@ func TestPodScanner(t *testing.T) {
|
||||||
})
|
})
|
||||||
Convey("Return PodResources should have values", func() {
|
Convey("Return PodResources should have values", func() {
|
||||||
Convey("Return PodResources should be zero", func() {
|
Convey("Return PodResources should be zero", func() {
|
||||||
So(len(res), ShouldEqual, 0)
|
So(len(res.PodResources), ShouldEqual, 0)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -923,7 +923,7 @@ func TestPodScanner(t *testing.T) {
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
})
|
})
|
||||||
Convey("Return PodResources should have values", func() {
|
Convey("Return PodResources should have values", func() {
|
||||||
So(len(res), ShouldEqual, 0)
|
So(len(res.PodResources), ShouldEqual, 0)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -981,7 +981,7 @@ func TestPodScanner(t *testing.T) {
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
})
|
})
|
||||||
Convey("Return PodResources should have values", func() {
|
Convey("Return PodResources should have values", func() {
|
||||||
So(len(res), ShouldBeGreaterThan, 0)
|
So(len(res.PodResources), ShouldBeGreaterThan, 0)
|
||||||
})
|
})
|
||||||
|
|
||||||
expected := []PodResources{
|
expected := []PodResources{
|
||||||
|
@ -1002,7 +1002,7 @@ func TestPodScanner(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
So(reflect.DeepEqual(res, expected), ShouldBeTrue)
|
So(reflect.DeepEqual(res.PodResources, expected), ShouldBeTrue)
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("When I successfully get valid response for (non-guaranteed) pods with devices with cpus", func() {
|
Convey("When I successfully get valid response for (non-guaranteed) pods with devices with cpus", func() {
|
||||||
|
@ -1065,7 +1065,7 @@ func TestPodScanner(t *testing.T) {
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
})
|
})
|
||||||
Convey("Return PodResources should have values", func() {
|
Convey("Return PodResources should have values", func() {
|
||||||
So(len(res), ShouldBeGreaterThan, 0)
|
So(len(res.PodResources), ShouldBeGreaterThan, 0)
|
||||||
})
|
})
|
||||||
|
|
||||||
expected := []PodResources{
|
expected := []PodResources{
|
||||||
|
@ -1085,7 +1085,7 @@ func TestPodScanner(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
So(reflect.DeepEqual(res, expected), ShouldBeTrue)
|
So(reflect.DeepEqual(res.PodResources, expected), ShouldBeTrue)
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
|
@ -53,9 +53,13 @@ type PodResources struct {
|
||||||
Containers []ContainerResources
|
Containers []ContainerResources
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ScanResponse struct {
|
||||||
|
PodResources []PodResources
|
||||||
|
}
|
||||||
|
|
||||||
// ResourcesScanner gathers all the PodResources from the system, using the podresources API client
|
// ResourcesScanner gathers all the PodResources from the system, using the podresources API client
|
||||||
type ResourcesScanner interface {
|
type ResourcesScanner interface {
|
||||||
Scan() ([]PodResources, error)
|
Scan() (ScanResponse, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResourcesAggregator aggregates resource information based on the received data from underlying hardware and podresource API
|
// ResourcesAggregator aggregates resource information based on the received data from underlying hardware and podresource API
|
||||||
|
|
Loading…
Add table
Reference in a new issue