2014-10-05 21:24:19 -04:00
|
|
|
/*
|
2015-05-01 12:19:44 -04:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2014-10-05 21:24:19 -04:00
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"io"
|
|
|
|
|
|
2015-08-05 18:05:17 -04:00
|
|
|
"github.com/golang/glog"
|
2015-08-05 18:03:47 -04:00
|
|
|
cmdconfig "k8s.io/kubernetes/pkg/kubectl/cmd/config"
|
2016-01-20 16:35:01 -05:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/cmd/rollout"
|
2015-08-05 18:03:47 -04:00
|
|
|
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
|
|
|
|
"k8s.io/kubernetes/pkg/util"
|
2014-11-17 13:29:45 -05:00
|
|
|
|
2014-10-05 21:24:19 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
)
|
|
|
|
|
|
2015-03-17 13:00:48 -04:00
|
|
|
const (
|
|
|
|
|
bash_completion_func = `# call kubectl get $1,
|
|
|
|
|
__kubectl_parse_get()
|
|
|
|
|
{
|
2015-05-09 09:41:54 -04:00
|
|
|
local template
|
|
|
|
|
template="{{ range .items }}{{ .metadata.name }} {{ end }}"
|
|
|
|
|
local kubectl_out
|
|
|
|
|
if kubectl_out=$(kubectl get -o template --template="${template}" "$1" 2>/dev/null); then
|
|
|
|
|
COMPREPLY=( $( compgen -W "${kubectl_out[*]}" -- "$cur" ) )
|
2015-03-17 13:00:48 -04:00
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
__kubectl_get_resource()
|
|
|
|
|
{
|
|
|
|
|
if [[ ${#nouns[@]} -eq 0 ]]; then
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
2015-05-04 14:50:17 -04:00
|
|
|
__kubectl_parse_get "${nouns[${#nouns[@]} -1]}"
|
2015-03-17 13:00:48 -04:00
|
|
|
}
|
|
|
|
|
|
2015-08-24 08:25:48 -04:00
|
|
|
__kubectl_get_resource_pod()
|
|
|
|
|
{
|
|
|
|
|
__kubectl_parse_get "pod"
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-31 19:37:44 -04:00
|
|
|
__kubectl_get_resource_rc()
|
|
|
|
|
{
|
|
|
|
|
__kubectl_parse_get "rc"
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-17 13:00:48 -04:00
|
|
|
# $1 is the name of the pod we want to get the list of containers inside
|
|
|
|
|
__kubectl_get_containers()
|
|
|
|
|
{
|
|
|
|
|
local template
|
2015-05-09 09:41:54 -04:00
|
|
|
template="{{ range .spec.containers }}{{ .name }} {{ end }}"
|
2015-05-04 14:50:17 -04:00
|
|
|
__debug "${FUNCNAME} nouns are ${nouns[*]}"
|
2015-03-17 13:00:48 -04:00
|
|
|
|
|
|
|
|
local len="${#nouns[@]}"
|
|
|
|
|
if [[ ${len} -ne 1 ]]; then
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
local last=${nouns[${len} -1]}
|
|
|
|
|
local kubectl_out
|
|
|
|
|
if kubectl_out=$(kubectl get -o template --template="${template}" pods "${last}" 2>/dev/null); then
|
|
|
|
|
COMPREPLY=( $( compgen -W "${kubectl_out[*]}" -- "$cur" ) )
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Require both a pod and a container to be specified
|
|
|
|
|
__kubectl_require_pod_and_container()
|
|
|
|
|
{
|
|
|
|
|
if [[ ${#nouns[@]} -eq 0 ]]; then
|
|
|
|
|
__kubectl_parse_get pods
|
|
|
|
|
return 0
|
|
|
|
|
fi;
|
|
|
|
|
__kubectl_get_containers
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
__custom_func() {
|
|
|
|
|
case ${last_command} in
|
2015-05-04 06:54:52 -04:00
|
|
|
kubectl_get | kubectl_describe | kubectl_delete | kubectl_label | kubectl_stop)
|
2015-08-24 08:25:48 -04:00
|
|
|
__kubectl_get_resource
|
|
|
|
|
return
|
|
|
|
|
;;
|
|
|
|
|
kubectl_logs)
|
|
|
|
|
__kubectl_require_pod_and_container
|
|
|
|
|
return
|
|
|
|
|
;;
|
|
|
|
|
kubectl_exec)
|
|
|
|
|
__kubectl_get_resource_pod
|
2015-03-17 13:00:48 -04:00
|
|
|
return
|
|
|
|
|
;;
|
2015-08-31 19:37:44 -04:00
|
|
|
kubectl_rolling-update)
|
|
|
|
|
__kubectl_get_resource_rc
|
|
|
|
|
return
|
|
|
|
|
;;
|
2015-03-17 13:00:48 -04:00
|
|
|
*)
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
}
|
2015-07-06 23:14:28 -04:00
|
|
|
`
|
2016-02-18 17:31:08 -05:00
|
|
|
|
|
|
|
|
// If you add a resource to this list, please also take a look at pkg/kubectl/kubectl.go
|
|
|
|
|
// and add a short forms entry in expandResourceShortcut() when appropriate.
|
2015-07-06 23:14:28 -04:00
|
|
|
valid_resources = `Valid resource types include:
|
2015-12-09 05:31:35 -05:00
|
|
|
* componentstatuses (aka 'cs')
|
2016-02-18 17:31:08 -05:00
|
|
|
* daemonsets (aka 'ds')
|
|
|
|
|
* deployments
|
2015-07-06 23:14:28 -04:00
|
|
|
* events (aka 'ev')
|
2015-12-09 05:31:35 -05:00
|
|
|
* endpoints (aka 'ep')
|
|
|
|
|
* horizontalpodautoscalers (aka 'hpa')
|
|
|
|
|
* ingress (aka 'ing')
|
|
|
|
|
* jobs
|
|
|
|
|
* limitranges (aka 'limits')
|
2015-07-30 04:43:42 -04:00
|
|
|
* nodes (aka 'no')
|
|
|
|
|
* namespaces (aka 'ns')
|
2015-12-09 05:31:35 -05:00
|
|
|
* pods (aka 'po')
|
2015-07-30 04:43:42 -04:00
|
|
|
* persistentvolumes (aka 'pv')
|
|
|
|
|
* persistentvolumeclaims (aka 'pvc')
|
2015-11-03 01:07:55 -05:00
|
|
|
* quota
|
2015-12-09 05:31:35 -05:00
|
|
|
* resourcequotas (aka 'quota')
|
2016-02-08 18:13:48 -05:00
|
|
|
* replicasets (aka 'rs')
|
2015-12-09 05:31:35 -05:00
|
|
|
* replicationcontrollers (aka 'rc')
|
|
|
|
|
* secrets
|
2015-11-24 00:57:51 -05:00
|
|
|
* serviceaccounts
|
2015-12-09 05:31:35 -05:00
|
|
|
* services (aka 'svc')
|
2015-03-17 13:00:48 -04:00
|
|
|
`
|
|
|
|
|
)
|
|
|
|
|
|
2014-12-28 00:27:52 -05:00
|
|
|
// NewKubectlCommand creates the `kubectl` command and its nested children.
|
2015-04-07 14:21:25 -04:00
|
|
|
func NewKubectlCommand(f *cmdutil.Factory, in io.Reader, out, err io.Writer) *cobra.Command {
|
2014-10-05 21:24:19 -04:00
|
|
|
// 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.
|
|
|
|
|
|
2015-08-12 16:18:47 -04:00
|
|
|
Find more information at https://github.com/kubernetes/kubernetes.`,
|
2014-10-05 21:24:19 -04:00
|
|
|
Run: runHelp,
|
2015-03-17 13:00:48 -04:00
|
|
|
BashCompletionFunction: bash_completion_func,
|
2014-10-05 21:24:19 -04:00
|
|
|
}
|
|
|
|
|
|
2014-12-28 00:27:52 -05:00
|
|
|
f.BindFlags(cmds.PersistentFlags())
|
2016-02-19 17:40:14 -05:00
|
|
|
f.BindExternalFlags(cmds.PersistentFlags())
|
2014-10-05 21:24:19 -04:00
|
|
|
|
2015-05-16 12:44:42 -04:00
|
|
|
// From this point and forward we get warnings on flags that contain "_" separators
|
|
|
|
|
cmds.SetGlobalNormalizationFunc(util.WarnWordSepNormalizeFunc)
|
|
|
|
|
|
2015-04-07 14:21:25 -04:00
|
|
|
cmds.AddCommand(NewCmdGet(f, out))
|
|
|
|
|
cmds.AddCommand(NewCmdDescribe(f, out))
|
|
|
|
|
cmds.AddCommand(NewCmdCreate(f, out))
|
2015-06-27 00:25:08 -04:00
|
|
|
cmds.AddCommand(NewCmdReplace(f, out))
|
2015-06-25 18:56:30 -04:00
|
|
|
cmds.AddCommand(NewCmdPatch(f, out))
|
2015-04-07 14:21:25 -04:00
|
|
|
cmds.AddCommand(NewCmdDelete(f, out))
|
2015-09-04 19:33:11 -04:00
|
|
|
cmds.AddCommand(NewCmdEdit(f, out))
|
2015-09-10 17:32:57 -04:00
|
|
|
cmds.AddCommand(NewCmdApply(f, out))
|
2014-10-26 22:21:31 -04:00
|
|
|
|
2014-10-21 14:11:53 -04:00
|
|
|
cmds.AddCommand(NewCmdNamespace(out))
|
2015-11-23 21:05:45 -05:00
|
|
|
cmds.AddCommand(NewCmdLogs(f, out))
|
2015-04-07 14:21:25 -04:00
|
|
|
cmds.AddCommand(NewCmdRollingUpdate(f, out))
|
2015-05-21 17:10:25 -04:00
|
|
|
cmds.AddCommand(NewCmdScale(f, out))
|
2015-10-30 20:16:57 -04:00
|
|
|
cmds.AddCommand(NewCmdCordon(f, out))
|
|
|
|
|
cmds.AddCommand(NewCmdDrain(f, out))
|
|
|
|
|
cmds.AddCommand(NewCmdUncordon(f, out))
|
2014-10-05 21:24:19 -04:00
|
|
|
|
2015-07-29 19:19:09 -04:00
|
|
|
cmds.AddCommand(NewCmdAttach(f, in, out, err))
|
2015-04-07 14:21:25 -04:00
|
|
|
cmds.AddCommand(NewCmdExec(f, in, out, err))
|
|
|
|
|
cmds.AddCommand(NewCmdPortForward(f))
|
|
|
|
|
cmds.AddCommand(NewCmdProxy(f, out))
|
2015-01-08 15:41:38 -05:00
|
|
|
|
2015-08-04 15:54:17 -04:00
|
|
|
cmds.AddCommand(NewCmdRun(f, in, out, err))
|
2015-04-07 14:21:25 -04:00
|
|
|
cmds.AddCommand(NewCmdStop(f, out))
|
|
|
|
|
cmds.AddCommand(NewCmdExposeService(f, out))
|
2015-10-15 18:15:13 -04:00
|
|
|
cmds.AddCommand(NewCmdAutoscale(f, out))
|
2016-01-20 16:35:01 -05:00
|
|
|
cmds.AddCommand(rollout.NewCmdRollout(f, out))
|
2015-01-12 18:30:52 -05:00
|
|
|
|
2015-04-07 14:21:25 -04:00
|
|
|
cmds.AddCommand(NewCmdLabel(f, out))
|
2015-07-23 18:43:48 -04:00
|
|
|
cmds.AddCommand(NewCmdAnnotate(f, out))
|
2015-03-25 23:00:12 -04:00
|
|
|
|
2015-04-08 10:32:32 -04:00
|
|
|
cmds.AddCommand(cmdconfig.NewCmdConfig(cmdconfig.NewDefaultPathOptions(), out))
|
2015-04-07 14:21:25 -04:00
|
|
|
cmds.AddCommand(NewCmdClusterInfo(f, out))
|
|
|
|
|
cmds.AddCommand(NewCmdApiVersions(f, out))
|
|
|
|
|
cmds.AddCommand(NewCmdVersion(f, out))
|
2015-08-12 21:33:25 -04:00
|
|
|
cmds.AddCommand(NewCmdExplain(f, out))
|
2015-07-16 05:20:53 -04:00
|
|
|
cmds.AddCommand(NewCmdConvert(f, out))
|
2015-02-06 19:33:42 -05:00
|
|
|
|
2014-12-28 00:27:52 -05:00
|
|
|
return cmds
|
2014-10-05 21:24:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func runHelp(cmd *cobra.Command, args []string) {
|
|
|
|
|
cmd.Help()
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-27 19:24:59 -04:00
|
|
|
func printDeprecationWarning(command, alias string) {
|
|
|
|
|
glog.Warningf("%s is DEPRECATED and will be removed in a future version. Use %s instead.", alias, command)
|
|
|
|
|
}
|