Skip to content
Merged
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
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [3.37.1-community] - 2026-11-15

### Changed

- `kubernetes nodegroup scale` uses the v9 SDK. Its existing JSON and YAML output shape is preserved.
- Command help no longer shows a default value for required flags, such as `kubernetes nodegroup scale --count`.

### Fixed

- Fix `kubernetes nodegroup scale --count 0` silently doing nothing. The node count is now always sent to the API, so node groups can be scaled down to zero.

## [3.37.0] - 2026-10-13

### Added
Expand Down Expand Up @@ -754,7 +765,8 @@ Initial public beta release :tada:
### Added
- Current feature set added! First internal release

[Unreleased]: https://github.com/UpCloudLtd/upcloud-cli/compare/v3.37.0...HEAD
[Unreleased]: https://github.com/upcloud-tools/upcloud-cli/compare/v3.37.1-community...HEAD
[3.37.1-community]: https://github.com/upcloud-tools/upcloud-cli/compare/v3.37.0...v3.37.1-community
[3.37.0]: https://github.com/UpCloudLtd/upcloud-cli/compare/v3.36.0...v3.37.0
[3.36.0]: https://github.com/UpCloudLtd/upcloud-cli/compare/v3.35.0...v3.36.0
[3.35.0]: https://github.com/UpCloudLtd/upcloud-cli/compare/v3.34.0...v3.35.0
Expand Down
38 changes: 38 additions & 0 deletions FORK.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Community fork

This repository is an independent community fork of [UpCloudLtd/upcloud-cli](https://github.com/UpCloudLtd/upcloud-cli).
It tracks upstream releases and carries fixes that have not (yet) been merged upstream.
Changes are kept small and self-contained so that upstream can merge them at any time.

## Versioning

- Fork releases are always suffixed with `-community`, e.g. `3.37.1-community` (git tag `v3.37.1-community`).
- The base version is the upstream release the fork is synced to; fork-only changes bump the patch (or minor) component on top of it.
- Like upstream, the binary version is derived from the git tag at build time (goreleaser injects it into `internal/config.Version`); release preparation only updates `CHANGELOG.md`.
- Compare links for `-community` versions point to this fork; links for upstream versions point to UpCloudLtd/upcloud-cli.

## Feature parity

**Base: upstream v3.37.0 (2026-10-13).** The fork contains all upstream features; no upstream feature is removed or disabled.

### Fork-only changes

| Change | Introduced in | Related issue | Upstream status |
| ------ | ------------- | ------------- | --------------- |
| `kubernetes node-group scale --count 0` sends `{"count": 0}` instead of silently doing nothing. The command now uses the v9 SDK; JSON/YAML output shape is preserved. | 3.37.1-community | [#1](https://github.com/upcloud-tools/upcloud-cli/issues/1) | Not fixed as of upstream v3.37.0 |
| Command help no longer shows `Default:` for required flags (e.g. `scale --count` showed a misleading `Default: 0`). | 3.37.1-community | [#1](https://github.com/upcloud-tools/upcloud-cli/issues/1) | Not fixed as of upstream v3.37.0 |

### Notes for upstream merging

- The scale fix works around a bug in the `upcloud-go-api` v8 SDK: `request.ModifyKubernetesNodeGroup.Count` is tagged `json:"count,omitempty"`, so a zero count is dropped from the PATCH body and the API no-ops while the CLI reports success. The v9 SDK (`KubernetesNodeGroupPatch`) models `count` without `omitempty` and is not affected. Fixing the v8 SDK would additionally benefit all other v8 consumers.
- The help renderer change is in `internal/ui/usage.go` (`formatFlags`) and applies to every command with required flags.

## Syncing with upstream

```sh
git remote add upstream git@github.com:UpCloudLtd/upcloud-cli.git
git fetch upstream
git merge upstream/main
```

After syncing: update the base version above, drop fork-only changes that upstream merged from the table, and base the next `-community` version on the new upstream release.
74 changes: 74 additions & 0 deletions internal/commands/kubernetes/nodegroup/fakeapi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package nodegroup

import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"sync"
"testing"

upcloudv9 "github.com/UpCloudLtd/upcloud-go-api/v9/pkg/upcloud"
"github.com/stretchr/testify/require"
)

type apiRequest struct {
Method string
Path string
Query url.Values
Body []byte
}

type fakeResponse struct {
status int
body string
contentType string
}

// fakeAPI serves canned responses keyed by "METHOD /path" and records every request.
type fakeAPI struct {
mu sync.Mutex
requests []apiRequest
client *upcloudv9.ClientWithResponses
}

func newFakeAPI(t *testing.T, responses map[string]fakeResponse) *fakeAPI {
t.Helper()
api := &fakeAPI{}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
t.Errorf("reading request body: %v", err)
}
key := r.Method + " " + r.URL.Path
api.mu.Lock()
api.requests = append(api.requests, apiRequest{Method: r.Method, Path: r.URL.Path, Query: r.URL.Query(), Body: body})
api.mu.Unlock()

res, ok := responses[key]
if !ok {
t.Errorf("unexpected request %s", key)
w.WriteHeader(http.StatusInternalServerError)
return
}
contentType := res.contentType
if contentType == "" {
contentType = "application/json"
}
w.Header().Set("Content-Type", contentType)
w.WriteHeader(res.status)
_, _ = io.WriteString(w, res.body)
}))
t.Cleanup(server.Close)

