Skip to content

Commit

Permalink
rpk: improve profile create, add cloud cluster use
Browse files Browse the repository at this point in the history
  • Loading branch information
twmb committed Mar 13, 2024
1 parent 117c58a commit ece9c0f
Show file tree
Hide file tree
Showing 8 changed files with 411 additions and 145 deletions.
3 changes: 2 additions & 1 deletion src/go/rpk/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ require (
github.com/lestrrat-go/jwx v1.2.28
github.com/linkedin/goavro/v2 v2.12.0
github.com/lorenzosaino/go-sysctl v0.3.1
github.com/mattn/go-isatty v0.0.20
github.com/moby/term v0.5.0
github.com/opencontainers/go-digest v1.0.0
github.com/opencontainers/image-spec v1.1.0
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
github.com/pkg/errors v0.9.1
github.com/prometheus/client_model v0.6.0
github.com/prometheus/common v0.49.0
github.com/rs/xid v1.5.0
github.com/safchain/ethtool v0.3.0
github.com/schollz/progressbar/v3 v3.14.2
github.com/sethgrid/pester v1.2.0
Expand Down Expand Up @@ -98,7 +100,6 @@ require (
github.com/lestrrat-go/option v1.0.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions src/go/rpk/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/safchain/ethtool v0.3.0 h1:gimQJpsI6sc1yIqP/y8GYgiXn/NjgvpM0RNoWLVVmP0=
github.com/safchain/ethtool v0.3.0/go.mod h1:SA9BwrgyAqNo7M+uaL6IYbxpm5wk3L7Mm6ocLW+CJUs=
Expand Down
2 changes: 2 additions & 0 deletions src/go/rpk/pkg/cli/cloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package cloud
import (
"github.com/redpanda-data/redpanda/src/go/rpk/pkg/cli/cloud/auth"
"github.com/redpanda-data/redpanda/src/go/rpk/pkg/cli/cloud/byoc"
"github.com/redpanda-data/redpanda/src/go/rpk/pkg/cli/cloud/cluster"
"github.com/redpanda-data/redpanda/src/go/rpk/pkg/cli/cloud/namespace"
"github.com/redpanda-data/redpanda/src/go/rpk/pkg/config"
"github.com/spf13/afero"
Expand All @@ -27,6 +28,7 @@ func NewCommand(fs afero.Fs, p *config.Params, execFn func(string, []string) err
cmd.AddCommand(
auth.NewCommand(fs, p),
byoc.NewCommand(fs, p, execFn),
cluster.NewCommand(fs, p),
namespace.NewCommand(fs, p),
newLoginCommand(fs, p),
newLogoutCommand(fs, p),
Expand Down
34 changes: 34 additions & 0 deletions src/go/rpk/pkg/cli/cloud/cluster/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2023 Redpanda Data, Inc.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.md
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0

package cluster

import (
"github.com/redpanda-data/redpanda/src/go/rpk/pkg/config"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)

func NewCommand(fs afero.Fs, p *config.Params) *cobra.Command {
cmd := &cobra.Command{
Use: "cluster",
Short: "Manage rpk cloud clusters",
Long: `Manage rpk cloud clusters.
This command allows you to manage cloud clusters, as well as easily switch
between which cluster you are talking to.
`,
}

cmd.AddCommand(
newUseCommand(fs, p),
)

return cmd
}
68 changes: 68 additions & 0 deletions src/go/rpk/pkg/cli/cloud/cluster/use.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2023 Redpanda Data, Inc.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.md
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0

package cluster

import (
"errors"
"fmt"
"os"

"github.com/redpanda-data/redpanda/src/go/rpk/pkg/cli/profile"
"github.com/redpanda-data/redpanda/src/go/rpk/pkg/config"
"github.com/redpanda-data/redpanda/src/go/rpk/pkg/out"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)

func newUseCommand(fs afero.Fs, p *config.Params) *cobra.Command {
var profileName string

cmd := &cobra.Command{
Use: "use [NAME]",
Short: "Update your rpk profile to talk to the requested cluster",
Long: `Update your rpk profile to talk to the requested cluster.
This command is essentially an alias for the following command:
rpk profile create --from-cloud=${NAME}
If you want to name this profile rather than creating or updating values in
the default cloud-dedicated profile, you can use the --profile flag.
`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
cfg, err := p.Load(fs)
out.MaybeDie(err, "rpk unable to load config: %v", err)

y, err := cfg.ActualRpkYamlOrEmpty()
out.MaybeDie(err, "unable to load rpk.yaml: %v", err)

err = profile.CreateFlow(cmd.Context(), fs, cfg, y, "", "", args[0], false, nil, profileName, "")
if ee := (*profile.ProfileExistsError)(nil); errors.As(err, &ee) {
fmt.Printf(`Unable to automatically create profile %q due to a name conflict with
an existing self-hosted profile, please rename that profile or use the
--profile flag to explicitly name your new profile.
Either:
rpk profile use %[1]q
rpk profile rename-to $something_else
rpk cloud cluster use %[2]q
Or:
rpk cloud cluster use %[2]q --profile $another_something
`, ee.Name, args[0])
os.Exit(1)
}
out.MaybeDieErr(err)
},
}

cmd.Flags().StringVar(&profileName, "profile", "", fmt.Sprintf("Name of a profile to create or update (avoids updating %q)", profile.RpkCloudProfileName))
return cmd
}
Loading

0 comments on commit ece9c0f

Please sign in to comment.