Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7894315

Browse files
committedMar 4, 2025··
Make resctrl optional/pluggable
Signed-off-by: Davanum Srinivas <[email protected]>
1 parent b518a3d commit 7894315

14 files changed

+113
-17
lines changed
 

‎cmd/cadvisor.go

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ import (
4141
_ "github.com/google/cadvisor/utils/cloudinfo/azure"
4242
_ "github.com/google/cadvisor/utils/cloudinfo/gce"
4343

44+
// Register resctrl plugin
45+
_ "github.com/google/cadvisor/resctrl/intel/install"
46+
4447
"k8s.io/klog/v2"
4548
)
4649

‎manager/manager.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func New(memoryCache *memory.InMemoryCache, sysfs sysfs.SysFs, HousekeepingConfi
221221
return nil, err
222222
}
223223

224-
newManager.resctrlManager, err = resctrl.NewManager(resctrlInterval, resctrl.Setup, machineInfo.CPUVendorID, inHostNamespace)
224+
newManager.resctrlManager, err = resctrl.NewManager(resctrlInterval, machineInfo.CPUVendorID, inHostNamespace)
225225
if err != nil {
226226
klog.V(4).Infof("Cannot gather resctrl metrics: %v", err)
227227
}
@@ -265,7 +265,7 @@ type manager struct {
265265
eventsChannel chan watcher.ContainerEvent
266266
collectorHTTPClient *http.Client
267267
perfManager stats.Manager
268-
resctrlManager resctrl.Manager
268+
resctrlManager resctrl.ResControlManager
269269
// List of raw container cgroup path prefix whitelist.
270270
rawContainerCgroupPathPrefixWhiteList []string
271271
// List of container env prefix whitelist, the matched container envs would be collected into metrics as extra labels.

‎resctrl/factory.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2025 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package resctrl
16+
17+
import (
18+
"fmt"
19+
"sync"
20+
"time"
21+
22+
"github.com/google/cadvisor/stats"
23+
24+
"k8s.io/klog/v2"
25+
)
26+
27+
type ResControlManager interface {
28+
Destroy()
29+
GetCollector(containerName string, getContainerPids func() ([]string, error), numberOfNUMANodes int) (stats.Collector, error)
30+
}
31+
32+
// All registered auth provider plugins.
33+
var pluginsLock sync.Mutex
34+
var plugins = make(map[string]ResControlManagerPlugin)
35+
36+
type ResControlManagerPlugin interface {
37+
NewManager(interval time.Duration, vendorID string, inHostNamespace bool) (ResControlManager, error)
38+
}
39+
40+
func RegisterPlugin(name string, plugin ResControlManagerPlugin) error {
41+
pluginsLock.Lock()
42+
defer pluginsLock.Unlock()
43+
if _, found := plugins[name]; found {
44+
return fmt.Errorf("ResControlManagerPlugin %q was registered twice", name)
45+
}
46+
klog.V(4).Infof("Registered ResControlManagerPlugin %q", name)
47+
plugins[name] = plugin
48+
return nil
49+
}
50+
51+
func NewManager(interval time.Duration, vendorID string, inHostNamespace bool) (ResControlManager, error) {
52+
pluginsLock.Lock()
53+
defer pluginsLock.Unlock()
54+
for _, plugin := range plugins {
55+
return plugin.NewManager(interval, vendorID, inHostNamespace)
56+
}
57+
return nil, fmt.Errorf("unable to find plugins for resctrl manager")
58+
}

‎resctrl/collector.go renamed to ‎resctrl/intel/collector.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// limitations under the License.
1717

1818
// Collector of resctrl for a container.
19-
package resctrl
19+
package intel
2020

2121
import (
2222
"fmt"

‎resctrl/collector_test.go renamed to ‎resctrl/intel/collector_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// limitations under the License.
1717

1818
// Collector tests.
19-
package resctrl
19+
package intel
2020

2121
import (
2222
"fmt"

‎resctrl/intel/install/install.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2025 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package install
16+
17+
import (
18+
"time"
19+
20+
"github.com/google/cadvisor/resctrl"
21+
"github.com/google/cadvisor/resctrl/intel"
22+
23+
"k8s.io/klog/v2"
24+
)
25+
26+
type managerplugin struct {
27+
}
28+
29+
func (m *managerplugin) NewManager(interval time.Duration, vendorID string, inHostNamespace bool) (resctrl.ResControlManager, error) {
30+
return intel.NewManager(interval, intel.Setup, vendorID, inHostNamespace)
31+
}
32+
33+
func init() {
34+
err := resctrl.RegisterPlugin("intel", &managerplugin{})
35+
if err != nil {
36+
klog.Fatalf("Failed to register intel resctrl plugin: %v", err)
37+
}
38+
}

‎resctrl/manager.go renamed to ‎resctrl/intel/manager.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
// See the License for the specific language governing permissions and
1616
// limitations under the License.
1717

18-
// Manager of resctrl for containers.
19-
package resctrl
18+
// ResControlManager of resctrl for containers.
19+
package intel
2020

2121
import (
2222
"errors"
23+
"github.com/google/cadvisor/resctrl"
2324
"time"
2425

2526
"k8s.io/klog/v2"
@@ -28,11 +29,6 @@ import (
2829
"github.com/google/cadvisor/stats"
2930
)
3031

31-
type Manager interface {
32-
Destroy()
33-
GetCollector(containerName string, getContainerPids func() ([]string, error), numberOfNUMANodes int) (stats.Collector, error)
34-
}
35-
3632
type manager struct {
3733
stats.NoopDestroy
3834
interval time.Duration
@@ -50,7 +46,7 @@ func (m *manager) GetCollector(containerName string, getContainerPids func() ([]
5046
return collector, nil
5147
}
5248

53-
func NewManager(interval time.Duration, setup func() error, vendorID string, inHostNamespace bool) (Manager, error) {
49+
func NewManager(interval time.Duration, setup func() error, vendorID string, inHostNamespace bool) (resctrl.ResControlManager, error) {
5450
err := setup()
5551
if err != nil {
5652
return &NoopManager{}, err

‎resctrl/manager_test.go renamed to ‎resctrl/intel/manager_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
// See the License for the specific language governing permissions and
1616
// limitations under the License.
1717

18-
// Manager tests.
19-
package resctrl
18+
// ResControlManager tests.
19+
package intel
2020

2121
import (
22+
"github.com/google/cadvisor/resctrl"
2223
"os"
2324
"testing"
2425

@@ -32,7 +33,7 @@ func TestNewManager(t *testing.T) {
3233
enabledMBM bool
3334
inHostNamespace bool
3435
err string
35-
expected Manager
36+
expected resctrl.ResControlManager
3637
}{
3738
{
3839
true,
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

‎resctrl/utils.go renamed to ‎resctrl/intel/utils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// limitations under the License.
1717

1818
// Utilities.
19-
package resctrl
19+
package intel
2020

2121
import (
2222
"bufio"

‎resctrl/utils_test.go renamed to ‎resctrl/intel/utils_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// Mocked environment:
2121
// - "container" first container with {1, 2, 3} processes.
2222
// - "another" second container with {5, 6} processes.
23-
package resctrl
23+
package intel
2424

2525
import (
2626
"fmt"

0 commit comments

Comments
 (0)
Please sign in to comment.