Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions changelog/fragments/allow-v-in-version.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# entries is a list of entries to include in
# release notes and/or the migration guide
entries:
- description: >
Allows the `v` char as a prefix in version option of the
`operator-sdk bundle` and `operator-sdk packagemanifests` commands. The
`v` char in version string, is extremely common, for example the `git describe`
command produces an output like this: `v0.2.0-13-ga74dac1`. When version
string with `v` prefix is given, the version will be normalized to be
semantically valid.

# kind is one of:
# - addition
# - change
# - deprecation
# - removal
# - bugfix
kind: "bugfix"

# Is this a breaking change?
breaking: false
7 changes: 5 additions & 2 deletions internal/cmd/operator-sdk/generate/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"sigs.k8s.io/yaml"

"github.com/blang/semver/v4"
"github.com/operator-framework/api/pkg/apis/scorecard/v1alpha3"
"github.com/operator-framework/operator-manifest-tools/pkg/image"
"github.com/operator-framework/operator-manifest-tools/pkg/imageresolver"
Expand Down Expand Up @@ -122,11 +123,13 @@ func (c *bundleCmd) setDefaults() (err error) {
}

// validateManifests validates c for bundle manifests generation.
func (c bundleCmd) validateManifests() (err error) {
func (c *bundleCmd) validateManifests() (err error) {
if c.version != "" {
if err := genutil.ValidateVersion(c.version); err != nil {
var sv *semver.Version
if sv, err = genutil.ParseVersion(c.version); err != nil {
return err
}
c.version = sv.String()
}

// The three possible usage modes (stdin, inputDir, and legacy dirs) are mutually exclusive
Expand Down
12 changes: 7 additions & 5 deletions internal/cmd/operator-sdk/generate/internal/genutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,20 @@ import (
"sigs.k8s.io/yaml"
)

// ValidateVersion returns an error if version is not a strict semantic version.
func ValidateVersion(version string) error {
// ParseVersion will parse the version into semver.Version, or return error if
// version is not a strict semantic version.
func ParseVersion(version string) (*semver.Version, error) {
version = strings.TrimPrefix(version, "v")
v, err := semver.Parse(version)
if err != nil {
return fmt.Errorf("%s is not a valid semantic version: %v", version, err)
return nil, fmt.Errorf("%s is not a valid semantic version: %v", version, err)
}
// Ensures numerical values composing csvVersion don't contain leading 0's,
// ex. 01.01.01
if v.String() != version {
return fmt.Errorf("version %s contains bad values (parses to %s)", version, v)
return nil, fmt.Errorf("version %s contains bad values (parses to %s)", version, v)
}
return nil
return &v, nil
}

// IsPipeReader returns true if stdin is an open pipe, i.e. the caller can
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2021 The Operator-SDK Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package genutil_test

import (
"fmt"

"github.com/blang/semver/v4"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
genutil "github.com/operator-framework/operator-sdk/internal/cmd/operator-sdk/generate/internal"
)

var _ = Describe("ParseVersion", func() {
vs := []string{
"0.1.2-12-g453fb31",
"v0.1.2-12-g453fb31",
}
for _, v := range vs {
v := v
It(fmt.Sprintf("should see %#v as valid semantic version", v), func() {
sv, err := genutil.ParseVersion(v)
Expect(err).To(BeNil())
Expect(sv.Major).To(BeEquivalentTo(0))
Expect(sv.Minor).To(BeEquivalentTo(1))
Expect(sv.Patch).To(BeEquivalentTo(2))
Expect(sv.Build).To(BeNil())
Expect(sv.Pre).To(Equal([]semver.PRVersion{{
VersionStr: "12-g453fb31",
}}))
})
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"path/filepath"

"github.com/blang/semver/v4"
metricsannotations "github.com/operator-framework/operator-sdk/internal/annotations/metrics"
genutil "github.com/operator-framework/operator-sdk/internal/cmd/operator-sdk/generate/internal"
gencsv "github.com/operator-framework/operator-sdk/internal/generate/clusterserviceversion"
Expand Down Expand Up @@ -111,20 +112,25 @@ func (c *packagemanifestsCmd) setDefaults() (err error) {
}

// validate validates c for package manifests generation.
func (c packagemanifestsCmd) validate() error {

func (c *packagemanifestsCmd) validate() error {
var (
err error
sv *semver.Version
)
if c.version != "" {
if err := genutil.ValidateVersion(c.version); err != nil {
if sv, err = genutil.ParseVersion(c.version); err != nil {
return err
}
c.version = sv.String()
} else {
return errors.New("--version must be set")
}

if c.fromVersion != "" {
if err := genutil.ValidateVersion(c.fromVersion); err != nil {
if sv, err = genutil.ParseVersion(c.fromVersion); err != nil {
return err
}
c.fromVersion = sv.String()
}

if c.inputDir == "" {
Expand Down Expand Up @@ -227,8 +233,8 @@ func (c packagemanifestsCmd) run() error {
}

func (c packagemanifestsCmd) generatePackageManifest() error {
//copy of genpkg withfilewriter()
//move out of internal util pkg?
// copy of genpkg withfilewriter()
// move out of internal util pkg?
if err := os.MkdirAll(c.outputDir, 0755); err != nil {
return err
}
Expand Down