cmd.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cmd
  14. import (
  15. "fmt"
  16. "io"
  17. "k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
  18. cmdconfig "k8s.io/kubernetes/pkg/kubectl/cmd/config"
  19. "k8s.io/kubernetes/pkg/kubectl/cmd/rollout"
  20. "k8s.io/kubernetes/pkg/kubectl/cmd/set"
  21. "k8s.io/kubernetes/pkg/kubectl/cmd/templates"
  22. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  23. "k8s.io/kubernetes/pkg/util/flag"
  24. "github.com/golang/glog"
  25. "github.com/spf13/cobra"
  26. )
  27. const (
  28. bash_completion_func = `# call kubectl get $1,
  29. __kubectl_override_flag_list=(kubeconfig cluster user context namespace server)
  30. __kubectl_override_flags()
  31. {
  32. local ${__kubectl_override_flag_list[*]} two_word_of of
  33. for w in "${words[@]}"; do
  34. if [ -n "${two_word_of}" ]; then
  35. eval "${two_word_of}=\"--${two_word_of}=\${w}\""
  36. two_word_of=
  37. continue
  38. fi
  39. for of in "${__kubectl_override_flag_list[@]}"; do
  40. case "${w}" in
  41. --${of}=*)
  42. eval "${of}=\"${w}\""
  43. ;;
  44. --${of})
  45. two_word_of="${of}"
  46. ;;
  47. esac
  48. done
  49. if [ "${w}" == "--all-namespaces" ]; then
  50. namespace="--all-namespaces"
  51. fi
  52. done
  53. for of in "${__kubectl_override_flag_list[@]}"; do
  54. if eval "test -n \"\$${of}\""; then
  55. eval "echo \${${of}}"
  56. fi
  57. done
  58. }
  59. __kubectl_get_namespaces()
  60. {
  61. local template kubectl_out
  62. template="{{ range .items }}{{ .metadata.name }} {{ end }}"
  63. if kubectl_out=$(kubectl get -o template --template="${template}" namespace 2>/dev/null); then
  64. COMPREPLY=( $( compgen -W "${kubectl_out[*]}" -- "$cur" ) )
  65. fi
  66. }
  67. __kubectl_parse_get()
  68. {
  69. local template
  70. template="{{ range .items }}{{ .metadata.name }} {{ end }}"
  71. local kubectl_out
  72. if kubectl_out=$(kubectl get $(__kubectl_override_flags) -o template --template="${template}" "$1" 2>/dev/null); then
  73. COMPREPLY=( $( compgen -W "${kubectl_out[*]}" -- "$cur" ) )
  74. fi
  75. }
  76. __kubectl_get_resource()
  77. {
  78. if [[ ${#nouns[@]} -eq 0 ]]; then
  79. return 1
  80. fi
  81. __kubectl_parse_get "${nouns[${#nouns[@]} -1]}"
  82. }
  83. __kubectl_get_resource_pod()
  84. {
  85. __kubectl_parse_get "pod"
  86. }
  87. __kubectl_get_resource_rc()
  88. {
  89. __kubectl_parse_get "rc"
  90. }
  91. __kubectl_get_resource_node()
  92. {
  93. __kubectl_parse_get "node"
  94. }
  95. # $1 is the name of the pod we want to get the list of containers inside
  96. __kubectl_get_containers()
  97. {
  98. local template
  99. template="{{ range .spec.containers }}{{ .name }} {{ end }}"
  100. __debug "${FUNCNAME} nouns are ${nouns[*]}"
  101. local len="${#nouns[@]}"
  102. if [[ ${len} -ne 1 ]]; then
  103. return
  104. fi
  105. local last=${nouns[${len} -1]}
  106. local kubectl_out
  107. if kubectl_out=$(kubectl get $(__kubectl_override_flags) -o template --template="${template}" pods "${last}" 2>/dev/null); then
  108. COMPREPLY=( $( compgen -W "${kubectl_out[*]}" -- "$cur" ) )
  109. fi
  110. }
  111. # Require both a pod and a container to be specified
  112. __kubectl_require_pod_and_container()
  113. {
  114. if [[ ${#nouns[@]} -eq 0 ]]; then
  115. __kubectl_parse_get pods
  116. return 0
  117. fi;
  118. __kubectl_get_containers
  119. return 0
  120. }
  121. __custom_func() {
  122. case ${last_command} in
  123. kubectl_get | kubectl_describe | kubectl_delete | kubectl_label | kubectl_stop | kubectl_edit | kubectl_patch |\
  124. kubectl_annotate | kubectl_expose | kubectl_scale | kubectl_autoscale | kubectl_taint | kubectl_rollout_*)
  125. __kubectl_get_resource
  126. return
  127. ;;
  128. kubectl_logs | kubectl_attach)
  129. __kubectl_require_pod_and_container
  130. return
  131. ;;
  132. kubectl_exec | kubectl_port-forward | kubectl_top_pod)
  133. __kubectl_get_resource_pod
  134. return
  135. ;;
  136. kubectl_rolling-update)
  137. __kubectl_get_resource_rc
  138. return
  139. ;;
  140. kubectl_cordon | kubectl_uncordon | kubectl_drain | kubectl_top_node)
  141. __kubectl_get_resource_node
  142. return
  143. ;;
  144. *)
  145. ;;
  146. esac
  147. }
  148. `
  149. // If you add a resource to this list, please also take a look at pkg/kubectl/kubectl.go
  150. // and add a short forms entry in expandResourceShortcut() when appropriate.
  151. // TODO: This should be populated using the discovery information from apiserver.
  152. valid_resources = `Valid resource types include:
  153. * clusters (valid only for federation apiservers)
  154. * componentstatuses (aka 'cs')
  155. * configmaps (aka 'cm')
  156. * daemonsets (aka 'ds')
  157. * deployments (aka 'deploy')
  158. * events (aka 'ev')
  159. * endpoints (aka 'ep')
  160. * horizontalpodautoscalers (aka 'hpa')
  161. * ingress (aka 'ing')
  162. * jobs
  163. * limitranges (aka 'limits')
  164. * nodes (aka 'no')
  165. * namespaces (aka 'ns')
  166. * petsets (alpha feature, may be unstable)
  167. * pods (aka 'po')
  168. * persistentvolumes (aka 'pv')
  169. * persistentvolumeclaims (aka 'pvc')
  170. * quota
  171. * resourcequotas (aka 'quota')
  172. * replicasets (aka 'rs')
  173. * replicationcontrollers (aka 'rc')
  174. * secrets
  175. * serviceaccounts (aka 'sa')
  176. * services (aka 'svc')
  177. `
  178. usage_template = `{{if gt .Aliases 0}}
  179. Aliases:
  180. {{.NameAndAliases}}{{end}}{{if .HasExample}}
  181. Examples:
  182. {{ .Example }}{{end}}{{ if .HasAvailableSubCommands}}
  183. Available Sub-commands:{{range .Commands}}{{if .IsAvailableCommand}}
  184. {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{ if .HasLocalFlags}}
  185. Flags:
  186. {{.LocalFlags.FlagUsages | trimRightSpace}}{{end}}{{ if .HasInheritedFlags}}
  187. Global Flags:
  188. {{.InheritedFlags.FlagUsages | trimRightSpace}}{{end}}{{if .HasHelpSubCommands}}
  189. Additional help topics:{{range .Commands}}{{if .IsHelpCommand}}
  190. {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}
  191. Usage:{{if .Runnable}}
  192. {{if .HasFlags}}{{appendIfNotPresent .UseLine "[flags]"}}{{else}}{{.UseLine}}{{end}}{{end}}{{ if .HasSubCommands }}
  193. {{ .CommandPath}} [command]
  194. Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
  195. `
  196. help_template = `{{with or .Long .Short }}{{. | trim}}{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`
  197. )
  198. // NewKubectlCommand creates the `kubectl` command and its nested children.
  199. func NewKubectlCommand(f *cmdutil.Factory, in io.Reader, out, err io.Writer) *cobra.Command {
  200. // Parent command to which all subcommands are added.
  201. cmds := &cobra.Command{
  202. Use: "kubectl",
  203. Short: "kubectl controls the Kubernetes cluster manager",
  204. Long: `kubectl controls the Kubernetes cluster manager.
  205. Find more information at https://github.com/kubernetes/kubernetes.`,
  206. Run: runHelp,
  207. BashCompletionFunction: bash_completion_func,
  208. }
  209. f.BindFlags(cmds.PersistentFlags())
  210. f.BindExternalFlags(cmds.PersistentFlags())
  211. // From this point and forward we get warnings on flags that contain "_" separators
  212. cmds.SetGlobalNormalizationFunc(flag.WarnWordSepNormalizeFunc)
  213. groups := templates.CommandGroups{
  214. {
  215. Message: "Basic Commands (Beginner):",
  216. Commands: []*cobra.Command{
  217. NewCmdCreate(f, out),
  218. NewCmdExposeService(f, out),
  219. NewCmdRun(f, in, out, err),
  220. set.NewCmdSet(f, out),
  221. },
  222. },
  223. {
  224. Message: "Basic Commands (Intermediate):",
  225. Commands: []*cobra.Command{
  226. NewCmdGet(f, out, err),
  227. NewCmdExplain(f, out, err),
  228. NewCmdEdit(f, out, err),
  229. NewCmdDelete(f, out),
  230. },
  231. },
  232. {
  233. Message: "Deploy Commands:",
  234. Commands: []*cobra.Command{
  235. rollout.NewCmdRollout(f, out),
  236. NewCmdRollingUpdate(f, out),
  237. NewCmdScale(f, out),
  238. NewCmdAutoscale(f, out),
  239. },
  240. },
  241. {
  242. Message: "Cluster Management Commands:",
  243. Commands: []*cobra.Command{
  244. NewCmdClusterInfo(f, out),
  245. NewCmdTop(f, out),
  246. NewCmdCordon(f, out),
  247. NewCmdUncordon(f, out),
  248. NewCmdDrain(f, out),
  249. NewCmdTaint(f, out),
  250. },
  251. },
  252. {
  253. Message: "Troubleshooting and Debugging Commands:",
  254. Commands: []*cobra.Command{
  255. NewCmdDescribe(f, out, err),
  256. NewCmdLogs(f, out),
  257. NewCmdAttach(f, in, out, err),
  258. NewCmdExec(f, in, out, err),
  259. NewCmdPortForward(f, out, err),
  260. NewCmdProxy(f, out),
  261. },
  262. },
  263. {
  264. Message: "Advanced Commands:",
  265. Commands: []*cobra.Command{
  266. NewCmdApply(f, out),
  267. NewCmdPatch(f, out),
  268. NewCmdReplace(f, out),
  269. NewCmdConvert(f, out),
  270. },
  271. },
  272. {
  273. Message: "Settings Commands:",
  274. Commands: []*cobra.Command{
  275. NewCmdLabel(f, out),
  276. NewCmdAnnotate(f, out),
  277. NewCmdCompletion(f, out),
  278. },
  279. },
  280. }
  281. groups.Add(cmds)
  282. filters := []string{
  283. "options",
  284. Deprecated("kubectl", "delete", cmds, NewCmdStop(f, out)),
  285. Deprecated("kubectl", "config set-context", cmds, NewCmdNamespace(out)),
  286. }
  287. templates.ActsAsRootCommand(cmds, filters, groups...)
  288. if cmds.Flag("namespace") != nil {
  289. if cmds.Flag("namespace").Annotations == nil {
  290. cmds.Flag("namespace").Annotations = map[string][]string{}
  291. }
  292. cmds.Flag("namespace").Annotations[cobra.BashCompCustom] = append(
  293. cmds.Flag("namespace").Annotations[cobra.BashCompCustom],
  294. "__kubectl_get_namespaces",
  295. )
  296. }
  297. cmds.AddCommand(cmdconfig.NewCmdConfig(clientcmd.NewDefaultPathOptions(), out))
  298. cmds.AddCommand(NewCmdVersion(f, out))
  299. cmds.AddCommand(NewCmdApiVersions(f, out))
  300. cmds.AddCommand(NewCmdOptions(out))
  301. return cmds
  302. }
  303. func runHelp(cmd *cobra.Command, args []string) {
  304. cmd.Help()
  305. }
  306. func printDeprecationWarning(command, alias string) {
  307. glog.Warningf("%s is DEPRECATED and will be removed in a future version. Use %s instead.", alias, command)
  308. }
  309. func Deprecated(baseName, to string, parent, cmd *cobra.Command) string {
  310. cmd.Long = fmt.Sprintf("Deprecated: This command is deprecated, all its functionalities are covered by \"%s %s\"", baseName, to)
  311. cmd.Short = fmt.Sprintf("Deprecated: %s", to)
  312. parent.AddCommand(cmd)
  313. return cmd.Name()
  314. }