Skip to content

Commit

Permalink
Fix override-src-port and override-src-ip flag parsing in register co…
Browse files Browse the repository at this point in the history
…mmand (#56)

* Fix flag names as identified by Micke.

* Add a test.

* Fix test code, add more tests

* Remove global variable RegisterClusterCmd

---------

Co-authored-by: Michael Burman <yak@iki.fi>
  • Loading branch information
Miles-Garnsey and burmanm authored Jul 16, 2024
1 parent 00b5fae commit 39a6ddd
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 22 deletions.
43 changes: 21 additions & 22 deletions cmd/kubectl-k8ssandra/register/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,38 @@ import (
"k8s.io/cli-runtime/pkg/genericclioptions"
)

var RegisterClusterCmd = &cobra.Command{
Use: "register [flags]",
Short: "register a data plane into the control plane.",
Long: `register creates a ServiceAccount on a source cluster, copies its credentials and then creates a secret containing them on the destination cluster. It then also creates a ClientConfig on the destination cluster to reference the secret.`,
RunE: entrypoint,
}

func SetupRegisterClusterCmd(cmd *cobra.Command, streams genericclioptions.IOStreams) {
RegisterClusterCmd.Flags().String("source-kubeconfig",
registerClusterCmd := &cobra.Command{
Use: "register [flags]",
Short: "register a data plane into the control plane.",
Long: `register creates a ServiceAccount on a source cluster, copies its credentials and then creates a secret containing them on the destination cluster. It then also creates a ClientConfig on the destination cluster to reference the secret.`,
RunE: entrypoint,
}

registerClusterCmd.Flags().String("source-kubeconfig",
"",
"path to source cluster's kubeconfig file - defaults to KUBECONFIG then ~/.kube/config")
RegisterClusterCmd.Flags().String("dest-kubeconfig",
registerClusterCmd.Flags().String("dest-kubeconfig",
"",
"path to destination cluster's kubeconfig file - defaults to KUBECONFIG then ~/.kube/config")
RegisterClusterCmd.Flags().String("source-context", "", "context name for source cluster")
RegisterClusterCmd.Flags().String("dest-context", "", "context name for destination cluster")
RegisterClusterCmd.Flags().String("source-namespace", "k8ssandra-operator", "namespace containing service account for source cluster")
RegisterClusterCmd.Flags().String("dest-namespace", "k8ssandra-operator", "namespace where secret and clientConfig will be created on destination cluster")
RegisterClusterCmd.Flags().String("serviceaccount-name", "k8ssandra-operator", "serviceaccount name for destination cluster")
RegisterClusterCmd.Flags().String("destination-name", "", "name for remote clientConfig and secret on destination cluster")
RegisterClusterCmd.Flags().String("oride-src-ip", "", "override source IP for when you need to specify a different IP for the source cluster than is contained in kubeconfig")
RegisterClusterCmd.Flags().String("oride-src-port", "", "override source port for when you need to specify a different port for the source cluster than is contained in src kubeconfig")
registerClusterCmd.Flags().String("source-context", "", "context name for source cluster")
registerClusterCmd.Flags().String("dest-context", "", "context name for destination cluster")
registerClusterCmd.Flags().String("source-namespace", "k8ssandra-operator", "namespace containing service account for source cluster")
registerClusterCmd.Flags().String("dest-namespace", "k8ssandra-operator", "namespace where secret and clientConfig will be created on destination cluster")
registerClusterCmd.Flags().String("serviceaccount-name", "k8ssandra-operator", "serviceaccount name for destination cluster")
registerClusterCmd.Flags().String("destination-name", "", "name for remote clientConfig and secret on destination cluster")
registerClusterCmd.Flags().String("override-src-ip", "", "override source IP for when you need to specify a different IP for the source cluster than is contained in kubeconfig")
registerClusterCmd.Flags().String("override-src-port", "", "override source port for when you need to specify a different port for the source cluster than is contained in src kubeconfig")

if err := RegisterClusterCmd.MarkFlagRequired("source-context"); err != nil {
if err := registerClusterCmd.MarkFlagRequired("source-context"); err != nil {
panic(err)

}
if err := RegisterClusterCmd.MarkFlagRequired("dest-context"); err != nil {
if err := registerClusterCmd.MarkFlagRequired("dest-context"); err != nil {
panic(err)
}
RegisterClusterCmd.MarkFlagsRequiredTogether("oride-src-ip", "oride-src-port")
cmd.AddCommand(RegisterClusterCmd)
registerClusterCmd.MarkFlagsRequiredTogether("override-src-ip", "override-src-port")
cmd.AddCommand(registerClusterCmd)
}

func entrypoint(cmd *cobra.Command, args []string) error {
Expand All @@ -65,7 +65,6 @@ func entrypoint(cmd *cobra.Command, args []string) error {
}

func NewRegistrationExecutorFromRegisterClusterCmd(cmd cobra.Command) *RegistrationExecutor {

destName := cmd.Flag("destination-name").Value.String()
srcContext := cmd.Flag("source-context").Value.String()
if destName == "" {
Expand Down
65 changes: 65 additions & 0 deletions cmd/kubectl-k8ssandra/register/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package register

import (
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
"k8s.io/cli-runtime/pkg/genericiooptions"
)

func TestInputParameters(t *testing.T) {
require := require.New(t)

var executor *RegistrationExecutor

cmd := &cobra.Command{}
SetupRegisterClusterCmd(cmd, genericiooptions.NewTestIOStreamsDiscard())
cmd.Commands()[0].RunE = func(cmd *cobra.Command, args []string) error {
executor = NewRegistrationExecutorFromRegisterClusterCmd(*cmd)
return nil
}
cmd.Root().SetArgs([]string{
"register",
"--source-context", "source-ctx",
"--source-kubeconfig", "testsourcekubeconfig",
"--dest-kubeconfig", "testdestkubeconfig",
"--dest-context", "dest-ctx",
"--source-namespace", "source-namespace",
"--dest-namespace", "dest-namespace",
"--serviceaccount-name", "test-sa",
"--override-src-ip", "127.0.0.2",
"--override-src-port", "9999"})

require.NoError(cmd.Execute())

require.Equal("127.0.0.2", executor.OverrideSourceIP)
require.Equal("9999", executor.OverrideSourcePort)
require.Equal("testsourcekubeconfig", executor.SourceKubeconfig)
require.Equal("source-ctx", executor.SourceContext)
require.Equal("testdestkubeconfig", executor.DestKubeconfig)
require.Equal("dest-ctx", executor.DestContext)
require.Equal("source-namespace", executor.SourceNamespace)
require.Equal("dest-namespace", executor.DestNamespace)
require.Equal("test-sa", executor.ServiceAccount)
}

func TestIncorrectParameters(t *testing.T) {
require := require.New(t)

cmd := &cobra.Command{}
cmd.SilenceUsage = true
SetupRegisterClusterCmd(cmd, genericiooptions.NewTestIOStreamsDiscard())
cmd.Commands()[0].RunE = func(cmd *cobra.Command, args []string) error {
return nil
}

cmd.Root().SetArgs([]string{
"register",
"--service-account", "test-sa",
})

err := cmd.Execute()
require.Error(err)
require.Equal("unknown flag: --service-account", err.Error())
}

0 comments on commit 39a6ddd

Please sign in to comment.