blob: 854e742816116944cbc555b676e5d0c0e80f2c58 [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
17//+build ignore
18
19/*
20Copyright 2014 The Kubernetes Authors.
21Licensed under the Apache License, Version 2.0 (the "License");
22you may not use this file except in compliance with the License.
23You may obtain a copy of the License at
24 http://www.apache.org/licenses/LICENSE-2.0
25Unless required by applicable law or agreed to in writing, software
26distributed under the License is distributed on an "AS IS" BASIS,
27WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28See the License for the specific language governing permissions and
29limitations under the License.
30*/
31
32// Adapted from https://github.com/dims/hyperkube
33
34package main
35
36import (
37 goflag "flag"
38 "math/rand"
39 "os"
40 "path/filepath"
41 "time"
42
43 "github.com/spf13/cobra"
44 "github.com/spf13/pflag"
45
46 cliflag "k8s.io/component-base/cli/flag"
47 "k8s.io/component-base/logs"
Lorenz Brun5d7d2a42020-04-06 14:11:02 +020048 _ "k8s.io/component-base/metrics/prometheus/restclient" // for client metric registration
49 _ "k8s.io/component-base/metrics/prometheus/version" // for version metric registration
Lorenz Brun04904962019-11-11 15:21:14 +010050 kubeapiserver "k8s.io/kubernetes/cmd/kube-apiserver/app"
51 kubecontrollermanager "k8s.io/kubernetes/cmd/kube-controller-manager/app"
52 kubescheduler "k8s.io/kubernetes/cmd/kube-scheduler/app"
Lorenz Brun04904962019-11-11 15:21:14 +010053)
54
55func main() {
56 rand.Seed(time.Now().UnixNano())
57
58 hyperkubeCommand, allCommandFns := NewHyperKubeCommand()
59
60 // TODO: once we switch everything over to Cobra commands, we can go back to calling
61 // cliflag.InitFlags() (by removing its pflag.Parse() call). For now, we have to set the
62 // normalize func and add the go flag set by hand.
63 pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
64 pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
65 // cliflag.InitFlags()
66 logs.InitLogs()
67 defer logs.FlushLogs()
68
69 basename := filepath.Base(os.Args[0])
70 if err := commandFor(basename, hyperkubeCommand, allCommandFns).Execute(); err != nil {
71 os.Exit(1)
72 }
73}
74
75func commandFor(basename string, defaultCommand *cobra.Command, commands []func() *cobra.Command) *cobra.Command {
76 for _, commandFn := range commands {
77 command := commandFn()
78 if command.Name() == basename {
79 return command
80 }
81 for _, alias := range command.Aliases {
82 if alias == basename {
83 return command
84 }
85 }
86 }
87
88 return defaultCommand
89}
90
91// NewHyperKubeCommand is the entry point for hyperkube
92func NewHyperKubeCommand() (*cobra.Command, []func() *cobra.Command) {
93 // these have to be functions since the command is polymorphic. Cobra wants you to be top level
94 // command to get executed
95 apiserver := func() *cobra.Command { return kubeapiserver.NewAPIServerCommand() }
96 controller := func() *cobra.Command { return kubecontrollermanager.NewControllerManagerCommand() }
97 scheduler := func() *cobra.Command { return kubescheduler.NewSchedulerCommand() }
98
99 commandFns := []func() *cobra.Command{
100 apiserver,
101 controller,
102 scheduler,
103 }
104
105 cmd := &cobra.Command{
106 Use: "kube-controlplane",
107 Short: "Combines all Kubernetes Control Plane components in a single binary",
108 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}