Skip to content

Commit

Permalink
Support validating internal list items on list types
Browse files Browse the repository at this point in the history
When using kubernetes-sigs/controller-tools v2 and above, there's a
know issue that the validation on intertal list items is no longer
supported.
See in: kubernetes-sigs/controller-tools#342.
This issue is opened 2 years ago and currently frozen(seems will not
fix in a short term). We can set a alia of the item type to introduce
the validation instead, otherwise, we'll need to remove these validations.

This commit gives an example about the effort to set the validation with
alia type on the subfunctions for review. Note: there's still many
validation of the items in a "StringList" need to implement.

Signed-off-by: Yuxing Jiang <Yuxing.Jiang@windriver.com>
  • Loading branch information
yjian118 committed May 23, 2022
1 parent 91f1507 commit fd7ca26
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 67 deletions.
8 changes: 7 additions & 1 deletion api/v1/constructors.go
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,13 @@ func NewHostProfileSpec(host v1info.HostInfo) (*HostProfileSpec, error) {

// Fill-in the basic attributes
spec.Personality = &host.Personality
subfunctions := strings.Split(host.SubFunctions, ",")
subfunctionStrings := strings.Split(host.SubFunctions, ",")
subfunctions := make([]SubFunction, 0)
if len(subfunctionStrings) > 0 {
for _, subfunctionString := range subfunctionStrings {
subfunctions = append(subfunctions, SubFunctionFromString(subfunctionString))
}
}
spec.SubFunctions = subfunctions
spec.AdministrativeState = &host.AdministrativeState
if host.BootMAC != zeroMAC {
Expand Down
21 changes: 10 additions & 11 deletions api/v1/hostprofile_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,13 @@ func (in RouteInfo) IsKeyEqual(x RouteInfo) bool {
return false
}

// +kubebuilder:validation:Enum=controller;worker;storage;lowlatency
type SubFunction string

func SubFunctionFromString(s string) SubFunction {
return SubFunction(s)
}

// +deepequal-gen:ignore-nil-fields=true
type ProfileBaseAttributes struct {
// Personality defines the role to be assigned to the host
Expand All @@ -680,11 +687,10 @@ type ProfileBaseAttributes struct {
// +optional
AdministrativeState *string `json:"administrativeState,omitempty"`

// SubFunctionList defines the set of subfunctions to be provisioned on the
// SubFunctions defines the set of subfunctions to be provisioned on the
// node at time of initial provisioning.
// +kubebuilder:validation:Enum=controller;worker;storage;lowlatency
// +optional
SubFunctions StringList `json:"subfunctions,omitempty"`
SubFunctions []SubFunction `json:"subfunctions,omitempty"`

// Location defines the physical location of the host in the data centre.
// +optional
Expand Down Expand Up @@ -881,12 +887,6 @@ type HostProfileSpec struct {
Routes RouteList `json:"routes,omitempty"`
}

// HostProfileStatus defines the observed state of HostProfile
type HostProfileStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
}

// HasWorkerSubfunction is a utility function that returns true if a profile
// is configured to require the compute subfunction.
func (in *HostProfileSpec) HasWorkerSubFunction() bool {
Expand Down Expand Up @@ -917,8 +917,7 @@ type HostProfile struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec HostProfileSpec `json:"spec,omitempty"`
Status HostProfileStatus `json:"status,omitempty"`
Spec HostProfileSpec `json:"spec,omitempty"`
}

// +kubebuilder:object:root=true
Expand Down
18 changes: 1 addition & 17 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 2 additions & 19 deletions api/v1/zz_generated.deepequal.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 7 additions & 10 deletions config/crd/bases/starlingx.windriver.com_hostprofiles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -891,20 +891,17 @@ spec:
type: array
type: object
subfunctions:
description: SubFunctionList defines the set of subfunctions to be
provisioned on the node at time of initial provisioning.
enum:
- controller
- worker
- storage
- lowlatency
description: SubFunctions defines the set of subfunctions to be provisioned
on the node at time of initial provisioning.
items:
enum:
- controller
- worker
- storage
- lowlatency
type: string
type: array
type: object
status:
description: HostProfileStatus defines the observed state of HostProfile
type: object
type: object
served: true
storage: true
Expand Down
14 changes: 7 additions & 7 deletions config/crd/bases/starlingx.windriver.com_hosts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -975,14 +975,14 @@ spec:
type: array
type: object
subfunctions:
description: SubFunctionList defines the set of subfunctions to
be provisioned on the node at time of initial provisioning.
enum:
- controller
- worker
- storage
- lowlatency
description: SubFunctions defines the set of subfunctions to be
provisioned on the node at time of initial provisioning.
items:
enum:
- controller
- worker
- storage
- lowlatency
type: string
type: array
type: object
Expand Down
8 changes: 6 additions & 2 deletions controllers/host/host_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,14 @@ func (r *HostReconciler) UpdateRequired(instance *starlingxv1.Host, profile *sta
}

if profile.SubFunctions != nil {
profileSubFunctions := make([]string, 0)
for _, single := range profile.SubFunctions {
profileSubFunctions = append(profileSubFunctions, string(single))
}
subfunctions := strings.Split(h.SubFunctions, ",")
if utils.ListChanged(profile.SubFunctions, subfunctions) {
if utils.ListChanged(profileSubFunctions, subfunctions) {
result = true
subfunctions := strings.Join(profile.SubFunctions, ",")
subfunctions := strings.Join(profileSubFunctions, ",")
opts.SubFunctions = &subfunctions
}
}
Expand Down

0 comments on commit fd7ca26

Please sign in to comment.