From 91bf00c218849c381b5822f9ac2d54e21b0008b9 Mon Sep 17 00:00:00 2001 From: Mamduh Alassi Date: Mon, 16 Nov 2020 14:12:58 +0200 Subject: [PATCH] Add config field for periodic update interval This change brings a new config field which is used to config the periodic update interval of the device plugin. When periodic update interval is 0 periodic update is disabled Signed-off-by: Mamduh Alassi --- README.md | 6 +++ cmd/k8s-rdma-shared-dp/main.go | 6 +-- ...k8s-rdma-shared-dev-plugin-config-map.yaml | 1 + pkg/resources/resources_manager.go | 37 +++++++++++++++---- pkg/resources/resources_manager_test.go | 14 +++++-- pkg/types/types.go | 3 +- 6 files changed, 50 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 392fbfc..a80f186 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,7 @@ The plugin has several configuration fields, this section explains each field us ```json { + "periodicUpdateInterval": 300, "configList": [{ "resourceName": "hca_shared_devices_a", "rdmaHcaMax": 1000, @@ -97,6 +98,11 @@ The plugin has several configuration fields, this section explains each field us } ``` +`periodicUpdateInterval` is the time interval in seconds to update the resources according to host devices changes if there changed. +Notes: + - if `periodicUpdateInterval` is 0 then periodic update for host devices will be disabled. + - if `periodicUpdateInterval` is not set then default periodic update interval of 60 seconds will be used. + `"configList"` should contain a list of config objects. Each config object may consist of following fields: diff --git a/cmd/k8s-rdma-shared-dp/main.go b/cmd/k8s-rdma-shared-dp/main.go index c379dbd..0ba4461 100644 --- a/cmd/k8s-rdma-shared-dp/main.go +++ b/cmd/k8s-rdma-shared-dp/main.go @@ -6,7 +6,6 @@ import ( "log" "os" "syscall" - "time" "github.com/Mellanox/k8s-rdma-shared-dev-plugin/pkg/resources" ) @@ -17,9 +16,6 @@ var ( date = "unknown date" ) -// Periodic Update interval -const periodicUpdateInterval = 60 * time.Second - func printVersionString() string { return fmt.Sprintf("k8s-rdma-shared-dev-plugin version:%s, commit:%s, date:%s", version, commit, date) } @@ -40,7 +36,7 @@ func main() { log.Println("Starting K8s RDMA Shared Device Plugin version=", version) - rm := resources.NewResourceManager(periodicUpdateInterval) + rm := resources.NewResourceManager() log.Println("resource manager reading configs") if err := rm.ReadConfig(); err != nil { diff --git a/images/k8s-rdma-shared-dev-plugin-config-map.yaml b/images/k8s-rdma-shared-dev-plugin-config-map.yaml index dbe8a47..89df3bd 100644 --- a/images/k8s-rdma-shared-dev-plugin-config-map.yaml +++ b/images/k8s-rdma-shared-dev-plugin-config-map.yaml @@ -6,6 +6,7 @@ metadata: data: config.json: | { + "periodicUpdateInterval": 300, "configList": [{ "resourceName": "hca_shared_devices_a", "rdmaHcaMax": 1000, diff --git a/pkg/resources/resources_manager.go b/pkg/resources/resources_manager.go index 13d7b54..4642c6c 100644 --- a/pkg/resources/resources_manager.go +++ b/pkg/resources/resources_manager.go @@ -26,6 +26,9 @@ const ( netClass = 0x02 // Device class - Network controller maxVendorNameLength = 20 maxProductNameLength = 40 + + // Default periodic update interval + defaultPeriodicUpdateInterval = 60 * time.Second ) var ( @@ -47,7 +50,7 @@ type resourceManager struct { PeriodicUpdateInterval time.Duration } -func NewResourceManager(periodicUpdateInterval time.Duration) types.ResourceManager { +func NewResourceManager() types.ResourceManager { watcherMode := detectPluginWatchMode(activeSockDir) if watcherMode { fmt.Println("Using Kubelet Plugin Registry Mode") @@ -55,13 +58,12 @@ func NewResourceManager(periodicUpdateInterval time.Duration) types.ResourceMana fmt.Println("Using Deprecated Devie Plugin Registry Path") } return &resourceManager{ - configFile: configFilePath, - resourcePrefix: rdmaHcaResourcePrefix, - socketSuffix: socketSuffix, - watchMode: watcherMode, - netlinkManager: &netlinkManager{}, - rds: NewRdmaDeviceSpec(requiredRdmaDevices), - PeriodicUpdateInterval: periodicUpdateInterval, + configFile: configFilePath, + resourcePrefix: rdmaHcaResourcePrefix, + socketSuffix: socketSuffix, + watchMode: watcherMode, + netlinkManager: &netlinkManager{}, + rds: NewRdmaDeviceSpec(requiredRdmaDevices), } } @@ -79,6 +81,21 @@ func (rm *resourceManager) ReadConfig() error { } log.Printf("loaded config: %+v \n", config.ConfigList) + + // if periodic update is not set then use the default value + if config.PeriodicUpdateInterval == nil { + log.Println("no periodic update interval is set, use default interval 60 seconds") + rm.PeriodicUpdateInterval = defaultPeriodicUpdateInterval + } else { + PeriodicUpdateInterval := *config.PeriodicUpdateInterval + if PeriodicUpdateInterval == 0 { + log.Println("warning: periodic update interval is 0, no periodic update will run") + } else { + log.Printf("periodic update interval: %+d \n", PeriodicUpdateInterval) + } + rm.PeriodicUpdateInterval = time.Duration(PeriodicUpdateInterval) * time.Second + } + for i := range config.ConfigList { rm.configList = append(rm.configList, &config.ConfigList[i]) } @@ -89,6 +106,10 @@ func (rm *resourceManager) ReadConfig() error { func (rm *resourceManager) ValidateConfigs() error { resourceName := make(map[string]string) + if rm.PeriodicUpdateInterval < 0 { + return fmt.Errorf("invalid \"periodicUpdateInterval\" configuration \"%d\"", rm.PeriodicUpdateInterval) + } + if len(rm.configList) < 1 { return fmt.Errorf("no resources configuration found") } diff --git a/pkg/resources/resources_manager_test.go b/pkg/resources/resources_manager_test.go index 4d71bb9..63154fb 100644 --- a/pkg/resources/resources_manager_test.go +++ b/pkg/resources/resources_manager_test.go @@ -46,7 +46,7 @@ var _ = Describe("ResourcesManger", func() { activeSockDir = activeSockDirBackUP }() - obj := NewResourceManager(1 * time.Millisecond) + obj := NewResourceManager() rm := obj.(*resourceManager) Expect(rm.watchMode).To(Equal(true)) }) @@ -58,7 +58,7 @@ var _ = Describe("ResourcesManger", func() { activeSockDir = activeSockDirBackUP }() - obj := NewResourceManager(1 * time.Microsecond) + obj := NewResourceManager() rm := obj.(*resourceManager) Expect(rm.watchMode).To(Equal(false)) }) @@ -77,7 +77,8 @@ var _ = Describe("ResourcesManger", func() { "deviceIDs": ["1017"], "ifNames": ["ib2", "ib3"]} } - ]}` + ], + "periodicUpdateInterval": 60}` fs := &utils.FakeFilesystem{ Dirs: []string{"tmp"}, Files: map[string][]byte{ @@ -94,6 +95,7 @@ var _ = Describe("ResourcesManger", func() { Expect(len(rm.configList[1].Selectors.Vendors)).To(Equal(1)) Expect(len(rm.configList[1].Selectors.DeviceIDs)).To(Equal(1)) Expect(len(rm.configList[1].Selectors.IfNames)).To(Equal(2)) + Expect(rm.PeriodicUpdateInterval).To(Equal(60 * time.Second)) }) It("non existing config file", func() { @@ -245,6 +247,12 @@ var _ = Describe("ResourcesManger", func() { err := rm.ValidateConfigs() Expect(err).To(HaveOccurred()) }) + It("configuration with invalid \"periodicUpdateInterval\"", func() { + rm := &resourceManager{PeriodicUpdateInterval: -10} + err := rm.ValidateConfigs() + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("invalid \"periodicUpdateInterval\" configuration \"-10\"")) + }) }) Context("GetDevices", func() { It("Get full list of devices", func() { diff --git a/pkg/types/types.go b/pkg/types/types.go index eab81f0..97bb9e2 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -29,7 +29,8 @@ type UserConfig struct { // UserConfigList config list for servers type UserConfigList struct { - ConfigList []UserConfig `json:"configList"` + PeriodicUpdateInterval *int `json:"periodicUpdateInterval"` + ConfigList []UserConfig `json:"configList"` } // ResourceServer is gRPC server implements K8s device plugin api