From 8b76e09798fdbba8e27611b0ff97cad9c7e97266 Mon Sep 17 00:00:00 2001 From: Deng Yun Date: Tue, 18 Jun 2024 11:29:02 +0000 Subject: [PATCH] Refactor subnet subnetset CRD 1.Remove subnet CRD unused DHCP options since these fields are not used, so not expose to uesers. 2.Add gatewayAddresses and DHCPServerAddresses into the subnet CRD status field, also adding them in subnet field of subnetset CRD. 3.Rename status ipAddress to networkAddresses --- build/yaml/crd/nsx.vmware.com_subnets.yaml | 34 +++++------------ build/yaml/crd/nsx.vmware.com_subnetsets.yaml | 37 +++++-------------- .../nsx.vmware.com/v1alpha1/subnet_types.go | 22 +++-------- .../v1alpha1/subnetset_types.go | 6 ++- .../v1alpha1/zz_generated.deepcopy.go | 35 ++++++++++++++---- pkg/apis/v1alpha1/subnet_types.go | 22 +++-------- pkg/apis/v1alpha1/subnetset_types.go | 6 ++- pkg/apis/v1alpha1/zz_generated.deepcopy.go | 35 ++++++++++++++---- pkg/controllers/subnet/subnet_controller.go | 11 +++++- pkg/nsx/services/subnet/subnet.go | 7 +++- test/e2e/nsx_subnet_test.go | 6 +-- 11 files changed, 108 insertions(+), 113 deletions(-) diff --git a/build/yaml/crd/nsx.vmware.com_subnets.yaml b/build/yaml/crd/nsx.vmware.com_subnets.yaml index 7d5de517b..fe7898881 100644 --- a/build/yaml/crd/nsx.vmware.com_subnets.yaml +++ b/build/yaml/crd/nsx.vmware.com_subnets.yaml @@ -51,30 +51,6 @@ spec: DHCPConfig: description: DHCPConfig DHCP configuration. properties: - dhcpRelayConfigPath: - description: DHCPRelayConfigPath is policy path of DHCP-relay-config. - type: string - dhcpV4PoolSize: - default: 80 - description: DHCPV4PoolSize IPs in % to be reserved for DHCP ranges. - By default, 80% of IPv4 IPs will be reserved for DHCP. Configure - 0 if no pool is required. - maximum: 100 - minimum: 0 - type: integer - dhcpV6PoolSize: - default: 2000 - description: DHCPV6PoolSize number of IPs to be reserved for DHCP - ranges. By default, 2000 IPv6 IPs will be reserved for DHCP. - type: integer - dnsClientConfig: - description: DNSClientConfig holds DNS configurations. - properties: - dnsServersIPs: - items: - type: string - type: array - type: object enableDHCP: default: false type: boolean @@ -116,6 +92,10 @@ spec: status: description: SubnetStatus defines the observed state of Subnet. properties: + DHCPServerAddresses: + items: + type: string + type: array conditions: items: description: Condition defines condition of custom resource. @@ -144,7 +124,11 @@ spec: - type type: object type: array - ipAddresses: + gatewayAddresses: + items: + type: string + type: array + networkAddreses: items: type: string type: array diff --git a/build/yaml/crd/nsx.vmware.com_subnetsets.yaml b/build/yaml/crd/nsx.vmware.com_subnetsets.yaml index aac120f98..912d1a93b 100644 --- a/build/yaml/crd/nsx.vmware.com_subnetsets.yaml +++ b/build/yaml/crd/nsx.vmware.com_subnetsets.yaml @@ -51,30 +51,6 @@ spec: DHCPConfig: description: DHCPConfig DHCP configuration. properties: - dhcpRelayConfigPath: - description: DHCPRelayConfigPath is policy path of DHCP-relay-config. - type: string - dhcpV4PoolSize: - default: 80 - description: DHCPV4PoolSize IPs in % to be reserved for DHCP ranges. - By default, 80% of IPv4 IPs will be reserved for DHCP. Configure - 0 if no pool is required. - maximum: 100 - minimum: 0 - type: integer - dhcpV6PoolSize: - default: 2000 - description: DHCPV6PoolSize number of IPs to be reserved for DHCP - ranges. By default, 2000 IPv6 IPs will be reserved for DHCP. - type: integer - dnsClientConfig: - description: DNSClientConfig holds DNS configurations. - properties: - dnsServersIPs: - items: - type: string - type: array - type: object enableDHCP: default: false type: boolean @@ -142,15 +118,20 @@ spec: description: SubnetInfo defines the observed state of a single Subnet of a SubnetSet. properties: - ipAddresses: + DHCPServerAddresses: + items: + type: string + type: array + gatewayAddresses: + items: + type: string + type: array + networkAddresses: items: type: string type: array nsxResourcePath: type: string - required: - - ipAddresses - - nsxResourcePath type: object type: array type: object diff --git a/pkg/apis/nsx.vmware.com/v1alpha1/subnet_types.go b/pkg/apis/nsx.vmware.com/v1alpha1/subnet_types.go index 5473a8021..d84391965 100644 --- a/pkg/apis/nsx.vmware.com/v1alpha1/subnet_types.go +++ b/pkg/apis/nsx.vmware.com/v1alpha1/subnet_types.go @@ -30,9 +30,11 @@ type SubnetSpec struct { // SubnetStatus defines the observed state of Subnet. type SubnetStatus struct { - NSXResourcePath string `json:"nsxResourcePath,omitempty"` - IPAddresses []string `json:"ipAddresses,omitempty"` - Conditions []Condition `json:"conditions,omitempty"` + NSXResourcePath string `json:"nsxResourcePath,omitempty"` + NetworkAddresses []string `json:"networkAddreses,omitempty"` + GatewayAddresses []string `json:"gatewayAddresses,omitempty"` + DHCPServerAddresses []string `json:"DHCPServerAddresses,omitempty"` + Conditions []Condition `json:"conditions,omitempty"` } // +genclient @@ -78,20 +80,6 @@ type StaticIPAllocation struct { type DHCPConfig struct { // +kubebuilder:default:=false EnableDHCP bool `json:"enableDHCP,omitempty"` - // DHCPRelayConfigPath is policy path of DHCP-relay-config. - DHCPRelayConfigPath string `json:"dhcpRelayConfigPath,omitempty"` - // DHCPV4PoolSize IPs in % to be reserved for DHCP ranges. - // By default, 80% of IPv4 IPs will be reserved for DHCP. - // Configure 0 if no pool is required. - // +kubebuilder:default:=80 - // +kubebuilder:validation:Maximum:=100 - // +kubebuilder:validation:Minimum:=0 - DHCPV4PoolSize int `json:"dhcpV4PoolSize,omitempty"` - // DHCPV6PoolSize number of IPs to be reserved for DHCP ranges. - // By default, 2000 IPv6 IPs will be reserved for DHCP. - // +kubebuilder:default:=2000 - DHCPV6PoolSize int `json:"dhcpV6PoolSize,omitempty"` - DNSClientConfig DNSClientConfig `json:"dnsClientConfig,omitempty"` } // DNSClientConfig holds DNS configurations. diff --git a/pkg/apis/nsx.vmware.com/v1alpha1/subnetset_types.go b/pkg/apis/nsx.vmware.com/v1alpha1/subnetset_types.go index 5c6864893..23df555bb 100644 --- a/pkg/apis/nsx.vmware.com/v1alpha1/subnetset_types.go +++ b/pkg/apis/nsx.vmware.com/v1alpha1/subnetset_types.go @@ -24,8 +24,10 @@ type SubnetSetSpec struct { // SubnetInfo defines the observed state of a single Subnet of a SubnetSet. type SubnetInfo struct { - NSXResourcePath string `json:"nsxResourcePath"` - IPAddresses []string `json:"ipAddresses"` + NSXResourcePath string `json:"nsxResourcePath,omitempty"` + NetworkAddresses []string `json:"networkAddresses,omitempty"` + GatewayAddresses []string `json:"gatewayAddresses,omitempty"` + DHCPServerAddresses []string `json:"DHCPServerAddresses,omitempty"` } // SubnetSetStatus defines the observed state of SubnetSet. diff --git a/pkg/apis/nsx.vmware.com/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/nsx.vmware.com/v1alpha1/zz_generated.deepcopy.go index 7acaa6a5c..74ddbb492 100644 --- a/pkg/apis/nsx.vmware.com/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/nsx.vmware.com/v1alpha1/zz_generated.deepcopy.go @@ -48,7 +48,6 @@ func (in *Condition) DeepCopy() *Condition { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *DHCPConfig) DeepCopyInto(out *DHCPConfig) { *out = *in - in.DNSClientConfig.DeepCopyInto(&out.DNSClientConfig) } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DHCPConfig. @@ -907,8 +906,18 @@ func (in *Subnet) DeepCopyObject() runtime.Object { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *SubnetInfo) DeepCopyInto(out *SubnetInfo) { *out = *in - if in.IPAddresses != nil { - in, out := &in.IPAddresses, &out.IPAddresses + if in.NetworkAddresses != nil { + in, out := &in.NetworkAddresses, &out.NetworkAddresses + *out = make([]string, len(*in)) + copy(*out, *in) + } + if in.GatewayAddresses != nil { + in, out := &in.GatewayAddresses, &out.GatewayAddresses + *out = make([]string, len(*in)) + copy(*out, *in) + } + if in.DHCPServerAddresses != nil { + in, out := &in.DHCPServerAddresses, &out.DHCPServerAddresses *out = make([]string, len(*in)) copy(*out, *in) } @@ -1089,7 +1098,7 @@ func (in *SubnetSet) DeepCopyInto(out *SubnetSet) { *out = *in out.TypeMeta = in.TypeMeta in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) - in.Spec.DeepCopyInto(&out.Spec) + out.Spec = in.Spec in.Status.DeepCopyInto(&out.Status) } @@ -1147,7 +1156,7 @@ func (in *SubnetSetList) DeepCopyObject() runtime.Object { func (in *SubnetSetSpec) DeepCopyInto(out *SubnetSetSpec) { *out = *in out.AdvancedConfig = in.AdvancedConfig - in.DHCPConfig.DeepCopyInto(&out.DHCPConfig) + out.DHCPConfig = in.DHCPConfig } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new SubnetSetSpec. @@ -1198,7 +1207,7 @@ func (in *SubnetSpec) DeepCopyInto(out *SubnetSpec) { copy(*out, *in) } out.AdvancedConfig = in.AdvancedConfig - in.DHCPConfig.DeepCopyInto(&out.DHCPConfig) + out.DHCPConfig = in.DHCPConfig } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new SubnetSpec. @@ -1214,8 +1223,18 @@ func (in *SubnetSpec) DeepCopy() *SubnetSpec { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *SubnetStatus) DeepCopyInto(out *SubnetStatus) { *out = *in - if in.IPAddresses != nil { - in, out := &in.IPAddresses, &out.IPAddresses + if in.NetworkAddresses != nil { + in, out := &in.NetworkAddresses, &out.NetworkAddresses + *out = make([]string, len(*in)) + copy(*out, *in) + } + if in.GatewayAddresses != nil { + in, out := &in.GatewayAddresses, &out.GatewayAddresses + *out = make([]string, len(*in)) + copy(*out, *in) + } + if in.DHCPServerAddresses != nil { + in, out := &in.DHCPServerAddresses, &out.DHCPServerAddresses *out = make([]string, len(*in)) copy(*out, *in) } diff --git a/pkg/apis/v1alpha1/subnet_types.go b/pkg/apis/v1alpha1/subnet_types.go index 5473a8021..d84391965 100644 --- a/pkg/apis/v1alpha1/subnet_types.go +++ b/pkg/apis/v1alpha1/subnet_types.go @@ -30,9 +30,11 @@ type SubnetSpec struct { // SubnetStatus defines the observed state of Subnet. type SubnetStatus struct { - NSXResourcePath string `json:"nsxResourcePath,omitempty"` - IPAddresses []string `json:"ipAddresses,omitempty"` - Conditions []Condition `json:"conditions,omitempty"` + NSXResourcePath string `json:"nsxResourcePath,omitempty"` + NetworkAddresses []string `json:"networkAddreses,omitempty"` + GatewayAddresses []string `json:"gatewayAddresses,omitempty"` + DHCPServerAddresses []string `json:"DHCPServerAddresses,omitempty"` + Conditions []Condition `json:"conditions,omitempty"` } // +genclient @@ -78,20 +80,6 @@ type StaticIPAllocation struct { type DHCPConfig struct { // +kubebuilder:default:=false EnableDHCP bool `json:"enableDHCP,omitempty"` - // DHCPRelayConfigPath is policy path of DHCP-relay-config. - DHCPRelayConfigPath string `json:"dhcpRelayConfigPath,omitempty"` - // DHCPV4PoolSize IPs in % to be reserved for DHCP ranges. - // By default, 80% of IPv4 IPs will be reserved for DHCP. - // Configure 0 if no pool is required. - // +kubebuilder:default:=80 - // +kubebuilder:validation:Maximum:=100 - // +kubebuilder:validation:Minimum:=0 - DHCPV4PoolSize int `json:"dhcpV4PoolSize,omitempty"` - // DHCPV6PoolSize number of IPs to be reserved for DHCP ranges. - // By default, 2000 IPv6 IPs will be reserved for DHCP. - // +kubebuilder:default:=2000 - DHCPV6PoolSize int `json:"dhcpV6PoolSize,omitempty"` - DNSClientConfig DNSClientConfig `json:"dnsClientConfig,omitempty"` } // DNSClientConfig holds DNS configurations. diff --git a/pkg/apis/v1alpha1/subnetset_types.go b/pkg/apis/v1alpha1/subnetset_types.go index 5c6864893..23df555bb 100644 --- a/pkg/apis/v1alpha1/subnetset_types.go +++ b/pkg/apis/v1alpha1/subnetset_types.go @@ -24,8 +24,10 @@ type SubnetSetSpec struct { // SubnetInfo defines the observed state of a single Subnet of a SubnetSet. type SubnetInfo struct { - NSXResourcePath string `json:"nsxResourcePath"` - IPAddresses []string `json:"ipAddresses"` + NSXResourcePath string `json:"nsxResourcePath,omitempty"` + NetworkAddresses []string `json:"networkAddresses,omitempty"` + GatewayAddresses []string `json:"gatewayAddresses,omitempty"` + DHCPServerAddresses []string `json:"DHCPServerAddresses,omitempty"` } // SubnetSetStatus defines the observed state of SubnetSet. diff --git a/pkg/apis/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/v1alpha1/zz_generated.deepcopy.go index 7acaa6a5c..74ddbb492 100644 --- a/pkg/apis/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/v1alpha1/zz_generated.deepcopy.go @@ -48,7 +48,6 @@ func (in *Condition) DeepCopy() *Condition { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *DHCPConfig) DeepCopyInto(out *DHCPConfig) { *out = *in - in.DNSClientConfig.DeepCopyInto(&out.DNSClientConfig) } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DHCPConfig. @@ -907,8 +906,18 @@ func (in *Subnet) DeepCopyObject() runtime.Object { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *SubnetInfo) DeepCopyInto(out *SubnetInfo) { *out = *in - if in.IPAddresses != nil { - in, out := &in.IPAddresses, &out.IPAddresses + if in.NetworkAddresses != nil { + in, out := &in.NetworkAddresses, &out.NetworkAddresses + *out = make([]string, len(*in)) + copy(*out, *in) + } + if in.GatewayAddresses != nil { + in, out := &in.GatewayAddresses, &out.GatewayAddresses + *out = make([]string, len(*in)) + copy(*out, *in) + } + if in.DHCPServerAddresses != nil { + in, out := &in.DHCPServerAddresses, &out.DHCPServerAddresses *out = make([]string, len(*in)) copy(*out, *in) } @@ -1089,7 +1098,7 @@ func (in *SubnetSet) DeepCopyInto(out *SubnetSet) { *out = *in out.TypeMeta = in.TypeMeta in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) - in.Spec.DeepCopyInto(&out.Spec) + out.Spec = in.Spec in.Status.DeepCopyInto(&out.Status) } @@ -1147,7 +1156,7 @@ func (in *SubnetSetList) DeepCopyObject() runtime.Object { func (in *SubnetSetSpec) DeepCopyInto(out *SubnetSetSpec) { *out = *in out.AdvancedConfig = in.AdvancedConfig - in.DHCPConfig.DeepCopyInto(&out.DHCPConfig) + out.DHCPConfig = in.DHCPConfig } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new SubnetSetSpec. @@ -1198,7 +1207,7 @@ func (in *SubnetSpec) DeepCopyInto(out *SubnetSpec) { copy(*out, *in) } out.AdvancedConfig = in.AdvancedConfig - in.DHCPConfig.DeepCopyInto(&out.DHCPConfig) + out.DHCPConfig = in.DHCPConfig } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new SubnetSpec. @@ -1214,8 +1223,18 @@ func (in *SubnetSpec) DeepCopy() *SubnetSpec { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *SubnetStatus) DeepCopyInto(out *SubnetStatus) { *out = *in - if in.IPAddresses != nil { - in, out := &in.IPAddresses, &out.IPAddresses + if in.NetworkAddresses != nil { + in, out := &in.NetworkAddresses, &out.NetworkAddresses + *out = make([]string, len(*in)) + copy(*out, *in) + } + if in.GatewayAddresses != nil { + in, out := &in.GatewayAddresses, &out.GatewayAddresses + *out = make([]string, len(*in)) + copy(*out, *in) + } + if in.DHCPServerAddresses != nil { + in, out := &in.DHCPServerAddresses, &out.DHCPServerAddresses *out = make([]string, len(*in)) copy(*out, *in) } diff --git a/pkg/controllers/subnet/subnet_controller.go b/pkg/controllers/subnet/subnet_controller.go index 3308718a0..a93d5c5bd 100644 --- a/pkg/controllers/subnet/subnet_controller.go +++ b/pkg/controllers/subnet/subnet_controller.go @@ -157,13 +157,20 @@ func (r *SubnetReconciler) updateSubnetStatus(obj *v1alpha1.Subnet) error { if nsxSubnet == nil { return errors.New("failed to get NSX Subnet from store") } - obj.Status.IPAddresses = obj.Status.IPAddresses[:0] + obj.Status.NetworkAddresses = obj.Status.NetworkAddresses[:0] + obj.Status.GatewayAddresses = obj.Status.GatewayAddresses[:0] + obj.Status.DHCPServerAddresses = obj.Status.DHCPServerAddresses[:0] statusList, err := r.SubnetService.GetSubnetStatus(nsxSubnet) if err != nil { return err } for _, status := range statusList { - obj.Status.IPAddresses = append(obj.Status.IPAddresses, *status.NetworkAddress) + obj.Status.NetworkAddresses = append(obj.Status.NetworkAddresses, *status.NetworkAddress) + obj.Status.GatewayAddresses = append(obj.Status.GatewayAddresses, *status.GatewayAddress) + // DHCPServerAddress is only for the subnet with DHCP enabled + if status.DhcpServerAddress != nil { + obj.Status.DHCPServerAddresses = append(obj.Status.DHCPServerAddresses, *status.DhcpServerAddress) + } } obj.Status.NSXResourcePath = *nsxSubnet.Path return nil diff --git a/pkg/nsx/services/subnet/subnet.go b/pkg/nsx/services/subnet/subnet.go index 3177a74bf..afc589a3f 100644 --- a/pkg/nsx/services/subnet/subnet.go +++ b/pkg/nsx/services/subnet/subnet.go @@ -264,7 +264,12 @@ func (service *SubnetService) UpdateSubnetSetStatus(obj *v1alpha1.SubnetSet) err NSXResourcePath: *subnet.Path, } for _, status := range statusList { - subnetInfo.IPAddresses = append(subnetInfo.IPAddresses, *status.NetworkAddress) + subnetInfo.NetworkAddresses = append(subnetInfo.NetworkAddresses, *status.NetworkAddress) + subnetInfo.GatewayAddresses = append(subnetInfo.GatewayAddresses, *status.GatewayAddress) + // DHCPServerAddress is only for the subnet with DHCP enabled + if status.DhcpServerAddress != nil { + subnetInfo.DHCPServerAddresses = append(subnetInfo.DHCPServerAddresses, *status.DhcpServerAddress) + } } subnetInfoList = append(subnetInfoList, subnetInfo) } diff --git a/test/e2e/nsx_subnet_test.go b/test/e2e/nsx_subnet_test.go index c88bc0fa0..f89b56408 100644 --- a/test/e2e/nsx_subnet_test.go +++ b/test/e2e/nsx_subnet_test.go @@ -244,7 +244,7 @@ func sharedSubnetSet(t *testing.T) { // 5. Check Subnet CIDR contains SubnetPort IP. portIP := net.ParseIP(strings.Split(port.Status.NetworkInterfaceConfig.IPAddresses[0].IPAddress, "/")[0]) - _, subnetCIDR, err := net.ParseCIDR(subnetSet.Status.Subnets[0].IPAddresses[0]) + _, subnetCIDR, err := net.ParseCIDR(subnetSet.Status.Subnets[0].NetworkAddresses[0]) assertNil(t, err) assertTrue(t, subnetCIDR.Contains(portIP)) } @@ -270,7 +270,7 @@ func SubnetCIDR(t *testing.T) { assertNil(t, err) allocatedSubnet, err := testData.crdClientset.NsxV1alpha1().Subnets(E2ENamespace).Get(context.TODO(), subnet.Name, v1.GetOptions{}) assertNil(t, err) - targetCIDR := allocatedSubnet.Status.IPAddresses[0] + targetCIDR := allocatedSubnet.Status.NetworkAddresses[0] err = testData.crdClientset.NsxV1alpha1().Subnets(E2ENamespace).Delete(context.TODO(), subnet.Name, v1.DeleteOptions{}) assertNil(t, err) @@ -293,5 +293,5 @@ func SubnetCIDR(t *testing.T) { assertNil(t, err) allocatedSubnet, err = testData.crdClientset.NsxV1alpha1().Subnets(E2ENamespace).Get(context.TODO(), subnet.Name, v1.GetOptions{}) assertNil(t, err) - assert.Equal(t, targetCIDR, allocatedSubnet.Status.IPAddresses[0]) + assert.Equal(t, targetCIDR, allocatedSubnet.Status.NetworkAddresses[0]) }