main.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. // A binary that can morph into all of the other kubernetes binaries. You can
  14. // also soft-link to it busybox style.
  15. //
  16. package main
  17. import (
  18. "errors"
  19. goflag "flag"
  20. "fmt"
  21. "math/rand"
  22. "os"
  23. "path"
  24. "path/filepath"
  25. "time"
  26. "github.com/spf13/cobra"
  27. "github.com/spf13/pflag"
  28. cliflag "k8s.io/component-base/cli/flag"
  29. "k8s.io/component-base/logs"
  30. cloudcontrollermanager "k8s.io/kubernetes/cmd/cloud-controller-manager/app"
  31. kubeapiserver "k8s.io/kubernetes/cmd/kube-apiserver/app"
  32. kubecontrollermanager "k8s.io/kubernetes/cmd/kube-controller-manager/app"
  33. kubeproxy "k8s.io/kubernetes/cmd/kube-proxy/app"
  34. kubescheduler "k8s.io/kubernetes/cmd/kube-scheduler/app"
  35. kubelet "k8s.io/kubernetes/cmd/kubelet/app"
  36. _ "k8s.io/kubernetes/pkg/client/metrics/prometheus" // for client metric registration
  37. kubectl "k8s.io/kubernetes/pkg/kubectl/cmd"
  38. _ "k8s.io/kubernetes/pkg/version/prometheus" // for version metric registration
  39. )
  40. func main() {
  41. rand.Seed(time.Now().UnixNano())
  42. hyperkubeCommand, allCommandFns := NewHyperKubeCommand()
  43. // TODO: once we switch everything over to Cobra commands, we can go back to calling
  44. // cliflag.InitFlags() (by removing its pflag.Parse() call). For now, we have to set the
  45. // normalize func and add the go flag set by hand.
  46. pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
  47. pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
  48. // cliflag.InitFlags()
  49. logs.InitLogs()
  50. defer logs.FlushLogs()
  51. basename := filepath.Base(os.Args[0])
  52. if err := commandFor(basename, hyperkubeCommand, allCommandFns).Execute(); err != nil {
  53. fmt.Fprintf(os.Stderr, "%v\n", err)
  54. os.Exit(1)
  55. }
  56. }
  57. func commandFor(basename string, defaultCommand *cobra.Command, commands []func() *cobra.Command) *cobra.Command {
  58. for _, commandFn := range commands {
  59. command := commandFn()
  60. if command.Name() == basename {
  61. return command
  62. }
  63. for _, alias := range command.Aliases {
  64. if alias == basename {
  65. return command
  66. }
  67. }
  68. }
  69. return defaultCommand
  70. }
  71. // NewHyperKubeCommand is the entry point for hyperkube
  72. func NewHyperKubeCommand() (*cobra.Command, []func() *cobra.Command) {
  73. // these have to be functions since the command is polymorphic. Cobra wants you to be top level
  74. // command to get executed
  75. apiserver := func() *cobra.Command { return kubeapiserver.NewAPIServerCommand() }
  76. controller := func() *cobra.Command { return kubecontrollermanager.NewControllerManagerCommand() }
  77. proxy := func() *cobra.Command { return kubeproxy.NewProxyCommand() }
  78. scheduler := func() *cobra.Command { return kubescheduler.NewSchedulerCommand() }
  79. kubectlCmd := func() *cobra.Command { return kubectl.NewDefaultKubectlCommand() }
  80. kubelet := func() *cobra.Command { return kubelet.NewKubeletCommand() }
  81. cloudController := func() *cobra.Command { return cloudcontrollermanager.NewCloudControllerManagerCommand() }
  82. commandFns := []func() *cobra.Command{
  83. apiserver,
  84. controller,
  85. proxy,
  86. scheduler,
  87. kubectlCmd,
  88. kubelet,
  89. cloudController,
  90. }
  91. makeSymlinksFlag := false
  92. cmd := &cobra.Command{
  93. Use: "hyperkube",
  94. Short: "Request a new project",
  95. Run: func(cmd *cobra.Command, args []string) {
  96. if len(args) != 0 || !makeSymlinksFlag {
  97. cmd.Help()
  98. os.Exit(1)
  99. }
  100. if err := makeSymlinks(os.Args[0], commandFns); err != nil {
  101. fmt.Fprintf(os.Stderr, "%v\n", err.Error())
  102. }
  103. },
  104. }
  105. cmd.Flags().BoolVar(&makeSymlinksFlag, "make-symlinks", makeSymlinksFlag, "create a symlink for each server in current directory")
  106. cmd.Flags().MarkHidden("make-symlinks") // hide this flag from appearing in servers' usage output
  107. cmd.Flags().MarkDeprecated("make-symlinks", "This feature will be removed in a later release.")
  108. for i := range commandFns {
  109. cmd.AddCommand(commandFns[i]())
  110. }
  111. return cmd, commandFns
  112. }
  113. // makeSymlinks will create a symlink for each command in the local directory.
  114. func makeSymlinks(targetName string, commandFns []func() *cobra.Command) error {
  115. wd, err := os.Getwd()
  116. if err != nil {
  117. return err
  118. }
  119. var errs bool
  120. for _, commandFn := range commandFns {
  121. command := commandFn()
  122. link := path.Join(wd, command.Name())
  123. err := os.Symlink(targetName, link)
  124. if err != nil {
  125. errs = true
  126. fmt.Println(err)
  127. }
  128. }
  129. if errs {
  130. return errors.New("Error creating one or more symlinks")
  131. }
  132. return nil
  133. }