Skip to content

Commit

Permalink
extension: use union discriminator patther in source specification (#645
Browse files Browse the repository at this point in the history
)

Signed-off-by: Joe Lanford <joe.lanford@gmail.com>
  • Loading branch information
joelanford authored Feb 22, 2024
1 parent f7823aa commit cc1ad3e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
14 changes: 12 additions & 2 deletions api/v1alpha1/extension_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ const (
ManagedStatePaused ExtensionManagedState = "Paused"
)

const (
SourceTypePackage = "package"
)

type ExtensionSourcePackage struct {
//+kubebuilder:validation:MaxLength:=48
//+kubebuilder:validation:Pattern:=^[a-z0-9]+(-[a-z0-9]+)*$
Expand Down Expand Up @@ -59,11 +63,17 @@ type ExtensionSourcePackage struct {
UpgradeConstraintPolicy UpgradeConstraintPolicy `json:"upgradeConstraintPolicy,omitempty"`
}

// +kubebuilder:validation:XValidation:rule="self.sourceType=='package' && has(self.__package__)",message="sourceType must match populated union field"
//
// ExtensionSource defines the source for this Extension, right now, only a package is supported.
type ExtensionSource struct {
// A source package defined by a name, version and/or channel
//+kubebuilder:validation:Enum:=package
//+kubebuilder:validation:Required
Package *ExtensionSourcePackage `json:"package"`
// sourceType is the discriminator for the source type
SourceType string `json:"sourceType"`

// package defines a reference for a bundle in a catalog defined by a name and a version and/or channel
Package *ExtensionSourcePackage `json:"package,omitempty"`
}

// ExtensionSpec defines the desired state of Extension
Expand Down
14 changes: 11 additions & 3 deletions config/crd/bases/olm.operatorframework.io_extensions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ spec:
description: source of Extension to be installed
properties:
package:
description: A source package defined by a name, version and/or
channel
description: package defines a reference for a bundle in a catalog
defined by a name and a version and/or channel
properties:
channel:
description: channel constraint definition
Expand Down Expand Up @@ -95,9 +95,17 @@ spec:
required:
- name
type: object
sourceType:
description: sourceType is the discriminator for the source type
enum:
- package
type: string
required:
- package
- sourceType
type: object
x-kubernetes-validations:
- message: sourceType must match populated union field
rule: self.sourceType=='package' && has(self.__package__)
required:
- serviceAccountName
- source
Expand Down
12 changes: 9 additions & 3 deletions internal/controllers/extension_admission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestExtensionAdmissionServiceAccount(t *testing.T) {
err := cl.Create(context.Background(), buildExtension(ocv1alpha1.ExtensionSpec{
ServiceAccountName: tc.saName,
Source: ocv1alpha1.ExtensionSource{
SourceType: ocv1alpha1.SourceTypePackage,
Package: &ocv1alpha1.ExtensionSourcePackage{
Name: "package",
},
Expand All @@ -65,9 +66,11 @@ func TestExtensionAdmissionSource(t *testing.T) {
source ocv1alpha1.ExtensionSource
err string
}{
{"empty source", ocv1alpha1.ExtensionSource{}, "spec.source.package: Required value"},
{"source with empty package", ocv1alpha1.ExtensionSource{Package: &ocv1alpha1.ExtensionSourcePackage{}}, "spec.source.package.name in body should match"},
{"source with minimal valid package", ocv1alpha1.ExtensionSource{Package: &ocv1alpha1.ExtensionSourcePackage{Name: "package"}}, ""},
{"empty source", ocv1alpha1.ExtensionSource{}, `spec.source.sourceType: Unsupported value: "": supported values: "package"`},
{"invalid sourceType", ocv1alpha1.ExtensionSource{SourceType: "invalid"}, `spec.source.sourceType: Unsupported value: "invalid": supported values: "package"`},
{"source with unset package", ocv1alpha1.ExtensionSource{SourceType: ocv1alpha1.SourceTypePackage}, `spec.source: Invalid value: "object": sourceType must match populated union field`},
{"source with empty package", ocv1alpha1.ExtensionSource{SourceType: ocv1alpha1.SourceTypePackage, Package: &ocv1alpha1.ExtensionSourcePackage{}}, "spec.source.package.name in body should match"},
{"source with minimal valid package", ocv1alpha1.ExtensionSource{SourceType: ocv1alpha1.SourceTypePackage, Package: &ocv1alpha1.ExtensionSourcePackage{Name: "package"}}, ""},
}

t.Parallel()
Expand Down Expand Up @@ -124,6 +127,7 @@ func TestExtensionAdmissionSourcePackageName(t *testing.T) {
err := cl.Create(context.Background(), buildExtension(ocv1alpha1.ExtensionSpec{
ServiceAccountName: "serviceaccount",
Source: ocv1alpha1.ExtensionSource{
SourceType: ocv1alpha1.SourceTypePackage,
Package: &ocv1alpha1.ExtensionSourcePackage{
Name: tc.pkgName,
},
Expand Down Expand Up @@ -216,6 +220,7 @@ func TestExtensionAdmissionSourcePackageVersion(t *testing.T) {
err := cl.Create(context.Background(), buildExtension(ocv1alpha1.ExtensionSpec{
ServiceAccountName: "serviceaccount",
Source: ocv1alpha1.ExtensionSource{
SourceType: ocv1alpha1.SourceTypePackage,
Package: &ocv1alpha1.ExtensionSourcePackage{
Name: "package",
Version: tc.version,
Expand Down Expand Up @@ -265,6 +270,7 @@ func TestExtensionAdmissionSourcePackageChannel(t *testing.T) {
err := cl.Create(context.Background(), buildExtension(ocv1alpha1.ExtensionSpec{
ServiceAccountName: "serviceaccount",
Source: ocv1alpha1.ExtensionSource{
SourceType: ocv1alpha1.SourceTypePackage,
Package: &ocv1alpha1.ExtensionSourcePackage{
Name: "package",
Channel: tc.channelName,
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/extension_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestExtensionReconcile(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{Name: extKey.Name, Namespace: extKey.Namespace},
Spec: ocv1alpha1.ExtensionSpec{
ServiceAccountName: "test-service-account",
Source: ocv1alpha1.ExtensionSource{Package: &ocv1alpha1.ExtensionSourcePackage{Name: "test-package"}},
Source: ocv1alpha1.ExtensionSource{SourceType: ocv1alpha1.SourceTypePackage, Package: &ocv1alpha1.ExtensionSourcePackage{Name: "test-package"}},
},
}
if tc.paused {
Expand Down

0 comments on commit cc1ad3e

Please sign in to comment.