Skip to content

Commit

Permalink
ignore k3s pre-releases (#18)
Browse files Browse the repository at this point in the history
* Bump latest release to v1.0.4 in manifests

* Fix pre-release being pushed.

* Warning message when a pre-release is available

* Fix unit tests.

* Add test build flag to unit tests
  • Loading branch information
cguertin14 authored Apr 1, 2022
1 parent 537371e commit 0cd440a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ build:
@go build -o ./${BIN_NAME} .

test:
@go test -v ./... -coverprofile coverage.out
@go test --tags unit -v ./... -coverprofile coverage.out
go tool cover -html=coverage.out -o coverage.html

generate-mock:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/cguertin14/k3supdater

go 1.17
go 1.18

require (
github.com/cguertin14/logger v1.0.6
Expand Down
2 changes: 1 addition & 1 deletion manifests/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ resources:
images:
# Needs to be updated on each new release
- name: quay.io/cguertin14/k3supdater
newTag: v1.0.3
newTag: v1.0.4
13 changes: 13 additions & 0 deletions pkg/updater/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,20 @@ func (c *ClientSet) getLatestK3sRelease(ctx context.Context, req getLatestK3sRel
// Find latest stable versions among all releases
latestStableVersions := make([]*github.RepositoryRelease, 0)
for _, r := range releases {
// Exclude pre-releases, since they're
// not fully ready yet
if *r.Prerelease {
logger.Warnf("A new pre-release is available: %q\n", *r.Name)
continue
}

// Exclude release candidates since
// they're not stable versions
if strings.Contains(*r.Name, "rc") {
continue
}

// Keep only three releases.
if len(latestStableVersions) < 3 {
latestStableVersions = append(latestStableVersions, r)
}
Expand All @@ -132,6 +141,10 @@ func (c *ClientSet) getLatestK3sRelease(ctx context.Context, req getLatestK3sRel
// version is more recent than the other ones.
latestRelease = v
logger.Warnf("A new k3s version is available: %q", *latestRelease.Name)

// FUTURE TODO: Send a notification via Slack, Email, Discord, whatever, to inform
// user about the new version available.

break
}
}
Expand Down
82 changes: 52 additions & 30 deletions pkg/updater/operations_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//+build test

package updater

import (
Expand Down Expand Up @@ -56,13 +58,13 @@ func TestUpdateK3sRelease(t *testing.T) {

for name, c := range cases {
t.Run(name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

if c.currentVersion == "" {
c.currentVersion = "v1.23.3"
}

ctrl := gomock.NewController(t)
defer ctrl.Finish()

// create new mock client instance
githubMockClient := github_mocks.NewMockClient(ctrl)

Expand All @@ -79,20 +81,24 @@ func TestUpdateK3sRelease(t *testing.T) {
MaxTimes(1).
Return([]*github.RepositoryRelease{
{
Body: github.String("some release notes"),
Name: github.String("v1.23.5-rc1"),
Body: github.String("some release notes"),
Name: github.String("v1.23.5-rc1"),
Prerelease: github.Bool(false),
},
{
Body: github.String("some release notes"),
Name: github.String("v1.23.4"),
Body: github.String("some release notes"),
Name: github.String("v1.23.4"),
Prerelease: github.Bool(false),
},
{
Body: github.String("some release notes"),
Name: github.String("v1.22.3"),
Body: github.String("some release notes"),
Name: github.String("v1.22.3"),
Prerelease: github.Bool(false),
},
{
Body: github.String("some release notes"),
Name: github.String("v1.21.5"),
Body: github.String("some release notes"),
Name: github.String("v1.21.5"),
Prerelease: github.Bool(false),
},
}, nil, c.latestReleaseError)
githubMockClient.EXPECT().GetBranch(gomock.Any(), gomock.Any()).
Expand Down Expand Up @@ -127,6 +133,7 @@ func TestUpdateK3sRelease(t *testing.T) {
Name: "k3s",
},
})
fmt.Println(err)
if c.expectError && err == nil {
t.FailNow()
}
Expand Down Expand Up @@ -195,14 +202,42 @@ func TestGetGroupVarsFileContent(t *testing.T) {
}

func TestGetLatestK3sRelease(t *testing.T) {
commonReleases := []*github.RepositoryRelease{
{
Body: github.String("some release notes"),
Name: github.String("v1.23.5-rc1"),
Prerelease: github.Bool(false),
},
{
Body: github.String("some release notes"),
Name: github.String("v1.23.4"),
Prerelease: github.Bool(false),
},
{
Body: github.String("some release notes"),
Name: github.String("v1.22.3"),
Prerelease: github.Bool(false),
},
{
Body: github.String("some release notes"),
Name: github.String("v1.21.5"),
Prerelease: github.Bool(false),
},
}

cases := map[string]struct {
fileContent string
preRelease bool

expectedError error
expectError bool
}{
"success case with no error": {
fileContent: base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s: %s", k3sVersionKey, "v1.23.3"))),
fileContent: fmt.Sprintf("%s: %s", k3sVersionKey, "v1.23.3"),
},
"success case with a pre-release": {
fileContent: fmt.Sprintf("%s: %s", k3sVersionKey, "v1.23.3"),
preRelease: true,
},
"error case with response error": {
expectedError: errors.New("some error"),
Expand All @@ -219,30 +254,17 @@ func TestGetLatestK3sRelease(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

if c.preRelease {
commonReleases[0].Prerelease = github.Bool(true)
}

// create new mock client instance
githubMockClient := github_mocks.NewMockClient(ctrl)

// define mock behavior
githubMockClient.EXPECT().GetRepositoryReleases(gomock.Any(), gomock.Any()).
MaxTimes(1).
Return([]*github.RepositoryRelease{
{
Body: github.String("some release notes"),
Name: github.String("v1.23.5-rc1"),
},
{
Body: github.String("some release notes"),
Name: github.String("v1.23.4"),
},
{
Body: github.String("some release notes"),
Name: github.String("v1.22.3"),
},
{
Body: github.String("some release notes"),
Name: github.String("v1.21.5"),
},
}, nil, c.expectedError)
Return(commonReleases, nil, c.expectedError)

// create mock updater client
client := NewClient(context.Background(), Dependencies{
Expand Down

0 comments on commit 0cd440a

Please sign in to comment.