Skip to content

Commit

Permalink
Merge pull request #257 from genesiscloud/configurable_OverlappingRanges
Browse files Browse the repository at this point in the history
fix overwriting of OverlappingRanges by mergo
  • Loading branch information
maiqueb committed Nov 18, 2022
2 parents cdad95c + d880900 commit 92d3cdf
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ func LoadIPAMConfig(bytes []byte, envArgs string, extraConfigPaths ...string) (*

// Now let's try to merge the configurations...
// NB: Don't try to do any initialization before this point or it won't account for merged flat file.
var OverlappingRanges bool = n.IPAM.OverlappingRanges
if err := mergo.Merge(&n, flatipam); err != nil {
logging.Errorf("Merge error with flat file: %s", err)
}
n.IPAM.OverlappingRanges = OverlappingRanges

// Logging
if n.IPAM.LogFile != "" {
Expand Down
67 changes: 67 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,44 @@ var _ = Describe("Allocation operations", func() {
Expect(ipamconfig.LeaderLeaseDuration).To(Equal(3000))
Expect(ipamconfig.LeaderRenewDeadline).To(Equal(2000))
Expect(ipamconfig.LeaderRetryPeriod).To(Equal(1000))
})

It("overlapping range can be set", func() {
var globalConf string = `{
"datastore": "kubernetes",
"kubernetes": {
"kubeconfig": "/etc/cni/net.d/whereabouts.d/whereabouts.kubeconfig"
},
"log_file": "/tmp/whereabouts.log",
"log_level": "debug",
"gateway": "192.168.5.5",
"enable_overlapping_ranges": false
}`
Expect(ioutil.WriteFile("/tmp/whereabouts.conf", []byte(globalConf), 0755)).To(Succeed())

ipamconfig, _, err := LoadIPAMConfig([]byte(generateIPAMConfWithOverlappingRanges()), "")
Expect(err).NotTo(HaveOccurred())

Expect(ipamconfig.OverlappingRanges).To(BeTrue())
})

It("overlapping range can be disabled", func() {
var globalConf string = `{
"datastore": "kubernetes",
"kubernetes": {
"kubeconfig": "/etc/cni/net.d/whereabouts.d/whereabouts.kubeconfig"
},
"log_file": "/tmp/whereabouts.log",
"log_level": "debug",
"gateway": "192.168.5.5",
"enable_overlapping_ranges": true
}`
Expect(ioutil.WriteFile("/tmp/whereabouts.conf", []byte(globalConf), 0755)).To(Succeed())

ipamconfig, _, err := LoadIPAMConfig([]byte(generateIPAMConfWithoutOverlappingRanges()), "")
Expect(err).NotTo(HaveOccurred())

Expect(ipamconfig.OverlappingRanges).To(BeFalse())
})

It("can load a config list", func() {
Expand Down Expand Up @@ -317,3 +354,33 @@ var _ = Describe("Allocation operations", func() {
"LoadIPAMConfig - JSON Parsing Error: invalid character 'a' looking for beginning of object key string")))
})
})

func generateIPAMConfWithOverlappingRanges() string {
return `{
"cniVersion": "0.3.1",
"name": "mynet",
"type": "ipvlan",
"master": "foo0",
"ipam": {
"range": "192.168.2.230/24",
"configuration_path": "/tmp/whereabouts.conf",
"type": "whereabouts",
"enable_overlapping_ranges": true
}
}`
}

func generateIPAMConfWithoutOverlappingRanges() string {
return `{
"cniVersion": "0.3.1",
"name": "mynet",
"type": "ipvlan",
"master": "foo0",
"ipam": {
"range": "192.168.2.230/24",
"configuration_path": "/tmp/whereabouts.conf",
"type": "whereabouts",
"enable_overlapping_ranges": false
}
}`
}

0 comments on commit 92d3cdf

Please sign in to comment.