Skip to content

Use GitVersion to parse version from discovery #2312

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

Merged
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
19 changes: 10 additions & 9 deletions pkg/lib/catalogsource/image_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strings"
"sync"

"github.com/blang/semver/v4"
"github.com/sirupsen/logrus"
versionutil "k8s.io/apimachinery/pkg/util/version"
"k8s.io/apimachinery/pkg/version"

"github.com/operator-framework/api/pkg/operators/v1alpha1"
Expand Down Expand Up @@ -171,14 +171,15 @@ func UpdateKubeVersion(serverVersion *version.Info, logger *logrus.Logger) {
return
}

templateNameToReplacementValuesMap[TemplKubeMajorV] = serverVersion.Major
templateNameToReplacementValuesMap[TemplKubeMinorV] = serverVersion.Minor

// api server does not explicitly give patch value, so we have to resort to parsing the git version
serverGitVersion, err := semver.Parse(serverVersion.GitVersion)
// need to use the gitversion from version.info.String() because minor version is not always Uint value
// and patch version is not returned as a first class field
semver, err := versionutil.ParseSemantic(serverVersion.String())
if err != nil {
templateNameToReplacementValuesMap[TemplKubePatchV] = strconv.FormatUint(serverGitVersion.Patch, 10)
} else {
logger.WithError(err).Warn("unable to obtain kube server patch value")
logger.WithError(err).Error("unable to parse kube server version")
return
}

templateNameToReplacementValuesMap[TemplKubeMajorV] = strconv.FormatUint(uint64(semver.Major()), 10)
templateNameToReplacementValuesMap[TemplKubeMinorV] = strconv.FormatUint(uint64(semver.Minor()), 10)
templateNameToReplacementValuesMap[TemplKubePatchV] = strconv.FormatUint(uint64(semver.Patch()), 10)
}
19 changes: 19 additions & 0 deletions pkg/lib/catalogsource/image_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ func TestImageTemplateFlow(t *testing.T) {
GitVersion: "v1.20.0+bd9e442",
}

serverVersionNonReleaseBuild := version.Info{
Major: "1",
Minor: "20+",
GitVersion: "v1.20.1+bd9e442",
}

var table = []struct {
description string // description of test case
catsrc v1alpha1.CatalogSource // fake catalog source for testing
Expand Down Expand Up @@ -124,6 +130,19 @@ func TestImageTemplateFlow(t *testing.T) {
expectedCatalogTemplate: "foo/v{foobar}",
expectedUnresolvedTemplates: []string{},
},
{
description: "multiple kube image template with patch and nonrelease build version",
catsrc: v1alpha1.CatalogSource{
ObjectMeta: v1.ObjectMeta{
Annotations: map[string]string{
CatalogImageTemplateAnnotation: fmt.Sprintf("foo/v%s.%s.%s", TemplKubeMajorV, TemplKubeMinorV, TemplKubePatchV),
},
},
},
serverVersion: &serverVersionNonReleaseBuild,
expectedCatalogTemplate: "foo/v1.20.1",
expectedUnresolvedTemplates: []string{},
},
}

logger := logrus.New()
Expand Down