From 0da544985452687d4175bf27e5b4e38892f67ddd Mon Sep 17 00:00:00 2001 From: Miguel Duarte Barroso Date: Fri, 12 Nov 2021 17:16:02 +0100 Subject: [PATCH] thick, config regen, test: fix test The test was just checking that a READ/WRITE fsnotify.Event for the multus configuration was being seen; this patch changes this behavior, and assures that the delegateCNI configuration update results in turn on the update of the multus configuration file. Signed-off-by: Miguel Duarte Barroso --- pkg/config/manager.go | 5 ++- pkg/config/manager_test.go | 63 +++++++++++++++++++++----------------- 2 files changed, 39 insertions(+), 29 deletions(-) diff --git a/pkg/config/manager.go b/pkg/config/manager.go index a57c7cc71..4120fef2a 100644 --- a/pkg/config/manager.go +++ b/pkg/config/manager.go @@ -35,7 +35,7 @@ const ( // Manager monitors the configuration of the primary CNI plugin, and // regenerates multus configuration whenever it gets updated. type Manager struct { - cniConfigData map[string]interface{} + cniConfigData map[string]interface{} configWatcher *fsnotify.Watcher multusConfig *MultusConf multusConfigDir string @@ -155,6 +155,9 @@ func (m Manager) MonitorDelegatedPluginConfiguration(shutDown chan struct{}, don if err := m.PersistMultusConfig(updatedConfig); err != nil { _ = logging.Errorf("failed to persist the multus configuration: %v", err) } + if err := m.loadPrimaryCNIConfigFromFile(); err != nil { + _ = logging.Errorf("failed to reload the updated config: %v", err) + } case err := <-m.configWatcher.Errors: if err == nil { diff --git a/pkg/config/manager_test.go b/pkg/config/manager_test.go index 680d3583b..6df78f85f 100644 --- a/pkg/config/manager_test.go +++ b/pkg/config/manager_test.go @@ -16,18 +16,13 @@ package config import ( - "encoding/json" "fmt" "io/ioutil" "os" "testing" - "time" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - "github.com/onsi/gomega/format" - - "github.com/fsnotify/fsnotify" ) const suiteName = "Configuration Manager" @@ -53,7 +48,7 @@ var _ = Describe(suiteName, func() { var configManager *Manager var multusConfigDir string - var multusConfigFilePath string + var defaultCniConfig string BeforeEach(func() { var err error @@ -63,9 +58,8 @@ var _ = Describe(suiteName, func() { }) BeforeEach(func() { - format.TruncatedDiff = false - multusConfigFilePath = fmt.Sprintf("%s/%s", multusConfigDir, primaryCNIPluginName) - Expect(ioutil.WriteFile(multusConfigFilePath, []byte(primaryCNIPluginTemplate), userRWPermission)).To(Succeed()) + defaultCniConfig = fmt.Sprintf("%s/%s", multusConfigDir, primaryCNIPluginName) + Expect(ioutil.WriteFile(defaultCniConfig, []byte(primaryCNIPluginTemplate), userRWPermission)).To(Succeed()) multusConf := NewMultusConfig( primaryCNIName, @@ -87,28 +81,37 @@ var _ = Describe(suiteName, func() { Expect(config).To(Equal(expectedResult)) }) - It("Reacts to updates of the delegated CNI configuration", func() { - stopChan := make(chan struct{}) - doneChan := make(chan struct{}) - go func(stopChannel, doneChan chan struct{}) { - Expect(configManager.MonitorDelegatedPluginConfiguration(stopChannel, doneChan)).To(Succeed()) - }(stopChan, doneChan) + Context("Updates to the delegate CNI configuration", func() { + var ( + doneChannel chan struct{} + stopChannel chan struct{} + ) - newCNIConfig := "{\"cniVersion\":\"0.4.0\",\"dns\":{},\"ipam\":{},\"name\":\"mycni-name\",\"type\":\"mycni\"}" - Expect(ioutil.WriteFile(multusConfigFilePath, []byte(newCNIConfig), userRWPermission)).To(Succeed()) - Eventually(<-configManager.configWatcher.Events, time.Second).Should(Equal( - fsnotify.Event{ - Name: multusConfigFilePath, - Op: fsnotify.Write, - })) + BeforeEach(func() { + doneChannel = make(chan struct{}) + stopChannel = make(chan struct{}) + go func() { + Expect(configManager.MonitorDelegatedPluginConfiguration(stopChannel, doneChannel)).To(Succeed()) + }() + }) - bytes, err := json.Marshal(configManager.cniConfigData) - Expect(err).NotTo(HaveOccurred()) - Expect(string(bytes)).To(Equal(newCNIConfig)) + AfterEach(func() { + go func() { stopChannel <- struct{}{} }() + Eventually(<-doneChannel).Should(Equal(struct{}{})) + close(doneChannel) + close(stopChannel) + }) - // cleanup the watcher - go func() { stopChan <- struct{}{} }() - Eventually(<-doneChan, time.Second).Should(Equal(struct{}{})) + It("Trigger the re-generation of the Multus CNI configuration", func() { + newCNIConfig := "{\"cniVersion\":\"0.4.0\",\"dns\":{},\"ipam\":{},\"name\":\"yoyo-newnet\",\"type\":\"mycni\"}" + Expect(ioutil.WriteFile(defaultCniConfig, []byte(newCNIConfig), userRWPermission)).To(Succeed()) + + multusCniConfigFile := fmt.Sprintf("%s/%s", multusConfigDir, multusConfigFileName) + Eventually(func() (string, error) { + multusCniData, err := ioutil.ReadFile(multusCniConfigFile) + return string(multusCniData), err + }).Should(Equal(multusConfigFromDelegate(newCNIConfig))) + }) }) When("the user requests the name of the multus configuration to be overridden", func() { @@ -124,3 +127,7 @@ var _ = Describe(suiteName, func() { }) }) }) + +func multusConfigFromDelegate(delegateConfig string) string { + return fmt.Sprintf("{\"cniVersion\":\"0.4.0\",\"delegates\":[%s],\"kubeconfig\":\"/a/b/c/kubeconfig.kubeconfig\",\"name\":\"multus-cni-network\",\"type\":\"myCNI\"}", delegateConfig) +}