Skip to content

Commit 19379d6

Browse files
committed
Add example of excluding subnets pattern
1 parent 544b3ca commit 19379d6

13 files changed

+560
-1
lines changed

example/v1/tests/stableconfigtypes.example.openshift.io/Example.yaml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,58 @@ tests:
103103
immutableField: foo
104104
subdomainNameField: foo.-bar.baz
105105
expectedError: "spec.subdomainNameField: Invalid value: \"string\": a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character."
106+
- name: Should allow subnets without exclusions
107+
initial: |
108+
apiVersion: example.openshift.io/v1
109+
kind: StableConfigType
110+
spec:
111+
immutableField: foo
112+
subnetsWithExclusions:
113+
subnets:
114+
- 192.168.0.0/16
115+
expected: |
116+
apiVersion: example.openshift.io/v1
117+
kind: StableConfigType
118+
spec:
119+
immutableField: foo
120+
nonZeroDefault: 8
121+
subnetsWithExclusions:
122+
subnets:
123+
- 192.168.0.0/16
124+
- name: Should allow subnets with exclusions within the subnet
125+
initial: |
126+
apiVersion: example.openshift.io/v1
127+
kind: StableConfigType
128+
spec:
129+
immutableField: foo
130+
subnetsWithExclusions:
131+
subnets:
132+
- 192.168.0.0/16
133+
excludeSubnets:
134+
- 192.168.0.0/24
135+
expected: |
136+
apiVersion: example.openshift.io/v1
137+
kind: StableConfigType
138+
spec:
139+
immutableField: foo
140+
nonZeroDefault: 8
141+
subnetsWithExclusions:
142+
subnets:
143+
- 192.168.0.0/16
144+
excludeSubnets:
145+
- 192.168.0.0/24
146+
- name: Should not allow subnets with exclusions not within the subnet
147+
initial: |
148+
apiVersion: example.openshift.io/v1
149+
kind: StableConfigType
150+
spec:
151+
immutableField: foo
152+
subnetsWithExclusions:
153+
subnets:
154+
- 192.168.0.0/16
155+
excludeSubnets:
156+
- 10.0.0.0/8
157+
expectedError: "spec.subnetsWithExclusions.excludeSubnets: Invalid value: \"object\": excludeSubnets must be subnetworks of the networks specified in the subnets field"
106158
onUpdate:
107159
- name: Should not allow removal of a tech preview field
108160
initial: |

example/v1/types_stable.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ type StableConfigTypeSpec struct {
9696
// +kubebuilder:validation:MaxLength:=253
9797
// +optional
9898
SubdomainNameField string `json:"subdomainNameField,omitempty"`
99+
100+
// subnetsWithExclusions demonstrates how to validate a list of subnets with exclusions
101+
// +optional
102+
SubnetsWithExclusions SubnetsWithExclusions `json:"subnetsWithExclusions,omitempty"`
99103
}
100104

101105
// SetValue defines the types allowed in string set type
@@ -178,6 +182,33 @@ type StableConfigTypeStatus struct {
178182
ImmutableField string `json:"immutableField,omitempty"`
179183
}
180184

185+
// SubnetsWithExclusions is used to validate a list of subnets with exclusions.
186+
// It demonstrates how exclusions should be validated as subnetworks of the networks listed in the subnets field.
187+
// +kubebuilder:validation:XValidation:rule="!has(self.excludeSubnets) || self.excludeSubnets.all(e, self.subnets.exists(s, cidr(s).containsCIDR(cidr(e))))",message="excludeSubnets must be subnetworks of the networks specified in the subnets field",fieldPath=".excludeSubnets"
188+
type SubnetsWithExclusions struct {
189+
// subnets is a list of subnets.
190+
// It may contain up to 2 subnets.
191+
// The list may be either 1 IPv4 subnet, 1 IPv6 subnet, or 1 of each.
192+
// +kubebuilder:validation:XValidation:rule="size(self) != 2 || !isCIDR(self[0]) || !isCIDR(self[1]) || cidr(self[0]).ip().family() != cidr(self[1]).ip().family()",message="subnets must not contain 2 subnets of the same IP family"
193+
// +kubebuilder:validation:MinItems=1
194+
// +kubebuilder:validation:MaxItems=2
195+
// +listType=atomic
196+
// +required
197+
Subnets []CIDR `json:"subnets"`
198+
199+
// excludeSubnets is a list of CIDR exclusions.
200+
// The subnets in this list must be subnetworks of the subnets in the subnets list.
201+
// +kubebuilder:validation:MaxItems=25
202+
// +optional
203+
ExcludeSubnets []CIDR `json:"excludeSubnets,omitempty"`
204+
}
205+
206+
// CIDR is used to validate a CIDR notation network.
207+
// The longest CIDR notation is 43 characters.
208+
// +kubebuilder:validation:XValidation:rule="isCIDR(self)",message="value must be a valid CIDR"
209+
// +kubebuilder:validation:MaxLength:=43
210+
type CIDR string
211+
181212
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
182213
// +openshift:compatibility-gen:level=1
183214

