Skip to content

Commit

Permalink
Return error when using inactive k8s paas template (#413)
Browse files Browse the repository at this point in the history
* upgrade gsclient-go v3.16.0

* print error when deprecated gsk version is set

* only consider active gsk templates when setting gsk release

* ensure the gsk template stay consistent when release is not changed

relates to #397

* print correct list of active gsk versions
  • Loading branch information
nvthongswansea authored Jan 7, 2025
1 parent 3499f0b commit 1fed27a
Show file tree
Hide file tree
Showing 46 changed files with 1,280 additions and 71 deletions.
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/terraform-providers/terraform-provider-gridscale

require (
github.com/aws/aws-sdk-go v1.55.5
github.com/gridscale/gsclient-go/v3 v3.15.0
github.com/gridscale/gsclient-go/v3 v3.16.0
github.com/hashicorp/go-version v1.7.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.35.0
)
Expand Down Expand Up @@ -53,7 +53,7 @@ require (
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/sys v0.29.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/appengine v1.6.8 // indirect
Expand All @@ -62,5 +62,6 @@ require (
google.golang.org/protobuf v1.35.1 // indirect
)

go 1.21
go 1.22.0

toolchain go1.22.5
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gridscale/gsclient-go/v3 v3.15.0 h1:xmA5TCmNKKqIBP9UhG6IPhPjL1kiO5g4f5wiGOJcpVU=
github.com/gridscale/gsclient-go/v3 v3.15.0/go.mod h1:B/xAA91QRonCLb+ScJix4ODj6wHWaDpPwvFHQD2KqsM=
github.com/gridscale/gsclient-go/v3 v3.16.0 h1:NroV9q8QQn6KIwu4d7qXkvf3Z5YCKtR3rEcYeL9W6+4=
github.com/gridscale/gsclient-go/v3 v3.16.0/go.mod h1:B/xAA91QRonCLb+ScJix4ODj6wHWaDpPwvFHQD2KqsM=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-checkpoint v0.5.0 h1:MFYpPZCnQqQTE18jFwSII6eUQrD/oxMFp3mlgcqk5mU=
Expand Down Expand Up @@ -188,8 +188,8 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.25.0 h1:WtHI/ltw4NvSUig5KARz9h521QvRC8RmF/cuYqifU24=
Expand Down
40 changes: 34 additions & 6 deletions gridscale/resource_gridscale_k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net"
"net/http"
"regexp"
"sort"
"strings"
"time"

Expand Down Expand Up @@ -396,7 +397,8 @@ func deriveK8sTemplateFromResourceDiff(client *gsclient.Client, d *schema.Resour
case "version":
return deriveK8sTemplateFromGSKVersion(client, version)
case "release":
return deriveK8sTemplateFromRelease(client, release)
currenTemplateUUID := d.Get("service_template_uuid").(string)
return deriveK8sTemplateFromRelease(client, release, currenTemplateUUID)
}
return nil, nil
}
Expand Down Expand Up @@ -448,7 +450,8 @@ func deriveK8sTemplateFromResourceData(client *gsclient.Client, d *schema.Resour
case "version":
return deriveK8sTemplateFromGSKVersion(client, version)
case "release":
return deriveK8sTemplateFromRelease(client, release)
currenTemplateUUID := d.Get("service_template_uuid").(string)
return deriveK8sTemplateFromRelease(client, release, currenTemplateUUID)
}
currentTemplateUUID := d.Get("service_template_uuid").(string)
return deriveK8sTemplateFromUUID(client, currentTemplateUUID)
Expand Down Expand Up @@ -490,14 +493,18 @@ func deriveK8sTemplateFromGSKVersion(client *gsclient.Client, version string) (*
}

var derived bool
var isActive bool
var versions []string
var template gsclient.PaaSTemplate

for _, paasTemplate := range paasTemplates {
if paasTemplate.Properties.Flavour == k8sTemplateFlavourName {
versions = append(versions, template.Properties.Version)
if paasTemplate.Properties.Active {
versions = append(versions, template.Properties.Version)
}

if paasTemplate.Properties.Version == version {
isActive = paasTemplate.Properties.Active
derived = true
template = paasTemplate
break
Expand All @@ -508,27 +515,48 @@ func deriveK8sTemplateFromGSKVersion(client *gsclient.Client, version string) (*
if !derived {
return nil, fmt.Errorf("%v is an invalid gridscale Kubernetes (GSK) version. Valid GSK versions are: %v", version, strings.Join(versions, ", "))
}
if !isActive {
return nil, fmt.Errorf("%v is a deprecated gridscale Kubernetes (GSK) version. Valid GSK versions are: %v", version, strings.Join(versions, ", "))
}
return &template, nil
}

// deriveK8sTemplateFromRelease derives the k8s service template from given release.
func deriveK8sTemplateFromRelease(client *gsclient.Client, release string) (*gsclient.PaaSTemplate, error) {
func deriveK8sTemplateFromRelease(client *gsclient.Client, release, currenTemplateUUID string) (*gsclient.PaaSTemplate, error) {
paasTemplates, err := client.GetPaaSTemplateList(context.Background())
if err != nil {
return nil, err
}

// Sort paasTemplates by version (ascending)
sort.Slice(paasTemplates, func(i, j int) bool {
return paasTemplates[i].Properties.Version < paasTemplates[j].Properties.Version
})

// if the current template's release is the same as the requested release, return the current template
// this is to prevent this function return a different template than the current one when the release is not changed
// E.g. Template of 1.30.4-gs0 and 1.30.5-gs0 have the same release 1.30. If a cluster is created with 1.30.4-gs0
// via setting release="1.29", the function should return the current template 1.30.4-gs0, not 1.30.5-gs0.
if currenTemplateUUID != "" {
for _, paasTemplate := range paasTemplates {
if paasTemplate.Properties.ObjectUUID == currenTemplateUUID &&
paasTemplate.Properties.Release == release {
return &paasTemplate, nil
}
}
}

var derived bool
var releases []string
var template gsclient.PaaSTemplate

for _, paasTemplate := range paasTemplates {
if paasTemplate.Properties.Flavour == k8sTemplateFlavourName {
if paasTemplate.Properties.Flavour == k8sTemplateFlavourName && paasTemplate.Properties.Active {
releases = append(releases, paasTemplate.Properties.Release)

if paasTemplate.Properties.Release == release {
derived = true
template = paasTemplate
break
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions vendor/github.com/gridscale/gsclient-go/v3/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/gridscale/gsclient-go/v3/config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/github.com/gridscale/gsclient-go/v3/paas.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions vendor/golang.org/x/sys/cpu/asm_darwin_x86_gc.s

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions vendor/golang.org/x/sys/cpu/cpu_darwin_x86.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vendor/golang.org/x/sys/cpu/cpu_gc_x86.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions vendor/golang.org/x/sys/cpu/cpu_gccgo_x86.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion vendor/golang.org/x/sys/cpu/cpu_linux_arm64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions vendor/golang.org/x/sys/cpu/cpu_other_x86.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions vendor/golang.org/x/sys/cpu/cpu_x86.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1fed27a

Please sign in to comment.