From 9bcd638a6f7bad2591c0c27e38c3886a22b3fb32 Mon Sep 17 00:00:00 2001 From: Miles Garnsey Date: Mon, 24 Jun 2024 11:25:21 +1000 Subject: [PATCH 1/3] Add ability to override ports and IPs for source cluster. --- cmd/kubectl-k8ssandra/register/command.go | 23 +++++++++++-------- .../register/registration.go | 22 ++++++++++-------- pkg/registration/kubeconfig.go | 7 +++++- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/cmd/kubectl-k8ssandra/register/command.go b/cmd/kubectl-k8ssandra/register/command.go index 358c8d6..bb9724f 100644 --- a/cmd/kubectl-k8ssandra/register/command.go +++ b/cmd/kubectl-k8ssandra/register/command.go @@ -30,6 +30,8 @@ func SetupRegisterClusterCmd(cmd *cobra.Command, streams genericclioptions.IOStr 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") if err := RegisterClusterCmd.MarkFlagRequired("source-context"); err != nil { panic(err) @@ -38,6 +40,7 @@ func SetupRegisterClusterCmd(cmd *cobra.Command, streams genericclioptions.IOStr if err := RegisterClusterCmd.MarkFlagRequired("dest-context"); err != nil { panic(err) } + RegisterClusterCmd.MarkFlagsRequiredTogether("oride-src-ip", "oride-src-port") cmd.AddCommand(RegisterClusterCmd) } @@ -69,14 +72,16 @@ func NewRegistrationExecutorFromRegisterClusterCmd(cmd cobra.Command) *Registrat destName = registration.CleanupForKubernetes(srcContext) } return &RegistrationExecutor{ - SourceKubeconfig: cmd.Flag("source-kubeconfig").Value.String(), - DestKubeconfig: cmd.Flag("dest-kubeconfig").Value.String(), - SourceContext: srcContext, - DestContext: cmd.Flag("dest-context").Value.String(), - SourceNamespace: cmd.Flag("source-namespace").Value.String(), - DestNamespace: cmd.Flag("dest-namespace").Value.String(), - ServiceAccount: cmd.Flag("serviceaccount-name").Value.String(), - Context: cmd.Context(), - DestinationName: destName, + SourceKubeconfig: cmd.Flag("source-kubeconfig").Value.String(), + DestKubeconfig: cmd.Flag("dest-kubeconfig").Value.String(), + SourceContext: srcContext, + DestContext: cmd.Flag("dest-context").Value.String(), + SourceNamespace: cmd.Flag("source-namespace").Value.String(), + DestNamespace: cmd.Flag("dest-namespace").Value.String(), + ServiceAccount: cmd.Flag("serviceaccount-name").Value.String(), + overrideSourceIP: cmd.Flag("oride-src-ip").Value.String(), + overrideSourcePort: cmd.Flag("oride-src-port").Value.String(), + Context: cmd.Context(), + DestinationName: destName, } } diff --git a/cmd/kubectl-k8ssandra/register/registration.go b/cmd/kubectl-k8ssandra/register/registration.go index f1d08c0..a5a727b 100644 --- a/cmd/kubectl-k8ssandra/register/registration.go +++ b/cmd/kubectl-k8ssandra/register/registration.go @@ -18,15 +18,17 @@ import ( ) type RegistrationExecutor struct { - DestinationName string - SourceKubeconfig string - DestKubeconfig string - SourceContext string - DestContext string - SourceNamespace string - DestNamespace string - ServiceAccount string - Context context.Context + DestinationName string + SourceKubeconfig string + DestKubeconfig string + SourceContext string + DestContext string + SourceNamespace string + DestNamespace string + ServiceAccount string + overrideSourceIP string + overrideSourcePort string + Context context.Context } func getDefaultSecret(saNamespace, saName string) *corev1.Secret { @@ -101,7 +103,7 @@ func (e *RegistrationExecutor) RegisterCluster() error { } // Create Secret on destination cluster - host, err := registration.KubeconfigToHost(e.SourceKubeconfig, e.SourceContext) + host, err := registration.KubeconfigToHost(e.SourceKubeconfig, e.SourceContext, e.overrideSourceIP, e.overrideSourcePort) if err != nil { return err } diff --git a/pkg/registration/kubeconfig.go b/pkg/registration/kubeconfig.go index 20f66af..810ee6a 100644 --- a/pkg/registration/kubeconfig.go +++ b/pkg/registration/kubeconfig.go @@ -63,12 +63,17 @@ func GetKubeconfigFileLocation(location string) (string, error) { } } -func KubeconfigToHost(configFileLocation string, contextName string) (string, error) { +func KubeconfigToHost(configFileLocation string, contextName string, overrideSourceIP string, overrideSourcePort string) (string, error) { + if overrideSourceIP != "" && overrideSourcePort != "" { + return fmt.Sprintf("https://%s:%s", overrideSourceIP, overrideSourcePort), nil + } path, err := GetKubeconfigFileLocation(configFileLocation) if err != nil { return "", err } + clientConfig, err := clientcmd.LoadFromFile(path) + if err != nil { return "", err } From 40131d2c7cf87b18a95d2674b5e8ad1a3097d758 Mon Sep 17 00:00:00 2001 From: Miles Garnsey Date: Mon, 24 Jun 2024 11:41:55 +1000 Subject: [PATCH 2/3] Public override fields. --- cmd/kubectl-k8ssandra/register/command.go | 4 ++-- cmd/kubectl-k8ssandra/register/registration.go | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/kubectl-k8ssandra/register/command.go b/cmd/kubectl-k8ssandra/register/command.go index bb9724f..e21f636 100644 --- a/cmd/kubectl-k8ssandra/register/command.go +++ b/cmd/kubectl-k8ssandra/register/command.go @@ -79,8 +79,8 @@ func NewRegistrationExecutorFromRegisterClusterCmd(cmd cobra.Command) *Registrat SourceNamespace: cmd.Flag("source-namespace").Value.String(), DestNamespace: cmd.Flag("dest-namespace").Value.String(), ServiceAccount: cmd.Flag("serviceaccount-name").Value.String(), - overrideSourceIP: cmd.Flag("oride-src-ip").Value.String(), - overrideSourcePort: cmd.Flag("oride-src-port").Value.String(), + OverrideSourceIP: cmd.Flag("oride-src-ip").Value.String(), + OverrideSourcePort: cmd.Flag("oride-src-port").Value.String(), Context: cmd.Context(), DestinationName: destName, } diff --git a/cmd/kubectl-k8ssandra/register/registration.go b/cmd/kubectl-k8ssandra/register/registration.go index a5a727b..2dcda23 100644 --- a/cmd/kubectl-k8ssandra/register/registration.go +++ b/cmd/kubectl-k8ssandra/register/registration.go @@ -26,8 +26,8 @@ type RegistrationExecutor struct { SourceNamespace string DestNamespace string ServiceAccount string - overrideSourceIP string - overrideSourcePort string + OverrideSourceIP string + OverrideSourcePort string Context context.Context } @@ -103,7 +103,7 @@ func (e *RegistrationExecutor) RegisterCluster() error { } // Create Secret on destination cluster - host, err := registration.KubeconfigToHost(e.SourceKubeconfig, e.SourceContext, e.overrideSourceIP, e.overrideSourcePort) + host, err := registration.KubeconfigToHost(e.SourceKubeconfig, e.SourceContext, e.OverrideSourceIP, e.OverrideSourcePort) if err != nil { return err } From b6dfff0cc033a96acee3b56eef4fd84350fa8e2d Mon Sep 17 00:00:00 2001 From: Miles Garnsey <11435896+Miles-Garnsey@users.noreply.github.com> Date: Wed, 26 Jun 2024 12:12:51 +1000 Subject: [PATCH 3/3] Update cmd/kubectl-k8ssandra/register/command.go Co-authored-by: Alexander Dejanovski --- cmd/kubectl-k8ssandra/register/command.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/kubectl-k8ssandra/register/command.go b/cmd/kubectl-k8ssandra/register/command.go index e21f636..15a5c66 100644 --- a/cmd/kubectl-k8ssandra/register/command.go +++ b/cmd/kubectl-k8ssandra/register/command.go @@ -79,8 +79,8 @@ func NewRegistrationExecutorFromRegisterClusterCmd(cmd cobra.Command) *Registrat SourceNamespace: cmd.Flag("source-namespace").Value.String(), DestNamespace: cmd.Flag("dest-namespace").Value.String(), ServiceAccount: cmd.Flag("serviceaccount-name").Value.String(), - OverrideSourceIP: cmd.Flag("oride-src-ip").Value.String(), - OverrideSourcePort: cmd.Flag("oride-src-port").Value.String(), + OverrideSourceIP: cmd.Flag("override-src-ip").Value.String(), + OverrideSourcePort: cmd.Flag("override-src-port").Value.String(), Context: cmd.Context(), DestinationName: destName, }