mirror of
https://github.com/k3s-io/k3s.git
synced 2024-06-07 19:41:36 +00:00
0c9bf36fe0
* initial windows port. Signed-off-by: Sean Yen <seanyen@microsoft.com> Signed-off-by: Derek Nola <derek.nola@suse.com> Co-authored-by: Derek Nola <derek.nola@suse.com> Co-authored-by: Wei Ran <weiran@microsoft.com>
63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
package kubectl
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
"os"
|
|
"runtime"
|
|
"strings"
|
|
"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"
|
|
)
|
|
|
|
func Main() {
|
|
if runtime.GOOS == "windows" {
|
|
os.Args = os.Args[1:]
|
|
}
|
|
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]
|
|
}
|
|
}
|
|
if kubenv == "" {
|
|
config, err := server.HomeKubeConfig(false, false)
|
|
if _, serr := os.Stat(config); err == nil && serr == nil {
|
|
os.Setenv("KUBECONFIG", config)
|
|
}
|
|
if err := checkReadConfigPermissions(config); err != nil {
|
|
logrus.Warn(err)
|
|
}
|
|
}
|
|
|
|
main()
|
|
}
|
|
|
|
func main() {
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
command := cmd.NewDefaultKubectlCommand()
|
|
if err := cli.RunNoErrOutput(command); err != nil {
|
|
util.CheckErr(err)
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|