Skip to content
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

Feature: Check lagoon-core version #91

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
73 changes: 73 additions & 0 deletions cmd/validate_lagoonversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/uselagoon/build-deploy-tool/internal/compat"
generator "github.com/uselagoon/build-deploy-tool/internal/generator"
)

var validateLagoonVersion = &cobra.Command{
Use: "lagoon-version",
Aliases: []string{"lagoon-ver", "lag-ver", "lver", "lv"},
Short: "Check if the Lagoon version provided is supported by this tool",
Run: func(cmd *cobra.Command, args []string) {
// check for the LAGOON_SYSTEM_CORE_VERSION
version, err := ValidateLagoonVersion(false)
if err != nil {
fmt.Println(fmt.Sprintf("Unable to validate lagoon version; %v", err))
os.Exit(1)
}
supported := compat.CheckVersion(version)
if !supported {
fmt.Println(fmt.Sprintf("Lagoon version %s is not supported by this build-deploy-tool, you will need to upgrade your lagoon-core to at least version %s", version, compat.SupportedMinVersion()))
os.Exit(1)
}
},
}

// ValidateLagoonVersion .
func ValidateLagoonVersion(debug bool) (string, error) {
lagoonBuild, err := generator.NewGenerator(
lagoonYml,
lagoonYmlOverride,
projectVariables,
environmentVariables,
projectName,
environmentName,
environmentType,
activeEnvironment,
standbyEnvironment,
buildType,
branch,
prNumber,
prTitle,
prHeadBranch,
prBaseBranch,
lagoonVersion,
defaultBackupSchedule,
hourlyDefaultBackupRetention,
dailyDefaultBackupRetention,
weeklyDefaultBackupRetention,
monthlyDefaultBackupRetention,
monitoringContact,
monitoringStatusPageID,
fastlyCacheNoCahce,
fastlyAPISecretPrefix,
fastlyServiceID,
ignoreNonStringKeyErrors,
ignoreMissingEnvFiles,
debug,
)
if err != nil {
return "", err
}

return lagoonBuild.BuildValues.LagoonCoreVersion, nil
}

func init() {
validateCmd.AddCommand(validateLagoonVersion)
}
143 changes: 143 additions & 0 deletions cmd/validate_lagoonversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package cmd

import (
"os"
"testing"

"github.com/uselagoon/build-deploy-tool/internal/helpers"
)

