Skip to content

Commit

Permalink
restructure validation checks to only check for image source type
Browse files Browse the repository at this point in the history
Signed-off-by: Ankita Thomas <[email protected]>
  • Loading branch information
ankitathomas committed Oct 1, 2024
1 parent ce16296 commit e7f6707
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 40 deletions.
6 changes: 2 additions & 4 deletions api/core/v1alpha1/clustercatalog_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ type ClusterCatalogStatus struct {
// CatalogSource is a discriminated union of possible sources for a Catalog.
// CatalogSource contains the sourcing information for a Catalog
// +union
// +kubebuilder:validation:XValidation:rule="self.type != 'Image' || has(self.image)",message="source type 'Image' requires image field"
// +kubebuilder:validation:XValidation:rule="self.type == 'Image' || !has(self.image)",message="image field must only be set for source type 'Image'"
// +kubebuilder:validation:XValidation:rule="self.type == 'Image' && has(self.image)",message="source type 'Image' requires image field"
type CatalogSource struct {
// type is a required reference to the type of source the catalog is sourced from.
//
Expand All @@ -167,8 +166,7 @@ type CatalogSource struct {
// ResolvedCatalogSource is a discriminated union of resolution information for a Catalog.
// ResolvedCatalogSource contains the information about a sourced Catalog
// +union
// +kubebuilder:validation:XValidation:rule="self.type != 'Image' || has(self.image)",message="source type 'Image' requires image field"
// +kubebuilder:validation:XValidation:rule="self.type == 'Image' || !has(self.image)",message="image field must only be set for source type 'Image'"
// +kubebuilder:validation:XValidation:rule="self.type == 'Image' && has(self.image)",message="source type 'Image' requires image field"
type ResolvedCatalogSource struct {
// type is a reference to the type of source the catalog is sourced from.
//
Expand Down
30 changes: 0 additions & 30 deletions api/core/v1alpha1/clustercatalog_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,6 @@ func TestSourceCELValidation(t *testing.T) {
source CatalogSource
wantErrs []string
}{
"unsupported source": {
source: CatalogSource{
Type: "unsupportedType",
},
wantErrs: []string{},
},
"non-image source with disallowed image field": {
source: CatalogSource{
Type: "notImageSourceType",
Image: &ImageSource{},
},
wantErrs: []string{
fmt.Sprintf("%s: Invalid value: \"object\": image field must only be set for source type '%s'", pth, SourceTypeImage),
},
},
"image source missing required image field": {
source: CatalogSource{
Type: SourceTypeImage,
Expand Down Expand Up @@ -133,21 +118,6 @@ func TestResolvedSourceCELValidation(t *testing.T) {
source ResolvedCatalogSource
wantErrs []string
}{
"unsupported source": {
source: ResolvedCatalogSource{
Type: "unsupportedType",
},
wantErrs: []string{},
},
"non-image source with disallowed image field": {
source: ResolvedCatalogSource{
Type: "notImageSourceType",
Image: &ResolvedImageSource{},
},
wantErrs: []string{
fmt.Sprintf("%s: Invalid value: \"object\": image field must only be set for source type '%s'", pth, SourceTypeImage),
},
},
"image source missing required image field": {
source: ResolvedCatalogSource{
Type: SourceTypeImage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ spec:
type: object
x-kubernetes-validations:
- message: source type 'Image' requires image field
rule: self.type != 'Image' || has(self.image)
- message: image field must only be set for source type 'Image'
rule: self.type == 'Image' || !has(self.image)
rule: self.type == 'Image' && has(self.image)
required:
- source
type: object
Expand Down Expand Up @@ -266,9 +264,7 @@ spec:
type: object
x-kubernetes-validations:
- message: source type 'Image' requires image field
rule: self.type != 'Image' || has(self.image)
- message: image field must only be set for source type 'Image'
rule: self.type == 'Image' || !has(self.image)
rule: self.type == 'Image' && has(self.image)
type: object
required:
- metadata
Expand Down

0 comments on commit e7f6707

Please sign in to comment.