Skip to content

Commit

Permalink
fix 🐛(claim): update ipclaim conditions to use ipCidrRef when request…
Browse files Browse the repository at this point in the history
…ing in the CR

Signed-off-by: Alan Amoyel <[email protected]>
  • Loading branch information
aamoyel committed Aug 19, 2024
1 parent ab1c6be commit f251777
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Image URL to use all building/pushing image targets
IMG ?= gcr.io/aamoyel/kubipam:v0.1.2
IMG ?= gcr.io/aamoyel/kubipam:v0.1.3
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.29.0

Expand Down
13 changes: 7 additions & 6 deletions api/v1alpha1/ipclaim_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ func (r *IPClaim) validateClaim() error {
func (r *IPClaim) validateSpec() *field.Error {
// The field helpers from the kubernetes API machinery help us return nicely
// structured validation errors.

if r.Spec.IPCidrRef != nil {
if r.Spec.IPCidrRef.Name == "" {
return field.Forbidden(field.NewPath("spec").Child("ipCidrRef").Child("name"), "'ipCidrRef.name' must be set when using 'ipCidrRef'")
}
}

if r.Spec.Type == "IP" {
noRefmsg := field.Forbidden(
field.NewPath("spec").Child("ipCidrRef").Child("name"),
Expand All @@ -96,9 +103,6 @@ func (r *IPClaim) validateSpec() *field.Error {
if r.Spec.IPCidrRef == nil {
return noRefmsg
}
if r.Spec.IPCidrRef.Name == "" {
return noRefmsg
}
}

if r.Spec.Type == "CIDR" {
Expand All @@ -110,9 +114,6 @@ func (r *IPClaim) validateSpec() *field.Error {
if r.Spec.IPCidrRef == nil {
return noRefmsg
}
if r.Spec.IPCidrRef.Name == "" {
return noRefmsg
}
}

if r.Spec.SpecificChildCidr == "" && r.Spec.CidrPrefixLength == 0 {
Expand Down
2 changes: 1 addition & 1 deletion config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@ kind: Kustomization
images:
- name: aamoyel/kubipam
newName: gcr.io/aamoyel/kubipam
newTag: v0.1.2
newTag: v0.1.3
2 changes: 1 addition & 1 deletion deploy/bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ spec:
- --metrics-bind-address=0.0.0.0:8080
command:
- /manager
image: gcr.io/aamoyel/kubipam:v0.1.2
image: gcr.io/aamoyel/kubipam:v0.1.3
livenessProbe:
httpGet:
path: /healthz
Expand Down
8 changes: 8 additions & 0 deletions examples/claim/ipclaim7.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
apiVersion: ipam.didactiklabs.io/v1alpha1
kind: IPClaim
metadata:
name: test7
spec:
type: CIDR
cidrPrefixLength: 108
10 changes: 10 additions & 0 deletions examples/claim/ipclaim8.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
apiVersion: ipam.didactiklabs.io/v1alpha1
kind: IPClaim
metadata:
name: test8
spec:
type: CIDR
ipCidrRef:
name: test-v6
cidrPrefixLength: 108
10 changes: 2 additions & 8 deletions internal/controllers/ipcidr_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,7 @@ func IPCidrProgressing(o *ipamv1alpha1.IPCidr) {
})
}

func (r *IPCidrReconciler) patchIPCidrStatus(
ctx context.Context,
ipCidr *ipamv1alpha1.IPCidr,
) error {
func (r *IPCidrReconciler) patchIPCidrStatus(ctx context.Context, ipCidr *ipamv1alpha1.IPCidr) error {
key := client.ObjectKeyFromObject(ipCidr)
latest := &ipamv1alpha1.IPCidr{}
if err := r.Client.Get(ctx, key, latest); err != nil {
Expand Down Expand Up @@ -337,10 +334,7 @@ func (r *IPCidrReconciler) isCidrOverlapping(ctx context.Context, cr *ipamv1alph
}

// ipCidrReconcileRequests returns a list of reconcile.Request based on the ipcidr resource.
func ipCidrReconcileRequests(
ctx context.Context,
mgr manager.Manager,
) ([]reconcile.Request, error) {
func ipCidrReconcileRequests(ctx context.Context, mgr manager.Manager) ([]reconcile.Request, error) {
IPCidrList := &ipamv1alpha1.IPCidrList{}
err := mgr.GetClient().List(ctx, IPCidrList)
if err != nil {
Expand Down
51 changes: 21 additions & 30 deletions internal/controllers/ipclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,7 @@ func IPClaimProgressing(o *ipamv1alpha1.IPClaim) {
})
}

func (r *IPClaimReconciler) patchIPClaimStatus(
ctx context.Context,
ipClaim *ipamv1alpha1.IPClaim,
) error {
func (r *IPClaimReconciler) patchIPClaimStatus(ctx context.Context, ipClaim *ipamv1alpha1.IPClaim) error {
key := client.ObjectKeyFromObject(ipClaim)
latest := &ipamv1alpha1.IPClaim{}
if err := r.Client.Get(ctx, key, latest); err != nil {
Expand Down Expand Up @@ -279,11 +276,7 @@ func (r *IPClaimReconciler) reconcileIPClaim(ctx context.Context, cr *ipamv1alph
return nil
}

func (r *IPClaimReconciler) getIpAddr(
ctx context.Context,
cr *ipamv1alpha1.IPClaim,
parentCidr string,
) error {
func (r *IPClaimReconciler) getIpAddr(ctx context.Context, cr *ipamv1alpha1.IPClaim, parentCidr string) error {
claim, err := r.Ipamer.AcquireSpecificIP(ctx, parentCidr, cr.Spec.SpecificIPAddress)
if claim == nil {
err = fmt.Errorf("%w", err)
Expand All @@ -307,10 +300,7 @@ func (r *IPClaimReconciler) getIpAddr(
return nil
}

func (r *IPClaimReconciler) getChildCidr(
ctx context.Context,
cr *ipamv1alpha1.IPClaim,
) (err error) {
func (r *IPClaimReconciler) getChildCidr(ctx context.Context, cr *ipamv1alpha1.IPClaim) (err error) {
var (
claim *goipam.Prefix
cidrCr *ipamv1alpha1.IPCidr
Expand Down Expand Up @@ -348,15 +338,15 @@ func (r *IPClaimReconciler) getChildCidr(
}
return err
}
} else if cr.Spec.SpecificChildCidr == "" {
} else {
// Check if we need to use a specific parent CIDR
if cr.Spec.IPCidrRef.Name != "" {
if cr.Spec.IPCidrRef != nil {
// Get parent cidr
cidrCR, err := r.getParentCidr(ctx, cr)
cidrCr, err = r.getParentCidr(ctx, cr)
if err != nil {
return err
}
if !cidrCR.Status.Registered {
if !cidrCr.Status.Registered {
err := fmt.Errorf("the cidr resource in the IPCidrRefSpec for the %s claim is not in a ready state", cr.Name)
setIPClaimErrorStatus(cr, err)
if patchStatusErr := r.patchIPClaimStatus(ctx, cr); patchStatusErr != nil {
Expand All @@ -366,6 +356,17 @@ func (r *IPClaimReconciler) getChildCidr(
}
return nil
}
claim, err = r.Ipamer.AcquireChildPrefix(ctx, cidrCr.Spec.Cidr, cr.Spec.CidrPrefixLength)
if err != nil {
err = fmt.Errorf("Unable to get a child cidr for your prefix lengh in the '"+cr.Spec.IPCidrRef.Name+"' , err: %w", err)
setIPClaimErrorStatus(cr, err)
if patchStatusErr := r.patchIPClaimStatus(ctx, cr); patchStatusErr != nil {
err = kerror.NewAggregate([]error{err, patchStatusErr})
err = fmt.Errorf("unable to patch status after reconciliation failed: %w", err)
return err
}
return err
}
} else {
// find available parent cidr for this prefix length
cidrList := &ipamv1alpha1.IPCidrList{}
Expand Down Expand Up @@ -425,10 +426,7 @@ func setIPClaimErrorStatus(cr *ipamv1alpha1.IPClaim, err error) {
}

// ipClaimReconcileRequests returns a list of reconcile.Request based on the ipclaim resource.
func ipClaimReconcileRequests(
ctx context.Context,
mgr manager.Manager,
) ([]reconcile.Request, error) {
func ipClaimReconcileRequests(ctx context.Context, mgr manager.Manager) ([]reconcile.Request, error) {
IPClaimList := &ipamv1alpha1.IPClaimList{}
err := mgr.GetClient().List(ctx, IPClaimList)
if err != nil {
Expand All @@ -446,10 +444,7 @@ func ipClaimReconcileRequests(
return requests, nil
}

func (r *IPClaimReconciler) getParentCidr(
ctx context.Context,
cr *ipamv1alpha1.IPClaim,
) (*ipamv1alpha1.IPCidr, error) {
func (r *IPClaimReconciler) getParentCidr(ctx context.Context, cr *ipamv1alpha1.IPClaim) (*ipamv1alpha1.IPCidr, error) {
cidrCR := &ipamv1alpha1.IPCidr{}
cidrNamepacedName := types.NamespacedName{
Name: cr.Spec.IPCidrRef.Name,
Expand Down Expand Up @@ -514,11 +509,7 @@ func (r *IPClaimReconciler) initRegisteredClaims(ctx context.Context) error {
}

// checkParentCidr return an error when the parent cidr is not registered in the ipam.
func (r *IPClaimReconciler) checkParentCidr(
ctx context.Context,
claimCr *ipamv1alpha1.IPClaim,
parentCidr string,
) error {
func (r *IPClaimReconciler) checkParentCidr(ctx context.Context, claimCr *ipamv1alpha1.IPClaim, parentCidr string) error {
_, err := r.Ipamer.PrefixFrom(ctx, parentCidr)
if err != nil {
err = fmt.Errorf("unable to find parent cidr in the ipam, err: %w", err)
Expand Down

0 comments on commit f251777

Please sign in to comment.