client, err := upcloudv9.NewClientWithResponses(server.URL)
require.NoError(t, err)
api.client = client
return api
}

func (a *fakeAPI) all() []apiRequest {
a.mu.Lock()
defer a.mu.Unlock()
return append([]apiRequest(nil), a.requests...)
}
29 changes: 23 additions & 6 deletions internal/commands/kubernetes/nodegroup/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import (
"fmt"

"github.com/UpCloudLtd/upcloud-cli/v3/internal/apierror"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/completion"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/resolver"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
upcloudv9 "github.com/UpCloudLtd/upcloud-go-api/v9/pkg/upcloud"
"github.com/google/uuid"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -49,16 +51,31 @@
msg := fmt.Sprintf("Scaling node group %s of cluster %v", s.name, arg)
exec.PushProgressStarted(msg)

res, err := exec.All().ModifyKubernetesNodeGroup(exec.Context(), &request.ModifyKubernetesNodeGroupRequest{
ClusterUUID: arg,
Name: s.name,
NodeGroup: request.ModifyKubernetesNodeGroup{Count: s.count},
client, err := v9Client(exec)
if err != nil {
return commands.HandleError(exec, msg, err)
}
id, err := uuid.Parse(arg)
if err != nil {
return commands.HandleError(exec, msg, fmt.Errorf("invalid kubernetes cluster UUID %q: %w", arg, err))
}

res, err := client.ModifyKubernetesClusterNodeGroupWithResponse(exec.Context(), id, s.name, upcloudv9.KubernetesNodeGroupPatch{
Count: int32(s.count),

Check failure on line 64 in internal/commands/kubernetes/nodegroup/scale.go

View workflow job for this annotation

GitHub Actions / golangci-lint

G115: integer overflow conversion int -> int32 (gosec)
})
if err != nil {
return commands.HandleError(exec, msg, err)
}
if res.JSON200 == nil {
return commands.HandleError(exec, msg, apierror.FromResponse(res.StatusCode(), res.Body))
}

legacy, err := legacyNodeGroupOutput(res.JSON200)
if err != nil {
return commands.HandleError(exec, msg, err)
}

exec.PushProgressSuccess(msg)

return output.OnlyMarshaled{Value: res}, nil
return output.OnlyMarshaled{Value: legacy}, nil
}
82 changes: 61 additions & 21 deletions internal/commands/kubernetes/nodegroup/scale_test.go
Original file line number Diff line number Diff line change
@@ -1,65 +1,105 @@
package nodegroup

import (
"net/http"
"strconv"
"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/v8/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestScaleKubernetesNodeGroup(t *testing.T) {
clusterUUID := "28c80353-98fd-4221-85e0-82d7603756ba"
path := "/1.3/kubernetes/" + clusterUUID + "/node-groups/my-node-group"
nodeGroup := func(count int) string {
return `{"name": "my-node-group", "count": ` + strconv.Itoa(count) + `, "plan": "dev-md", "state": "running"}`
}

for _, test := range []struct {
name string
args []string
expected request.ModifyKubernetesNodeGroupRequest
errorMsg string
name string
args []string
responses map[string]fakeResponse
body string
errorMsg string
}{
{
name: "no args",
args: []string{clusterUUID},
errorMsg: `required flag(s) "name", "count" not set`,
},
{
name: "delete success",
name: "scale to positive count",
args: []string{
clusterUUID,
"--name", "my-node-group",
"--count", "6",
},
expected: request.ModifyKubernetesNodeGroupRequest{
ClusterUUID: clusterUUID,
Name: "my-node-group",
NodeGroup: request.ModifyKubernetesNodeGroup{Count: 6},
responses: map[string]fakeResponse{"PATCH " + path: {status: http.StatusOK, body: nodeGroup(6)}},
body: `{"count": 6}`,
},
{
name: "scale to zero",
args: []string{
clusterUUID,
"--name", "my-node-group",
"--count", "0",
},
responses: map[string]fakeResponse{"PATCH " + path: {status: http.StatusOK, body: nodeGroup(0)}},
body: `{"count": 0}`,
},
{
name: "API error is reported",
args: []string{
clusterUUID,
"--name", "my-node-group",
"--count", "0",
},
responses: map[string]fakeResponse{"PATCH " + path: {
status: http.StatusUnprocessableEntity,
contentType: "application/problem+json",
body: `{"type": "https://developers.upcloud.com/1.3/errors#ERROR_INVALID_REQUEST", "title": "Invalid request", "status": 422}`,
}},
body: `{"count": 0}`,
errorMsg: "Invalid request (type=https://developers.upcloud.com/1.3/errors#ERROR_INVALID_REQUEST, status=422)",
},
{
name: "invalid UUID",
args: []string{
"not-a-uuid",
"--name", "my-node-group",
"--count", "0",
},
errorMsg: `invalid kubernetes cluster UUID "not-a-uuid": invalid UUID length: 10`,
},
} {
t.Run(test.name, func(t *testing.T) {
api := newFakeAPI(t, test.responses)
conf := config.New()
testCmd := ScaleCommand()
mService := new(smock.Service)

expected := test.expected
mService.On("ModifyKubernetesNodeGroup", &expected).Return(&upcloud.KubernetesNodeGroup{}, nil)

c := commands.BuildCommand(testCmd, nil, conf)

c := commands.BuildCommand(ScaleCommand(), nil, conf)
c.Cobra().SetArgs(test.args)
_, err := mockexecute.MockExecute(c, mService, conf)

_, err := mockexecute.MockExecuteWithV9(c, &smock.Service{}, api.client, conf)

if test.errorMsg != "" {
assert.EqualError(t, err, test.errorMsg)
} else {
require.NoError(t, err)
mService.AssertNumberOfCalls(t, "ModifyKubernetesNodeGroup", 1)
}

requests := api.all()
if test.body != "" {
require.Len(t, requests, 1)
assert.Equal(t, http.MethodPatch, requests[0].Method)
assert.Equal(t, path, requests[0].Path)
assert.JSONEq(t, test.body, string(requests[0].Body))
} else {
assert.Empty(t, requests)
}
})
}
Expand Down
32 changes: 32 additions & 0 deletions internal/commands/kubernetes/nodegroup/v9.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package nodegroup

import (
"encoding/json"
"errors"
"fmt"

"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
upcloudv8 "github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
upcloudv9 "github.com/UpCloudLtd/upcloud-go-api/v9/pkg/upcloud"
)

func v9Client(exec commands.Executor) (*upcloudv9.ClientWithResponses, error) {
client := exec.V9()
if client == nil {
return nil, errors.New("UpCloud API v9 client is not available, run with --debug for details")
}
return client, nil
}

// legacyNodeGroupOutput preserves the node group JSON/YAML contract while the API call uses v9.
func legacyNodeGroupOutput(ng *upcloudv9.KubernetesNodeGroup) (*upcloudv8.KubernetesNodeGroup, error) {
body, err := json.Marshal(ng)
if err != nil {
return nil, fmt.Errorf("cannot marshal v9 node group response: %w", err)
}
legacy := &upcloudv8.KubernetesNodeGroup{}
if err := json.Unmarshal(body, legacy); err != nil {
return nil, fmt.Errorf("cannot convert v9 node group response to legacy output: %w", err)
}
return legacy, nil
}
3 changes: 2 additions & 1 deletion internal/ui/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ func formatFlags(fs *pflag.FlagSet) string {
def = strings.TrimPrefix(def, "[")
def = strings.TrimSuffix(def, "]")
}
if def != "" {
_, required := flag.Annotations[cobra.BashCompOneRequiredFlag]
if def != "" && !required {
fmt.Fprintf(&flagUsage, "\nDefault: %s", def)
}
t.Append(table.Row{flagText.String(), flagUsage.String()})
Expand Down
24 changes: 24 additions & 0 deletions internal/ui/usage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ui

import (
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestFormatFlagsRequiredHidesDefault(t *testing.T) {
cmd := &cobra.Command{Use: "test"}
cmd.Flags().Int("count", 0, "Node count")
cmd.Flags().String("name", "default-name", "Node group name")
cmd.Flags().Int("replicas", 3, "Replica count")
require.NoError(t, cmd.MarkFlagRequired("count"))
require.NoError(t, cmd.MarkFlagRequired("name"))

out := formatFlags(cmd.Flags())

assert.NotContains(t, out, "Default: 0")
assert.NotContains(t, out, "Default: default-name")
assert.Contains(t, out, "Default: 3")
}
Loading