Exemple #1
0
// GetKubeConfig returns a config used for the Kubernetes client with CLI
// options parsed.
func GetKubeConfig(cmd *cobra.Command) *client.Config {
	config := &client.Config{}

	var host string
	if hostFlag := GetFlagString(cmd, "server"); len(hostFlag) > 0 {
		host = hostFlag
		glog.V(2).Infof("Using server from -s flag: %s", host)
	} else if len(os.Getenv("KUBERNETES_MASTER")) > 0 {
		host = os.Getenv("KUBERNETES_MASTER")
		glog.V(2).Infof("Using server from env var KUBERNETES_MASTER: %s", host)
	} else {
		// TODO: eventually apiserver should start on 443 and be secure by default
		host = "http://localhost:8080"
		glog.V(2).Infof("No server found in flag or env var, using default: %s", host)
	}
	config.Host = host

	if client.IsConfigTransportSecure(config) {
		// Get the values from the file on disk (or from the user at the
		// command line). Override them with the command line parameters, if
		// provided.
		authPath := GetFlagString(cmd, "auth-path")
		authInfo, err := kubectl.LoadAuthInfo(authPath, os.Stdin)
		if err != nil {
			glog.Fatalf("Error loading auth: %v", err)
		}

		config.Username = authInfo.User
		config.Password = authInfo.Password
		// First priority is flag, then file.
		config.CAFile = FirstNonEmptyString(GetFlagString(cmd, "certificate-authority"), authInfo.CAFile)
		config.CertFile = FirstNonEmptyString(GetFlagString(cmd, "client-certificate"), authInfo.CertFile)
		config.KeyFile = FirstNonEmptyString(GetFlagString(cmd, "client-key"), authInfo.KeyFile)
		config.BearerToken = authInfo.BearerToken
		// For config.Insecure, the command line ALWAYS overrides the authInfo
		// file, regardless of its setting.
		if insecureFlag := GetFlagBoolPtr(cmd, "insecure-skip-tls-verify"); insecureFlag != nil {
			config.Insecure = *insecureFlag
		} else if authInfo.Insecure != nil {
			config.Insecure = *authInfo.Insecure
		}
	}

	// The API version (e.g. v1beta1), not the binary version.
	config.Version = GetFlagString(cmd, "api-version")

	return config
}
Exemple #2
0
func main() {
	flag.Usage = func() {
		usage()
	}

	flag.Parse()
	util.InitLogs()
	defer util.FlushLogs()

	verflag.PrintAndExitIfRequested()

	// Initialize the client
	if clientConfig.Host == "" {
		clientConfig.Host = os.Getenv("KUBERNETES_MASTER")
	}

	// Load namespace information for requests
	// Check if the namespace was overriden by the -ns argument
	ctx := api.NewDefaultContext()
	if len(*ns) > 0 {
		ctx = api.WithNamespace(ctx, *ns)
	} else {
		nsInfo, err := kubecfg.LoadNamespaceInfo(*nsFile)
		if err != nil {
			glog.Fatalf("Error loading current namespace: %v", err)
		}
		ctx = api.WithNamespace(ctx, nsInfo.Namespace)
	}

	if clientConfig.Host == "" {
		// TODO: eventually apiserver should start on 443 and be secure by default
		clientConfig.Host = "http://localhost:8080"
	}
	if client.IsConfigTransportSecure(clientConfig) {
		auth, err := kubecfg.LoadAuthInfo(*authConfig, os.Stdin)
		if err != nil {
			glog.Fatalf("Error loading auth: %v", err)
		}
		clientConfig.Username = auth.User
		clientConfig.Password = auth.Password
		if auth.CAFile != "" {
			clientConfig.CAFile = auth.CAFile
		}
		if auth.CertFile != "" {
			clientConfig.CertFile = auth.CertFile
		}
		if auth.KeyFile != "" {
			clientConfig.KeyFile = auth.KeyFile
		}
		if auth.BearerToken != "" {
			clientConfig.BearerToken = auth.BearerToken
		}
		if auth.Insecure != nil {
			clientConfig.Insecure = *auth.Insecure
		}
	}
	kubeClient, err := client.New(clientConfig)
	if err != nil {
		glog.Fatalf("Can't configure client: %v", err)
	}

	if *serverVersion != verflag.VersionFalse {
		got, err := kubeClient.ServerVersion()
		if err != nil {
			fmt.Printf("Couldn't read version from server: %v\n", err)
			os.Exit(1)
		}
		if *serverVersion == verflag.VersionRaw {
			fmt.Printf("%#v\n", *got)
			os.Exit(0)
		} else {
			fmt.Printf("Server: Kubernetes %s\n", got)
			os.Exit(0)
		}
	}

	if *preventSkew {
		got, err := kubeClient.ServerVersion()
		if err != nil {
			fmt.Printf("Couldn't read version from server: %v\n", err)
			os.Exit(1)
		}
		if c, s := version.Get(), *got; !reflect.DeepEqual(c, s) {
			fmt.Printf("Server version (%#v) differs from client version (%#v)!\n", s, c)
			os.Exit(1)
		}
	}

	if *proxy {
		glog.Info("Starting to serve on localhost:8001")
		if *openBrowser {
			go func() {
				time.Sleep(2 * time.Second)
				open.Start("http://localhost:8001/static/")
			}()
		}
		server, err := kubecfg.NewProxyServer(*www, clientConfig)
		if err != nil {
			glog.Fatalf("Error creating proxy server: %v", err)
		}
		glog.Fatal(server.Serve())
	}

	if len(flag.Args()) < 1 {
		usage()
		os.Exit(1)
	}
	method := flag.Arg(0)

	matchFound := executeAPIRequest(ctx, method, kubeClient) || executeControllerRequest(ctx, method, kubeClient) || executeNamespaceRequest(method, kubeClient)
	if matchFound == false {
		glog.Fatalf("Unknown command %s", method)
	}
}