blob: 3b4ac08da9a94239bf53a9cb9a7b37226beb7b5e [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"
43
44 cliflag "k8s.io/component-base/cli/flag"
45 "k8s.io/component-base/logs"
Lorenz Brun5d7d2a42020-04-06 14:11:02 +020046 _ "k8s.io/component-base/metrics/prometheus/restclient" // for client metric registration
47 _ "k8s.io/component-base/metrics/prometheus/version" // for version metric registration
Lorenz Brun04904962019-11-11 15:21:14 +010048 kubeapiserver "k8s.io/kubernetes/cmd/kube-apiserver/app"
49 kubecontrollermanager "k8s.io/kubernetes/cmd/kube-controller-manager/app"
50 kubescheduler "k8s.io/kubernetes/cmd/kube-scheduler/app"
Lorenz Brun878f5f92020-05-12 16:15:39 +020051 kubelet "k8s.io/kubernetes/cmd/kubelet/app"
Lorenz Brun04904962019-11-11 15:21:14 +010052)
53
54func main() {
55 rand.Seed(time.Now().UnixNano())
56
57 hyperkubeCommand, allCommandFns := NewHyperKubeCommand()
58
59 // TODO: once we switch everything over to Cobra commands, we can go back to calling
60 // cliflag.InitFlags() (by removing its pflag.Parse() call). For now, we have to set the
61 // normalize func and add the go flag set by hand.
62 pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
63 pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
64 // cliflag.InitFlags()
65 logs.InitLogs()
66 defer logs.FlushLogs()
67
68 basename := filepath.Base(os.Args[0])
69 if err := commandFor(basename, hyperkubeCommand, allCommandFns).Execute(); err != nil {
70 os.Exit(1)
71 }
72}
73
74func commandFor(basename string, defaultCommand *cobra.Command, commands []func() *cobra.Command) *cobra.Command {
75 for _, commandFn := range commands {
76 command := commandFn()
77 if command.Name() == basename {
78 return command
79 }
80 for _, alias := range command.Aliases {
81 if alias == basename {
82 return command
83 }
84 }
85 }
86
87 return defaultCommand
88}
89
90// NewHyperKubeCommand is the entry point for hyperkube
91func NewHyperKubeCommand() (*cobra.Command, []func() *cobra.Command) {
92 // these have to be functions since the command is polymorphic. Cobra wants you to be top level
93 // command to get executed
94 apiserver := func() *cobra.Command { return kubeapiserver.NewAPIServerCommand() }
95 controller := func() *cobra.Command { return kubecontrollermanager.NewControllerManagerCommand() }
96 scheduler := func() *cobra.Command { return kubescheduler.NewSchedulerCommand() }
Lorenz Brun878f5f92020-05-12 16:15:39 +020097 kubelet := func() *cobra.Command { return kubelet.NewKubeletCommand() }
Lorenz Brun04904962019-11-11 15:21:14 +010098
99 commandFns := []func() *cobra.Command{
100 apiserver,
101 controller,
102 scheduler,
Lorenz Brun878f5f92020-05-12 16:15:39 +0200103 kubelet,
Lorenz Brun04904962019-11-11 15:21:14 +0100104 }
105
106 cmd := &cobra.Command{
Lorenz Brun878f5f92020-05-12 16:15:39 +0200107 Use: "kube",
108 Short: "Combines all Kubernetes components in a single binary",
Lorenz Brun04904962019-11-11 15:21:14 +0100109 Run: func(cmd *cobra.Command, args []string) {
110 if len(args) != 0 {
111 cmd.Help()
112 os.Exit(1)
113 }
114 },
115 }
116
117 for i := range commandFns {
118 cmd.AddCommand(commandFns[i]())
119 }
120
121 return cmd, commandFns
122}