mirror of
https://github.com/fluxcd/flux2.git
synced 2025-11-02 18:58:33 +08:00
Add kubeconfig global flag
This commit is contained in:
@ -4,12 +4,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"k8s.io/client-go/kubernetes"
|
|
||||||
_ "k8s.io/client-go/plugin/pkg/client/auth"
|
|
||||||
"k8s.io/client-go/tools/clientcmd"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var checkCmd = &cobra.Command{
|
var checkCmd = &cobra.Command{
|
||||||
@ -17,24 +13,16 @@ var checkCmd = &cobra.Command{
|
|||||||
Short: "Check for potential problems",
|
Short: "Check for potential problems",
|
||||||
Long: `
|
Long: `
|
||||||
The check command will perform a series of checks to validate that
|
The check command will perform a series of checks to validate that
|
||||||
the local environment and Kubernetes cluster are configured correctly.`,
|
the local environment is configured correctly.`,
|
||||||
Example: ` check --pre`,
|
Example: ` check --pre`,
|
||||||
RunE: runCheckCmd,
|
RunE: runCheckCmd,
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
kubeconfig string
|
checkPre bool
|
||||||
checkPre bool
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
if home := homeDir(); home != "" {
|
|
||||||
checkCmd.Flags().StringVarP(&kubeconfig, "kubeconfig", "", filepath.Join(home, ".kube", "config"),
|
|
||||||
"path to the kubeconfig file")
|
|
||||||
} else {
|
|
||||||
checkCmd.Flags().StringVarP(&kubeconfig, "kubeconfig", "", "",
|
|
||||||
"absolute path to the kubeconfig file")
|
|
||||||
}
|
|
||||||
checkCmd.Flags().BoolVarP(&checkPre, "pre", "", false,
|
checkCmd.Flags().BoolVarP(&checkPre, "pre", "", false,
|
||||||
"only run pre-installation checks")
|
"only run pre-installation checks")
|
||||||
|
|
||||||
@ -58,13 +46,6 @@ func runCheckCmd(cmd *cobra.Command, args []string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func homeDir() string {
|
|
||||||
if h := os.Getenv("HOME"); h != "" {
|
|
||||||
return h
|
|
||||||
}
|
|
||||||
return os.Getenv("USERPROFILE") // windows
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkLocal() bool {
|
func checkLocal() bool {
|
||||||
ok := true
|
ok := true
|
||||||
for _, cmd := range []string{"kubectl", "kustomize"} {
|
for _, cmd := range []string{"kubectl", "kustomize"} {
|
||||||
@ -80,13 +61,7 @@ func checkLocal() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func checkRemote() bool {
|
func checkRemote() bool {
|
||||||
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
|
client, err := NewKubernetesClient()
|
||||||
if err != nil {
|
|
||||||
fmt.Println(`✗`, "kubernetes client initialization failed", err.Error())
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
client, err := kubernetes.NewForConfig(config)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(`✗`, "kubernetes client initialization failed", err.Error())
|
fmt.Println(`✗`, "kubernetes client initialization failed", err.Error())
|
||||||
return false
|
return false
|
||||||
|
|||||||
@ -4,9 +4,13 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"k8s.io/client-go/kubernetes"
|
||||||
|
_ "k8s.io/client-go/plugin/pkg/client/auth"
|
||||||
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
)
|
)
|
||||||
|
|
||||||
var VERSION = "0.0.1"
|
var VERSION = "0.0.1"
|
||||||
@ -17,6 +21,20 @@ var rootCmd = &cobra.Command{
|
|||||||
Version: VERSION,
|
Version: VERSION,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
kubeconfig string
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
if home := homeDir(); home != "" {
|
||||||
|
rootCmd.PersistentFlags().StringVarP(&kubeconfig, "kubeconfig", "", filepath.Join(home, ".kube", "config"),
|
||||||
|
"path to the kubeconfig file")
|
||||||
|
} else {
|
||||||
|
checkCmd.PersistentFlags().StringVarP(&kubeconfig, "kubeconfig", "", "",
|
||||||
|
"absolute path to the kubeconfig file")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.SetFlags(0)
|
log.SetFlags(0)
|
||||||
|
|
||||||
@ -27,3 +45,24 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func homeDir() string {
|
||||||
|
if h := os.Getenv("HOME"); h != "" {
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
return os.Getenv("USERPROFILE") // windows
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewKubernetesClient() (*kubernetes.Clientset, error) {
|
||||||
|
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := kubernetes.NewForConfig(config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return client, nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user