Skip to content

Commit

Permalink
Refactor kubecross retrieval logic
Browse files Browse the repository at this point in the history
We now introduce a new `kubecross` package which provides a new API for
retrieving the kubecross version for the default as well as a specific
branch. This API will be re-used to set the `KUBE_CROSS_VERSION_LATEST`
substitution, which is being used to build the `krel` binary before
building the actual release.

Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
  • Loading branch information
saschagrunert committed Mar 23, 2021
1 parent d41c865 commit b38a891
Show file tree
Hide file tree
Showing 11 changed files with 327 additions and 66 deletions.
2 changes: 1 addition & 1 deletion gcb/release/cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ steps:
echo "Checking out ${_TOOL_REF}"
git checkout ${_TOOL_REF}
- name: k8s.gcr.io/releng/releng-ci:v0.5.0
- name: gcr.io/k8s-staging-releng/k8s-cloud-builder:${_KUBE_CROSS_VERSION_LATEST}
dir: "go/src/k8s.io/release"
env:
- "GOPATH=/workspace/go"
Expand Down
2 changes: 1 addition & 1 deletion gcb/stage/cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ steps:
echo "Checking out ${_TOOL_REF}"
git checkout ${_TOOL_REF}
- name: k8s.gcr.io/releng/releng-ci:v0.5.0
- name: gcr.io/k8s-staging-releng/k8s-cloud-builder:${_KUBE_CROSS_VERSION_LATEST}
dir: "go/src/k8s.io/release"
env:
- "GOPATH=/workspace/go"
Expand Down
19 changes: 9 additions & 10 deletions pkg/announce/announce.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ import (

"github.com/pkg/errors"
"github.com/sirupsen/logrus"

"k8s.io/release/pkg/git"
"k8s.io/release/pkg/release"
"sigs.k8s.io/release-utils/command"
"sigs.k8s.io/release-utils/util"

"k8s.io/release/pkg/kubecross"
)

