blob: fd0703d746a97537ab007b34b4e15bc4f8a96ebf [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
30// Adapted from https://github.com/dims/hyperkube
31
32package main
33
34import (
35 goflag "flag"
36 "math/rand"
37 "os"
38 "path/filepath"
39 "time"
40
41 "github.com/spf13/cobra"
42 "github.com/spf13/pflag"
Lorenz Brun04904962019-11-11 15:21:14 +010043 cliflag "k8s.io/component-base/cli/flag"
44 "k8s.io/component-base/logs"
Lorenz Brun5d7d2a42020-04-06 14:11:02 +020045 _ "k8s.io/component-base/metrics/prometheus/restclient" // for client metric registration
46 _ "k8s.io/component-base/metrics/prometheus/version" // for version metric registration
Lorenz Brun04904962019-11-11 15:21:14 +010047 kubeapiserver "k8s.io/kubernetes/cmd/kube-apiserver/app"
48 kubecontrollermanager "k8s.io/kubernetes/cmd/kube-controller-manager/app"
49 kubescheduler "k8s.io/kubernetes/cmd/kube-scheduler/app"
Lorenz Brun878f5f92020-05-12 16:15:39 +020050 kubelet "k8s.io/kubernetes/cmd/kubelet/app"
Lorenz Brun04904962019-11-11 15:21:14 +010051)
52
53func main() {
54 rand.Seed(time.Now().UnixNano())
55
56 hyperkubeCommand, allCommandFns := NewHyperKubeCommand()
57
Serge Bazanski216fe7b2021-05-21 18:36:16 +020058 // TODO: once we switch everything over to Cobra commands, we can go back
59 // to calling cliflag.InitFlags() (by removing its pflag.Parse() call). For
60 // now, we have to set the normalize func and add the go flag set by hand.
Lorenz Brun04904962019-11-11 15:21:14 +010061 pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
62 pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
63 // cliflag.InitFlags()
64 logs.InitLogs()
65 defer logs.FlushLogs()
66
67 basename := filepath.Base(os.Args[0])
68 if err := commandFor(basename, hyperkubeCommand, allCommandFns).Execute(); err != nil {
69 os.Exit(1)
70 }
71}
72
73func commandFor(basename string, defaultCommand *cobra.Command, commands []func() *cobra.Command) *cobra.Command {
74 for _, commandFn := range commands {
75 command := commandFn()
76 if command.Name() == basename {
77 return command
78 }
79 for _, alias := range command.Aliases {
80 if alias == basename {
81 return command
82 }
83 }
84 }
85
86 return defaultCommand
87}
88
89// NewHyperKubeCommand is the entry point for hyperkube
90func NewHyperKubeCommand() (*cobra.Command, []func() *cobra.Command) {
Serge Bazanski216fe7b2021-05-21 18:36:16 +020091 // these have to be functions since the command is polymorphic. Cobra wants
92 // you to be top level command to get executed
Lorenz Brun04904962019-11-11 15:21:14 +010093 apiserver := func() *cobra.Command { return kubeapiserver.NewAPIServerCommand() }
94 controller := func() *cobra.Command { return kubecontrollermanager.NewControllerManagerCommand() }
95 scheduler := func() *cobra.Command { return kubescheduler.NewSchedulerCommand() }
Lorenz Brun878f5f92020-05-12 16:15:39 +020096 kubelet := func() *cobra.Command { return kubelet.NewKubeletCommand() }
Lorenz Brun04904962019-11-11 15:21:14 +010097
98 commandFns := []func() *cobra.Command{
99 apiserver,
100 controller,
101 scheduler,
Lorenz Brun878f5f92020-05-12 16:15:39 +0200102 kubelet,
Lorenz Brun04904962019-11-11 15:21:14 +0100103 }
104
105 cmd := &cobra.Command{
Lorenz Brun878f5f92020-05-12 16:15:39 +0200106 Use: "kube",
107 Short: "Combines all Kubernetes components in a single binary",
Lorenz Brun04904962019-11-11 15:21:14 +0100108 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}