| 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 | |
| Lorenz Brun | 732a884 | 2024-08-26 23:25:37 +0200 | [diff] [blame] | 30 | // This is the entry point for our multicall Kubernetes binary. It can act as |
| 31 | // any of the Kubernetes components we use depending on its first argument. |
| 32 | // This saves us a bunch of duplicated code and thus system partition size as |
| 33 | // a large amount of library code is shared between all of the Kubernetes |
| 34 | // components. |
| 35 | // |
| 36 | // As this is not intended by the K8s developers the Cobra setup is unusual |
| 37 | // in that even the command structs are only created on-demand and not |
| 38 | // registered with AddCommand. This is done as Kubernetes performs one-off |
| 39 | // global setup inside their NewXYZCommand functions, for example for signal |
| 40 | // handling and their global registries. |
| Lorenz Brun | 0490496 | 2019-11-11 15:21:14 +0100 | [diff] [blame] | 41 | package main |
| 42 | |
| 43 | import ( |
| Lorenz Brun | 732a884 | 2024-08-26 23:25:37 +0200 | [diff] [blame] | 44 | "fmt" |
| Lorenz Brun | 0490496 | 2019-11-11 15:21:14 +0100 | [diff] [blame] | 45 | "os" |
| Lorenz Brun | 0490496 | 2019-11-11 15:21:14 +0100 | [diff] [blame] | 46 | |
| 47 | "github.com/spf13/cobra" |
| Lorenz Brun | 732a884 | 2024-08-26 23:25:37 +0200 | [diff] [blame] | 48 | "k8s.io/component-base/cli" |
| Lorenz Brun | 5d7d2a4 | 2020-04-06 14:11:02 +0200 | [diff] [blame] | 49 | _ "k8s.io/component-base/metrics/prometheus/restclient" // for client metric registration |
| 50 | _ "k8s.io/component-base/metrics/prometheus/version" // for version metric registration |
| Lorenz Brun | 0490496 | 2019-11-11 15:21:14 +0100 | [diff] [blame] | 51 | kubeapiserver "k8s.io/kubernetes/cmd/kube-apiserver/app" |
| 52 | kubecontrollermanager "k8s.io/kubernetes/cmd/kube-controller-manager/app" |
| 53 | kubescheduler "k8s.io/kubernetes/cmd/kube-scheduler/app" |
| Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 54 | kubelet "k8s.io/kubernetes/cmd/kubelet/app" |
| Lorenz Brun | 0490496 | 2019-11-11 15:21:14 +0100 | [diff] [blame] | 55 | ) |
| 56 | |
| Lorenz Brun | 732a884 | 2024-08-26 23:25:37 +0200 | [diff] [blame] | 57 | // Map of subcommand to Cobra command generator for all subcommands |
| 58 | var subcommands = map[string]func() *cobra.Command{ |
| 59 | "kube-apiserver": kubeapiserver.NewAPIServerCommand, |
| 60 | "kube-controller-manager": kubecontrollermanager.NewControllerManagerCommand, |
| 61 | "kube-scheduler": func() *cobra.Command { return kubescheduler.NewSchedulerCommand() }, |
| 62 | "kubelet": kubelet.NewKubeletCommand, |
| 63 | } |
| 64 | |
| Lorenz Brun | 0490496 | 2019-11-11 15:21:14 +0100 | [diff] [blame] | 65 | func main() { |
| Lorenz Brun | 732a884 | 2024-08-26 23:25:37 +0200 | [diff] [blame] | 66 | if len(os.Args) < 2 || subcommands[os.Args[1]] == nil { |
| 67 | fmt.Fprintf(os.Stderr, "Unknown subcommand\n") |
| 68 | } else { |
| 69 | cmdGen := subcommands[os.Args[1]] |
| 70 | cmd := cmdGen() |
| 71 | // Strip first argument as it has already been consumed |
| 72 | cmd.SetArgs(os.Args[2:]) |
| 73 | os.Exit(cli.Run(cmd)) |
| Lorenz Brun | 0490496 | 2019-11-11 15:21:14 +0100 | [diff] [blame] | 74 | } |
| 75 | } |