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