From 633985d82fd700a7f6ebdbb6dd1f529307ed4260 Mon Sep 17 00:00:00 2001 From: Hidehito Yabuuchi Date: Thu, 25 Apr 2024 18:30:03 +0900 Subject: [PATCH] thin plugin: Handle --multus-master-cni-file-name flag Multus v3.9.3 has `--multus-master-cni-file-name` flag to specify the name of a primary CNI config file. https://github.com/k8snetworkplumbingwg/multus-cni/blob/v3.9.3/images/entrypoint.sh#L22 In Multus v4.0.2, the thin plugin has the flag defined, but it is not read and so does not have effect. This pull request fixes the problem by making the thin plugin correctly handles `--multus-master-cni-file-name` flag. Fixes #1226 Signed-off-by: Hidehito Yabuuchi --- cmd/thin_entrypoint/main.go | 26 +++++++++----- cmd/thin_entrypoint/main_test.go | 61 ++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 8 deletions(-) diff --git a/cmd/thin_entrypoint/main.go b/cmd/thin_entrypoint/main.go index 5f32a87de..8250a3e99 100644 --- a/cmd/thin_entrypoint/main.go +++ b/cmd/thin_entrypoint/main.go @@ -310,22 +310,32 @@ const multusConfTemplate = `{ } ` -func (o *Options) createMultusConfig(prevMasterConfigFileHash []byte) (string, []byte, error) { - // find master file from MultusAutoconfigDir +func (o *Options) getMasterConfigPath() (string, error) { + // Master config file is specified + if o.MultusMasterCNIFileName != "" { + return filepath.Join(o.MultusAutoconfigDir, o.MultusMasterCNIFileName), nil + } + + // Pick the alphabetically first config file from MultusAutoconfigDir files, err := libcni.ConfFiles(o.MultusAutoconfigDir, []string{".conf", ".conflist"}) if err != nil { - return "", nil, fmt.Errorf("cannot find master CNI config in %q: %v", o.MultusAutoconfigDir, err) + return "", fmt.Errorf("cannot find master CNI config in %q: %v", o.MultusAutoconfigDir, err) } - masterConfigPath := "" for _, filename := range files { if !strings.HasPrefix(filepath.Base(filename), "00-multus.conf") { - masterConfigPath = filename - break + return filename, nil } } - if masterConfigPath == "" { - return "", nil, fmt.Errorf("cannot find valid master CNI config in %q", o.MultusAutoconfigDir) + + // No config file found + return "", fmt.Errorf("cannot find valid master CNI config in %q", o.MultusAutoconfigDir) +} + +func (o *Options) createMultusConfig(prevMasterConfigFileHash []byte) (string, []byte, error) { + masterConfigPath, err := o.getMasterConfigPath() + if err != nil { + return "", nil, err } masterConfigBytes, masterConfigFileHash, err := getFileAndHash(masterConfigPath) diff --git a/cmd/thin_entrypoint/main_test.go b/cmd/thin_entrypoint/main_test.go index bc6f7de8d..c9cf8f83d 100644 --- a/cmd/thin_entrypoint/main_test.go +++ b/cmd/thin_entrypoint/main_test.go @@ -411,4 +411,65 @@ var _ = Describe("thin entrypoint testing", func() { Expect(os.RemoveAll(tmpDir)).To(Succeed()) }) + It("Run createMultusConfig(), with options, conflist", func() { + // create directory and files + tmpDir, err := os.MkdirTemp("", "multus_thin_entrypoint_tmp") + Expect(err).NotTo(HaveOccurred()) + + multusAutoConfigDir := fmt.Sprintf("%s/auto_conf", tmpDir) + cniConfDir := fmt.Sprintf("%s/cni_conf", tmpDir) + + Expect(os.Mkdir(multusAutoConfigDir, 0755)).To(Succeed()) + Expect(os.Mkdir(cniConfDir, 0755)).To(Succeed()) + + // create master CNI config + masterCNIConfigFileName := "10-testcni.conf" + masterCNIConfig := ` + { + "cniVersion": "1.0.0", + "name": "test1", + "type": "cnitesttype" + }` + Expect(os.WriteFile(fmt.Sprintf("%s/%s", multusAutoConfigDir, masterCNIConfigFileName), []byte(masterCNIConfig), 0755)).To(Succeed()) + + // create another CNI config + anotherCNIConfigFileName := "09-test2cni.conf" // Alphabetically before masterCNIConfigFileName + anotherCNIConfig := ` + { + "cniVersion": "1.0.0", + "name": "test2", + "type": "cnitest2type" + }` + Expect(os.WriteFile(fmt.Sprintf("%s/%s", multusAutoConfigDir, anotherCNIConfigFileName), []byte(anotherCNIConfig), 0755)).To(Succeed()) + + masterConfigPath, masterConfigHash, err := (&Options{ + MultusAutoconfigDir: multusAutoConfigDir, + MultusMasterCNIFileName: masterCNIConfigFileName, + CNIConfDir: cniConfDir, + MultusKubeConfigFileHost: "/etc/foobar_kubeconfig", + }).createMultusConfig(nil) + Expect(err).NotTo(HaveOccurred()) + + Expect(masterConfigPath).NotTo(Equal("")) + Expect(masterConfigHash).NotTo(Equal("")) + + expectedResult := + `{ + "cniVersion": "1.0.0", + "name": "multus-cni-network", + "plugins": [ { + "type": "multus", + "logToStderr": false, + "kubeconfig": "/etc/foobar_kubeconfig", + "delegates": [ + {"cniVersion":"1.0.0","name":"test1","type":"cnitesttype"} + ] + }] +} +` + conf, err := os.ReadFile(fmt.Sprintf("%s/00-multus.conflist", cniConfDir)) + Expect(string(conf)).To(Equal(expectedResult)) + + Expect(os.RemoveAll(tmpDir)).To(Succeed()) + }) })