Skip to content

Commit

Permalink
Use GitVersion to parse version from discovery (#2312)
Browse files Browse the repository at this point in the history
In some cases, the discovery client can return a kube server version
with the minor version field as a non uint value. To properly parse the
value and get all version fields correctly, instead directly parse the
GitVersion string for all values of the kube server version
  • Loading branch information
kevinrizza authored Aug 3, 2021
1 parent 4fc476f commit 31350db
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
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

0 comments on commit 31350db

Please sign in to comment.