func TestValidateLagoonVersion(t *testing.T) {
type args struct {
debug bool
lagoonSystemCoreVersion string
alertContact string
statusPageID string
projectName string
environmentName string
branch string
prNumber string
prHeadBranch string
prBaseBranch string
environmentType string
buildType string
activeEnvironment string
standbyEnvironment string
cacheNoCache string
serviceID string
secretPrefix string
projectVars string
envVars string
lagoonVersion string
lagoonYAML string
valuesFilePath string
templatePath string
}
tests := []struct {
name string
args args
want string
wantErr bool
vars []helpers.EnvironmentVariable
}{
{
name: "test1",
args: args{
alertContact: "alertcontact",
statusPageID: "statuspageid",
projectName: "example-project",
environmentName: "main",
environmentType: "production",
buildType: "branch",
lagoonVersion: "v2.7.x",
branch: "main",
projectVars: `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"},
{"name":"LAGOON_SYSTEM_CORE_VERSION","value":"v2.10.0","scope":"internal_system"}]`,
envVars: `[]`,
lagoonYAML: "../test-resources/basic/lagoon.yml",
},
want: "v2.10.0",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// set the environment variables from args
err := os.Setenv("MONITORING_ALERTCONTACT", tt.args.alertContact)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("MONITORING_STATUSPAGEID", tt.args.statusPageID)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("PROJECT", tt.args.projectName)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("ENVIRONMENT", tt.args.environmentName)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("BRANCH", tt.args.branch)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("PR_NUMBER", tt.args.prNumber)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("PR_HEAD_BRANCH", tt.args.prHeadBranch)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("PR_BASE_BRANCH", tt.args.prBaseBranch)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("ENVIRONMENT_TYPE", tt.args.environmentType)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("BUILD_TYPE", tt.args.buildType)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("ACTIVE_ENVIRONMENT", tt.args.activeEnvironment)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("STANDBY_ENVIRONMENT", tt.args.standbyEnvironment)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("LAGOON_FASTLY_NOCACHE_SERVICE_ID", tt.args.cacheNoCache)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("LAGOON_PROJECT_VARIABLES", tt.args.projectVars)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("LAGOON_ENVIRONMENT_VARIABLES", tt.args.envVars)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("LAGOON_VERSION", tt.args.lagoonVersion)
if err != nil {
t.Errorf("%v", err)
}
lagoonYml = tt.args.lagoonYAML
got, err := ValidateLagoonVersion(tt.args.debug)
if (err != nil) != tt.wantErr {
t.Errorf("ValidateLagoonVersion() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ValidateLagoonVersion() = %v, want %v", got, tt.want)
}
t.Cleanup(func() {
helpers.UnsetEnvVars(tt.vars)
})
})
}
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ require (
github.com/compose-spec/compose-go v1.2.7
github.com/cxmcc/unixsums v0.0.0-20131125091133-89564297d82f
github.com/google/go-cmp v0.5.7
github.com/imdario/mergo v0.3.13
github.com/spf13/cobra v1.4.0
github.com/vshn/k8up v1.99.99
golang.org/x/mod v0.4.2
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.23.6
k8s.io/apimachinery v0.23.6
Expand All @@ -27,7 +29,6 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mattn/go-shellwords v1.0.12 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,7 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.1-0.20200828183125-ce943fd02449/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20170114055629-f2499483f923/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
27 changes: 27 additions & 0 deletions internal/compat/compat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package compat

import (
"golang.org/x/mod/semver"
)

// this is the minimum supported version of Lagoon that the build tool can be used with
// if there is a change in lagoon-core that the build tool has to support, then this value
// should be adjusted to the version of lagoon-core to support
// this could also be modified with a compiler flag for developing
var supportedMinVersion = "v2.9.0"

func checkVersion(version, minVersion string) bool {
comp := semver.Compare(version, minVersion)
if comp == -1 {
return false
}
return true
}

func CheckVersion(version string) bool {
return checkVersion(version, supportedMinVersion)
}

func SupportedMinVersion() string {
return supportedMinVersion
}
83 changes: 83 additions & 0 deletions internal/compat/compat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package compat

import (
"testing"
)

func Test_checkVersion(t *testing.T) {
type args struct {
version string
}
tests := []struct {
name string
args args
supportedMinVersion string
want bool
}{
{
name: "test1",
args: args{
version: "v2.8.4",
},
supportedMinVersion: "v2.9.0",
want: false,
},
{
name: "test2",
args: args{
version: "v2.9.0",
},
supportedMinVersion: "v2.9.0",
want: true,
},
{
name: "test3",
args: args{
version: "v2.9.3",
},
supportedMinVersion: "v2.9.0",
want: true,
},
{
name: "test4",
args: args{
version: "",
},
supportedMinVersion: "v2.9.0",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := checkVersion(tt.args.version, tt.supportedMinVersion); got != tt.want {
t.Errorf("checkVersion() = %v, want %v", got, tt.want)
}
})
}
}

func TestCheckVersion(t *testing.T) {
type args struct {
version string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "test1",
args: args{
version: "v1.0.0",
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := CheckVersion(tt.args.version); got != tt.want {
t.Errorf("CheckVersion() = %v, want %v", got, tt.want)
}
})
}
}
3 changes: 2 additions & 1 deletion internal/generator/buildvalues.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ type BuildValues struct {
GitSha string `json:"gitSha"`
BuildType string `json:"buildType"`
Kubernetes string `json:"kubernetes"`
LagoonVersion string `json:"lagoonVersion"`
LagoonVersion string `json:"lagoonVersion"` // this is the version that is bundled in images, probably stop using this?
LagoonCoreVersion string `json:"lagoonCoreVersion"` // this is the version that will come from lagoon-core
ActiveEnvironment string `json:"activeEnvironment"`
StandbyEnvironment string `json:"standbyEnvironment"`
IsActiveEnvironment bool `json:"isActiveEnvironment"`
Expand Down
6 changes: 6 additions & 0 deletions internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ func NewGenerator(
ingressClass := CheckFeatureFlag("INGRESS_CLASS", lagoonEnvVars, debug)
buildValues.IngressClass = ingressClass

// get the lagoon core version from the project variables internal_system scope
lagoonCoreVersion, _ := lagoon.GetLagoonVariable("LAGOON_SYSTEM_CORE_VERSION", []string{"internal_system"}, lagoonEnvVars)
if lagoonCoreVersion != nil {
buildValues.LagoonCoreVersion = lagoonCoreVersion.Value
}

// get any variables from the API here
lagoonServiceTypes, _ := lagoon.GetLagoonVariable("LAGOON_SERVICE_TYPES", nil, lagoonEnvVars)
buildValues.ServiceTypeOverrides = lagoonServiceTypes
Expand Down
Loading