Skip to content

Namespace is ignored when using ReadAllPrefixCidrs #131

@moonlord899

Description

@moonlord899

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
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions