Skip to content

Commit 8faf2c8

Browse files
authored
Remove type aliases for controller-tools validation (#493)
Replace custom type aliases with direct slice types to leverage controller-tools' array items validation feature. This eliminates the need for type conversion functions and resolves pointer copying limitations. Previously: // +kubebuilder:validation:MaxLength=255 // +kubebuilder:validation:Pattern=^[a-zA-Z0-9\-_]+$ type PtpInterfaceItem string type PtpInterfaceItemList []PtpInterfaceItem Now: // +kubebuilder:validation:items:MaxLength=255 // +kubebuilder:validation:items:Pattern=^[a-zA-Z0-9\-_]+$ // +nullable type PtpInterfaceItemList []string This change removes unnecessary conversion functions and avoids controller-tools limitations with array pointer copying. The +nullable maintains existing behavior where nil preserves current system configuration, which was previously handled through pointer semantics. This change also upgrades controllers-tools to v0.15.0 to allow :items: validations. Test Plan: - PASS: build crd and ensure it has the same schema as before, validations - PASS: deploy SX subcloud with: - ptp instances & interfaces - service parameters - dns & ntp servers - storage configured - PASS: perform day-2 operations on system & host Signed-off-by: Wallysson Silva <wallysson.silva@windriver.com>
1 parent 49e0274 commit 8faf2c8

38 files changed

+931
-909
lines changed

Makefile

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SPDX-License-Identifier: Apache-2.0
2-
# Copyright(c) 2019-2023 Wind River Systems, Inc.
2+
# Copyright(c) 2019-2025 Wind River Systems, Inc.
33

44
# The Helm package command is not capable of figuring out if a package actually
55
# needs to be re-built therefore this Makefile will only invoke that command
@@ -76,22 +76,6 @@ manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and Cust
7676
.PHONY: generate
7777
generate: controller-gen deepequal-gen ## Generate code containing DeepCopy, DeepEqual, DeepCopyInto, and DeepCopyObject method implementations.
7878
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
79-
## Replace array to rarray type
80-
## This is fix for https://github.com/kubernetes-sigs/controller-tools/issues/586
81-
## For example, []PlatformNetworkItem -> PlatformNetworkItemList
82-
##
83-
sed -i 's#\[\]PlatformNetworkItem#PlatformNetworkItemList#g' $(DEEPCOPY_GEN_FILE)
84-
sed -i 's#\[\]DataNetworkItem#DataNetworkItemList#g' $(DEEPCOPY_GEN_FILE)
85-
sed -i 's#\[\]PtpInterfaceItem#PtpInterfaceItemList#g' $(DEEPCOPY_GEN_FILE)
86-
sed -i 's#\[\]OSDInfo#OSDList#g' $(DEEPCOPY_GEN_FILE)
87-
sed -i 's#\[\]VolumeGroupInfo#VolumeGroupList#g' $(DEEPCOPY_GEN_FILE)
88-
sed -i 's#\[\]FileSystemInfo#FileSystemList#g' $(DEEPCOPY_GEN_FILE)
89-
sed -i 's#\[\]DNSServer#DNSServerList#g' $(DEEPCOPY_GEN_FILE)
90-
sed -i 's#\[\]NTPServer#NTPServerList#g' $(DEEPCOPY_GEN_FILE)
91-
sed -i 's#\[\]CertificateInfo#CertificateList#g' $(DEEPCOPY_GEN_FILE)
92-
sed -i 's#\[\]ServiceParameterInfo#ServiceParameterList#g' $(DEEPCOPY_GEN_FILE)
93-
sed -i 's#\[\]StorageBackend#StorageBackendList#g' $(DEEPCOPY_GEN_FILE)
94-
sed -i 's#\[\]ControllerFileSystemInfo#ControllerFileSystemList#g' $(DEEPCOPY_GEN_FILE)
9579
$(DEEPEQUAL_GEN) -v 1 -o ${PWD} -O zz_generated.deepequal -i ./api/v1 -h ./hack/boilerplate.go.txt --gen-package-path ./api/v1
9680

9781
.PHONY: fmt
@@ -171,7 +155,7 @@ DEEPEQUAL_GEN ?= $(LOCALBIN)/deepequal-gen
171155

172156
## Tool Versions
173157
KUSTOMIZE_VERSION ?= v3.8.7
174-
CONTROLLER_TOOLS_VERSION ?= v0.14.0
158+
CONTROLLER_TOOLS_VERSION ?= v0.15.0
175159
SETPUP_ENVTEST_VERSION ?= release-0.16
176160

177161
KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"

api/v1/constructors.go

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -348,19 +348,10 @@ func parseInterfaceInfo(profile *HostProfileSpec, host v1info.HostInfo) error {
348348
}
349349
}
350350

351-
netList := StringsToPlatformNetworkItemList(nets)
352-
data.PlatformNetworks = &netList
353-
354-
dataNets := host.BuildInterfaceDataNetworkList(iface)
355-
356-
dataNetList := StringsToDataNetworkItemList(dataNets)
357-
data.DataNetworks = &dataNetList
358-
351+
data.PlatformNetworks = nets
352+
data.DataNetworks = host.BuildInterfaceDataNetworkList(iface)
359353
data.PTPRole = iface.PTPRole
360-
361-
dataPtpInterfaceList := host.FindPTPInterfaceNameByInterface(iface)
362-
dataPtpInterfaces := StringsToPtpInterfaceItemList(dataPtpInterfaceList)
363-
data.PtpInterfaces = &dataPtpInterfaces
354+
data.PtpInterfaces = host.FindPTPInterfaceNameByInterface(iface)
364355

