diff --git a/cmd/kubectl-k8ssandra/register/command.go b/cmd/kubectl-k8ssandra/register/command.go index 15a5c66..ae5b614 100644 --- a/cmd/kubectl-k8ssandra/register/command.go +++ b/cmd/kubectl-k8ssandra/register/command.go @@ -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 { @@ -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 == "" { diff --git a/cmd/kubectl-k8ssandra/register/command_test.go b/cmd/kubectl-k8ssandra/register/command_test.go new file mode 100644 index 0000000..6ffe4b9 --- /dev/null +++ b/cmd/kubectl-k8ssandra/register/command_test.go @@ -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()) +}