core -> metropolis
Smalltown is now called Metropolis!
This is the first commit in a series of cleanup commits that prepare us
for an open source release. This one just some Bazel packages around to
follow a stricter directory layout.
All of Metropolis now lives in `//metropolis`.
All of Metropolis Node code now lives in `//metropolis/node`.
All of the main /init now lives in `//m/n/core`.
All of the Kubernetes functionality/glue now lives in `//m/n/kubernetes`.
Next steps:
- hunt down all references to Smalltown and replace them appropriately
- narrow down visibility rules
- document new code organization
- move `//build/toolchain` to `//monogon/build/toolchain`
- do another cleanup pass between `//golibs` and
`//monogon/node/{core,common}`.
- remove `//delta` and `//anubis`
Fixes T799.
Test Plan: Just a very large refactor. CI should help us out here.
Bug: T799
X-Origin-Diff: phab/D667
GitOrigin-RevId: 6029b8d4edc42325d50042596b639e8b122d0ded
diff --git a/metropolis/node/kubernetes/hyperkube/BUILD b/metropolis/node/kubernetes/hyperkube/BUILD
new file mode 100644
index 0000000..dced1c7
--- /dev/null
+++ b/metropolis/node/kubernetes/hyperkube/BUILD
@@ -0,0 +1,29 @@
+load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
+load("@//third_party/go:kubernetes_version_def.bzl", "version_x_defs")
+
+go_library(
+ name = "go_default_library",
+ srcs = ["main.go"],
+ importpath = "git.monogon.dev/source/nexantic.git/metropolis/node/kubernetes/hyperkube",
+ visibility = ["//visibility:private"],
+ deps = [
+ "@com_github_spf13_cobra//:go_default_library",
+ "@com_github_spf13_pflag//:go_default_library",
+ "@io_k8s_component_base//cli/flag:go_default_library",
+ "@io_k8s_component_base//logs:go_default_library",
+ "@io_k8s_component_base//metrics/prometheus/restclient:go_default_library",
+ "@io_k8s_component_base//metrics/prometheus/version:go_default_library",
+ "@io_k8s_kubernetes//cmd/kube-apiserver/app:go_default_library",
+ "@io_k8s_kubernetes//cmd/kube-controller-manager/app:go_default_library",
+ "@io_k8s_kubernetes//cmd/kube-scheduler/app:go_default_library",
+ "@io_k8s_kubernetes//cmd/kubelet/app:go_default_library",
+ ],
+)
+
+go_binary(
+ name = "hyperkube",
+ embed = [":go_default_library"],
+ pure = "on",
+ visibility = ["//visibility:public"],
+ x_defs = version_x_defs(),
+)
diff --git a/metropolis/node/kubernetes/hyperkube/main.go b/metropolis/node/kubernetes/hyperkube/main.go
new file mode 100644
index 0000000..3b4ac08
--- /dev/null
+++ b/metropolis/node/kubernetes/hyperkube/main.go
@@ -0,0 +1,122 @@
+// Copyright 2020 The Monogon Project Authors.
+//
+// SPDX-License-Identifier: Apache-2.0
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+/*
+Copyright 2014 The Kubernetes Authors.
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+ http://www.apache.org/licenses/LICENSE-2.0
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+// Adapted from https://github.com/dims/hyperkube
+
+package main
+
+import (
+ goflag "flag"
+ "math/rand"
+ "os"
+ "path/filepath"
+ "time"
+
+ "github.com/spf13/cobra"
+ "github.com/spf13/pflag"
+
+ cliflag "k8s.io/component-base/cli/flag"
+ "k8s.io/component-base/logs"
+ _ "k8s.io/component-base/metrics/prometheus/restclient" // for client metric registration
+ _ "k8s.io/component-base/metrics/prometheus/version" // for version metric registration
+ kubeapiserver "k8s.io/kubernetes/cmd/kube-apiserver/app"
+ kubecontrollermanager "k8s.io/kubernetes/cmd/kube-controller-manager/app"
+ kubescheduler "k8s.io/kubernetes/cmd/kube-scheduler/app"
+ kubelet "k8s.io/kubernetes/cmd/kubelet/app"
+)
+
+func main() {
+ rand.Seed(time.Now().UnixNano())
+
+ hyperkubeCommand, allCommandFns := NewHyperKubeCommand()
+
+ // TODO: once we switch everything over to Cobra commands, we can go back to calling
+ // cliflag.InitFlags() (by removing its pflag.Parse() call). For now, we have to set the
+ // normalize func and add the go flag set by hand.
+ pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
+ pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
+ // cliflag.InitFlags()
+ logs.InitLogs()
+ defer logs.FlushLogs()
+
+ basename := filepath.Base(os.Args[0])
+ if err := commandFor(basename, hyperkubeCommand, allCommandFns).Execute(); err != nil {
+ os.Exit(1)
+ }
+}
+
+func commandFor(basename string, defaultCommand *cobra.Command, commands []func() *cobra.Command) *cobra.Command {
+ for _, commandFn := range commands {
+ command := commandFn()
+ if command.Name() == basename {
+ return command
+ }
+ for _, alias := range command.Aliases {
+ if alias == basename {
+ return command
+ }
+ }
+ }
+
+ return defaultCommand
+}
+
+// NewHyperKubeCommand is the entry point for hyperkube
+func NewHyperKubeCommand() (*cobra.Command, []func() *cobra.Command) {
+ // these have to be functions since the command is polymorphic. Cobra wants you to be top level
+ // command to get executed
+ apiserver := func() *cobra.Command { return kubeapiserver.NewAPIServerCommand() }
+ controller := func() *cobra.Command { return kubecontrollermanager.NewControllerManagerCommand() }
+ scheduler := func() *cobra.Command { return kubescheduler.NewSchedulerCommand() }
+ kubelet := func() *cobra.Command { return kubelet.NewKubeletCommand() }
+
+ commandFns := []func() *cobra.Command{
+ apiserver,
+ controller,
+ scheduler,
+ kubelet,
+ }
+
+ cmd := &cobra.Command{
+ Use: "kube",
+ Short: "Combines all Kubernetes components in a single binary",
+ Run: func(cmd *cobra.Command, args []string) {
+ if len(args) != 0 {
+ cmd.Help()
+ os.Exit(1)
+ }
+ },
+ }
+
+ for i := range commandFns {
+ cmd.AddCommand(commandFns[i]())
+ }
+
+ return cmd, commandFns
+}