Skip to content

Commit

Permalink
feat(kubernetes): add --version parameter to kubernetes create co…
Browse files Browse the repository at this point in the history
…mmand (#266)

Also adds version to output of `kubernetes show`.
  • Loading branch information
kangasta authored Nov 6, 2023
1 parent c88b964 commit 5fc9b04
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Add `network_peerings`, `ntp_excess_gib`, `storage_maxiops` and `load_balancers` fields to `account show` outputs.
- Add `--version` parameter to `kubernetes create` and `version` field to `kubernetes show` output.

### Changed

- **Breaking**: Update return type of `kubernetes versions` from list of strings to list of objects. (No major version bump, because this end-point has not been included in the API docs)

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/UpCloudLtd/progress v1.0.1
github.com/UpCloudLtd/upcloud-go-api/v6 v6.8.3
github.com/UpCloudLtd/upcloud-go-api/v6 v6.9.0
github.com/adrg/xdg v0.3.2
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
github.com/gemalto/flume v0.12.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tN
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M=
github.com/UpCloudLtd/progress v1.0.1 h1:e0ptyD2oOGa3udRcLzgRemIN9enGx4Bc9GQ0sZ/1/EY=
github.com/UpCloudLtd/progress v1.0.1/go.mod h1:hKsRsvlCffcYt/s0krpWvOFozOjpfUYjSkL6CzZztoI=
github.com/UpCloudLtd/upcloud-go-api/v6 v6.8.3 h1:WVdmHkyYOTawkwcdTStCKR0N0XN+psZpvnxq4V+ie6Q=
github.com/UpCloudLtd/upcloud-go-api/v6 v6.8.3/go.mod h1:I8rWmBBl+OhiY3AGzKbrobiE5TsLCLNYkCQxE4eJcTg=
github.com/UpCloudLtd/upcloud-go-api/v6 v6.9.0 h1:d4N5tYgVowcZ0zVtr4gLIyO/m3QD7Dbmpo0BoogHwX4=
github.com/UpCloudLtd/upcloud-go-api/v6 v6.9.0/go.mod h1:I8rWmBBl+OhiY3AGzKbrobiE5TsLCLNYkCQxE4eJcTg=
github.com/adrg/xdg v0.3.2 h1:GUSGQ5pHdev83AYhDSS1A/CX+0JIsxbiWtow2DSA+RU=
github.com/adrg/xdg v0.3.2/go.mod h1:7I2hH/IT30IsupOpKZ5ue7/qNi3CoKzD6tL3HwpaRMQ=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down
2 changes: 2 additions & 0 deletions internal/commands/kubernetes/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func (c *createCommand) InitCommand() {

fs.StringVar(&c.params.Name, "name", "", "Kubernetes cluster name.")
fs.StringVar(&c.params.Plan, "plan", "development", "Plan to use for the cluster. Run `upctl kubernetes plans` to list all available plans.")
fs.StringVar(&c.params.Version, "version", "", "Identifier of the version of Kubernetes to use when creating the cluster. Run `upctl kubernetes versions` to list all available versions.")
fs.StringVar(&c.params.networkArg, "network", "", "Network to use. The value should be name or UUID of a private network.")
fs.StringArrayVar(
&c.params.nodeGroups,
Expand Down Expand Up @@ -145,6 +146,7 @@ func (c *createCommand) InitCommand() {
func (c *createCommand) InitCommandWithConfig(cfg *config.Config) {
_ = c.Cobra().RegisterFlagCompletionFunc("network", namedargs.CompletionFunc(completion.Network{}, cfg))
_ = c.Cobra().RegisterFlagCompletionFunc("zone", namedargs.CompletionFunc(completion.Zone{}, cfg))
_ = c.Cobra().RegisterFlagCompletionFunc("version", namedargs.CompletionFunc(completion.KubernetesVersion{}, cfg))
}

// ExecuteWithoutArguments implements commands.NoArgumentCommand
Expand Down
10 changes: 10 additions & 0 deletions internal/commands/kubernetes/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ func TestCreateKubernetes(t *testing.T) {
privateNodeGroupsRequest := nodeGroupRequest
privateNodeGroupsRequest.PrivateNodeGroups = true

versionArg := []string{"--version", "0.99"}
versionRequest := nodeGroupRequest
versionRequest.Version = "0.99"

for _, test := range []struct {
name string
args []string
Expand Down Expand Up @@ -130,6 +134,12 @@ func TestCreateKubernetes(t *testing.T) {
request: privateNodeGroupsRequest,
wantErr: false,
},
{
name: "with non-default version",
args: append(nodeGroupArgs(network.Name), versionArg...),
request: versionRequest,
wantErr: false,
},
} {
t.Run(test.name, func(t *testing.T) {
conf := config.New()
Expand Down
1 change: 1 addition & 0 deletions internal/commands/kubernetes/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func (s *showCommand) Execute(exec commands.Executor, uuid string) (output.Outpu
Rows: []output.DetailRow{
{Title: "UUID:", Value: cluster.UUID, Colour: ui.DefaultUUUIDColours},
{Title: "Name:", Value: cluster.Name},
{Title: "Version:", Value: cluster.Version},
{Title: "Network UUID:", Value: cluster.Network, Colour: ui.DefaultUUUIDColours},
{Title: "Network name:", Value: networkName, Format: format.PossiblyUnknownString},
{Title: "Network CIDR:", Value: cluster.NetworkCIDR, Colour: ui.DefaultAddressColours},
Expand Down
8 changes: 5 additions & 3 deletions internal/commands/kubernetes/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ var testCluster = upcloud.KubernetesCluster{
UtilityNetworkAccess: false,
},
},
State: upcloud.KubernetesClusterStateRunning,
UUID: "0ddab8f4-97c0-4222-91ba-85a4fff7499b",
Zone: "de-fra1",
State: upcloud.KubernetesClusterStateRunning,
UUID: "0ddab8f4-97c0-4222-91ba-85a4fff7499b",
Version: "2.54",
Zone: "de-fra1",
}

func TestShowCommand(t *testing.T) {
Expand All @@ -107,6 +108,7 @@ func TestShowCommand(t *testing.T) {
Overview:
UUID: 0ddab8f4-97c0-4222-91ba-85a4fff7499b
Name: upcloud-upctl-unit-test
Version: 2.54
Network UUID: 03a98be3-7daa-443f-bb25-4bc6854b396c
Network name: Test network
Network CIDR: 172.16.1.0/24
Expand Down
4 changes: 3 additions & 1 deletion internal/commands/kubernetes/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ func (s *versionsCommand) ExecuteWithoutArguments(exec commands.Executor) (outpu
rows := []output.TableRow{}
for _, version := range versions {
rows = append(rows, output.TableRow{
version,
version.Id,
version.Version,
})
}

return output.MarshaledWithHumanOutput{
Value: versions,
Output: output.Table{
Columns: []output.TableColumn{
{Key: "id", Header: "ID"},
{Key: "version", Header: "Version"},
},
Rows: rows,
Expand Down
49 changes: 49 additions & 0 deletions internal/commands/kubernetes/versions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package kubernetes

import (
"testing"

"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/config"
smock "github.com/UpCloudLtd/upcloud-cli/v3/internal/mock"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/mockexecute"

"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

var versions = []upcloud.KubernetesVersion{
{
Id: "1.26",
Version: "v1.26.3",
},
{
Id: "1.27",
Version: "v1.27.4",
},
}

func TestVersionsCommand(t *testing.T) {
text.DisableColors()

expected := `
ID Version
────── ─────────
1.26 v1.26.3
1.27 v1.27.4
`

mService := smock.Service{}
mService.On("GetKubernetesVersions", mock.Anything).Return(versions, nil)

conf := config.New()
command := commands.BuildCommand(VersionsCommand(), nil, conf)

output, err := mockexecute.MockExecute(command, &mService, conf)

assert.NoError(t, err)
assert.Equal(t, expected, output)
}
20 changes: 20 additions & 0 deletions internal/completion/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,23 @@ func (s Kubernetes) CompleteArgument(ctx context.Context, svc service.AllService

return MatchStringPrefix(vals, toComplete, true), cobra.ShellCompDirectiveNoFileComp
}

// KubernetesVersion implements argument completion for Kubernetes versions by version id.
type KubernetesVersion struct{}

// make sure Kubernetes implements the interface
var _ Provider = KubernetesVersion{}

// CompleteArgument implements completion.Provider
func (s KubernetesVersion) CompleteArgument(ctx context.Context, svc service.AllServices, toComplete string) ([]string, cobra.ShellCompDirective) {
versions, err := svc.GetKubernetesVersions(ctx, &request.GetKubernetesVersionsRequest{})
if err != nil {
return None(toComplete)
}
var vals []string
for _, version := range versions {
vals = append(vals, version.Id)
}

return MatchStringPrefix(vals, toComplete, true), cobra.ShellCompDirectiveNoFileComp
}
8 changes: 6 additions & 2 deletions internal/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -939,8 +939,12 @@ func (m *Service) GetKubernetesKubeconfig(_ context.Context, r *request.GetKuber
return args[0].(string), args.Error(1)
}

func (m *Service) GetKubernetesVersions(_ context.Context, r *request.GetKubernetesVersionsRequest) ([]string, error) {
return nil, nil
func (m *Service) GetKubernetesVersions(_ context.Context, r *request.GetKubernetesVersionsRequest) ([]upcloud.KubernetesVersion, error) {
args := m.Called(r)
if args[0] == nil {
return nil, args.Error(1)
}
return args[0].([]upcloud.KubernetesVersion), args.Error(1)
}

func (m *Service) WaitForKubernetesClusterState(context.Context, *request.WaitForKubernetesClusterStateRequest) (*upcloud.KubernetesCluster, error) {
Expand Down

0 comments on commit 5fc9b04

Please sign in to comment.