diff --git a/cmd/cadvisor.go b/cmd/cadvisor.go index fc34d967bd..fac72809bd 100644 --- a/cmd/cadvisor.go +++ b/cmd/cadvisor.go @@ -41,6 +41,9 @@ import ( _ "github.com/google/cadvisor/utils/cloudinfo/azure" _ "github.com/google/cadvisor/utils/cloudinfo/gce" + // Register resctrl plugin + _ "github.com/google/cadvisor/resctrl/intel/install" + "k8s.io/klog/v2" ) diff --git a/manager/manager.go b/manager/manager.go index a5a12559b0..7dbb01ecfb 100644 --- a/manager/manager.go +++ b/manager/manager.go @@ -221,7 +221,7 @@ func New(memoryCache *memory.InMemoryCache, sysfs sysfs.SysFs, HousekeepingConfi return nil, err } - newManager.resctrlManager, err = resctrl.NewManager(resctrlInterval, resctrl.Setup, machineInfo.CPUVendorID, inHostNamespace) + newManager.resctrlManager, err = resctrl.NewManager(resctrlInterval, machineInfo.CPUVendorID, inHostNamespace) if err != nil { klog.V(4).Infof("Cannot gather resctrl metrics: %v", err) } @@ -265,7 +265,7 @@ type manager struct { eventsChannel chan watcher.ContainerEvent collectorHTTPClient *http.Client perfManager stats.Manager - resctrlManager resctrl.Manager + resctrlManager resctrl.ResControlManager // List of raw container cgroup path prefix whitelist. rawContainerCgroupPathPrefixWhiteList []string // List of container env prefix whitelist, the matched container envs would be collected into metrics as extra labels. diff --git a/resctrl/factory.go b/resctrl/factory.go new file mode 100644 index 0000000000..097b0bf6c1 --- /dev/null +++ b/resctrl/factory.go @@ -0,0 +1,58 @@ +// Copyright 2025 Google Inc. All Rights Reserved. +// +// 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 resctrl + +import ( + "fmt" + "sync" + "time" + + "github.com/google/cadvisor/stats" + + "k8s.io/klog/v2" +) + +type ResControlManager interface { + Destroy() + GetCollector(containerName string, getContainerPids func() ([]string, error), numberOfNUMANodes int) (stats.Collector, error) +} + +// All registered auth provider plugins. +var pluginsLock sync.Mutex +var plugins = make(map[string]ResControlManagerPlugin) + +type ResControlManagerPlugin interface { + NewManager(interval time.Duration, vendorID string, inHostNamespace bool) (ResControlManager, error) +} + +func RegisterPlugin(name string, plugin ResControlManagerPlugin) error { + pluginsLock.Lock() + defer pluginsLock.Unlock() + if _, found := plugins[name]; found { + return fmt.Errorf("ResControlManagerPlugin %q was registered twice", name) + } + klog.V(4).Infof("Registered ResControlManagerPlugin %q", name) + plugins[name] = plugin + return nil +} + +func NewManager(interval time.Duration, vendorID string, inHostNamespace bool) (ResControlManager, error) { + pluginsLock.Lock() + defer pluginsLock.Unlock() + for _, plugin := range plugins { + return plugin.NewManager(interval, vendorID, inHostNamespace) + } + return nil, fmt.Errorf("unable to find plugins for resctrl manager") +} diff --git a/resctrl/collector.go b/resctrl/intel/collector.go similarity index 99% rename from resctrl/collector.go rename to resctrl/intel/collector.go index e5e71a7e48..ae5df69ee6 100644 --- a/resctrl/collector.go +++ b/resctrl/intel/collector.go @@ -16,7 +16,7 @@ // limitations under the License. // Collector of resctrl for a container. -package resctrl +package intel import ( "fmt" diff --git a/resctrl/collector_test.go b/resctrl/intel/collector_test.go similarity index 99% rename from resctrl/collector_test.go rename to resctrl/intel/collector_test.go index e8b665eb5e..8ec75c9c24 100644 --- a/resctrl/collector_test.go +++ b/resctrl/intel/collector_test.go @@ -16,7 +16,7 @@ // limitations under the License. // Collector tests. -package resctrl +package intel import ( "fmt" diff --git a/resctrl/intel/install/install.go b/resctrl/intel/install/install.go new file mode 100644 index 0000000000..98bca0e385 --- /dev/null +++ b/resctrl/intel/install/install.go @@ -0,0 +1,38 @@ +// Copyright 2025 Google Inc. All Rights Reserved. +// +// 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 install + +import ( + "time" + + "github.com/google/cadvisor/resctrl" + "github.com/google/cadvisor/resctrl/intel" + + "k8s.io/klog/v2" +) + +type managerplugin struct { +} + +func (m *managerplugin) NewManager(interval time.Duration, vendorID string, inHostNamespace bool) (resctrl.ResControlManager, error) { + return intel.NewManager(interval, intel.Setup, vendorID, inHostNamespace) +} + +func init() { + err := resctrl.RegisterPlugin("intel", &managerplugin{}) + if err != nil { + klog.Fatalf("Failed to register intel resctrl plugin: %v", err) + } +} diff --git a/resctrl/manager.go b/resctrl/intel/manager.go similarity index 88% rename from resctrl/manager.go rename to resctrl/intel/manager.go index 672e0c74e0..722ed2389c 100644 --- a/resctrl/manager.go +++ b/resctrl/intel/manager.go @@ -15,8 +15,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Manager of resctrl for containers. -package resctrl +// ResControlManager of resctrl for containers. +package intel import ( "errors" @@ -25,14 +25,10 @@ import ( "k8s.io/klog/v2" "github.com/google/cadvisor/container/raw" + "github.com/google/cadvisor/resctrl" "github.com/google/cadvisor/stats" ) -type Manager interface { - Destroy() - GetCollector(containerName string, getContainerPids func() ([]string, error), numberOfNUMANodes int) (stats.Collector, error) -} - type manager struct { stats.NoopDestroy interval time.Duration @@ -50,7 +46,7 @@ func (m *manager) GetCollector(containerName string, getContainerPids func() ([] return collector, nil } -func NewManager(interval time.Duration, setup func() error, vendorID string, inHostNamespace bool) (Manager, error) { +func NewManager(interval time.Duration, setup func() error, vendorID string, inHostNamespace bool) (resctrl.ResControlManager, error) { err := setup() if err != nil { return &NoopManager{}, err diff --git a/resctrl/manager_test.go b/resctrl/intel/manager_test.go similarity index 95% rename from resctrl/manager_test.go rename to resctrl/intel/manager_test.go index 854a89ce84..612408ce42 100644 --- a/resctrl/manager_test.go +++ b/resctrl/intel/manager_test.go @@ -15,13 +15,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Manager tests. -package resctrl +// ResControlManager tests. +package intel import ( "os" "testing" + "github.com/google/cadvisor/resctrl" "github.com/stretchr/testify/assert" ) @@ -32,7 +33,7 @@ func TestNewManager(t *testing.T) { enabledMBM bool inHostNamespace bool err string - expected Manager + expected resctrl.ResControlManager }{ { true, diff --git a/resctrl/testing/tasks_empty b/resctrl/intel/testing/tasks_empty similarity index 100% rename from resctrl/testing/tasks_empty rename to resctrl/intel/testing/tasks_empty diff --git a/resctrl/testing/tasks_invalid b/resctrl/intel/testing/tasks_invalid similarity index 100% rename from resctrl/testing/tasks_invalid rename to resctrl/intel/testing/tasks_invalid diff --git a/resctrl/testing/tasks_one b/resctrl/intel/testing/tasks_one similarity index 100% rename from resctrl/testing/tasks_one rename to resctrl/intel/testing/tasks_one diff --git a/resctrl/testing/tasks_two b/resctrl/intel/testing/tasks_two similarity index 100% rename from resctrl/testing/tasks_two rename to resctrl/intel/testing/tasks_two diff --git a/resctrl/utils.go b/resctrl/intel/utils.go similarity index 99% rename from resctrl/utils.go rename to resctrl/intel/utils.go index e38e13dca4..5d6e09bca9 100644 --- a/resctrl/utils.go +++ b/resctrl/intel/utils.go @@ -16,7 +16,7 @@ // limitations under the License. // Utilities. -package resctrl +package intel import ( "bufio" diff --git a/resctrl/utils_test.go b/resctrl/intel/utils_test.go similarity index 99% rename from resctrl/utils_test.go rename to resctrl/intel/utils_test.go index c2fe25ecf6..352d2c47b1 100644 --- a/resctrl/utils_test.go +++ b/resctrl/intel/utils_test.go @@ -20,7 +20,7 @@ // Mocked environment: // - "container" first container with {1, 2, 3} processes. // - "another" second container with {5, 6} processes. -package resctrl +package intel import ( "fmt"