|
| 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 | +} |
0 commit comments