plugin.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. Copyright 2017 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 plugin
  14. import (
  15. "bytes"
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "path/filepath"
  20. "runtime"
  21. "strings"
  22. "github.com/spf13/cobra"
  23. "k8s.io/cli-runtime/pkg/genericclioptions"
  24. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  25. "k8s.io/kubernetes/pkg/kubectl/util/i18n"
  26. "k8s.io/kubernetes/pkg/kubectl/util/templates"
  27. )
  28. var (
  29. pluginLong = templates.LongDesc(`
  30. Provides utilities for interacting with plugins.
  31. Plugins provide extended functionality that is not part of the major command-line distribution.
  32. Please refer to the documentation and examples for more information about how write your own plugins.`)
  33. pluginListLong = templates.LongDesc(`
  34. List all available plugin files on a user's PATH.
  35. Available plugin files are those that are:
  36. - executable
  37. - anywhere on the user's PATH
  38. - begin with "kubectl-"
  39. `)
  40. ValidPluginFilenamePrefixes = []string{"kubectl"}
  41. )
  42. func NewCmdPlugin(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
  43. cmd := &cobra.Command{
  44. Use: "plugin [flags]",
  45. DisableFlagsInUseLine: true,
  46. Short: i18n.T("Provides utilities for interacting with plugins."),
  47. Long: pluginLong,
  48. Run: func(cmd *cobra.Command, args []string) {
  49. cmdutil.DefaultSubCommandRun(streams.ErrOut)(cmd, args)
  50. },
  51. }
  52. cmd.AddCommand(NewCmdPluginList(f, streams))
  53. return cmd
  54. }
  55. type PluginListOptions struct {
  56. Verifier PathVerifier
  57. NameOnly bool
  58. PluginPaths []string
  59. genericclioptions.IOStreams
  60. }
  61. // NewCmdPluginList provides a way to list all plugin executables visible to kubectl
  62. func NewCmdPluginList(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
  63. o := &PluginListOptions{
  64. IOStreams: streams,
  65. }
  66. cmd := &cobra.Command{
  67. Use: "list",
  68. Short: "list all visible plugin executables on a user's PATH",
  69. Long: pluginListLong,
  70. Run: func(cmd *cobra.Command, args []string) {
  71. cmdutil.CheckErr(o.Complete(cmd))
  72. cmdutil.CheckErr(o.Run())
  73. },
  74. }
  75. cmd.Flags().BoolVar(&o.NameOnly, "name-only", o.NameOnly, "If true, display only the binary name of each plugin, rather than its full path")
  76. return cmd
  77. }
  78. func (o *PluginListOptions) Complete(cmd *cobra.Command) error {
  79. o.Verifier = &CommandOverrideVerifier{
  80. root: cmd.Root(),
  81. seenPlugins: make(map[string]string, 0),
  82. }
  83. o.PluginPaths = filepath.SplitList(os.Getenv("PATH"))
  84. return nil
  85. }
  86. func (o *PluginListOptions) Run() error {
  87. pluginsFound := false
  88. isFirstFile := true
  89. pluginErrors := []error{}
  90. pluginWarnings := 0
  91. for _, dir := range uniquePathsList(o.PluginPaths) {
  92. files, err := ioutil.ReadDir(dir)
  93. if err != nil {
  94. if _, ok := err.(*os.PathError); ok {
  95. fmt.Fprintf(o.ErrOut, "Unable read directory %q from your PATH: %v. Skipping...", dir, err)
  96. continue
  97. }
  98. pluginErrors = append(pluginErrors, fmt.Errorf("error: unable to read directory %q in your PATH: %v", dir, err))
  99. continue
  100. }
  101. for _, f := range files {
  102. if f.IsDir() {
  103. continue
  104. }
  105. if !hasValidPrefix(f.Name(), ValidPluginFilenamePrefixes) {
  106. continue
  107. }
  108. if isFirstFile {
  109. fmt.Fprintf(o.ErrOut, "The following compatible plugins are available:\n\n")
  110. pluginsFound = true
  111. isFirstFile = false
  112. }
  113. pluginPath := f.Name()
  114. if !o.NameOnly {
  115. pluginPath = filepath.Join(dir, pluginPath)
  116. }
  117. fmt.Fprintf(o.Out, "%s\n", pluginPath)
  118. if errs := o.Verifier.Verify(filepath.Join(dir, f.Name())); len(errs) != 0 {
  119. for _, err := range errs {
  120. fmt.Fprintf(o.ErrOut, " - %s\n", err)
  121. pluginWarnings++
  122. }
  123. }
  124. }
  125. }
  126. if !pluginsFound {
  127. pluginErrors = append(pluginErrors, fmt.Errorf("error: unable to find any kubectl plugins in your PATH"))
  128. }
  129. if pluginWarnings > 0 {
  130. if pluginWarnings == 1 {
  131. pluginErrors = append(pluginErrors, fmt.Errorf("error: one plugin warning was found"))
  132. } else {
  133. pluginErrors = append(pluginErrors, fmt.Errorf("error: %v plugin warnings were found", pluginWarnings))
  134. }
  135. }
  136. if len(pluginErrors) > 0 {
  137. fmt.Fprintln(o.ErrOut)
  138. errs := bytes.NewBuffer(nil)
  139. for _, e := range pluginErrors {
  140. fmt.Fprintln(errs, e)
  141. }
  142. return fmt.Errorf("%s", errs.String())
  143. }
  144. return nil
  145. }
  146. // pathVerifier receives a path and determines if it is valid or not
  147. type PathVerifier interface {
  148. // Verify determines if a given path is valid
  149. Verify(path string) []error
  150. }
  151. type CommandOverrideVerifier struct {
  152. root *cobra.Command
  153. seenPlugins map[string]string
  154. }
  155. // Verify implements PathVerifier and determines if a given path
  156. // is valid depending on whether or not it overwrites an existing
  157. // kubectl command path, or a previously seen plugin.
  158. func (v *CommandOverrideVerifier) Verify(path string) []error {
  159. if v.root == nil {
  160. return []error{fmt.Errorf("unable to verify path with nil root")}
  161. }
  162. // extract the plugin binary name
  163. segs := strings.Split(path, "/")
  164. binName := segs[len(segs)-1]
  165. cmdPath := strings.Split(binName, "-")
  166. if len(cmdPath) > 1 {
  167. // the first argument is always "kubectl" for a plugin binary
  168. cmdPath = cmdPath[1:]
  169. }
  170. errors := []error{}
  171. if isExec, err := isExecutable(path); err == nil && !isExec {
  172. errors = append(errors, fmt.Errorf("warning: %s identified as a kubectl plugin, but it is not executable", path))
  173. } else if err != nil {
  174. errors = append(errors, fmt.Errorf("error: unable to identify %s as an executable file: %v", path, err))
  175. }
  176. if existingPath, ok := v.seenPlugins[binName]; ok {
  177. errors = append(errors, fmt.Errorf("warning: %s is overshadowed by a similarly named plugin: %s", path, existingPath))
  178. } else {
  179. v.seenPlugins[binName] = path
  180. }
  181. if cmd, _, err := v.root.Find(cmdPath); err == nil {
  182. errors = append(errors, fmt.Errorf("warning: %s overwrites existing command: %q", binName, cmd.CommandPath()))
  183. }
  184. return errors
  185. }
  186. func isExecutable(fullPath string) (bool, error) {
  187. info, err := os.Stat(fullPath)
  188. if err != nil {
  189. return false, err
  190. }
  191. if runtime.GOOS == "windows" {
  192. fileExt := strings.ToLower(filepath.Ext(fullPath))
  193. switch fileExt {
  194. case ".bat", ".cmd", ".com", ".exe", ".ps1":
  195. return true, nil
  196. }
  197. return false, nil
  198. }
  199. if m := info.Mode(); !m.IsDir() && m&0111 != 0 {
  200. return true, nil
  201. }
  202. return false, nil
  203. }
  204. // uniquePathsList deduplicates a given slice of strings without
  205. // sorting or otherwise altering its order in any way.
  206. func uniquePathsList(paths []string) []string {
  207. seen := map[string]bool{}
  208. newPaths := []string{}
  209. for _, p := range paths {
  210. if seen[p] {
  211. continue
  212. }
  213. seen[p] = true
  214. newPaths = append(newPaths, p)
  215. }
  216. return newPaths
  217. }
  218. func hasValidPrefix(filepath string, validPrefixes []string) bool {
  219. for _, prefix := range validPrefixes {
  220. if !strings.HasPrefix(filepath, prefix+"-") {
  221. continue
  222. }
  223. return true
  224. }
  225. return false
  226. }