example/v1/zz_generated.crd-manifests/0000_50_my-operator_01_stableconfigtypes-CustomNoUpgrade.crd.yaml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,56 @@ spec:
162162
alphanumeric characters, '-' or '.', and must start and end with
163163
an alphanumeric character.
164164
rule: '!format.dns1123Subdomain().validate(self).hasValue()'
165+
subnetsWithExclusions:
166+
description: subnetsWithExclusions demonstrates how to validate a
167+
list of subnets with exclusions
168+
properties:
169+
excludeSubnets:
170+
description: |-
171+
excludeSubnets is a list of CIDR exclusions.
172+
The subnets in this list must be subnetworks of the subnets in the subnets list.
173+
items:
174+
description: |-
175+
CIDR is used to validate a CIDR notation network.
176+
The longest CIDR notation is 43 characters.
177+
maxLength: 43
178+
type: string
179+
x-kubernetes-validations:
180+
- message: value must be a valid CIDR
181+
rule: isCIDR(self)
182+
maxItems: 25
183+
type: array
184+
subnets:
185+
description: |-
186+
subnets is a list of subnets.
187+
It may contain up to 2 subnets.
188+
The list may be either 1 IPv4 subnet, 1 IPv6 subnet, or 1 of each.
189+
items:
190+
description: |-
191+
CIDR is used to validate a CIDR notation network.
192+
The longest CIDR notation is 43 characters.
193+
maxLength: 43
194+
type: string
195+
x-kubernetes-validations:
196+
- message: value must be a valid CIDR
197+
rule: isCIDR(self)
198+
maxItems: 2
199+
minItems: 1
200+
type: array
201+
x-kubernetes-list-type: atomic
202+
x-kubernetes-validations:
203+
- message: subnets must not contain 2 subnets of the same IP family
204+
rule: size(self) != 2 || !isCIDR(self[0]) || !isCIDR(self[1])
205+
|| cidr(self[0]).ip().family() != cidr(self[1]).ip().family()
206+
required:
207+
- subnets
208+
type: object
209+
x-kubernetes-validations:
210+
- fieldPath: .excludeSubnets
211+
message: excludeSubnets must be subnetworks of the networks specified
212+
in the subnets field
213+
rule: '!has(self.excludeSubnets) || self.excludeSubnets.all(e, self.subnets.exists(s,
214+
cidr(s).containsCIDR(cidr(e))))'
165215
required:
166216
- immutableField
167217
type: object

example/v1/zz_generated.crd-manifests/0000_50_my-operator_01_stableconfigtypes-Default.crd.yaml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,56 @@ spec:
157157
alphanumeric characters, '-' or '.', and must start and end with
158158
an alphanumeric character.
159159
rule: '!format.dns1123Subdomain().validate(self).hasValue()'
160+
subnetsWithExclusions:
161+
description: subnetsWithExclusions demonstrates how to validate a
162+
list of subnets with exclusions
163+
properties:
164+
excludeSubnets:
165+
description: |-
166+
excludeSubnets is a list of CIDR exclusions.
167+
The subnets in this list must be subnetworks of the subnets in the subnets list.
168+
items:
169+
description: |-
170+
CIDR is used to validate a CIDR notation network.
171+
The longest CIDR notation is 43 characters.
172+
maxLength: 43
173+
type: string
174+
x-kubernetes-validations:
175+
- message: value must be a valid CIDR
176+
rule: isCIDR(self)
177+
maxItems: 25
178+
type: array
179+
subnets:
180+
description: |-
181+
subnets is a list of subnets.
182+
It may contain up to 2 subnets.
183+
The list may be either 1 IPv4 subnet, 1 IPv6 subnet, or 1 of each.
184+
items:
185+
description: |-
186+
CIDR is used to validate a CIDR notation network.
187+
The longest CIDR notation is 43 characters.
188+
maxLength: 43
189+
type: string
190+
x-kubernetes-validations:
191+
- message: value must be a valid CIDR
192+
rule: isCIDR(self)
193+
maxItems: 2
194+
minItems: 1
195+
type: array
196+
x-kubernetes-list-type: atomic
197+
x-kubernetes-validations:
198+
- message: subnets must not contain 2 subnets of the same IP family
199+
rule: size(self) != 2 || !isCIDR(self[0]) || !isCIDR(self[1])
200+
|| cidr(self[0]).ip().family() != cidr(self[1]).ip().family()
201+
required:
202+
- subnets
203+
type: object
204+
x-kubernetes-validations:
205+
- fieldPath: .excludeSubnets
206+
message: excludeSubnets must be subnetworks of the networks specified
207+
in the subnets field
208+
rule: '!has(self.excludeSubnets) || self.excludeSubnets.all(e, self.subnets.exists(s,
209+
cidr(s).containsCIDR(cidr(e))))'
160210
required:
161211
- immutableField
162212
type: object

