Skip to content

Commit 5fc9b04

Browse files
authored
feat(kubernetes): add --version parameter to kubernetes create command (#266)
Also adds version to output of `kubernetes show`.
1 parent c88b964 commit 5fc9b04

11 files changed

Lines changed: 104 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- Add `network_peerings`, `ntp_excess_gib`, `storage_maxiops` and `load_balancers` fields to `account show` outputs.
13+
- Add `--version` parameter to `kubernetes create` and `version` field to `kubernetes show` output.
14+
15+
### Changed
16+
17+
- **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)
1318

1419
### Fixed
1520

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.20
44

55
require (
66
github.com/UpCloudLtd/progress v1.0.1
7-
github.com/UpCloudLtd/upcloud-go-api/v6 v6.8.3
7+
github.com/UpCloudLtd/upcloud-go-api/v6 v6.9.0
88
github.com/adrg/xdg v0.3.2
99
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
1010
github.com/gemalto/flume v0.12.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tN
4040
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M=
4141
github.com/UpCloudLtd/progress v1.0.1 h1:e0ptyD2oOGa3udRcLzgRemIN9enGx4Bc9GQ0sZ/1/EY=
4242
github.com/UpCloudLtd/progress v1.0.1/go.mod h1:hKsRsvlCffcYt/s0krpWvOFozOjpfUYjSkL6CzZztoI=
43-
github.com/UpCloudLtd/upcloud-go-api/v6 v6.8.3 h1:WVdmHkyYOTawkwcdTStCKR0N0XN+psZpvnxq4V+ie6Q=
44-
github.com/UpCloudLtd/upcloud-go-api/v6 v6.8.3/go.mod h1:I8rWmBBl+OhiY3AGzKbrobiE5TsLCLNYkCQxE4eJcTg=
43+
github.com/UpCloudLtd/upcloud-go-api/v6 v6.9.0 h1:d4N5tYgVowcZ0zVtr4gLIyO/m3QD7Dbmpo0BoogHwX4=
44+
github.com/UpCloudLtd/upcloud-go-api/v6 v6.9.0/go.mod h1:I8rWmBBl+OhiY3AGzKbrobiE5TsLCLNYkCQxE4eJcTg=
4545
github.com/adrg/xdg v0.3.2 h1:GUSGQ5pHdev83AYhDSS1A/CX+0JIsxbiWtow2DSA+RU=
4646
github.com/adrg/xdg v0.3.2/go.mod h1:7I2hH/IT30IsupOpKZ5ue7/qNi3CoKzD6tL3HwpaRMQ=
4747
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=

internal/commands/kubernetes/create.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func (c *createCommand) InitCommand() {
106106

107107
fs.StringVar(&c.params.Name, "name", "", "Kubernetes cluster name.")
108108
fs.StringVar(&c.params.Plan, "plan", "development", "Plan to use for the cluster. Run `upctl kubernetes plans` to list all available plans.")
109+
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.")
109110
fs.StringVar(&c.params.networkArg, "network", "", "Network to use. The value should be name or UUID of a private network.")
110111
fs.StringArrayVar(
111112
&c.params.nodeGroups,
@@ -145,6 +146,7 @@ func (c *createCommand) InitCommand() {
145146
func (c *createCommand) InitCommandWithConfig(cfg *config.Config) {
146147
_ = c.Cobra().RegisterFlagCompletionFunc("network", namedargs.CompletionFunc(completion.Network{}, cfg))
147148
_ = c.Cobra().RegisterFlagCompletionFunc("zone", namedargs.CompletionFunc(completion.Zone{}, cfg))
149+
_ = c.Cobra().RegisterFlagCompletionFunc("version", namedargs.CompletionFunc(completion.KubernetesVersion{}, cfg))
148150
}
149151

150152
// ExecuteWithoutArguments implements commands.NoArgumentCommand

internal/commands/kubernetes/create_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ func TestCreateKubernetes(t *testing.T) {
100100
privateNodeGroupsRequest := nodeGroupRequest
101101
privateNodeGroupsRequest.PrivateNodeGroups = true
102102

103+
versionArg := []string{"--version", "0.99"}
104+
versionRequest := nodeGroupRequest
105+
versionRequest.Version = "0.99"
106+
103107
for _, test := range []struct {
104108
name string
105109
args []string
@@ -130,6 +134,12 @@ func TestCreateKubernetes(t *testing.T) {
130134
request: privateNodeGroupsRequest,
131135
wantErr: false,
132136
},
137+
{
138+
name: "with non-default version",
139+
args: append(nodeGroupArgs(network.Name), versionArg...),
140+
request: versionRequest,
141+
wantErr: false,
142+
},
133143
} {
134144
t.Run(test.name, func(t *testing.T) {
135145
conf := config.New()

internal/commands/kubernetes/show.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ func (s *showCommand) Execute(exec commands.Executor, uuid string) (output.Outpu
8181
Rows: []output.DetailRow{
8282
{Title: "UUID:", Value: cluster.UUID, Colour: ui.DefaultUUUIDColours},
8383
{Title: "Name:", Value: cluster.Name},
84+
{Title: "Version:", Value: cluster.Version},
8485
{Title: "Network UUID:", Value: cluster.Network, Colour: ui.DefaultUUUIDColours},
8586
{Title: "Network name:", Value: networkName, Format: format.PossiblyUnknownString},
8687
{Title: "Network CIDR:", Value: cluster.NetworkCIDR, Colour: ui.DefaultAddressColours},

internal/commands/kubernetes/show_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,10 @@ var testCluster = upcloud.KubernetesCluster{
9595
UtilityNetworkAccess: false,
9696
},
9797
},
98-
State: upcloud.KubernetesClusterStateRunning,
99-
UUID: "0ddab8f4-97c0-4222-91ba-85a4fff7499b",
100-
Zone: "de-fra1",
98+
State: upcloud.KubernetesClusterStateRunning,
99+
UUID: "0ddab8f4-97c0-4222-91ba-85a4fff7499b",
100+
Version: "2.54",
101+
Zone: "de-fra1",
101102
}
102103

103104
func TestShowCommand(t *testing.T) {
@@ -107,6 +108,7 @@ func TestShowCommand(t *testing.T) {
107108
Overview:
108109
UUID: 0ddab8f4-97c0-4222-91ba-85a4fff7499b
109110
Name: upcloud-upctl-unit-test
111+
Version: 2.54
110112
Network UUID: 03a98be3-7daa-443f-bb25-4bc6854b396c
111113
Network name: Test network
112114
Network CIDR: 172.16.1.0/24

internal/commands/kubernetes/versions.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@ func (s *versionsCommand) ExecuteWithoutArguments(exec commands.Executor) (outpu
2929
rows := []output.TableRow{}
3030
for _, version := range versions {
3131
rows = append(rows, output.TableRow{
32-
version,
32+
version.Id,
33+
version.Version,
3334
})
3435
}
3536

3637
return output.MarshaledWithHumanOutput{
3738
Value: versions,
3839
Output: output.Table{
3940
Columns: []output.TableColumn{
41+
{Key: "id", Header: "ID"},
4042
{Key: "version", Header: "Version"},
4143
},
4244
Rows: rows,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package kubernetes
2+
3+
import (
4+
"testing"
5+
6+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
7+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/config"
8+
smock "github.com/UpCloudLtd/upcloud-cli/v3/internal/mock"
9+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/mockexecute"
10+
11+
"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud"
12+
"github.com/jedib0t/go-pretty/v6/text"
13+
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/mock"
15+
)
16+
17+
var versions = []upcloud.KubernetesVersion{
18+
{
19+
Id: "1.26",
20+
Version: "v1.26.3",
21+
},
22+
{
23+
Id: "1.27",
24+
Version: "v1.27.4",
25+
},
26+
}
27+
28+
func TestVersionsCommand(t *testing.T) {
29+
text.DisableColors()
30+
31+
expected := `
32+
ID Version
33+
────── ─────────
34+
1.26 v1.26.3
35+
1.27 v1.27.4
36+
37+
`
38+
39+
mService := smock.Service{}
40+
mService.On("GetKubernetesVersions", mock.Anything).Return(versions, nil)
41+
42+
conf := config.New()
43+
command := commands.BuildCommand(VersionsCommand(), nil, conf)
44+
45+
output, err := mockexecute.MockExecute(command, &mService, conf)
46+
47+
assert.NoError(t, err)
48+
assert.Equal(t, expected, output)
49+
}

internal/completion/kubernetes.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,23 @@ func (s Kubernetes) CompleteArgument(ctx context.Context, svc service.AllService
2828

2929
return MatchStringPrefix(vals, toComplete, true), cobra.ShellCompDirectiveNoFileComp
3030
}
31+
32+
// KubernetesVersion implements argument completion for Kubernetes versions by version id.
33+
type KubernetesVersion struct{}
34+
35+
// make sure Kubernetes implements the interface
36+
var _ Provider = KubernetesVersion{}
37+
38+
// CompleteArgument implements completion.Provider
39+
func (s KubernetesVersion) CompleteArgument(ctx context.Context, svc service.AllServices, toComplete string) ([]string, cobra.ShellCompDirective) {
40+
versions, err := svc.GetKubernetesVersions(ctx, &request.GetKubernetesVersionsRequest{})
41+
if err != nil {
42+
return None(toComplete)
43+
}
44+
var vals []string
45+
for _, version := range versions {
46+
vals = append(vals, version.Id)
47+
}
48+
49+
return MatchStringPrefix(vals, toComplete, true), cobra.ShellCompDirectiveNoFileComp
50+
}

0 commit comments

Comments
 (0)