Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to override ports and IPs for source cluster. #53

Merged
merged 3 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions cmd/kubectl-k8ssandra/register/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}

Expand Down Expand Up @@ -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(),
Miles-Garnsey marked this conversation as resolved.
Show resolved Hide resolved
Context: cmd.Context(),
DestinationName: destName,
}
}
22 changes: 12 additions & 10 deletions cmd/kubectl-k8ssandra/register/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/registration/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading