You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
k3s/pkg/kubectl/main.go

67 lines
1.4 KiB

6 years ago
package kubectl
import (
"fmt"
"math/rand"
"os"
"runtime"
"strings"
6 years ago
"time"
"github.com/k3s-io/k3s/pkg/server"
"github.com/sirupsen/logrus"
"k8s.io/component-base/cli"
"k8s.io/kubectl/pkg/cmd"
"k8s.io/kubectl/pkg/cmd/util"
// Import to initialize client auth plugins.
_ "k8s.io/client-go/plugin/pkg/client/auth"
6 years ago
)
func Main() {
if runtime.GOOS == "windows" {
os.Args = os.Args[1:]
}
6 years ago
kubenv := os.Getenv("KUBECONFIG")
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]
}
}
6 years ago
if kubenv == "" {
config, err := server.HomeKubeConfig(false, false)
6 years ago
if _, serr := os.Stat(config); err == nil && serr == nil {
os.Setenv("KUBECONFIG", config)
}
if err := checkReadConfigPermissions(config); err != nil {
logrus.Warn(err)
}
6 years ago
}
main()
}
func main() {
rand.Seed(time.Now().UnixNano())
command := cmd.NewDefaultKubectlCommand()
if err := cli.RunNoErrOutput(command); err != nil {
util.CheckErr(err)
6 years ago
}
}
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 or --write-kubeconfig-group "+
"to modify kube config permissions", configFile)
}
}
file.Close()
return nil
}