365356
switch iface.Type {
366357
case interfaces.IFTypeEthernet:
@@ -560,8 +551,7 @@ func parseVolumeGroupInfo(profile *HostProfileSpec, host v1info.HostInfo) error
560551
}
561552

562553
if len(result) > 0 {
563-
list := VolumeGroupList(result)
564-
profile.Storage.VolumeGroups = &list
554+
profile.Storage.VolumeGroups = result
565555
}
566556

567557
return nil
@@ -611,8 +601,7 @@ func parseOSDInfo(profile *HostProfileSpec, host v1info.HostInfo) error {
611601
}
612602

613603
if len(result) > 0 {
614-
list := OSDList(result)
615-
profile.Storage.OSDs = &list
604+
profile.Storage.OSDs = result
616605
}
617606

618607
return nil
@@ -648,8 +637,7 @@ func parseHostFileSystemInfo(spec *HostProfileSpec, fileSystems []hostFilesystem
648637
result = append(result, info)
649638
}
650639

651-
list := FileSystemList(result)
652-
spec.Storage.FileSystems = &list
640+
spec.Storage.FileSystems = result
653641

654642
return nil
655643
}
@@ -843,9 +831,7 @@ func NewHostProfileSpec(host v1info.HostInfo) (*HostProfileSpec, error) {
843831
clock := *host.ClockSynchronization
844832
clockCopied := clock[0:] // Copy ClockSynchronization value
845833
spec.ClockSynchronization = &clockCopied
846-
ptpInstances := host.BuildPTPInstanceList()
847-
ptpInstanceList := StringsToPtpInstanceItemList(ptpInstances)
848-
spec.PtpInstances = ptpInstanceList
834+
spec.PtpInstances = host.BuildPTPInstanceList()
849835

850836
// Assume that the board is powered on unless there is a clear indication
851837
// that it is not.
@@ -965,8 +951,7 @@ func parseCertificateInfo(spec *SystemSpec, certificates []certificates.Certific
965951
}
966952

967953
if len(result) > 0 {
968-
list := CertificateList(result)
969-
spec.Certificates = &list
954+
spec.Certificates = result
970955
} else {
971956
spec.Certificates = nil
972957
}
@@ -975,8 +960,7 @@ func parseCertificateInfo(spec *SystemSpec, certificates []certificates.Certific
975960
}
976961

977962
func parseServiceParameterInfo(spec *SystemSpec, serviceParams []serviceparameters.ServiceParameter) error {
978-
result := make([]ServiceParameterInfo, 0)
979-
963+
result := []ServiceParameterInfo{}
980964
for _, sp := range serviceParams {
981965
info := ServiceParameterInfo{
982966
Service: sp.Service,
@@ -989,10 +973,7 @@ func parseServiceParameterInfo(spec *SystemSpec, serviceParams []serviceparamete
989973

990974
result = append(result, info)
991975
}
992-
993-
list := ServiceParameterList(result)
994-
spec.ServiceParameters = &list
995-
976+
spec.ServiceParameters = result
996977
return nil
997978
}
998979

@@ -1012,8 +993,7 @@ func parseFileSystemInfo(spec *SystemSpec, fileSystems []controllerFilesystems.F
1012993
spec.Storage = &SystemStorageInfo{}
1013994
}
1014995

1015-
list := ControllerFileSystemList(result)
1016-
spec.Storage.FileSystems = &list
996+
spec.Storage.FileSystems = result
1017997

1018998
return nil
1019999
}
@@ -1036,8 +1016,7 @@ func parseStorageBackendInfo(spec *SystemSpec, storageBackends []storagebackends
10361016
spec.Storage = &SystemStorageInfo{}
10371017
}
10381018

1039-
list := StorageBackendList(result)
1040-
spec.Storage.Backends = &list
1019+
spec.Storage.Backends = StorageBackendList(result)
10411020

10421021
return nil
10431022
}
@@ -1103,22 +1082,18 @@ func NewSystemSpec(systemInfo v1info.SystemInfo) (*SystemSpec, error) {
11031082
}
11041083

11051084
if systemInfo.DNS != nil {
1106-
if systemInfo.DNS.Nameservers != "" {
1107-
nameservers := StringsToDNSServerList(strings.Split(systemInfo.DNS.Nameservers, ","))
1108-
spec.DNSServers = &nameservers
1085+
if len(systemInfo.DNS.Nameservers) > 0 {
1086+
spec.DNSServers = strings.Split(systemInfo.DNS.Nameservers, ",")
11091087
} else {
1110-
empty := StringsToDNSServerList(make([]string, 0))
1111-
spec.DNSServers = &empty
1088+
spec.DNSServers = []string{}
11121089
}
11131090
}
11141091

11151092
if systemInfo.NTP != nil {
1116-
if systemInfo.NTP.NTPServers != "" {
1117-
nameservers := StringsToNTPServerList(strings.Split(systemInfo.NTP.NTPServers, ","))
1118-
spec.NTPServers = &nameservers
1093+
if len(systemInfo.NTP.NTPServers) > 0 {
1094+
spec.NTPServers = strings.Split(systemInfo.NTP.NTPServers, ",")
11191095
} else {
1120-
empty := StringsToNTPServerList(make([]string, 0))
1121-
spec.NTPServers = &empty
1096+
spec.NTPServers = []string{}
11221097
}
11231098
}
11241099

0 commit comments

Comments
 (0)