-
Notifications
You must be signed in to change notification settings - Fork 78
add Release version as an optional field in the CSV #454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package release | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "slices" | ||
| "strings" | ||
|
|
||
| semver "github.com/blang/semver/v4" | ||
| ) | ||
|
|
||
| // +k8s:openapi-gen=true | ||
| // OperatorRelease is a wrapper around a slice of semver.PRVersion which supports correct | ||
| // marshaling to YAML and JSON. | ||
| // +kubebuilder:validation:Type=string | ||
| // +kubebuilder:validation:MaxLength=20 | ||
| // +kubebuilder:validation:XValidation:rule="self.matches('^[0-9A-Za-z-]+(\\\\.[0-9A-Za-z-]+)*$')",message="release version must be a valid semver prerelease format (dot-separated identifiers containing only alphanumerics and hyphens)" | ||
| // +kubebuilder:validation:XValidation:rule="!self.split('.').exists(x, x.matches('^0[0-9]+$'))",message="numeric identifiers in release version must not have leading zeros" | ||
| type OperatorRelease struct { | ||
| Release []semver.PRVersion `json:"-"` | ||
| } | ||
|
|
||
| // DeepCopyInto creates a deep-copy of the Version value. | ||
| func (v *OperatorRelease) DeepCopyInto(out *OperatorRelease) { | ||
| out.Release = slices.Clone(v.Release) | ||
| } | ||
|
|
||
| // MarshalJSON implements the encoding/json.Marshaler interface. | ||
| func (v OperatorRelease) MarshalJSON() ([]byte, error) { | ||
| segments := []string{} | ||
| for _, segment := range v.Release { | ||
| segments = append(segments, segment.String()) | ||
| } | ||
| return json.Marshal(strings.Join(segments, ".")) | ||
| } | ||
|
|
||
| // UnmarshalJSON implements the encoding/json.Unmarshaler interface. | ||
| func (v *OperatorRelease) UnmarshalJSON(data []byte) (err error) { | ||
| var versionString string | ||
|
|
||
| if err = json.Unmarshal(data, &versionString); err != nil { | ||
| return | ||
| } | ||
|
|
||
| segments := strings.Split(versionString, ".") | ||
| for _, segment := range segments { | ||
| release, err := semver.NewPRVersion(segment) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| v.Release = append(v.Release, release) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // OpenAPISchemaType is used by the kube-openapi generator when constructing | ||
| // the OpenAPI spec of this type. | ||
| // | ||
| // See: https://github.com/kubernetes/kube-openapi/tree/master/pkg/generators | ||
| func (_ OperatorRelease) OpenAPISchemaType() []string { return []string{"string"} } | ||
|
|
||
| // OpenAPISchemaFormat is used by the kube-openapi generator when constructing | ||
| // the OpenAPI spec of this type. | ||
| // "semver" is not a standard openapi format but tooling may use the value regardless | ||
| func (_ OperatorRelease) OpenAPISchemaFormat() string { return "semver" } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| package release | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| semver "github.com/blang/semver/v4" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestOperatorReleaseMarshal(t *testing.T) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not seeing any tests for alpha-numeric? |
||
| tests := []struct { | ||
| name string | ||
| in OperatorRelease | ||
| out []byte | ||
| err error | ||
| }{ | ||
| { | ||
| name: "single-segment", | ||
| in: OperatorRelease{Release: []semver.PRVersion{mustNewPRVersion("1")}}, | ||
| out: []byte(`"1"`), | ||
| }, | ||
| { | ||
| name: "two-segments", | ||
| in: OperatorRelease{Release: []semver.PRVersion{mustNewPRVersion("1"), mustNewPRVersion("0")}}, | ||
| out: []byte(`"1.0"`), | ||
| }, | ||
| { | ||
| name: "multi-segment", | ||
| in: OperatorRelease{Release: []semver.PRVersion{ | ||
| mustNewPRVersion("1"), | ||
| mustNewPRVersion("2"), | ||
| mustNewPRVersion("3"), | ||
| }}, | ||
| out: []byte(`"1.2.3"`), | ||
| }, | ||
| { | ||
| name: "numeric-segments", | ||
| in: OperatorRelease{Release: []semver.PRVersion{ | ||
| mustNewPRVersion("20240101"), | ||
| mustNewPRVersion("12345"), | ||
| }}, | ||
| out: []byte(`"20240101.12345"`), | ||
| }, | ||
| { | ||
| name: "alphanumeric-segments", | ||
| in: OperatorRelease{Release: []semver.PRVersion{ | ||
| mustNewPRVersion("alpha"), | ||
| mustNewPRVersion("beta"), | ||
| mustNewPRVersion("1"), | ||
| }}, | ||
| out: []byte(`"alpha.beta.1"`), | ||
| }, | ||
| { | ||
| name: "alphanumeric-with-hyphens", | ||
| in: OperatorRelease{Release: []semver.PRVersion{ | ||
| mustNewPRVersion("rc-1"), | ||
| mustNewPRVersion("build-123"), | ||
| }}, | ||
| out: []byte(`"rc-1.build-123"`), | ||
| }, | ||
| { | ||
| name: "mixed-alphanumeric", | ||
| in: OperatorRelease{Release: []semver.PRVersion{ | ||
| mustNewPRVersion("1"), | ||
| mustNewPRVersion("2-beta"), | ||
| mustNewPRVersion("x86-64"), | ||
| }}, | ||
| out: []byte(`"1.2-beta.x86-64"`), | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| m, err := tt.in.MarshalJSON() | ||
| require.Equal(t, tt.out, m, string(m)) | ||
| require.Equal(t, tt.err, err) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestOperatorReleaseUnmarshal(t *testing.T) { | ||
| type TestStruct struct { | ||
| Release OperatorRelease `json:"r"` | ||
| } | ||
| tests := []struct { | ||
| name string | ||
| in []byte | ||
| out TestStruct | ||
| err error | ||
| }{ | ||
| { | ||
| name: "single-segment", | ||
| in: []byte(`{"r": "1"}`), | ||
| out: TestStruct{Release: OperatorRelease{Release: []semver.PRVersion{mustNewPRVersion("1")}}}, | ||
| }, | ||
| { | ||
| name: "two-segments", | ||
| in: []byte(`{"r": "1.0"}`), | ||
| out: TestStruct{Release: OperatorRelease{Release: []semver.PRVersion{mustNewPRVersion("1"), mustNewPRVersion("0")}}}, | ||
| }, | ||
| { | ||
| name: "multi-segment", | ||
| in: []byte(`{"r": "1.2.3"}`), | ||
| out: TestStruct{Release: OperatorRelease{Release: []semver.PRVersion{ | ||
| mustNewPRVersion("1"), | ||
| mustNewPRVersion("2"), | ||
| mustNewPRVersion("3"), | ||
| }}}, | ||
| }, | ||
| { | ||
| name: "numeric-segments", | ||
| in: []byte(`{"r": "20240101.12345"}`), | ||
| out: TestStruct{Release: OperatorRelease{Release: []semver.PRVersion{ | ||
| mustNewPRVersion("20240101"), | ||
| mustNewPRVersion("12345"), | ||
| }}}, | ||
| }, | ||
| { | ||
| name: "alphanumeric-segments", | ||
| in: []byte(`{"r": "alpha.beta.1"}`), | ||
| out: TestStruct{Release: OperatorRelease{Release: []semver.PRVersion{ | ||
| mustNewPRVersion("alpha"), | ||
| mustNewPRVersion("beta"), | ||
| mustNewPRVersion("1"), | ||
| }}}, | ||
| }, | ||
| { | ||
| name: "alphanumeric-with-hyphens", | ||
| in: []byte(`{"r": "rc-1.build-123"}`), | ||
| out: TestStruct{Release: OperatorRelease{Release: []semver.PRVersion{ | ||
| mustNewPRVersion("rc-1"), | ||
| mustNewPRVersion("build-123"), | ||
| }}}, | ||
| }, | ||
| { | ||
| name: "mixed-alphanumeric", | ||
| in: []byte(`{"r": "1.2-beta.x86-64"}`), | ||
| out: TestStruct{Release: OperatorRelease{Release: []semver.PRVersion{ | ||
| mustNewPRVersion("1"), | ||
| mustNewPRVersion("2-beta"), | ||
| mustNewPRVersion("x86-64"), | ||
| }}}, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| s := TestStruct{} | ||
| err := json.Unmarshal(tt.in, &s) | ||
| require.Equal(t, tt.out, s) | ||
| require.Equal(t, tt.err, err) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func mustNewPRVersion(s string) semver.PRVersion { | ||
| v, err := semver.NewPRVersion(s) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| return v | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -13,6 +13,7 @@ import ( | |||||
| "k8s.io/apimachinery/pkg/labels" | ||||||
| "k8s.io/apimachinery/pkg/util/intstr" | ||||||
|
|
||||||
| "github.com/operator-framework/api/pkg/lib/release" | ||||||
| "github.com/operator-framework/api/pkg/lib/version" | ||||||
| ) | ||||||
|
|
||||||
|
|
@@ -274,8 +275,10 @@ type APIServiceDefinitions struct { | |||||
| // that can manage apps for a given version. | ||||||
| // +k8s:openapi-gen=true | ||||||
| type ClusterServiceVersionSpec struct { | ||||||
| InstallStrategy NamedInstallStrategy `json:"install"` | ||||||
| Version version.OperatorVersion `json:"version,omitempty"` | ||||||
| InstallStrategy NamedInstallStrategy `json:"install"` | ||||||
| Version version.OperatorVersion `json:"version,omitempty"` | ||||||
| // +optional | ||||||
| Release release.OperatorRelease `json:"release,omitzero"` | ||||||
|
Comment on lines
+280
to
+281
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better late than never to add all the nice GoDoc, similar to what we've done in OLMv1 APIs?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, please include user-friendly GoDoc here.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make it a pointer and use omitempty instead of omitzero?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To solve what problem?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it's a question of whether we want to differentiate between "field not set" which is the nil case vs "field explicitly set but empty"?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the empty string a valid value? What does it mean for this to be set to the empty string vs being omitted entirely? |
||||||
| Maturity string `json:"maturity,omitempty"` | ||||||
| CustomResourceDefinitions CustomResourceDefinitions `json:"customresourcedefinitions,omitempty"` | ||||||
| APIServiceDefinitions APIServiceDefinitions `json:"apiservicedefinitions,omitempty"` | ||||||
|
|
@@ -595,6 +598,7 @@ type ResourceInstance struct { | |||||
| // +kubebuilder:subresource:status | ||||||
| // +kubebuilder:printcolumn:name="Display",type=string,JSONPath=`.spec.displayName`,description="The name of the CSV" | ||||||
| // +kubebuilder:printcolumn:name="Version",type=string,JSONPath=`.spec.version`,description="The version of the CSV" | ||||||
| // +kubebuilder:printcolumn:name="Release",type=string,JSONPath=`.spec.release`,description="The release version of the CSV" | ||||||
| // +kubebuilder:printcolumn:name="Replaces",type=string,JSONPath=`.spec.replaces`,description="The name of a CSV that this one replaces" | ||||||
| // +kubebuilder:printcolumn:name="Phase",type=string,JSONPath=`.status.phase` | ||||||
|
|
||||||
|
|
||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add more validation here:
Was there any API review on this front? They'd be better at pointing out all the things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR was provided as part of the internal enhancement review, yes.
Links are in epic