Skip to content

Commit 6ef2a1b

Browse files
authored
Add support for built-in field family in ParentPrefixSelector (#138)
Add support for built-in field `family`
1 parent f002bf8 commit 6ef2a1b

6 files changed

+23
-6
lines changed

ParentPrefixSelectorGuide.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ spec:
2828
parentPrefixSelector:
2929
tenant: "MY_TENANT"
3030
site: "DM-Buffalo"
31+
family: "IPv4"
3132
environment: "Production"
3233
poolName: "Pool 1"
3334
```
@@ -47,10 +48,10 @@ The map contains a set of query conditions for selecting a set of prefixes that
4748
The query conditions will be chained by the AND operator, and exact match of the keys and values will be performed.
4849

4950
The fields that can be used as query conditions in the `parentPrefixSelector` are:
50-
- `tenant` and `site` (in lowercase characters)
51-
- these 2 fields are built-in fields from NetBox, so you do *not* need to create custom fields for them
52-
- please provide the *name*, not the *slug* value
53-
- if the entry for tenant or site is missing, it will *not* inherit from the tenant and site from the Spec
51+
- `tenant`, `site`, and `family` (in lowercase characters)
52+
- these fields are built-in fields from NetBox, so you do *not* need to create custom fields for them
53+
- please provide the *name*, not the *slug* value for `tenant` and `site`
54+
- if the entry for `tenant` and `site` fields is missing, it will *not* inherit from the Spec
5455
- custom fields
5556
- the data types tested and supported so far are `string`, `integer`, and `boolean`
5657
- for `boolean` type, please use `true` and `false` as the value

api/v1/prefixclaim_types.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ type PrefixClaimSpec struct {
3939
// The `parentPrefixSelector` is a key-value map, where all the entries are of data type `<string-string>`
4040
// The map contains a set of query conditions for selecting a set of prefixes that can be used as the parent prefix
4141
// The query conditions will be chained by the AND operator, and exact match of the keys and values will be performed
42-
// 2 built-in fields, namely `tenant` and `site`, along with custom fields, can be used
42+
// The built-in fields `tenant`, `site`, and `family`, along with custom fields, can be used
4343
// For more information, please see ParentPrefixSelectorGuide.md
4444
//+kubebuilder:validation:XValidation:rule="self == oldSelf",message="Field 'parentPrefixSelector' is immutable"
45+
//+kubebuilder:validation:XValidation:rule="!has(self.family) || (self.family == 'IPv4' || self.family == 'IPv6')"
4546
ParentPrefixSelector map[string]string `json:"parentPrefixSelector,omitempty"`
4647

4748
//+kubebuilder:validation:Required

config/crd/bases/netbox.dev_prefixclaims.yaml

+3-1
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,14 @@ spec:
7777
The `parentPrefixSelector` is a key-value map, where all the entries are of data type `<string-string>`
7878
The map contains a set of query conditions for selecting a set of prefixes that can be used as the parent prefix
7979
The query conditions will be chained by the AND operator, and exact match of the keys and values will be performed
80-
2 built-in fields, namely `tenant` and `site`, along with custom fields, can be used
80+
The built-in fields `tenant`, `site`, and `family`, along with custom fields, can be used
8181
For more information, please see ParentPrefixSelectorGuide.md
8282
type: object
8383
x-kubernetes-validations:
8484
- message: Field 'parentPrefixSelector' is immutable
8585
rule: self == oldSelf
86+
- rule: '!has(self.family) || (self.family == ''IPv4'' || self.family
87+
== ''IPv6'')'
8688
prefixLength:
8789
pattern: ^\/[0-9]|[1-9][0-9]|1[01][0-9]|12[0-8]$
8890
type: string

config/samples/netbox_v1_prefixclaim_parentprefixselector.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ spec:
1616
# if the entry for tenant or site is missing, it will *not* inherit from the tenant and site from the Spec
1717
tenant: "MY_TENANT" # Use the `name` value instead of the `slug` value
1818
site: "DM-Buffalo" # Use the `name` value instead of the `slug` value
19+
family: "IPv4" # Can only be either IPv4 or IPv6"
1920

2021
# custom fields of your interest
2122
environment: "Production"

pkg/netbox/api/errors.go

+1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ var (
2222
ErrParentPrefixExhausted = errors.New("parent prefix exhausted")
2323
ErrParentPrefixNotFound = errors.New("parent prefix not found")
2424
ErrWrongMatchingPrefixSubnetFormat = errors.New("wrong matchingPrefix subnet format")
25+
ErrInvalidIpFamily = errors.New("invalid IP Family")
2526
)

pkg/netbox/api/prefix_claim.go

+11
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@ func (r *NetboxClient) GetAvailablePrefixByParentPrefixSelector(prefixClaimSpec
114114
fieldEntries["site_id"] = strconv.Itoa(int(details.Id))
115115
}
116116

117+
if family, ok := prefixClaimSpec.ParentPrefixSelector["family"]; ok {
118+
if family == "IPv4" {
119+
family = "4"
120+
} else if family == "IPv6" {
121+
family = "6"
122+
} else {
123+
return nil, ErrInvalidIpFamily
124+
}
125+
fieldEntries["family"] = family
126+
}
127+
117128
var conditions func(co *runtime.ClientOperation)
118129
parentPrefixSelectorEntries := make([]CustomFieldEntry, 0, len(prefixClaimSpec.ParentPrefixSelector))
119130
for k, v := range prefixClaimSpec.ParentPrefixSelector {

0 commit comments

Comments
 (0)