Example #1
0
// SetupContext creates a new cluster and context in ~/.kube using the provided API Server. If setCurrent is true, it is made the current context.
func SetupContext(clusterName, contextName, kubeAPIServer string, setCurrent bool) error {
	pathOpts := kubectlcfg.NewDefaultPathOptions()
	config, err := getConfig(pathOpts)
	if err != nil {
		return err
	}

	cluster, exists := config.Clusters[clusterName]
	if !exists {
		cluster = kubecfg.NewCluster()
	}

	// configure cluster
	cluster.Server = kubeAPIServer
	cluster.InsecureSkipTLSVerify = true
	config.Clusters[clusterName] = cluster

	context, exists := config.Contexts[contextName]
	if !exists {
		context = kubecfg.NewContext()
	}

	// configure context
	context.Cluster = clusterName
	config.Contexts[contextName] = context

	// set as current if requested
	if setCurrent {
		config.CurrentContext = contextName
	}

	return kubectlcfg.ModifyConfig(pathOpts, *config, true)
}
Example #2
0
// defaultLoadingRules use the same rules (as of 2/17/16) as kubectl.
func defaultLoadingRules() *clientcmd.ClientConfigLoadingRules {
	opts := config.NewDefaultPathOptions()

	loadingRules := opts.LoadingRules
	loadingRules.Precedence = opts.GetLoadingPrecedence()
	return loadingRules
}
Example #3
0
// NewKubectlCommand creates the `kubectl` command and its nested children.
func NewKubectlCommand(f *cmdutil.Factory, in io.Reader, out, err io.Writer) *cobra.Command {
	// Parent command to which all subcommands are added.
	cmds := &cobra.Command{
		Use:   "kubectl",
		Short: "kubectl controls the Kubernetes cluster manager",
		Long: `kubectl controls the Kubernetes cluster manager.

Find more information at https://github.com/kubernetes/kubernetes.`,
		Run: runHelp,
		BashCompletionFunction: bash_completion_func,
	}

	f.BindFlags(cmds.PersistentFlags())
	f.BindExternalFlags(cmds.PersistentFlags())

	// From this point and forward we get warnings on flags that contain "_" separators
	cmds.SetGlobalNormalizationFunc(flag.WarnWordSepNormalizeFunc)

	cmds.AddCommand(NewCmdGet(f, out))
	cmds.AddCommand(NewCmdDescribe(f, out))
	cmds.AddCommand(NewCmdCreate(f, out))
	cmds.AddCommand(NewCmdReplace(f, out))
	cmds.AddCommand(NewCmdPatch(f, out))
	cmds.AddCommand(NewCmdDelete(f, out))
	cmds.AddCommand(NewCmdEdit(f, out, err))
	cmds.AddCommand(NewCmdApply(f, out))

	cmds.AddCommand(NewCmdNamespace(out))
	cmds.AddCommand(NewCmdLogs(f, out))
	cmds.AddCommand(NewCmdRollingUpdate(f, out))
	cmds.AddCommand(NewCmdScale(f, out))
	cmds.AddCommand(NewCmdCordon(f, out))
	cmds.AddCommand(NewCmdDrain(f, out))
	cmds.AddCommand(NewCmdUncordon(f, out))

	cmds.AddCommand(NewCmdAttach(f, in, out, err))
	cmds.AddCommand(NewCmdExec(f, in, out, err))
	cmds.AddCommand(NewCmdPortForward(f))
	cmds.AddCommand(NewCmdProxy(f, out))

	cmds.AddCommand(NewCmdRun(f, in, out, err))
	cmds.AddCommand(NewCmdStop(f, out))
	cmds.AddCommand(NewCmdExposeService(f, out))
	cmds.AddCommand(NewCmdAutoscale(f, out))
	cmds.AddCommand(rollout.NewCmdRollout(f, out))

	cmds.AddCommand(NewCmdLabel(f, out))
	cmds.AddCommand(NewCmdAnnotate(f, out))

	cmds.AddCommand(cmdconfig.NewCmdConfig(cmdconfig.NewDefaultPathOptions(), out))
	cmds.AddCommand(NewCmdClusterInfo(f, out))
	cmds.AddCommand(NewCmdApiVersions(f, out))
	cmds.AddCommand(NewCmdVersion(f, out))
	cmds.AddCommand(NewCmdExplain(f, out))
	cmds.AddCommand(NewCmdConvert(f, out))

	return cmds
}
Example #4
0
// GetCurrentContext returns the context currently being used by kubectl
func GetCurrentContext() (string, error) {
	pathOpts := kubectlcfg.NewDefaultPathOptions()
	config, err := getConfig(pathOpts)
	if err != nil {
		return "", err
	}

	return config.CurrentContext, nil
}
Example #5
0
// SetCurrentContext changes the kubectl context to the given value
func SetCurrentContext(context string) error {
	pathOpts := kubectlcfg.NewDefaultPathOptions()
	config, err := getConfig(pathOpts)
	if err != nil {
		return err
	}

	config.CurrentContext = context
	return kubectlcfg.ModifyConfig(pathOpts, *config, true)
}
// ValidateToken validates that the given token is valid on the given server
//
// It returns a client config if successful, or an error
func ValidateToken(server string, token string) (*kclient.Config, error) {
	opts := &cmd.LoginOptions{
		Server:             server,
		Token:              token,
		InsecureTLS:        true,
		APIVersion:         api.Version,
		StartingKubeConfig: kclientcmdapi.NewConfig(),
		PathOptions:        kcmdconfig.NewDefaultPathOptions(),
		Out:                ioutil.Discard,
	}

	// check the token
	if err := opts.GatherInfo(); err != nil {
		return nil, err
	}

	return opts.Config, nil
}
// Login uses the given server/username/password to login on an openshift instance
//
// It returns a client config if successful, or an error
func Login(server string, username string, password string) (*kclient.Config, error) {
	opts := &cmd.LoginOptions{
		Server:             server,
		Username:           username,
		Password:           password,
		InsecureTLS:        true,
		APIVersion:         api.Version,
		StartingKubeConfig: kclientcmdapi.NewConfig(),
		PathOptions:        kcmdconfig.NewDefaultPathOptions(),
		Out:                ioutil.Discard,
	}

	// perform the login, and store a new token in opts
	if err := opts.GatherInfo(); err != nil {
		return nil, err
	}

	return opts.Config, nil
}