example/v1/zz_generated.crd-manifests/0000_50_my-operator_01_stableconfigtypes-DevPreviewNoUpgrade.crd.yaml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,56 @@ spec:
162162
alphanumeric characters, '-' or '.', and must start and end with
163163
an alphanumeric character.
164164
rule: '!format.dns1123Subdomain().validate(self).hasValue()'
165+
subnetsWithExclusions:
166+
description: subnetsWithExclusions demonstrates how to validate a
167+
list of subnets with exclusions
168+
properties:
169+
excludeSubnets:
170+
description: |-
171+
excludeSubnets is a list of CIDR exclusions.
172+
The subnets in this list must be subnetworks of the subnets in the subnets list.
173+
items:
174+
description: |-
175+
CIDR is used to validate a CIDR notation network.
176+
The longest CIDR notation is 43 characters.
177+
maxLength: 43
178+
type: string
179+
x-kubernetes-validations:
180+
- message: value must be a valid CIDR
181+
rule: isCIDR(self)
182+
maxItems: 25
183+
type: array
184+
subnets:
185+
description: |-
186+
subnets is a list of subnets.
187+
It may contain up to 2 subnets.
188+
The list may be either 1 IPv4 subnet, 1 IPv6 subnet, or 1 of each.
189+
items:
190+
description: |-
191+
CIDR is used to validate a CIDR notation network.
192+
The longest CIDR notation is 43 characters.
193+
maxLength: 43
194+
type: string
195+
x-kubernetes-validations:
196+
- message: value must be a valid CIDR
197+
rule: isCIDR(self)
198+
maxItems: 2
199+
minItems: 1
200+
type: array
201+
x-kubernetes-list-type: atomic
202+
x-kubernetes-validations:
203+
- message: subnets must not contain 2 subnets of the same IP family
204+
rule: size(self) != 2 || !isCIDR(self[0]) || !isCIDR(self[1])
205+
|| cidr(self[0]).ip().family() != cidr(self[1]).ip().family()
206+
required:
207+
- subnets
208+
type: object
209+
x-kubernetes-validations:
210+
- fieldPath: .excludeSubnets
211+
message: excludeSubnets must be subnetworks of the networks specified
212+
in the subnets field
213+
rule: '!has(self.excludeSubnets) || self.excludeSubnets.all(e, self.subnets.exists(s,
214+
cidr(s).containsCIDR(cidr(e))))'
165215
required:
166216
- immutableField
167217
type: object

example/v1/zz_generated.crd-manifests/0000_50_my-operator_01_stableconfigtypes-TechPreviewNoUpgrade.crd.yaml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,56 @@ spec:
162162
alphanumeric characters, '-' or '.', and must start and end with
163163
an alphanumeric character.
164164
rule: '!format.dns1123Subdomain().validate(self).hasValue()'
165+
subnetsWithExclusions:
166+
description: subnetsWithExclusions demonstrates how to validate a
167+
list of subnets with exclusions
168+
properties:
169+
excludeSubnets:
170+
description: |-
171+
excludeSubnets is a list of CIDR exclusions.
172+
The subnets in this list must be subnetworks of the subnets in the subnets list.
173+
items:
174+
description: |-
175+
CIDR is used to validate a CIDR notation network.
176+
The longest CIDR notation is 43 characters.
177+
maxLength: 43
178+
type: string
179+
x-kubernetes-validations:
180+
- message: value must be a valid CIDR
181+
rule: isCIDR(self)
182+
maxItems: 25
183+
type: array
184+
subnets:
185+
description: |-
186+
subnets is a list of subnets.
187+
It may contain up to 2 subnets.
188+
The list may be either 1 IPv4 subnet, 1 IPv6 subnet, or 1 of each.
189+
items:
190+
description: |-
191+
CIDR is used to validate a CIDR notation network.
192+
The longest CIDR notation is 43 characters.
193+
maxLength: 43
194+
type: string
195+
x-kubernetes-validations:
196+
- message: value must be a valid CIDR
197+
rule: isCIDR(self)
198+
maxItems: 2
199+
minItems: 1
200+
type: array
201+
x-kubernetes-list-type: atomic
202+
x-kubernetes-validations:
203+
- message: subnets must not contain 2 subnets of the same IP family
204+
rule: size(self) != 2 || !isCIDR(self[0]) || !isCIDR(self[1])
205+
|| cidr(self[0]).ip().family() != cidr(self[1]).ip().family()
206+
required:
207+
- subnets
208+
type: object
209+
x-kubernetes-validations:
210+
- fieldPath: .excludeSubnets
211+
message: excludeSubnets must be subnetworks of the networks specified
212+
in the subnets field
213+
rule: '!has(self.excludeSubnets) || self.excludeSubnets.all(e, self.subnets.exists(s,
214+
cidr(s).containsCIDR(cidr(e))))'
165215
required:
166216
- immutableField
167217
type: object

example/v1/zz_generated.deepcopy.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)