Lorenz Brun | 0490496 | 2019-11-11 15:21:14 +0100 | [diff] [blame] | 1 | // Copyright 2020 The Monogon Project Authors. |
| 2 | // |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
Lorenz Brun | 0490496 | 2019-11-11 15:21:14 +0100 | [diff] [blame] | 17 | /* |
| 18 | Copyright 2014 The Kubernetes Authors. |
| 19 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 20 | you may not use this file except in compliance with the License. |
| 21 | You may obtain a copy of the License at |
| 22 | http://www.apache.org/licenses/LICENSE-2.0 |
| 23 | Unless required by applicable law or agreed to in writing, software |
| 24 | distributed under the License is distributed on an "AS IS" BASIS, |
| 25 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 26 | See the License for the specific language governing permissions and |
| 27 | limitations under the License. |
| 28 | */ |
| 29 | |
| 30 | // Adapted from https://github.com/dims/hyperkube |
| 31 | |
| 32 | package main |
| 33 | |
| 34 | import ( |
| 35 | goflag "flag" |
| 36 | "math/rand" |
| 37 | "os" |
| 38 | "path/filepath" |
| 39 | "time" |
| 40 | |
| 41 | "github.com/spf13/cobra" |
| 42 | "github.com/spf13/pflag" |
| 43 | |
| 44 | cliflag "k8s.io/component-base/cli/flag" |
| 45 | "k8s.io/component-base/logs" |
Lorenz Brun | 5d7d2a4 | 2020-04-06 14:11:02 +0200 | [diff] [blame] | 46 | _ "k8s.io/component-base/metrics/prometheus/restclient" // for client metric registration |
| 47 | _ "k8s.io/component-base/metrics/prometheus/version" // for version metric registration |
Lorenz Brun | 0490496 | 2019-11-11 15:21:14 +0100 | [diff] [blame] | 48 | kubeapiserver "k8s.io/kubernetes/cmd/kube-apiserver/app" |
| 49 | kubecontrollermanager "k8s.io/kubernetes/cmd/kube-controller-manager/app" |
| 50 | kubescheduler "k8s.io/kubernetes/cmd/kube-scheduler/app" |
Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame^] | 51 | kubelet "k8s.io/kubernetes/cmd/kubelet/app" |
Lorenz Brun | 0490496 | 2019-11-11 15:21:14 +0100 | [diff] [blame] | 52 | ) |
| 53 | |
| 54 | func main() { |
| 55 | rand.Seed(time.Now().UnixNano()) |
| 56 | |
| 57 | hyperkubeCommand, allCommandFns := NewHyperKubeCommand() |
| 58 | |
| 59 | // TODO: once we switch everything over to Cobra commands, we can go back to calling |
| 60 | // cliflag.InitFlags() (by removing its pflag.Parse() call). For now, we have to set the |
| 61 | // normalize func and add the go flag set by hand. |
| 62 | pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc) |
| 63 | pflag.CommandLine.AddGoFlagSet(goflag.CommandLine) |
| 64 | // cliflag.InitFlags() |
| 65 | logs.InitLogs() |
| 66 | defer logs.FlushLogs() |
| 67 | |
| 68 | basename := filepath.Base(os.Args[0]) |
| 69 | if err := commandFor(basename, hyperkubeCommand, allCommandFns).Execute(); err != nil { |
| 70 | os.Exit(1) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func commandFor(basename string, defaultCommand *cobra.Command, commands []func() *cobra.Command) *cobra.Command { |
| 75 | for _, commandFn := range commands { |
| 76 | command := commandFn() |
| 77 | if command.Name() == basename { |
| 78 | return command |
| 79 | } |
| 80 | for _, alias := range command.Aliases { |
| 81 | if alias == basename { |
| 82 | return command |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return defaultCommand |
| 88 | } |
| 89 | |
| 90 | // NewHyperKubeCommand is the entry point for hyperkube |
| 91 | func NewHyperKubeCommand() (*cobra.Command, []func() *cobra.Command) { |
| 92 | // these have to be functions since the command is polymorphic. Cobra wants you to be top level |
| 93 | // command to get executed |
| 94 | apiserver := func() *cobra.Command { return kubeapiserver.NewAPIServerCommand() } |
| 95 | controller := func() *cobra.Command { return kubecontrollermanager.NewControllerManagerCommand() } |
| 96 | scheduler := func() *cobra.Command { return kubescheduler.NewSchedulerCommand() } |
Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame^] | 97 | kubelet := func() *cobra.Command { return kubelet.NewKubeletCommand() } |
Lorenz Brun | 0490496 | 2019-11-11 15:21:14 +0100 | [diff] [blame] | 98 | |
| 99 | commandFns := []func() *cobra.Command{ |
| 100 | apiserver, |
| 101 | controller, |
| 102 | scheduler, |
Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame^] | 103 | kubelet, |
Lorenz Brun | 0490496 | 2019-11-11 15:21:14 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | cmd := &cobra.Command{ |
Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame^] | 107 | Use: "kube", |
| 108 | Short: "Combines all Kubernetes components in a single binary", |
Lorenz Brun | 0490496 | 2019-11-11 15:21:14 +0100 | [diff] [blame] | 109 | Run: func(cmd *cobra.Command, args []string) { |
| 110 | if len(args) != 0 { |
| 111 | cmd.Help() |
| 112 | os.Exit(1) |
| 113 | } |
| 114 | }, |
| 115 | } |
| 116 | |
| 117 | for i := range commandFns { |
| 118 | cmd.AddCommand(commandFns[i]()) |
| 119 | } |
| 120 | |
| 121 | return cmd, commandFns |
| 122 | } |