blob: 0809fa476d198bb2528c29ae937cbd127df87610 [file] [log] [blame]
Lorenz Brun04904962019-11-11 15:21:14 +01001// 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 Brun04904962019-11-11 15:21:14 +010017/*
18Copyright 2014 The Kubernetes Authors.
19Licensed under the Apache License, Version 2.0 (the "License");
20you may not use this file except in compliance with the License.
21You may obtain a copy of the License at
22 http://www.apache.org/licenses/LICENSE-2.0
23Unless required by applicable law or agreed to in writing, software
24distributed under the License is distributed on an "AS IS" BASIS,
25WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26See the License for the specific language governing permissions and
27limitations under the License.
28*/
29
Lorenz Brun732a8842024-08-26 23:25:37 +020030// 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 Brun04904962019-11-11 15:21:14 +010041package main
42
43import (
Lorenz Brun732a8842024-08-26 23:25:37 +020044 "fmt"
Lorenz Brun04904962019-11-11 15:21:14 +010045 "os"
Lorenz Brun04904962019-11-11 15:21:14 +010046
47 "github.com/spf13/cobra"
Lorenz Brun732a8842024-08-26 23:25:37 +020048 "k8s.io/component-base/cli"
Lorenz Brun5d7d2a42020-04-06 14:11:02 +020049 _ "k8s.io/component-base/metrics/prometheus/restclient" // for client metric registration
50 _ "k8s.io/component-base/metrics/prometheus/version" // for version metric registration
Lorenz Brun04904962019-11-11 15:21:14 +010051 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 Brun878f5f92020-05-12 16:15:39 +020054 kubelet "k8s.io/kubernetes/cmd/kubelet/app"
Lorenz Brun04904962019-11-11 15:21:14 +010055)
56
Lorenz Brun732a8842024-08-26 23:25:37 +020057// Map of subcommand to Cobra command generator for all subcommands
58var 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 Brun04904962019-11-11 15:21:14 +010065func main() {
Lorenz Brun732a8842024-08-26 23:25:37 +020066 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 Brun04904962019-11-11 15:21:14 +010074 }
75}