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

Namespace is ignored when using ReadAllPrefixCidrs #131

Open
moonlord899 opened this issue Nov 21, 2023 · 1 comment
Open

Namespace is ignored when using ReadAllPrefixCidrs #131

moonlord899 opened this issue Nov 21, 2023 · 1 comment

Comments

@moonlord899
Copy link

Using file.json ( other storage types untested ), creating new prefixes in separate namespaces works fine. However, when usingReadAllPrefixCidrs, it always returns values from the root namespace, completely ignoring the ones in the namespace it should query.

Code sample:

package main

import (
	context "context"
	"log"
	"time"

	"github.com/metal-stack/go-ipam"
)

var (
	rootNamespace = "root"
	ns            = "tenant-a"
	rootPrfx      = "10.76.0.0/25"
	contextPrfx   = "10.76.0.128/25"
)

func main() {

	rootCtx := context.Background()
	rootCtx, cancel := context.WithTimeout(rootCtx, 5*time.Second)
	defer cancel()

	storage := ipam.NewLocalFile(rootCtx, "file.json")

	ipamer := ipam.NewWithStorage(storage)

	ipamer.CreateNamespace(rootCtx, ns)
	log.Printf("\nCreated namespace %v", ns)

	log.Printf("\nCreating CIDR %v in %v namespace", rootPrfx, rootNamespace)
	_, err := ipamer.NewPrefix(rootCtx, rootPrfx)

	if err != nil {
		panic(err.Error())
	}

	tenantCtx := ipam.NewContextWithNamespace(rootCtx, ns)

	log.Printf("\nCreating CIDR %v in %v namespace", contextPrfx, ns)
	_, err = ipamer.NewPrefix(tenantCtx, contextPrfx)

	if err != nil {
		panic(err.Error())
	}

	rootCidrs, err := ipamer.ReadAllPrefixCidrs(rootCtx)
	if err != nil {
		panic(err.Error())
	}

	log.Printf("Discovered context cidr %v for %v", rootCidrs, rootNamespace)

	contextCidrs, err := ipamer.ReadAllPrefixCidrs(tenantCtx)
	if err != nil {
		panic(err.Error())
	}

	log.Printf("Discovered context cidr %v for %v", contextCidrs, ns)
}

Current result:

2023/11/21 16:41:56
Created namespace tenant-a
2023/11/21 16:41:56
Creating CIDR 10.76.0.0/25 in root namespace
2023/11/21 16:41:56
Creating CIDR 10.76.0.128/25 in tenant-a namespace
2023/11/21 16:41:56 Discovered context cidr [10.76.0.0/25] for root
2023/11/21 16:41:56 Discovered context cidr [10.76.0.0/25] for tenant-a

Expected result:

2023/11/21 16:41:56
Created namespace tenant-a
2023/11/21 16:41:56
Creating CIDR 10.76.0.0/25 in root namespace
2023/11/21 16:41:56
Creating CIDR 10.76.0.128/25 in tenant-a namespace
2023/11/21 16:41:56 Discovered context cidr [10.76.0.0/25] for root
2023/11/21 16:41:56 Discovered context cidr [10.76.0.128/25] for tenant-a

For reference, the json file that is being created looks like this:

{
  "root": {
    "10.76.0.0/25": {
      "Cidr": "10.76.0.0/25",
      "ParentCidr": "",
      "Namespace": "",
      "AvailableChildPrefixes": {},
      "ChildPrefixLength": 0,
      "IsParent": false,
      "IPs": {
        "10.76.0.0": true,
        "10.76.0.127": true
      },
      "Version": 0
    }
  },
  "tenant-a": {
    "10.76.0.128/25": {
      "Cidr": "10.76.0.128/25",
      "ParentCidr": "",
      "Namespace": "",
      "AvailableChildPrefixes": {},
      "ChildPrefixLength": 0,
      "IsParent": false,
      "IPs": {
        "10.76.0.128": true,
        "10.76.0.255": true
      },
      "Version": 0
    }
  }
}
@majst01
Copy link
Contributor

majst01 commented Dec 7, 2023

@nazarew can you please check

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

No branches or pull requests

2 participants