diff --git a/README.md b/README.md index 5dc3b40..3317009 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ It works with 2 custom resources: - IPClaim IPCidr allows you to create a specific CIDR in the IPAM (IPv4 and IPv6). -IPClaim is used to allocate/claim specific or non specific IPs and child CIDRs in the IPAM. +IPClaim is used to allocate/claim specific or non-specific IPs and child CIDRs in the IPAM. ## Getting Started @@ -74,7 +74,7 @@ spec: specificChildCidr: 172.16.1.0/24 ``` -### Non specific +### Non-specific #### IP diff --git a/internal/controllers/ipcidr_controller_test.go b/internal/controllers/ipcidr_controller_test.go index c14dede..027858e 100644 --- a/internal/controllers/ipcidr_controller_test.go +++ b/internal/controllers/ipcidr_controller_test.go @@ -12,18 +12,19 @@ import ( ipamv1alpha1 "github.com/aamoyel/kubipam/api/v1alpha1" ) +const ( + timeout = time.Second * 4 + interval = time.Second * 1 + validCidrName = "test-cidr" +) + var _ = Describe("IPCidr controller", func() { - const ( - timeout = time.Second * 10 - interval = time.Millisecond * 250 - validCidrName = "test-cidr" - ) Context("When create IPCidr", func() { It("Should change 'registered' field in status and create the cidr in the ipam", func() { ipCidrLookupKey := types.NamespacedName{Name: validCidrName} createdIpCidr := &ipamv1alpha1.IPCidr{} - + By("Creating a new IPCidr") ctx := context.Background() ipcidr := getIpCidr(validCidrName, "192.168.0.0/16") @@ -47,12 +48,12 @@ var _ = Describe("IPCidr controller", func() { By("Creating a new IPCidr with bad prefix") ipCidrName := "bad-prefix" ctx := context.Background() - ipcidr := getIpCidr(ipCidrName, "172.16.0.0/33") + ipcidr := getIpCidr(ipCidrName, "10.10.10.0/33") Expect(k8sClient.Create(ctx, ipcidr)).Should(Succeed()) - ipCidrLookupKey := types.NamespacedName{Name: ipCidrName} - createdIpCidr := &ipamv1alpha1.IPCidr{} By("Checking if the 'registered' status field is set to 'false' for the IPCidr that has a bad prefix") + ipCidrLookupKey := types.NamespacedName{Name: ipCidrName} + createdIpCidr := &ipamv1alpha1.IPCidr{} Eventually(func() (bool, error) { err := k8sClient.Get(ctx, ipCidrLookupKey, createdIpCidr) if err != nil { @@ -65,10 +66,10 @@ var _ = Describe("IPCidr controller", func() { ipCidrName = "overlap-cidr" ipcidr = getIpCidr(ipCidrName, "192.168.1.0/24") Expect(k8sClient.Create(ctx, ipcidr)).Should(Succeed()) - ipCidrLookupKey = types.NamespacedName{Name: ipCidrName} - createdIpCidr = &ipamv1alpha1.IPCidr{} By("Checking if the 'registered' status field is set to 'false' for the IPCidr that overlap an existing registered cidr") + ipCidrLookupKey = types.NamespacedName{Name: ipCidrName} + createdIpCidr = &ipamv1alpha1.IPCidr{} Eventually(func() (bool, error) { err := k8sClient.Get(ctx, ipCidrLookupKey, createdIpCidr) if err != nil { diff --git a/internal/controllers/ipclaim_controller_test.go b/internal/controllers/ipclaim_controller_test.go new file mode 100644 index 0000000..ad5292e --- /dev/null +++ b/internal/controllers/ipclaim_controller_test.go @@ -0,0 +1,66 @@ +package controllers + +import ( + "context" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" + + ipamv1alpha1 "github.com/aamoyel/kubipam/api/v1alpha1" +) + +var _ = Describe("IPClaim controller", func() { + Context("When create a claim for a non specific IP", func() { + It("Should change 'registered' field in status and get an ip from the ipam", func() { + ipClaimCidr := "ipclaim-cidr" + ctx := context.Background() + + By("Creating a new IPCidr") + ipcidr := getIpCidr(ipClaimCidr, "172.16.0.0/16") + Expect(k8sClient.Create(ctx, ipcidr)).Should(Succeed()) + + By("Creating a new IPClaim with bad prefix") + ipClaimName := "test-ip" + ipclaim := getIPClaim("IP", false, ipClaimName, ipClaimCidr) + Expect(k8sClient.Create(ctx, ipclaim)).Should(Succeed()) + + By("Checking if the 'registered' status field is set to 'true'") + ipClaimLookupKey := types.NamespacedName{Name: ipClaimName} + claimedIP := &ipamv1alpha1.IPClaim{} + Eventually(func() (bool, error) { + err := k8sClient.Get(ctx, ipClaimLookupKey, claimedIP) + if err != nil { + return false, err + } + return claimedIP.Status.Registered, nil + }, timeout, interval).Should(Equal(true)) + }) + }) +}) + +func getIPClaim(claimType string, specific bool, name string, cidrName string) *ipamv1alpha1.IPClaim { + claim := &ipamv1alpha1.IPClaim{} + if claimType == "IP" { + if !specific { + claim = &ipamv1alpha1.IPClaim{ + TypeMeta: metav1.TypeMeta{ + APIVersion: ipamv1alpha1.GroupVersion.Group + "/" + ipamv1alpha1.GroupVersion.Version, + Kind: "IPClaim", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: name, + }, + Spec: ipamv1alpha1.IPClaimSpec{ + Type: claimType, + IPCidrRef: &ipamv1alpha1.IPCidrRefSpec{ + Name: cidrName, + }, + }, + } + } + } + + return claim +}