Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- sks: allow `major.minor` format for kube version when creating a cluster

### Breaking changes

### Features
Expand Down
51 changes: 41 additions & 10 deletions cmd/compute/sks/sks_create.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sks

import (
"context"
"errors"
"fmt"
"slices"
Expand Down Expand Up @@ -178,16 +179,9 @@
}
}

if clusterReq.Version == "latest" {
versions, err := client.ListSKSClusterVersions(ctx)
if err != nil || len(versions.SKSClusterVersions) == 0 {
return fmt.Errorf("unable to retrieve SKS versions: %w", err)
}
if versions == nil || len(versions.SKSClusterVersions) == 0 {
return errors.New("no version returned by the API")
}

clusterReq.Version = versions.SKSClusterVersions[0]
clusterReq.Version, err = resolveSKSClusterVersion(ctx, client, clusterReq.Version)
if err != nil {
return err
}

if c.OIDCClientID != "" {
Expand Down Expand Up @@ -303,3 +297,40 @@
}))

}

// Computes the major.minor.patch version of an SKS cluster
// Defaults to the latest version
func resolveSKSClusterVersion(ctx context.Context, client *v3.Client, inputVersion string) (string, error) {

inputVersionLength := len(strings.Split(inputVersion, "."))
isMajorMinor := inputVersionLength == 2
isMajorMinorPatch := inputVersionLength == 3

if isMajorMinorPatch {
return inputVersion, nil
}

availableVersions, err := client.ListSKSClusterVersions(ctx)
if err != nil {
return "", err
}
if len(availableVersions.SKSClusterVersions) == 0 {
return "", fmt.Errorf("ListSKSClusterVersions: API returned empty list")
}

defaultVersion := availableVersions.SKSClusterVersions[0]
if "latest" == inputVersion {

Check failure on line 322 in cmd/compute/sks/sks_create.go

View workflow job for this annotation

GitHub Actions / lint

ST1017: don't use Yoda conditions (staticcheck)

Check failure on line 322 in cmd/compute/sks/sks_create.go

View workflow job for this annotation

GitHub Actions / Build + Run Unit Tests

don't use Yoda conditions (ST1017)
return defaultVersion, nil
}

if isMajorMinor {
for _, v := range availableVersions.SKSClusterVersions {
if inputVersion == strings.Join(strings.Split(v, ".")[:2], ".") {
return v, nil
}
}
return "", fmt.Errorf("the SKS cluster version %s is not supported. Available versions: %s", inputVersion, strings.Join(availableVersions.SKSClusterVersions, ", "))
}

return "", fmt.Errorf("error resolving the provided SKS cluster version: %s. Available versions: %s", inputVersion, strings.Join(availableVersions.SKSClusterVersions, ", "))
}
Loading