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

Implement dynamic selection of parent prefix from a set of custom fields #90

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

henrybear327
Copy link
Collaborator

@henrybear327 henrybear327 commented Oct 8, 2024

Design

We introduce a new parentPrefixSelector field in the CR. It's a list of custom field key-value mapping, where both the key and value are of the type string. For example

parentPrefixSelector:
    environment: "Production"
    ipVersion: "4"

We now have 3 cases:

  • If only parentPrefix is set, we continue on as-is
  • If only parentPrefixSelector is set, we would take all Prefixes that exactly match all custom field's key-value specified in parentPrefixSelector. We would then pick the first prefix that is able to satisfy the prefixLength requirement as the parent prefix.
  • If we have both parentPrefix and parentPrefixSelector present in the CR, the CRD validation will reject this yaml file

The parentPrefix will be set in the status field ParentPrefix, and this will be used as the source of truth for the rest of the reconcile function.

Known issue

TODO

  • Discuss if using status field to store the computed ParentPrefix value is desired
  • The restoration hash mechanism for parentPrefixSelector would require some documentation, as there might be the case there the picked parent prefix is different for the same parentPrefixSelector (e.g. due to prefix space exhaustion), and thus, the restoration might not work as user anticipated.

Notes

@henrybear327 henrybear327 self-assigned this Oct 8, 2024
@henrybear327 henrybear327 marked this pull request as draft October 8, 2024 11:08
@henrybear327 henrybear327 force-pushed the feat/issue_79 branch 7 times, most recently from 335e04b to 97c512d Compare October 8, 2024 11:25
@henrybear327
Copy link
Collaborator Author

Discuss if using the status field to store the computed ParentPrefix value is desired

We agree that the spec field is read-only, so we wouldn't put any computed ParentPrefix there.

The status field is what we have write access to and where we reflect the relevant internal states to the external user. Thus, we have decided to use it to store the computed ParentPrefix and use it as the source of truth.

@henrybear327
Copy link
Collaborator Author

Improved CRD validation: According to [2], the oneOf validation will only come in the next version.

cc: @alexandernorth @bruelea

Reference:
[1] https://kubernetes.io/blog/2022/09/29/enforce-immutability-using-cel/
[2] https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#validation-ratcheting

@henrybear327 henrybear327 force-pushed the feat/issue_79 branch 2 times, most recently from 787fecd to 1ad0f81 Compare October 10, 2024 21:25
@henrybear327 henrybear327 marked this pull request as ready for review October 10, 2024 21:27
@henrybear327
Copy link
Collaborator Author

henrybear327 commented Oct 10, 2024

For some reason, when we are querying for custom fields, for example using this URL http://localhost:8080/ipam/prefixes/?q=&cf_poolName=Pool+2&cf_environment=production, the value of the custom field cf_environment will be capitalized automatically. This has an impact on us as we can only enter the value in custom fields in capitalized form.

Reproduction step:

  • type in http://localhost:8080/ipam/prefixes/?q=&cf_poolName=Pool+2&cf_environment=production in your browser
  • you can see that the search that went through NetBox is actually http://localhost:8080/ipam/prefixes/?q=&cf_poolName=Pool+2&cf_environment=Production

The demo SQL files have adhered to this "observation", but I am not able to wrap my head around this, since for the restoration hash, we have no issue with it

cc: @alexandernorth @bruelea

@henrybear327
Copy link
Collaborator Author

To maintain the backward compatibility of the restoration hash and extend it to support parentPrefixSelection, we have made the following changes to the hash:

  • append the parentPrefixSelection key-value pairs to the end of the hash during hash calculation. The exact string that is appended is done by sorting the keys first before appending, so in the case that only the key order of the parentPrefixSelection changes, the hash value won't be affected

With the aforementioned change, the hash can by design distinguish the prefix claimed by ParentPrefix or parentPrefixSelection, thus, making the restoration process relying on the hash achievable.

@@ -132,9 +132,19 @@ func (r *PrefixReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
return ctrl.Result{}, err
}

if prefixClaim.Status.ParentPrefix == "" {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this check required? I would expect the prefix CR not to be created if the ParentPrefix wasn't calculated.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am not sure if this is required, but I would like to log this in case we change more code in the future and this condition gets tripped somehow. What do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

internal/controller/prefixclaim_controller.go Show resolved Hide resolved
pkg/netbox/api/ip_address_claim.go Outdated Show resolved Hide resolved
@henrybear327
Copy link
Collaborator Author

Comment from @bruelea: add the newly created sample to the kustomization: config/samples/kustomization.yaml

Done

@jitendrs
Copy link
Collaborator

Please add me as reviewer.

@henrybear327 henrybear327 force-pushed the feat/issue_79 branch 2 times, most recently from 2181370 to 5c97a94 Compare October 15, 2024 08:45
Introduce new conditions for ParentPrefixSelector
Add new custom fields and prefixes for testing ParentPrefixSelector
Signed-off-by: Hoanganh.Mai <[email protected]>
Signed-off-by: Chun-Hung Tseng <[email protected]>
@henrybear327
Copy link
Collaborator Author

henrybear327 commented Oct 15, 2024

For some reason, when we are querying for custom fields, for example using this URL http://localhost:8080/ipam/prefixes/?q=&cf_poolName=Pool+2&cf_environment=production, the value of the custom field cf_environment will be capitalized automatically. This has an impact on us as we can only enter the value in custom fields in capitalized form.

Reproduction step:

  • type in http://localhost:8080/ipam/prefixes/?q=&cf_poolName=Pool+2&cf_environment=production in your browser
  • you can see that the search that went through NetBox is actually http://localhost:8080/ipam/prefixes/?q=&cf_poolName=Pool+2&cf_environment=Production

The demo SQL files have adhered to this "observation", but I am not able to wrap my head around this, since for the restoration hash, we have no issue with it

cc: @alexandernorth @bruelea

So as @alexandernorth explained, on browsers, they might modify the URLs. But for the API endpoints what we send is directly being processed.

So in the case, this is not an issue for us when querying using API endpoints! But when we are doing debugging using URLs, we might run into issues.

Add unit test for restoration hash backward compatibility
Support multiple custom field in a query
Improve CRD validation to reject having both ParentPrefix and ParentPrefixSelector
Reorder column sequence for PrefixClaim
Update kustomization.yaml

Notes:
According to [2], the `oneOf` validation will only come in the next version

Reference:
[1] https://kubernetes.io/blog/2022/09/29/enforce-immutability-using-cel/
[2] https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#validation-ratcheting

Signed-off-by: Hoanganh.Mai <[email protected]>
Signed-off-by: Chun-Hung Tseng <[email protected]>
Copy link
Collaborator

@jitendrs jitendrs left a comment

Choose a reason for hiding this comment

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

Great work! The changes look solid, and everything is well-structured. I believe it aligns with our expectations overall. However, the section concerning site could be improved. I understand there is already open issue #100 for this expectation.

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.

Implement dynamic selection of parent prefix from a set of custom fields
3 participants