diff --git a/CHANGELOG.md b/CHANGELOG.md index 393728bad..ecf0c4147 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/FORK.md b/FORK.md new file mode 100644 index 000000000..e2468678a --- /dev/null +++ b/FORK.md @@ -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. \ No newline at end of file diff --git a/internal/commands/kubernetes/nodegroup/fakeapi_test.go b/internal/commands/kubernetes/nodegroup/fakeapi_test.go new file mode 100644 index 000000000..1f5c01782 --- /dev/null +++ b/internal/commands/kubernetes/nodegroup/fakeapi_test.go @@ -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...) +} diff --git a/internal/commands/kubernetes/nodegroup/scale.go b/internal/commands/kubernetes/nodegroup/scale.go index 420b36af8..3daba0810 100644 --- a/internal/commands/kubernetes/nodegroup/scale.go +++ b/internal/commands/kubernetes/nodegroup/scale.go @@ -3,11 +3,13 @@ package nodegroup 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" ) @@ -49,16 +51,31 @@ func (s *scaleCommand) ExecuteSingleArgument(exec commands.Executor, arg string) 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), }) 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 } diff --git a/internal/commands/kubernetes/nodegroup/scale_test.go b/internal/commands/kubernetes/nodegroup/scale_test.go index 16e6de1ff..e1cc1c12b 100644 --- a/internal/commands/kubernetes/nodegroup/scale_test.go +++ b/internal/commands/kubernetes/nodegroup/scale_test.go @@ -1,6 +1,8 @@ package nodegroup import ( + "net/http" + "strconv" "testing" "github.com/UpCloudLtd/upcloud-cli/v3/internal/commands" @@ -8,20 +10,23 @@ import ( 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", @@ -29,37 +34,72 @@ func TestScaleKubernetesNodeGroup(t *testing.T) { 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) } }) } diff --git a/internal/commands/kubernetes/nodegroup/v9.go b/internal/commands/kubernetes/nodegroup/v9.go new file mode 100644 index 000000000..846585d7e --- /dev/null +++ b/internal/commands/kubernetes/nodegroup/v9.go @@ -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 +} diff --git a/internal/ui/usage.go b/internal/ui/usage.go index dc5a5e95c..e14866815 100644 --- a/internal/ui/usage.go +++ b/internal/ui/usage.go @@ -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()}) diff --git a/internal/ui/usage_test.go b/internal/ui/usage_test.go new file mode 100644 index 000000000..00cae1854 --- /dev/null +++ b/internal/ui/usage_test.go @@ -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") +}