const (
Expand Down Expand Up @@ -168,14 +167,14 @@ func getGoVersion(tag string) (string, error) {
return "", errors.Wrap(err, "parse version tag")
}

kubecrossBranches := []string{
fmt.Sprintf("release-%d.%d", semver.Major, semver.Minor),
git.DefaultBranch,
}

kubecrossVer, err := release.GetKubecrossVersion(kubecrossBranches...)
branch := fmt.Sprintf("release-%d.%d", semver.Major, semver.Minor)
kc := kubecross.New()
kubecrossVer, err := kc.ForBranch(branch)
if err != nil {
return "", errors.Wrap(err, "get kubecross version")
kubecrossVer, err = kc.Latest()
if err != nil {
return "", errors.Wrap(err, "get kubecross version")
}
}

kubecrossImg := fmt.Sprintf("k8s.gcr.io/build-image/kube-cross:%s", kubecrossVer)
Expand Down
20 changes: 13 additions & 7 deletions pkg/gcp/gcb/gcb.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"k8s.io/release/pkg/gcp/auth"
"k8s.io/release/pkg/gcp/build"
"k8s.io/release/pkg/git"
"k8s.io/release/pkg/kubecross"
"k8s.io/release/pkg/release"
"sigs.k8s.io/release-utils/util"
)
Expand Down Expand Up @@ -350,16 +351,21 @@ func (g *GCB) SetGCBSubstitutions(toolOrg, toolRepo, toolRef string) (map[string

gcbSubs["BUILDVERSION"] = buildVersion

kubecrossBranches := []string{
g.options.Branch,
git.DefaultBranch,
kc := kubecross.New()
kcVersionBranch, err := kc.ForBranch(g.options.Branch)
if err != nil {
return gcbSubs, errors.Wrap(err, "retrieve kube-cross version")
}
gcbSubs["KUBE_CROSS_VERSION"] = kcVersionBranch

kubecrossVersion, kubecrossVersionErr := release.GetKubecrossVersion(kubecrossBranches...)
if kubecrossVersionErr != nil {
return gcbSubs, kubecrossVersionErr
kcVersionLatest := kcVersionBranch
if g.options.Branch != git.DefaultBranch {
kcVersionLatest, err = kc.Latest()
if err != nil {
return gcbSubs, errors.Wrap(err, "retrieve latest kube-cross version")
}
}
gcbSubs["KUBE_CROSS_VERSION"] = kubecrossVersion
gcbSubs["KUBE_CROSS_VERSION_LATEST"] = kcVersionLatest

buildVersionSemver, err := util.TagStringToSemver(buildVersion)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/gcp/gcb/gcb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package gcb_test
import (
"errors"
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -530,7 +531,7 @@ func dropDynamicSubstitutions(orig map[string]string) (result map[string]string)
result = orig

for k := range result {
if k == "BUILDVERSION" || k == "GCP_USER_TAG" || k == "KUBE_CROSS_VERSION" {
if k == "BUILDVERSION" || k == "GCP_USER_TAG" || strings.HasPrefix(k, "KUBE_CROSS_VERSION") {
delete(result, k)
}
}
Expand Down
31 changes: 31 additions & 0 deletions pkg/kubecross/impl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright 2021 The Kubernetes 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 kubecross

import "sigs.k8s.io/release-utils/http"

//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
//counterfeiter:generate . impl
type impl interface {
GetURLResponse(url string, trim bool) (string, error)
}

type defaultImpl struct{}

func (*defaultImpl) GetURLResponse(url string, trim bool) (string, error) {
return http.GetURLResponse(url, trim)
}
60 changes: 60 additions & 0 deletions pkg/kubecross/kubecross.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright 2021 The Kubernetes 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 kubecross

import (
"fmt"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"

"k8s.io/release/pkg/git"
)

// KubeCross is the main structure of this package.
type KubeCross struct {
impl impl
}

// New creates a new KubeCross instance.
func New() *KubeCross {
return &KubeCross{&defaultImpl{}}
}

// Latest returns the latest available kubecross version.
func (k *KubeCross) Latest() (string, error) {
return k.ForBranch(git.DefaultBranch)
}

// ForBranch returns the kubecross version for the provided branch.
func (k *KubeCross) ForBranch(branch string) (string, error) {
logrus.Infof("Trying to retrieve kube-cross version for branch %s", branch)

const (
baseURL = "https://raw.githubusercontent.com/kubernetes/kubernetes"
versionPath = "build/build-image/cross/VERSION"
)

url := fmt.Sprintf("%s/%s/%s", baseURL, branch, versionPath)
version, err := k.impl.GetURLResponse(url, true)
if err != nil {
return "", errors.Wrap(err, "get URL response")
}

logrus.Infof("Retrieved kube-cross version: %s", version)
return version, nil
}
96 changes: 96 additions & 0 deletions pkg/kubecross/kubecross_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
Copyright 2021 The Kubernetes 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 kubecross

import (
"testing"

"github.com/pkg/errors"
"github.com/stretchr/testify/require"

"k8s.io/release/pkg/kubecross/kubecrossfakes"
)

func TestLatest(t *testing.T) {
for _, tc := range []struct {
prepare func(*kubecrossfakes.FakeImpl)
expect func(res string, err error)
}{
{ // success
prepare: func(mock *kubecrossfakes.FakeImpl) {
mock.GetURLResponseReturns("test", nil)
},
expect: func(res string, err error) {
require.Nil(t, err)
require.Equal(t, "test", res)
},
},
{ // failure GetURLResponse
prepare: func(mock *kubecrossfakes.FakeImpl) {
mock.GetURLResponseReturns("", errors.New(""))
},
expect: func(res string, err error) {
require.NotNil(t, err)
require.Empty(t, res)
},
},
} {
mock := &kubecrossfakes.FakeImpl{}
tc.prepare(mock)

kc := New()
kc.impl = mock

res, err := kc.Latest()
tc.expect(res, err)
}
}

func TestForBranch(t *testing.T) {
for _, tc := range []struct {
prepare func(*kubecrossfakes.FakeImpl)
expect func(res string, err error)
}{
{ // success
prepare: func(mock *kubecrossfakes.FakeImpl) {
mock.GetURLResponseReturns("test", nil)
},
expect: func(res string, err error) {
require.Nil(t, err)
require.Equal(t, "test", res)
},
},
{ // failure GetURLResponse
prepare: func(mock *kubecrossfakes.FakeImpl) {
mock.GetURLResponseReturns("", errors.New(""))
},
expect: func(res string, err error) {
require.NotNil(t, err)
require.Empty(t, res)
},
},
} {
mock := &kubecrossfakes.FakeImpl{}
tc.prepare(mock)

kc := New()
kc.impl = mock

res, err := kc.ForBranch("")
tc.expect(res, err)
}
}
Loading

0 comments on commit b38a891

Please sign in to comment.