Skip to content

Commit

Permalink
Merge pull request #323 from equinix-ms/error-for-no-config
Browse files Browse the repository at this point in the history
Provide error when no config was found
  • Loading branch information
maiqueb authored May 5, 2023
2 parents e12772b + 55b24e2 commit 8d5d9d1
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 28 deletions.
83 changes: 66 additions & 17 deletions cmd/whereabouts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/fs"
"net"
"os"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -307,7 +308,9 @@ var _ = Describe("Whereabouts operations", func() {
Args: cniArgs(podNamespace, podName),
}

ipamConf, cniVersion, err := config.LoadIPAMConfig([]byte(conf), cniArgs(podNamespace, podName), "")
confPath := filepath.Join(tmpDir, "whereabouts.conf")
Expect(os.WriteFile(confPath, []byte(conf), 0755)).To(Succeed())
ipamConf, cniVersion, err := config.LoadIPAMConfig([]byte(conf), cniArgs(podNamespace, podName), confPath)
Expect(err).NotTo(HaveOccurred())
Expect(ipamConf.IPRanges).NotTo(BeEmpty())
k8sClient = newK8sIPAM(
Expand Down Expand Up @@ -379,7 +382,9 @@ var _ = Describe("Whereabouts operations", func() {
Args: cniArgs(podNamespace, podName),
}

ipamConf, cniVersion, err := config.LoadIPAMConfig([]byte(conf), cniArgs(podNamespace, podName), "")
confPath := filepath.Join(tmpDir, "whereabouts.conf")
Expect(os.WriteFile(confPath, []byte(conf), 0755)).To(Succeed())
ipamConf, cniVersion, err := config.LoadIPAMConfig([]byte(conf), cniArgs(podNamespace, podName), confPath)
Expect(err).NotTo(HaveOccurred())
Expect(ipamConf.IPRanges).NotTo(BeEmpty())
k8sClient = newK8sIPAM(
Expand Down Expand Up @@ -448,7 +453,9 @@ var _ = Describe("Whereabouts operations", func() {
Args: cniArgs(podNamespace, podName),
}

ipamConf, cniVersion, err := config.LoadIPAMConfig([]byte(conf), cniArgs(podNamespace, podName), "")
confPath := filepath.Join(tmpDir, "whereabouts.conf")
Expect(os.WriteFile(confPath, []byte(conf), 0755)).To(Succeed())
ipamConf, cniVersion, err := config.LoadIPAMConfig([]byte(conf), cniArgs(podNamespace, podName), confPath)
Expect(err).NotTo(HaveOccurred())
Expect(ipamConf.IPRanges).NotTo(BeEmpty())
k8sClient = newK8sIPAM(
Expand Down Expand Up @@ -528,7 +535,9 @@ var _ = Describe("Whereabouts operations", func() {
Args: cniArgs(podNamespace, podName),
}

ipamConf, cniVersion, err := config.LoadIPAMConfig([]byte(conf), cniArgs(podNamespace, podName), "")
confPath := filepath.Join(tmpDir, "whereabouts.conf")
Expect(os.WriteFile(confPath, []byte(conf), 0755)).To(Succeed())
ipamConf, cniVersion, err := config.LoadIPAMConfig([]byte(conf), cniArgs(podNamespace, podName), confPath)
Expect(err).NotTo(HaveOccurred())
Expect(ipamConf.IPRanges).NotTo(BeEmpty())
k8sClient = newK8sIPAM(
Expand Down Expand Up @@ -617,7 +626,9 @@ var _ = Describe("Whereabouts operations", func() {
Args: cniArgs(podNamespace, podName),
}

ipamConf, cniVersion, err := config.LoadIPAMConfig([]byte(conf), cniArgs(podNamespace, podName), "")
confPath := filepath.Join(tmpDir, "whereabouts.conf")
Expect(os.WriteFile(confPath, []byte(conf), 0755)).To(Succeed())
ipamConf, cniVersion, err := config.LoadIPAMConfig([]byte(conf), cniArgs(podNamespace, podName), confPath)
Expect(err).NotTo(HaveOccurred())
Expect(ipamConf.IPRanges).NotTo(BeEmpty())
k8sClient = newK8sIPAM(
Expand Down Expand Up @@ -681,7 +692,9 @@ var _ = Describe("Whereabouts operations", func() {
Args: cniArgs(podNamespace, podName),
}

ipamConf, cniVersion, err := config.LoadIPAMConfig([]byte(conf), cniArgs(podNamespace, podName), "")
confPath := filepath.Join(tmpDir, "whereabouts.conf")
Expect(os.WriteFile(confPath, []byte(conf), 0755)).To(Succeed())
ipamConf, cniVersion, err := config.LoadIPAMConfig([]byte(conf), cniArgs(podNamespace, podName), confPath)
Expect(err).NotTo(HaveOccurred())
Expect(ipamConf.IPRanges).To(HaveLen(2))
k8sClient = newK8sIPAM(
Expand Down Expand Up @@ -746,7 +759,9 @@ var _ = Describe("Whereabouts operations", func() {
Args: cniArgs(podNamespace, podName),
}

ipamConf, cniVersion, err := config.LoadIPAMConfig([]byte(conf), cniArgs(podNamespace, podName), "")
confPath := filepath.Join(tmpDir, "whereabouts.conf")
Expect(os.WriteFile(confPath, []byte(conf), 0755)).To(Succeed())
ipamConf, cniVersion, err := config.LoadIPAMConfig([]byte(conf), cniArgs(podNamespace, podName), confPath)
Expect(err).NotTo(HaveOccurred())
Expect(ipamConf.IPRanges).To(HaveLen(2))
k8sClient = newK8sIPAM(
Expand Down Expand Up @@ -809,7 +824,9 @@ var _ = Describe("Whereabouts operations", func() {
Args: cniArgs(podNamespace, podName),
}

ipamConf, cniVersion, err := config.LoadIPAMConfig([]byte(conf), cniArgs(podNamespace, podName), "")
confPath := filepath.Join(tmpDir, "whereabouts.conf")
Expect(os.WriteFile(confPath, []byte(conf), 0755)).To(Succeed())
ipamConf, cniVersion, err := config.LoadIPAMConfig([]byte(conf), cniArgs(podNamespace, podName), confPath)
Expect(err).NotTo(HaveOccurred())
Expect(ipamConf.IPRanges).NotTo(BeEmpty())
k8sClient = newK8sIPAM(
Expand Down Expand Up @@ -875,7 +892,9 @@ var _ = Describe("Whereabouts operations", func() {
Args: cniArgs(podNamespace, podName),
}

ipamConf, cniVersion, err := config.LoadIPAMConfig([]byte(conf), cniArgs(podNamespace, podName), "")
confPath := filepath.Join(tmpDir, "whereabouts.conf")
Expect(os.WriteFile(confPath, []byte(conf), 0755)).To(Succeed())
ipamConf, cniVersion, err := config.LoadIPAMConfig([]byte(conf), cniArgs(podNamespace, podName), confPath)
Expect(err).NotTo(HaveOccurred())
Expect(ipamConf.IPRanges).NotTo(BeEmpty())
k8sClient = newK8sIPAM(
Expand Down Expand Up @@ -934,7 +953,9 @@ var _ = Describe("Whereabouts operations", func() {
}
}`, backend)

ipamConf, cniVersion, err := config.LoadIPAMConfig([]byte(conf), cniArgs(podNamespace, podName), "")
confPath := filepath.Join(tmpDir, "whereabouts.conf")
Expect(os.WriteFile(confPath, []byte(conf), 0755)).To(Succeed())
ipamConf, cniVersion, err := config.LoadIPAMConfig([]byte(conf), cniArgs(podNamespace, podName), confPath)
Expect(err).NotTo(HaveOccurred())
Expect(ipamConf.IPRanges).NotTo(BeEmpty())
wbClient := *kubernetes.NewKubernetesClient(
Expand Down Expand Up @@ -1031,7 +1052,9 @@ var _ = Describe("Whereabouts operations", func() {
Args: cniArgs(podNamespace, podName),
}

ipamConf, cniVersion, err := config.LoadIPAMConfig([]byte(conf), cniArgs(podNamespace, podName), "")
confPath := filepath.Join(tmpDir, "whereabouts.conf")
Expect(os.WriteFile(confPath, []byte(conf), 0755)).To(Succeed())
ipamConf, cniVersion, err := config.LoadIPAMConfig([]byte(conf), cniArgs(podNamespace, podName), confPath)
Expect(err).NotTo(HaveOccurred())
Expect(ipamConf.IPRanges).NotTo(BeEmpty())
wbClient := *kubernetes.NewKubernetesClient(
Expand Down Expand Up @@ -1083,7 +1106,9 @@ var _ = Describe("Whereabouts operations", func() {
Args: cniArgs(podNamespace, podName),
}

secondIPAMConf, secondCNIVersion, err := config.LoadIPAMConfig([]byte(confsecond), cniArgs(podNamespace, podName), "")
secondConfPath := filepath.Join(tmpDir, "whereabouts.conf")
Expect(os.WriteFile(confPath, []byte(confsecond), 0755)).To(Succeed())
secondIPAMConf, secondCNIVersion, err := config.LoadIPAMConfig([]byte(confsecond), cniArgs(podNamespace, podName), secondConfPath)
Expect(err).NotTo(HaveOccurred())

// Allocate the IP
Expand Down Expand Up @@ -1149,7 +1174,9 @@ var _ = Describe("Whereabouts operations", func() {
Args: cniArgs(podNamespace, podName),
}

ipamConf, cniVersion, err := config.LoadIPAMConfig([]byte(conf), cniArgs(podNamespace, podName), "")
confPath := filepath.Join(tmpDir, "whereabouts.conf")
Expect(os.WriteFile(confPath, []byte(conf), 0755)).To(Succeed())
ipamConf, cniVersion, err := config.LoadIPAMConfig([]byte(conf), cniArgs(podNamespace, podName), confPath)
Expect(err).NotTo(HaveOccurred())
Expect(ipamConf.IPRanges).NotTo(BeEmpty())
wbClient := *kubernetes.NewKubernetesClient(
Expand Down Expand Up @@ -1201,7 +1228,9 @@ var _ = Describe("Whereabouts operations", func() {
Args: cniArgs(podNamespace, podName),
}

secondIPAMConf, secondCNIVersion, err := config.LoadIPAMConfig([]byte(confsecond), cniArgs(podNamespace, podName), "")
secondConfPath := filepath.Join(tmpDir, "whereabouts.conf")
Expect(os.WriteFile(confPath, []byte(confsecond), 0755)).To(Succeed())
secondIPAMConf, secondCNIVersion, err := config.LoadIPAMConfig([]byte(confsecond), cniArgs(podNamespace, podName), secondConfPath)
Expect(err).NotTo(HaveOccurred())

// Allocate the IP
Expand Down Expand Up @@ -1268,7 +1297,9 @@ var _ = Describe("Whereabouts operations", func() {
Args: cniArgs(podNamespace, podName),
}

ipamConf, cniVersion, err := config.LoadIPAMConfig([]byte(conf), cniArgs(podNamespace, podName), "")
confPath := filepath.Join(tmpDir, "whereabouts.conf")
Expect(os.WriteFile(confPath, []byte(conf), 0755)).To(Succeed())
ipamConf, cniVersion, err := config.LoadIPAMConfig([]byte(conf), cniArgs(podNamespace, podName), confPath)
Expect(err).NotTo(HaveOccurred())
Expect(ipamConf.IPRanges).NotTo(BeEmpty())
wbClient := *kubernetes.NewKubernetesClient(
Expand Down Expand Up @@ -1320,7 +1351,9 @@ var _ = Describe("Whereabouts operations", func() {
Args: cniArgs(podNamespace, podName),
}

secondIPAMConf, secondCNIVersion, err := config.LoadIPAMConfig([]byte(confsecond), cniArgs(podNamespace, podName), "")
secondConfPath := filepath.Join(tmpDir, "whereabouts.conf")
Expect(os.WriteFile(secondConfPath, []byte(confsecond), 0755)).To(Succeed())
secondIPAMConf, secondCNIVersion, err := config.LoadIPAMConfig([]byte(confsecond), cniArgs(podNamespace, podName), secondConfPath)
Expect(err).NotTo(HaveOccurred())

// Allocate the IP
Expand Down Expand Up @@ -1415,10 +1448,26 @@ func ipamConfig(podName string, namespace string, ipRange string, gw string, kub
return nil
}

ipamConfWithDefaults, _, err := config.LoadIPAMConfig(bytes, cniArgs(namespace, podName), "")
tmpDir, err := os.MkdirTemp("", "whereabouts")
if err != nil {
return nil
}
confPath := filepath.Join(tmpDir, "wherebouts.conf")
err = os.WriteFile(confPath, bytes, 0755)
if err != nil {
return nil
}

ipamConfWithDefaults, _, err := config.LoadIPAMConfig(bytes, cniArgs(namespace, podName), confPath)
if err != nil {
return nil
}

err = os.RemoveAll(tmpDir)
if err != nil {
return nil
}

return ipamConfWithDefaults
}

Expand Down
16 changes: 13 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,11 @@ func GetFlatIPAM(isControlLoop bool, IPAM *types.IPAMConfig, extraConfigPaths ..
}

foundflatfile = confpath
return flatipam, foundflatfile, err
return flatipam, foundflatfile, nil
}
}
var err error
return flatipam, foundflatfile, err

return flatipam, foundflatfile, NewConfigFileNotFoundError()
}

func handleEnvArgs(n *types.Net, numV6 int, numV4 int, args types.IPAMEnvArgs) (int, int, error) {
Expand Down Expand Up @@ -370,6 +370,16 @@ func (e *InvalidPluginError) Error() string {
return fmt.Sprintf("only interested in networks whose IPAM type is 'whereabouts'. This one was: %s", e.ipamType)
}

type ConfigFileNotFoundError struct{}

func NewConfigFileNotFoundError() *ConfigFileNotFoundError {
return &ConfigFileNotFoundError{}
}

func (e *ConfigFileNotFoundError) Error() string {
return "config file not found"
}

func storageError() error {
return fmt.Errorf("you have not configured the storage engine (looks like you're using an invalid `kubernetes.kubeconfig` parameter in your config)")
}
61 changes: 53 additions & 8 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"fmt"
"net"
"os"
"path/filepath"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/k8snetworkplumbingwg/whereabouts/pkg/types"
)

func TestAllocate(t *testing.T) {
Expand All @@ -16,6 +19,18 @@ func TestAllocate(t *testing.T) {
}

var _ = Describe("Allocation operations", func() {
var tmpDir string

BeforeEach(func() {
var err error
tmpDir, err = os.MkdirTemp("", "whereabouts")
Expect(err).NotTo(HaveOccurred())
})

AfterEach(func() {
Expect(os.RemoveAll(tmpDir)).To(Succeed())
})

It("can load a basic config", func() {

conf := `{
Expand All @@ -35,7 +50,10 @@ var _ = Describe("Allocation operations", func() {
}
}`

ipamconfig, _, err := LoadIPAMConfig([]byte(conf), "")
confPath := filepath.Join(tmpDir, "whereabouts.conf")
Expect(os.WriteFile(confPath, []byte(conf), 0755)).To(Succeed())

ipamconfig, _, err := LoadIPAMConfig([]byte(conf), "", confPath)
Expect(err).NotTo(HaveOccurred())
Expect(ipamconfig.LogLevel).To(Equal("debug"))
Expect(ipamconfig.LogFile).To(Equal("/tmp/whereabouts.log"))
Expand All @@ -49,6 +67,11 @@ var _ = Describe("Allocation operations", func() {

})

It("throws an error when no flat-files are found", func() {
_, _, err := GetFlatIPAM(true, &types.IPAMConfig{})
Expect(err).To(MatchError(NewConfigFileNotFoundError()))
})

It("can load a global flat-file config", func() {

globalconf := `{
Expand Down Expand Up @@ -160,7 +183,10 @@ var _ = Describe("Allocation operations", func() {
]
}`

ipamconfig, err := LoadIPAMConfiguration([]byte(conf), "")
confPath := filepath.Join(tmpDir, "whereabouts.conf")
Expect(os.WriteFile(confPath, []byte(conf), 0755)).To(Succeed())

ipamconfig, err := LoadIPAMConfiguration([]byte(conf), "", confPath)
Expect(err).NotTo(HaveOccurred())
Expect(ipamconfig.LogLevel).To(Equal("debug"))
Expect(ipamconfig.LogFile).To(Equal("/tmp/whereabouts.log"))
Expand Down Expand Up @@ -207,7 +233,10 @@ var _ = Describe("Allocation operations", func() {
}
}`

ipamConfig, _, err := LoadIPAMConfig([]byte(conf), "")
confPath := filepath.Join(tmpDir, "whereabouts.conf")
Expect(os.WriteFile(confPath, []byte(conf), 0755)).To(Succeed())

ipamConfig, _, err := LoadIPAMConfig([]byte(conf), "", confPath)
Expect(err).NotTo(HaveOccurred())
Expect(ipamConfig.IPRanges[0].Range).To(Equal("192.168.1.0/24"))
Expect(ipamConfig.IPRanges[0].RangeStart).To(Equal(net.ParseIP("192.168.1.5")))
Expand All @@ -232,7 +261,10 @@ var _ = Describe("Allocation operations", func() {
}
}`

ipamConfig, _, err := LoadIPAMConfig([]byte(conf), "")
confPath := filepath.Join(tmpDir, "whereabouts.conf")
Expect(os.WriteFile(confPath, []byte(conf), 0755)).To(Succeed())

ipamConfig, _, err := LoadIPAMConfig([]byte(conf), "", confPath)
Expect(err).NotTo(HaveOccurred())
Expect(ipamConfig.IPRanges[0].Range).To(Equal("192.168.1.0/24"))
Expect(ipamConfig.IPRanges[0].RangeStart).To(Equal(net.ParseIP("192.168.1.0")))
Expand All @@ -257,7 +289,10 @@ var _ = Describe("Allocation operations", func() {
}
}`

ipamConfig, _, err := LoadIPAMConfig([]byte(conf), "")
confPath := filepath.Join(tmpDir, "whereabouts.conf")
Expect(os.WriteFile(confPath, []byte(conf), 0755)).To(Succeed())

ipamConfig, _, err := LoadIPAMConfig([]byte(conf), "", confPath)
Expect(err).NotTo(HaveOccurred())
Expect(ipamConfig.IPRanges[0].Range).To(Equal("192.168.1.0/24"))
Expect(ipamConfig.IPRanges[0].RangeStart).To(Equal(net.ParseIP("192.168.1.44")))
Expand All @@ -283,7 +318,10 @@ var _ = Describe("Allocation operations", func() {
}
}`

ipamConfig, _, err := LoadIPAMConfig([]byte(conf), "")
confPath := filepath.Join(tmpDir, "whereabouts.conf")
Expect(os.WriteFile(confPath, []byte(conf), 0755)).To(Succeed())

ipamConfig, _, err := LoadIPAMConfig([]byte(conf), "", confPath)
Expect(err).NotTo(HaveOccurred())
Expect(ipamConfig.IPRanges[0].Range).To(Equal("192.168.1.0/24"))
Expect(ipamConfig.IPRanges[0].RangeStart).To(Equal(net.ParseIP("192.168.1.44")))
Expand Down Expand Up @@ -311,7 +349,10 @@ var _ = Describe("Allocation operations", func() {
}
}`

ipamConfig, _, err := LoadIPAMConfig([]byte(conf), "")
confPath := filepath.Join(tmpDir, "whereabouts.conf")
Expect(os.WriteFile(confPath, []byte(conf), 0755)).To(Succeed())

ipamConfig, _, err := LoadIPAMConfig([]byte(conf), "", confPath)
Expect(err).NotTo(HaveOccurred())
Expect(ipamConfig.IPRanges[0].Range).To(Equal("192.168.1.0/24"))
Expect(ipamConfig.IPRanges[0].RangeStart).To(Equal(net.ParseIP("192.168.1.44")))
Expand All @@ -333,7 +374,11 @@ var _ = Describe("Allocation operations", func() {
"gateway": "192.168.10.1"
}
}`
_, _, err := LoadIPAMConfig([]byte(invalidConf), "")

confPath := filepath.Join(tmpDir, "whereabouts.conf")
Expect(os.WriteFile(confPath, []byte(invalidConf), 0755)).To(Succeed())

_, _, err := LoadIPAMConfig([]byte(invalidConf), "", confPath)
Expect(err).To(MatchError("invalid range start for CIDR 192.168.2.16/28: 192.168.1.5"))
})

Expand Down

0 comments on commit 8d5d9d1

Please sign in to comment.