Skip to content

Commit

Permalink
Improve SubnetDHCPConfig validation rule (#976) (#978)
Browse files Browse the repository at this point in the history
Subnet controller will update the SubnetSpec as the following when
either accessMode or ipv4SubnetSize is not provided.
```
spec:
  accessMode: Public
  ipv4SubnetSize: 64
  subnetDHCPConfig:{}
```
Thus we need another validation rule on subnetDHCPConfig to prevent
subnetDHCPConfig being updated from no mode specified to
DHCPServer/DHCPRelay mode.

Signed-off-by: Yanjun Zhou <[email protected]>
  • Loading branch information
yanjunz97 authored Dec 26, 2024
1 parent 53f0db8 commit 5a693bc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
4 changes: 4 additions & 0 deletions build/yaml/crd/vpc/crd.nsx.vmware.com_subnets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ spec:
to other modes
rule: oldSelf!='DHCPDeactivated' || oldSelf==self
type: object
x-kubernetes-validations:
- message: subnetDHCPConfig cannot switch from DHCPDeactivated to
other modes
rule: has(oldSelf.mode) || !has(self.mode) || self.mode=='DHCPDeactivated'
type: object
x-kubernetes-validations:
- message: subnetDHCPConfig cannot switch from DHCPDeactivated to other
Expand Down
4 changes: 4 additions & 0 deletions build/yaml/crd/vpc/crd.nsx.vmware.com_subnetsets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ spec:
to other modes
rule: oldSelf!='DHCPDeactivated' || oldSelf==self
type: object
x-kubernetes-validations:
- message: subnetDHCPConfig cannot switch from DHCPDeactivated to
other modes
rule: has(oldSelf.mode) || !has(self.mode) || self.mode=='DHCPDeactivated'
type: object
x-kubernetes-validations:
- message: subnetDHCPConfig cannot switch from DHCPDeactivated to other
Expand Down
15 changes: 9 additions & 6 deletions pkg/apis/vpc/v1alpha1/subnet_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ type SubnetSpec struct {
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
IPAddresses []string `json:"ipAddresses,omitempty"`

// DHCP mode of a SubnetSet cannot switch from DHCPDeactivated to DHCPServer or DHCPRelay.
// DHCP mode of a Subnet cannot switch from DHCPDeactivated to DHCPServer or DHCPRelay.
// If subnetDHCPConfig is not set, the DHCP mode is DHCPDeactivated by default.
// In order to enforce this rule, two XValidation rules are defined.
// The rule in SubnetSetSpec prevents the condition that subnetDHCPConfig is not set in
// old SubnetSetSpec while the new SubnetSetSpec specifies a field other than DHCPDeactivated.
// The rule in SubnetDHCPConfig prevents the mode changing from empty or
// DHCPDeactivated to DHCPServer or DHCPRelay.
// In order to enforce this rule, three XValidation rules are defined.
// The rule on SubnetSpec prevents the condition that subnetDHCPConfig is not set in
// old SubnetSpec while the new SubnetSpec specifies a Mode other than DHCPDeactivated.
// The rule on SubnetDHCPConfig prevents the condition that Mode is not set in old
// SubnetDHCPConfig while the new one specifies a Mode other than DHCPDeactivated.
// The rule on SubnetDHCPConfig.Mode prevents the Mode changing from DHCPDeactivated
// to DHCPServer or DHCPRelay.

// DHCP configuration for Subnet.
SubnetDHCPConfig SubnetDHCPConfig `json:"subnetDHCPConfig,omitempty"`
Expand Down Expand Up @@ -90,6 +92,7 @@ type SubnetList struct {
}

// SubnetDHCPConfig is DHCP configuration for Subnet.
// +kubebuilder:validation:XValidation:rule="has(oldSelf.mode) || !has(self.mode) || self.mode=='DHCPDeactivated'", message="subnetDHCPConfig cannot switch from DHCPDeactivated to other modes"
type SubnetDHCPConfig struct {
// DHCP Mode. DHCPDeactivated will be used if it is not defined.
// It cannot switch from DHCPDeactivated to DHCPServer or DHCPRelay.
Expand Down
12 changes: 7 additions & 5 deletions pkg/apis/vpc/v1alpha1/subnetset_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ type SubnetSetSpec struct {
AccessMode AccessMode `json:"accessMode,omitempty"`
// DHCP mode of a SubnetSet cannot switch from DHCPDeactivated to DHCPServer or DHCPRelay.
// If subnetDHCPConfig is not set, the DHCP mode is DHCPDeactivated by default.
// In order to enforce this rule, two XValidation rules are defined.
// The rule in SubnetSetSpec prevents the condition that subnetDHCPConfig is not set in
// old SubnetSetSpec while the new SubnetSetSpec specifies a field other than DHCPDeactivated.
// The rule in SubnetDHCPConfig prevents the mode changing from empty or
// DHCPDeactivated to DHCPServer or DHCPRelay.
// In order to enforce this rule, three XValidation rules are defined.
// The rule on SubnetSetSpec prevents the condition that subnetDHCPConfig is not set in
// old SubnetSetSpec while the new SubnetSetSpec specifies a Mode other than DHCPDeactivated.
// The rule on SubnetDHCPConfig prevents the condition that Mode is not set in old
// SubnetDHCPConfig while the new one specifies a Mode other than DHCPDeactivated.
// The rule on SubnetDHCPConfig.Mode prevents the Mode changing from DHCPDeactivated
// to DHCPServer or DHCPRelay.

// Subnet DHCP configuration.
SubnetDHCPConfig SubnetDHCPConfig `json:"subnetDHCPConfig,omitempty"`
Expand Down

0 comments on commit 5a693bc

Please sign in to comment.