Skip to content

Commit

Permalink
feat(api): add validation rules for Promotion steps
Browse files Browse the repository at this point in the history
- `PromotionTemplate` steps can contain either a reference to `uses` OR
  to a `task`.
- `PromotionTask` steps can not contain a reference to another `task`,
  but must contain a `uses` reference.
- `Promotion` steps can not contain a reference to a `task` (as we
  expect a `Promotion` to contain deflated tasks), but must contain
  a `uses` reference.
- Small other tweaks, to e.g. ensure we limit the max lenght of fields
  that contain name references to Kubernetes objects.

Signed-off-by: Hidde Beydals <[email protected]>
  • Loading branch information
hiddeco committed Dec 11, 2024
1 parent a985451 commit d4050a8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
3 changes: 2 additions & 1 deletion api/v1alpha1/promotion_task_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ type PromotionTaskSpec struct {
//
// +kubebuilder:validation:Required
// +kubebuilder:validation:MinItems=1
Steps []PromotionStep `json:"steps,omitempty" protobuf:"bytes,2,rep,name=steps"`
// +kubebuilder:validation:items:XValidation:message="PromotionTask step must have uses set and must not reference another task",rule="has(self.uses) && !has(self.task)"
Steps []PromotionStep `json:"steps" protobuf:"bytes,2,rep,name=steps"`
}

// PromotionTaskInput defines an input parameter for a PromotionTask. This input
Expand Down
15 changes: 11 additions & 4 deletions api/v1alpha1/promotion_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,30 @@ type PromotionSpec struct {
// applies. The Stage referenced by this field MUST be in the same
// namespace as the Promotion.
//
// +kubebuilder:validation:Required
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=253
// +kubebuilder:validation:Pattern=^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
Stage string `json:"stage" protobuf:"bytes,1,opt,name=stage"`
// Freight specifies the piece of Freight to be promoted into the Stage
// referenced by the Stage field.
//
// +kubebuilder:validation:Required
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=253
// +kubebuilder:validation:Pattern=^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
Freight string `json:"freight" protobuf:"bytes,2,opt,name=freight"`
// Vars is a list of variables that can be referenced by expressions in
// promotion steps.
Vars []PromotionVariable `json:"vars,omitempty" protobuf:"bytes,4,rep,name=vars"`
// Steps specifies the directives to be executed as part of this Promotion.
// The order in which the directives are executed is the order in which they
// are listed in this field.
Steps []PromotionStep `json:"steps,omitempty" protobuf:"bytes,3,rep,name=steps"`
//
// +kubebuilder:validation:Required
// +kubebuilder:validation:MinItems=1
// +kubebuilder:validation:items:XValidation:message="Promotion step must have uses set and must not reference a task",rule="has(self.uses) && !has(self.task)"
Steps []PromotionStep `json:"steps" protobuf:"bytes,3,rep,name=steps"`
}

// PromotionVariable describes a single variable that may be referenced by
Expand Down Expand Up @@ -191,11 +200,9 @@ type PromotionStep struct {
//
// +kubebuilder:validation:Optional
// +kubebuilder:validation:MinLength=1
Uses string `json:"uses" protobuf:"bytes,1,opt,name=uses"`
Uses string `json:"uses,omitempty" protobuf:"bytes,1,opt,name=uses"`
// Task is a reference to a PromotionTask that should be deflated into a
// Promotion when it is built from a PromotionTemplate.
//
// +kubebuilder:validation:Optional
Task *PromotionTaskReference `json:"task,omitempty" protobuf:"bytes,5,opt,name=task"`
// As is the alias this step can be referred to as.
As string `json:"as,omitempty" protobuf:"bytes,2,opt,name=as"`
Expand Down
1 change: 1 addition & 0 deletions api/v1alpha1/stage_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ type PromotionTemplateSpec struct {
// are listed in this field.
//
// +kubebuilder:validation:MinItems=1
// +kubebuilder:validation:items:XValidation:message="PromotionTemplate step must have exactly one of uses or task set",rule="(has(self.uses) ? !has(self.task) : has(self.task))"
Steps []PromotionStep `json:"steps,omitempty" protobuf:"bytes,1,rep,name=steps"`
}

Expand Down

0 comments on commit d4050a8

Please sign in to comment.