From fdb7cbd9d4c9ff6966fb1ff5c96f60699690b126 Mon Sep 17 00:00:00 2001 From: Liang Deng <283304489@qq.com> Date: Sun, 3 Mar 2024 16:43:27 +0800 Subject: [PATCH] feat: add a kubeconfig flag to specify kubeconfig file location Signed-off-by: Liang Deng <283304489@qq.com> --- README.md | 13 +++++++------ cmd/print.go | 4 ---- cmd/root.go | 26 +++++++++++++++++++++++--- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index deda1a224..95113bd81 100644 --- a/README.md +++ b/README.md @@ -61,13 +61,14 @@ This above command will: ### `print` command -| Flag | Default Value | Required | Description | -| -------------- | ----------------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| namespace | | No | If present, the namespace scope for the invocation | -| all-namespaces | False | No | If present, list the requested object(s) across all namespaces. Namespace in the current context is ignored even if specified with --namespace | -| output | yaml | No | The output format, either yaml or json | -| input_file | | No | Path to the manifest file. When set, the tool will read ingresses from the file instead of reading from the cluster. Supported files are yaml and json | +| Flag | Default Value | Required | Description | +| -------------- | ----------------------- | -------- | ------------------------------------------------------------ | +| namespace | | No | If present, the namespace scope for the invocation | +| all-namespaces | False | No | If present, list the requested object(s) across all namespaces. Namespace in the current context is ignored even if specified with --namespace | +| output | yaml | No | The output format, either yaml or json | +| input_file | | No | Path to the manifest file. When set, the tool will read ingresses from the file instead of reading from the cluster. Supported files are yaml and json | | providers | all supported providers | No | Comma-separated list of providers. If present, the tool will try to convert only resources related to the specified providers. Otherwise it will default to all the supported providers | +| kubeconfig | | No | The kubeconfig file to use when talking to the cluster. If the flag is not set, a set of standard locations can be searched for an existing kubeconfig file. | ## Conversion of Ingress resources to Gateway API diff --git a/cmd/print.go b/cmd/print.go index 261f19ac7..b916bc0f9 100644 --- a/cmd/print.go +++ b/cmd/print.go @@ -261,7 +261,3 @@ func getNamespaceInCurrentContext() (string, error) { return currentNamespace, err } - -func init() { - rootCmd.AddCommand(newPrintCommand()) -} diff --git a/cmd/root.go b/cmd/root.go index 79ecef1ad..594d02b69 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -22,12 +22,32 @@ import ( "github.com/spf13/cobra" ) -var rootCmd = &cobra.Command{ - Use: "ingress2gateway", - Short: "Convert Ingress manifests to Gateway API manifests", +// kubeconfig indicates kubeconfig file location. +var kubeconfig string + +func newRootCmd() *cobra.Command { + rootCmd := &cobra.Command{ + Use: "ingress2gateway", + Short: "Convert Ingress manifests to Gateway API manifests", + PersistentPreRun: func(cmd *cobra.Command, args []string) { + getKubeconfig() + }, + } + + rootCmd.PersistentFlags().StringVar(&kubeconfig, "kubeconfig", "", + `The kubeconfig file to use when talking to the cluster. If the flag is not set, a set of standard locations can be searched for an existing kubeconfig file.`) + return rootCmd +} + +func getKubeconfig() { + if kubeconfig != "" { + os.Setenv("KUBECONFIG", kubeconfig) + } } func Execute() { + rootCmd := newRootCmd() + rootCmd.AddCommand(newPrintCommand()) err := rootCmd.Execute() if err != nil { os.Exit(1)