Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix overwriting of OverlappingRanges by mergo #257

Conversation

toelke
Copy link
Contributor

@toelke toelke commented Aug 4, 2022

What this PR does / why we need it:

When configuring enable_overlapping_ranges in the pod-configuration (e.g. by using a multus NetworkAttachmentDefinition, the parsing of the configuration makes an error. This was already noticed in #164 but I did not manage to get that solution to work.

What happens is that parsing the flatfile (where enable_overlapping_ranges is not set) results in an in-memory representation of the configuration where OverlappingRanges is set to true; this will overwrite the pod-specific configuration of false.

@toelke toelke marked this pull request as ready for review August 4, 2022 09:26
@toelke toelke requested a review from dougbtv as a code owner August 4, 2022 09:26
Copy link
Collaborator

@maiqueb maiqueb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

We'd need a unit test to verify it does what we want it to though.

@toelke toelke force-pushed the configurable_OverlappingRanges branch from 063a273 to 244af37 Compare August 19, 2022 08:31
@toelke
Copy link
Contributor Author

toelke commented Aug 19, 2022

I have expanded an existing test. It indeed fails before the fix and is successful afterwards.

@maiqueb
Copy link
Collaborator

maiqueb commented Aug 19, 2022

I have expanded an existing test. It indeed fails before the fix and is successful afterwards.

I would still prefer to see a new unit test, focused explicitly on this - that is easier to follow, and will work as documentation of what we expect around this flag.

Modifying an existing test does the job, but it does not work for well for documenting the behavior.
Since we're doing BDD on (most of) the tests, I think that should be the focus.

I would add something like:

        var globalConf string

        BeforeEach(func() {
        		globalConf = `{
      "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"
    }`

		Expect(ioutil.WriteFile("/tmp/whereabouts.conf", []byte(globalconf), 0755)).To(Succeed())
        })
        
	It("overlapping range can be set", func() {
		ipamconfig, _, err := LoadIPAMConfig([]byte(generateIPAMConfWithOverlappingRanges()), "")
		Expect(err).NotTo(HaveOccurred())
		
		Expect(ipamconfig.OverlappingRanges).To(BeTrue())
	})
	
	It("overlapping range can be disabled", func() {
		ipamconfig, _, err := LoadIPAMConfig([]byte(generateIPAMConfWithoutOverlappingRanges()), "")
		Expect(err).NotTo(HaveOccurred())
		
		Expect(ipamconfig.OverlappingRanges).To(BeFalse())
	})

func generateIPAMConfWithOverlappingRanges() string {
	return `{
      "cniVersion": "0.3.1",
      "name": "mynet",
      "type": "ipvlan",
      "master": "foo0",
      "ipam": {
        "configuration_path": "/tmp/whereabouts.conf",
        "type": "whereabouts",
        "range": "192.168.2.230/24",
        "range_start": "192.168.2.223",
        "gateway": "192.168.10.1",
        "leader_lease_duration": 3000,
        "leader_renew_deadline": 2000,
        "leader_retry_period": 1000
        "leader_retry_period": 1000,
        "enable_overlapping_ranges": true
      }
      }`
}

func generateIPAMConfWithoutOverlappingRanges() string {
	return `{
      "cniVersion": "0.3.1",
      "name": "mynet",
      "type": "ipvlan",
      "master": "foo0",
      "ipam": {
        "configuration_path": "/tmp/whereabouts.conf",
        "type": "whereabouts",
        "range": "192.168.2.230/24",
        "range_start": "192.168.2.223",
        "gateway": "192.168.10.1",
        "leader_lease_duration": 3000,
        "leader_renew_deadline": 2000,
        "leader_retry_period": 1000
        "leader_retry_period": 1000,
        "enable_overlapping_ranges": false
      }
      }`
}

You could even use a table for these 2 tests, thus avoiding the duplicate test code (pass the configuration and the matcher as inputs to the func, for instance).

@toelke toelke force-pushed the configurable_OverlappingRanges branch from 244af37 to a3dd4f6 Compare August 19, 2022 10:50
@toelke
Copy link
Contributor Author

toelke commented Aug 19, 2022

You are correct. I also changed your proposed test to actually overwrite the flatfile in both directions -- the original code does not allow overwriting a true with a false. The fixed code allows both directions.

@toelke toelke force-pushed the configurable_OverlappingRanges branch from a3dd4f6 to e8f0988 Compare August 19, 2022 10:52
@coveralls
Copy link

coveralls commented Aug 19, 2022

Pull Request Test Coverage Report for Build 2891521153

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.1%) to 65.15%

Totals Coverage Status
Change from base Build 2889580130: 0.1%
Covered Lines: 931
Relevant Lines: 1429

💛 - Coveralls

pkg/config/config_test.go Outdated Show resolved Hide resolved
"gateway": "192.168.5.5",
"enable_overlapping_ranges": false
}`
Expect(ioutil.WriteFile("/tmp/whereabouts.conf", []byte(globalConf), 0755)).To(Succeed())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to me we're leaking this file after the execution, but I've noticed that also happens for other tests.

I'll handle that in a follow-up.

@toelke toelke force-pushed the configurable_OverlappingRanges branch from e8f0988 to de639f1 Compare August 19, 2022 12:45
@maiqueb
Copy link
Collaborator

maiqueb commented Aug 19, 2022

You should rebase latest master, to give the e2e tests a chance to work - with the current state of your tree, it'll fail trying to install the CNI plugins in the k8s nodes.

@toelke toelke force-pushed the configurable_OverlappingRanges branch from de639f1 to d880900 Compare August 19, 2022 18:52
@toelke
Copy link
Contributor Author

toelke commented Aug 19, 2022

Done

Copy link
Collaborator

@maiqueb maiqueb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had forgotten to hit the approve button - sorry for that :)

@maiqueb maiqueb merged commit 92d3cdf into k8snetworkplumbingwg:master Nov 18, 2022
@toelke toelke deleted the configurable_OverlappingRanges branch November 18, 2022 12:53
@coveralls
Copy link

coveralls commented Oct 7, 2024

Pull Request Test Coverage Report for Build 2888908291

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.1%) to 65.15%

Totals Coverage Status
Change from base Build 2882834733: 0.1%
Covered Lines: 931
Relevant Lines: 1429

💛 - Coveralls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants