Skip to content

Commit

Permalink
filter for supported versions when loading version options (#44)
Browse files Browse the repository at this point in the history
Co-authored-by: Dean Oren <deangili.oren@mail.schwarz>
  • Loading branch information
do87 and Dean Oren authored Nov 15, 2022
1 parent f5f8fdf commit 5810fa7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

const run_this_test = true
const run_this_test = false

func TestAcc_PostgresInstance(t *testing.T) {
if !common.ShouldAccTestRun(run_this_test) {
Expand Down
17 changes: 14 additions & 3 deletions stackit/internal/resources/kubernetes/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package kubernetes
import (
"context"
"fmt"
"strings"

"github.com/Masterminds/semver"
"github.com/pkg/errors"

"github.com/SchwarzIT/community-stackit-go-client/pkg/api/v1/kubernetes/clusters"
"github.com/SchwarzIT/community-stackit-go-client/pkg/consts"
"github.com/SchwarzIT/terraform-provider-stackit/stackit/internal/common"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand Down Expand Up @@ -35,13 +37,16 @@ func (r Resource) loadAvaiableVersions(ctx context.Context) ([]*semver.Version,
return nil, errors.Wrap(err, "Couldn't fetch K8s options")
}

versionOptions = make([]*semver.Version, len(opts.KubernetesVersions))
for i, v := range opts.KubernetesVersions {
versionOptions = []*semver.Version{}
for _, v := range opts.KubernetesVersions {
if !strings.EqualFold(v.State, consts.SKE_VERSION_STATE_SUPPORTED) {
continue
}
versionOption, err := semver.NewVersion(v.Version)
if err != nil {
return nil, err
}
versionOptions[i] = versionOption
versionOptions = append(versionOptions, versionOption)
}
return versionOptions, nil
}
Expand All @@ -60,6 +65,9 @@ func (c *Cluster) clusterConfig(versionOptions []*semver.Version) (clusters.Kube
return clusters.Kubernetes{}, err
}
clusterConfigVersion = maxVersionOption(clusterVersionConstraint, versionOptions)
if clusterConfigVersion == nil {
return clusters.Kubernetes{}, fmt.Errorf("returned version is nil\nthe options were: %+v", versionOptions)
}

cfg := clusters.Kubernetes{
Version: clusterConfigVersion.String(),
Expand All @@ -84,6 +92,9 @@ func toVersionConstraint(version *semver.Version) (*semver.Constraints, error) {
// maxVersionOption returns the maximal version that matches the given version. A matching option is required.
// If the given version only contains major and minor version, the latest patch version is returned.
func maxVersionOption(versionConstraint *semver.Constraints, versionOptions []*semver.Version) *semver.Version {
if len(versionOptions) == 0 || versionOptions[0] == nil {
return nil
}
ret := versionOptions[0]
for _, v := range versionOptions[1:] {
if versionConstraint.Check(v) && v.GreaterThan(ret) {
Expand Down
10 changes: 4 additions & 6 deletions stackit/internal/resources/kubernetes/validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,11 @@ func validateKubernetesVersion(version string, versionOptions []options.Kubernet
found := false
accepted := ""
for _, v := range versionOptions {
if strings.EqualFold(v.State, consts.SKE_VERSION_STATE_SUPPORTED) {
if v.Version == version {
found = true
break
}
accepted = fmt.Sprintf("%s- %s (state: %s, expires: %s)\n", accepted, v.Version, v.State, v.ExpirationDate)
if v.Version == version {
found = true
break
}
accepted = fmt.Sprintf("%s- %s (state: %s, expires: %s)\n", accepted, v.Version, v.State, v.ExpirationDate)
}
if !found {
return fmt.Errorf(
Expand Down

0 comments on commit 5810fa7

Please sign in to comment.