Skip to content

Commit 1633f2e

Browse files
authored
feat: add a kubeconfig flag to specify kubeconfig file location (#133)
Signed-off-by: Liang Deng <[email protected]>
1 parent c0fd754 commit 1633f2e

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,14 @@ This above command will:
6161

6262
### `print` command
6363

64-
| Flag | Default Value | Required | Description |
65-
| -------------- | ----------------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
66-
| namespace | | No | If present, the namespace scope for the invocation |
67-
| 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 |
68-
| output | yaml | No | The output format, either yaml or json |
69-
| 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 |
64+
| Flag | Default Value | Required | Description |
65+
| -------------- | ----------------------- | -------- | ------------------------------------------------------------ |
66+
| namespace | | No | If present, the namespace scope for the invocation |
67+
| 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 |
68+
| output | yaml | No | The output format, either yaml or json |
69+
| 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 |
7070
| 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 |
71+
| 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. |
7172

7273
## Conversion of Ingress resources to Gateway API
7374

cmd/print.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,3 @@ func getNamespaceInCurrentContext() (string, error) {
261261

262262
return currentNamespace, err
263263
}
264-
265-
func init() {
266-
rootCmd.AddCommand(newPrintCommand())
267-
}

cmd/root.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,32 @@ import (
2222
"github.com/spf13/cobra"
2323
)
2424

25-
var rootCmd = &cobra.Command{
26-
Use: "ingress2gateway",
27-
Short: "Convert Ingress manifests to Gateway API manifests",
25+
// kubeconfig indicates kubeconfig file location.
26+
var kubeconfig string
27+
28+
func newRootCmd() *cobra.Command {
29+
rootCmd := &cobra.Command{
30+
Use: "ingress2gateway",
31+
Short: "Convert Ingress manifests to Gateway API manifests",
32+
PersistentPreRun: func(cmd *cobra.Command, args []string) {
33+
getKubeconfig()
34+
},
35+
}
36+
37+
rootCmd.PersistentFlags().StringVar(&kubeconfig, "kubeconfig", "",
38+
`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.`)
39+
return rootCmd
40+
}
41+
42+
func getKubeconfig() {
43+
if kubeconfig != "" {
44+
os.Setenv("KUBECONFIG", kubeconfig)
45+
}
2846
}
2947

3048
func Execute() {
49+
rootCmd := newRootCmd()
50+
rootCmd.AddCommand(newPrintCommand())
3151
err := rootCmd.Execute()
3252
if err != nil {
3353
os.Exit(1)

0 commit comments

Comments
 (0)