blob: 5f9d443769b6f4af78ee7a21754ea7bc869d7e1b [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"
Lorenz Brun04904962019-11-11 15:21:14 +010036 "os"
37 "path/filepath"
Lorenz Brun04904962019-11-11 15:21:14 +010038
39 "github.com/spf13/cobra"
40 "github.com/spf13/pflag"
Lorenz Brun04904962019-11-11 15:21:14 +010041 cliflag "k8s.io/component-base/cli/flag"
42 "k8s.io/component-base/logs"
Lorenz Brun5d7d2a42020-04-06 14:11:02 +020043 _ "k8s.io/component-base/metrics/prometheus/restclient" // for client metric registration
44 _ "k8s.io/component-base/metrics/prometheus/version" // for version metric registration
Lorenz Brun04904962019-11-11 15:21:14 +010045 kubeapiserver "k8s.io/kubernetes/cmd/kube-apiserver/app"
46 kubecontrollermanager "k8s.io/kubernetes/cmd/kube-controller-manager/app"
47 kubescheduler "k8s.io/kubernetes/cmd/kube-scheduler/app"
Lorenz Brun878f5f92020-05-12 16:15:39 +020048 kubelet "k8s.io/kubernetes/cmd/kubelet/app"
Lorenz Brun04904962019-11-11 15:21:14 +010049)
50
51func main() {
Lorenz Brun04904962019-11-11 15:21:14 +010052 hyperkubeCommand, allCommandFns := NewHyperKubeCommand()
53
Serge Bazanski216fe7b2021-05-21 18:36:16 +020054 // TODO: once we switch everything over to Cobra commands, we can go back
55 // to calling cliflag.InitFlags() (by removing its pflag.Parse() call). For
56 // now, we have to set the normalize func and add the go flag set by hand.
Lorenz Brun04904962019-11-11 15:21:14 +010057 pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
58 pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
59 // cliflag.InitFlags()
60 logs.InitLogs()
61 defer logs.FlushLogs()
62
63 basename := filepath.Base(os.Args[0])
64 if err := commandFor(basename, hyperkubeCommand, allCommandFns).Execute(); err != nil {
65 os.Exit(1)
66 }
67}
68
69func commandFor(basename string, defaultCommand *cobra.Command, commands []func() *cobra.Command) *cobra.Command {
70 for _, commandFn := range commands {
71 command := commandFn()
72 if command.Name() == basename {
73 return command
74 }
75 for _, alias := range command.Aliases {
76 if alias == basename {
77 return command
78 }
79 }
80 }
81
82 return defaultCommand
83}
84
85// NewHyperKubeCommand is the entry point for hyperkube
86func NewHyperKubeCommand() (*cobra.Command, []func() *cobra.Command) {
Serge Bazanski216fe7b2021-05-21 18:36:16 +020087 // these have to be functions since the command is polymorphic. Cobra wants
88 // you to be top level command to get executed
Lorenz Brun04904962019-11-11 15:21:14 +010089 apiserver := func() *cobra.Command { return kubeapiserver.NewAPIServerCommand() }
90 controller := func() *cobra.Command { return kubecontrollermanager.NewControllerManagerCommand() }
91 scheduler := func() *cobra.Command { return kubescheduler.NewSchedulerCommand() }
Lorenz Brun878f5f92020-05-12 16:15:39 +020092 kubelet := func() *cobra.Command { return kubelet.NewKubeletCommand() }
Lorenz Brun04904962019-11-11 15:21:14 +010093
94 commandFns := []func() *cobra.Command{
95 apiserver,
96 controller,
97 scheduler,
Lorenz Brun878f5f92020-05-12 16:15:39 +020098 kubelet,
Lorenz Brun04904962019-11-11 15:21:14 +010099 }
100
101 cmd := &cobra.Command{
Lorenz Brun878f5f92020-05-12 16:15:39 +0200102 Use: "kube",
103 Short: "Combines all Kubernetes components in a single binary",
Lorenz Brun04904962019-11-11 15:21:14 +0100104 Run: func(cmd *cobra.Command, args []string) {
105 if len(args) != 0 {
106 cmd.Help()
107 os.Exit(1)
108 }
109 },
110 }
111
112 for i := range commandFns {
113 cmd.AddCommand(commandFns[i]())
114 }
115
116 return cmd, commandFns
117}