2014-10-06 01:24:19 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2014-10-06 01:24:19 +00: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 22:05:17 +00:00
|
|
|
"github.com/golang/glog"
|
2015-08-05 22:03:47 +00:00
|
|
|
cmdconfig "k8s.io/kubernetes/pkg/kubectl/cmd/config"
|
|
|
|
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
|
|
|
"k8s.io/kubernetes/pkg/util"
|
2014-11-17 18:29:45 +00:00
|
|
|
|
2014-10-06 01:24:19 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2015-03-17 17:00:48 +00:00
|
|
|
const (
|
|
|
|
bash_completion_func = `# call kubectl get $1,
|
|
|
|
__kubectl_parse_get()
|
|
|
|
{
|
2015-05-09 13:41:54 +00: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 17:00:48 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
__kubectl_get_resource()
|
|
|
|
{
|
|
|
|
if [[ ${#nouns[@]} -eq 0 ]]; then
|
|
|
|
return 1
|
|
|
|
fi
|
2015-05-04 18:50:17 +00:00
|
|
|
__kubectl_parse_get "${nouns[${#nouns[@]} -1]}"
|
2015-03-17 17:00:48 +00:00
|
|
|
}
|
|
|
|
|
2015-08-24 12:25:48 +00:00
|
|
|
__kubectl_get_resource_pod()
|
|
|
|
{
|
|
|
|
__kubectl_parse_get "pod"
|
|
|
|
}
|
|
|
|
|
2015-03-17 17:00:48 +00: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 13:41:54 +00:00
|
|
|
template="{{ range .spec.containers }}{{ .name }} {{ end }}"
|
2015-05-04 18:50:17 +00:00
|
|
|
__debug "${FUNCNAME} nouns are ${nouns[*]}"
|
2015-03-17 17:00:48 +00: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 10:54:52 +00:00
|
|
|
kubectl_get | kubectl_describe | kubectl_delete | kubectl_label | kubectl_stop)
|
2015-08-24 12:25:48 +00:00
|
|
|
__kubectl_get_resource
|
|
|
|
return
|
|
|
|
;;
|
|
|
|
kubectl_logs)
|
|
|
|
__kubectl_require_pod_and_container
|
|
|
|
return
|
|
|
|
;;
|
|
|
|
kubectl_exec)
|
|
|
|
__kubectl_get_resource_pod
|
2015-03-17 17:00:48 +00:00
|
|
|
return
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
2015-07-07 03:14:28 +00:00
|
|
|
`
|
|
|
|
valid_resources = `Valid resource types include:
|
|
|
|
* pods (aka 'po')
|
|
|
|
* replicationcontrollers (aka 'rc')
|
|
|
|
* services (aka 'svc')
|
|
|
|
* events (aka 'ev')
|
2015-07-30 08:43:42 +00:00
|
|
|
* nodes (aka 'no')
|
|
|
|
* namespaces (aka 'ns')
|
2015-07-07 03:14:28 +00:00
|
|
|
* secrets
|
2015-07-30 08:43:42 +00:00
|
|
|
* persistentvolumes (aka 'pv')
|
|
|
|
* persistentvolumeclaims (aka 'pvc')
|
|
|
|
* limitranges (aka 'limits')
|
|
|
|
* resourcequotas (aka 'quota')
|
2015-03-17 17:00:48 +00:00
|
|
|
`
|
|
|
|
)
|
|
|
|
|
2014-12-28 05:27:52 +00:00
|
|
|
// NewKubectlCommand creates the `kubectl` command and its nested children.
|
2015-04-07 18:21:25 +00:00
|
|
|
func NewKubectlCommand(f *cmdutil.Factory, in io.Reader, out, err io.Writer) *cobra.Command {
|
2014-10-06 01:24:19 +00: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 20:18:47 +00:00
|
|
|
Find more information at https://github.com/kubernetes/kubernetes.`,
|
2014-10-06 01:24:19 +00:00
|
|
|
Run: runHelp,
|
2015-03-17 17:00:48 +00:00
|
|
|
BashCompletionFunction: bash_completion_func,
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
2014-12-28 05:27:52 +00:00
|
|
|
f.BindFlags(cmds.PersistentFlags())
|
2014-10-06 01:24:19 +00:00
|
|
|
|
2015-05-16 16:44:42 +00:00
|
|
|
// From this point and forward we get warnings on flags that contain "_" separators
|
|
|
|
cmds.SetGlobalNormalizationFunc(util.WarnWordSepNormalizeFunc)
|
|
|
|
|
2015-04-07 18:21:25 +00:00
|
|
|
cmds.AddCommand(NewCmdGet(f, out))
|
|
|
|
cmds.AddCommand(NewCmdDescribe(f, out))
|
|
|
|
cmds.AddCommand(NewCmdCreate(f, out))
|
2015-06-27 04:25:08 +00:00
|
|
|
cmds.AddCommand(NewCmdReplace(f, out))
|
2015-06-25 22:56:30 +00:00
|
|
|
cmds.AddCommand(NewCmdPatch(f, out))
|
2015-04-07 18:21:25 +00:00
|
|
|
cmds.AddCommand(NewCmdDelete(f, out))
|
2014-10-27 02:21:31 +00:00
|
|
|
|
2014-10-21 18:11:53 +00:00
|
|
|
cmds.AddCommand(NewCmdNamespace(out))
|
2015-04-07 18:21:25 +00:00
|
|
|
cmds.AddCommand(NewCmdLog(f, out))
|
|
|
|
cmds.AddCommand(NewCmdRollingUpdate(f, out))
|
2015-05-21 21:10:25 +00:00
|
|
|
cmds.AddCommand(NewCmdScale(f, out))
|
2014-10-06 01:24:19 +00:00
|
|
|
|
2015-07-29 23:19:09 +00:00
|
|
|
cmds.AddCommand(NewCmdAttach(f, in, out, err))
|
2015-04-07 18:21:25 +00:00
|
|
|
cmds.AddCommand(NewCmdExec(f, in, out, err))
|
|
|
|
cmds.AddCommand(NewCmdPortForward(f))
|
|
|
|
cmds.AddCommand(NewCmdProxy(f, out))
|
2015-01-08 20:41:38 +00:00
|
|
|
|
2015-08-04 19:54:17 +00:00
|
|
|
cmds.AddCommand(NewCmdRun(f, in, out, err))
|
2015-04-07 18:21:25 +00:00
|
|
|
cmds.AddCommand(NewCmdStop(f, out))
|
|
|
|
cmds.AddCommand(NewCmdExposeService(f, out))
|
2015-01-12 23:30:52 +00:00
|
|
|
|
2015-04-07 18:21:25 +00:00
|
|
|
cmds.AddCommand(NewCmdLabel(f, out))
|
2015-07-23 22:43:48 +00:00
|
|
|
cmds.AddCommand(NewCmdAnnotate(f, out))
|
2015-03-26 03:00:12 +00:00
|
|
|
|
2015-04-08 14:32:32 +00:00
|
|
|
cmds.AddCommand(cmdconfig.NewCmdConfig(cmdconfig.NewDefaultPathOptions(), out))
|
2015-04-07 18:21:25 +00:00
|
|
|
cmds.AddCommand(NewCmdClusterInfo(f, out))
|
|
|
|
cmds.AddCommand(NewCmdApiVersions(f, out))
|
|
|
|
cmds.AddCommand(NewCmdVersion(f, out))
|
2015-02-07 00:33:42 +00:00
|
|
|
|
2014-12-28 05:27:52 +00:00
|
|
|
return cmds
|
2014-10-06 01:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func runHelp(cmd *cobra.Command, args []string) {
|
|
|
|
cmd.Help()
|
|
|
|
}
|
|
|
|
|
2015-03-27 23:24:59 +00:00
|
|
|
func printDeprecationWarning(command, alias string) {
|
|
|
|
glog.Warningf("%s is DEPRECATED and will be removed in a future version. Use %s instead.", alias, command)
|
|
|
|
}
|