2019-01-01 08:23:01 +00:00
|
|
|
package kubectl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
2022-02-03 17:00:24 +00:00
|
|
|
"strings"
|
2019-01-01 08:23:01 +00:00
|
|
|
"time"
|
|
|
|
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/server"
|
2019-05-08 22:54:52 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-03-16 21:08:43 +00:00
|
|
|
"k8s.io/component-base/cli"
|
2020-12-01 01:06:26 +00:00
|
|
|
"k8s.io/kubectl/pkg/cmd"
|
2022-03-16 21:08:43 +00:00
|
|
|
"k8s.io/kubectl/pkg/cmd/util"
|
2019-01-01 08:23:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Main() {
|
|
|
|
kubenv := os.Getenv("KUBECONFIG")
|
2022-02-03 17:00:24 +00:00
|
|
|
for i, arg := range os.Args {
|
|
|
|
if strings.HasPrefix(arg, "--kubeconfig=") {
|
|
|
|
kubenv = strings.Split(arg, "=")[1]
|
|
|
|
} else if strings.HasPrefix(arg, "--kubeconfig") && i+1 < len(os.Args) {
|
|
|
|
kubenv = os.Args[i+1]
|
|
|
|
}
|
|
|
|
}
|
2019-01-01 08:23:01 +00:00
|
|
|
if kubenv == "" {
|
2019-03-08 22:47:44 +00:00
|
|
|
config, err := server.HomeKubeConfig(false, false)
|
2019-01-01 08:23:01 +00:00
|
|
|
if _, serr := os.Stat(config); err == nil && serr == nil {
|
|
|
|
os.Setenv("KUBECONFIG", config)
|
|
|
|
}
|
2019-05-08 22:54:52 +00:00
|
|
|
if err := checkReadConfigPermissions(config); err != nil {
|
|
|
|
logrus.Warn(err)
|
|
|
|
}
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
main()
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
|
|
|
|
command := cmd.NewDefaultKubectlCommand()
|
2022-03-16 21:08:43 +00:00
|
|
|
if err := cli.RunNoErrOutput(command); err != nil {
|
|
|
|
util.CheckErr(err)
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-08 22:54:52 +00:00
|
|
|
|
|
|
|
func checkReadConfigPermissions(configFile string) error {
|
|
|
|
file, err := os.OpenFile(configFile, os.O_RDONLY, 0600)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsPermission(err) {
|
|
|
|
return fmt.Errorf("Unable to read %s, please start server "+
|
|
|
|
"with --write-kubeconfig-mode to modify kube config permissions", configFile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
file.Close()
|
|
|
|
return nil
|
|
|
|
}
|