From 1b23d6cc418cfc9f3aea86f7aa470756eb40a44c Mon Sep 17 00:00:00 2001 From: kaiyou <dev@kaiyou.fr> Date: Wed, 27 Sep 2023 16:28:56 +0200 Subject: [PATCH] Switch back to single hepto command for the sake of simplicity --- cmd/hepto/config.go | 48 ++++++++++++++--------- cmd/hepto/{service.go => hepto.go} | 61 +++++++++--------------------- 2 files changed, 49 insertions(+), 60 deletions(-) rename cmd/hepto/{service.go => hepto.go} (69%) diff --git a/cmd/hepto/config.go b/cmd/hepto/config.go index d7b2f54..3df2b14 100644 --- a/cmd/hepto/config.go +++ b/cmd/hepto/config.go @@ -1,13 +1,13 @@ package hepto import ( - "io" "net" "github.com/go-logr/logr" - "github.com/go-logr/zapr" + "github.com/spf13/cobra" + "github.com/spf13/pflag" + "github.com/spf13/viper" "go.acides.org/hepto/services" - "go.acides.org/hepto/utils" ) type Config struct { @@ -31,18 +31,32 @@ type Config struct { var config Config -func (c *Config) SetupLogger(output io.ReadWriteCloser) error { - // Initialize logging, default to warn level - zapLogger, logrusLogger, err := utils.NewLoggers(c.LogLevel, output) - if err != nil { - return err - } - // Use a standard logr logger for most things - logger := zapr.NewLogger(zapLogger) - c.Logger = logger - c.Cluster.Logger = logger - // Pass low level loggers for hooking (containerd and k8s mostly) - c.Cluster.ZapLogger = zapLogger - c.Cluster.LogrusLogger = logrusLogger - return nil +func init() { + // Hide unwanted flags declared inside k8s init() directly + pflag.CommandLine.MarkHidden("azure-container-registry-config") + cobra.OnInitialize(viper.AutomaticEnv) + + // General config + Hepto.PersistentFlags().CountVarP(&config.LogLevel, "verbose", "v", "Make logs more verbose") + Hepto.PersistentFlags().BoolVar(&config.Pprof, "pprof", false, "Enable Golang pprof profiling") + Hepto.PersistentFlags().StringVar(&config.DataDir, "data", "/var/lib", "Data root directory") + Hepto.PersistentFlags().BoolVar(&config.BypassIPCheck, "bypass-ip-check", false, "Bypass initial IP check") + + // Cluster settings + Hepto.PersistentFlags().StringVar(&config.Cluster.Name, "cluster", "hepto", "Hepto cluster name") + Hepto.PersistentFlags().BytesHexVar(&config.Cluster.Key, "key", []byte{}, "Main cluster 32bytes key, hex-encoded") + Hepto.PersistentFlags().IntVar(&config.Cluster.LoopbackPort, "loopback", 6443, "Loopback apiserver port") + + // Container settings + Hepto.PersistentFlags().StringVar(&config.Iface, "iface", "eth0", "Master network interface") + Hepto.PersistentFlags().IPNetVar(&config.Address, "ip", net.IPNet{}, "IP address for the public interface") + Hepto.PersistentFlags().IPVar(&config.Gateway, "gw", net.IP{}, "IP address of the network gateway") + Hepto.PersistentFlags().IPSliceVar(&config.DNS, "dns", defaultDNS, "DNS server IP addresses") + Hepto.PersistentFlags().StringToStringVar(&config.Mounts, "bind", map[string]string{}, "Additional bind mounts") + + // Node settings + Hepto.PersistentFlags().IntVar(&config.Node.Port, "discovery-port", 7123, "TCP port used for discovering the cluster") + Hepto.PersistentFlags().StringVar(&config.Node.Name, "name", "", "Hepto node name") + Hepto.PersistentFlags().StringSliceVar(&config.Node.Anchors, "anchors", []string{}, "List of cluster anchors") + Hepto.PersistentFlags().StringVar(&config.Node.Role, "role", "node", "Node role inside the cluster") } diff --git a/cmd/hepto/service.go b/cmd/hepto/hepto.go similarity index 69% rename from cmd/hepto/service.go rename to cmd/hepto/hepto.go index 49fe401..b134e6b 100644 --- a/cmd/hepto/service.go +++ b/cmd/hepto/hepto.go @@ -8,11 +8,11 @@ import ( "path" "time" + "github.com/go-logr/zapr" "github.com/spf13/cobra" - "github.com/spf13/pflag" - "github.com/spf13/viper" "go.acides.org/dolly" "go.acides.org/hepto/services" + "go.acides.org/hepto/utils" "k8s.io/component-base/version/verflag" "k8s.io/component-helpers/node/util/sysctl" ) @@ -22,19 +22,24 @@ var Hepto = &cobra.Command{ Short: "A highly opinionated geo-distributed Kubernetes distro", Long: `Hepto is a Kubernetes distribution designed for geo-distributed deployments, including across links with noticeable latency.`, - PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + PreRunE: func(cmd *cobra.Command, args []string) error { config.Cluster.DataDir = "/data" // Print version if requested, verflag flags are declared // by init() functions deep in k8s code verflag.PrintAndExitIfRequested() - return config.SetupLogger(os.Stderr) - }, -} -var Start = &cobra.Command{ - Use: "start", - Short: "Start the hepto service", - PreRunE: func(cmd *cobra.Command, args []string) error { + // Initialize logging, default to warn level + zapLogger, logrusLogger, err := utils.NewLoggers(config.LogLevel, os.Stderr) + if err != nil { + return err + } + // Use a standard logr logger for most things + logger := zapr.NewLogger(zapLogger) + config.Logger = logger + config.Cluster.Logger = logger + // Pass low level loggers for hooking (containerd and k8s mostly) + config.Cluster.ZapLogger = zapLogger + config.Cluster.LogrusLogger = logrusLogger // Set the proper sysctl for the cluster // Copied from https://github.com/kubernetes/kubernetes/blob/master/pkg/kubelet/cm/container_manager_linux.go desiredState := map[string]int{ @@ -111,11 +116,12 @@ var Start = &cobra.Command{ ContainerPort: 6443, }, ) - return container.Run(runHepto) + return container.Run(hepto) }, } -func runHepto() error { +// The actuall hepto code, run inside a container by the command +func hepto() error { // Install ourselves as a hooking binary self, err := os.Executable() if err != nil { @@ -180,34 +186,3 @@ func runHepto() error { } return c.Run() } - -func init() { - // Hide unwanted flags declared inside k8s init() directly - pflag.CommandLine.MarkHidden("azure-container-registry-config") - cobra.OnInitialize(viper.AutomaticEnv) - Hepto.AddCommand(Start) - - // General config - Hepto.PersistentFlags().CountVarP(&config.LogLevel, "verbose", "v", "Make logs more verbose") - Hepto.PersistentFlags().BoolVar(&config.Pprof, "pprof", false, "Enable Golang pprof profiling") - Hepto.PersistentFlags().StringVar(&config.DataDir, "data", "/var/lib", "Data root directory") - Hepto.PersistentFlags().BoolVar(&config.BypassIPCheck, "bypass-ip-check", false, "Bypass initial IP check") - - // Cluster settings - Hepto.PersistentFlags().StringVar(&config.Cluster.Name, "cluster", "hepto", "Hepto cluster name") - Hepto.PersistentFlags().BytesHexVar(&config.Cluster.Key, "key", []byte{}, "Main cluster 32bytes key, hex-encoded") - Hepto.PersistentFlags().IntVar(&config.Cluster.LoopbackPort, "loopback", 6443, "Loopback apiserver port") - - // Container settings - Hepto.PersistentFlags().StringVar(&config.Iface, "iface", "eth0", "Master network interface") - Hepto.PersistentFlags().IPNetVar(&config.Address, "ip", net.IPNet{}, "IP address for the public interface") - Hepto.PersistentFlags().IPVar(&config.Gateway, "gw", net.IP{}, "IP address of the network gateway") - Hepto.PersistentFlags().IPSliceVar(&config.DNS, "dns", defaultDNS, "DNS server IP addresses") - Hepto.PersistentFlags().StringToStringVar(&config.Mounts, "bind", map[string]string{}, "Additional bind mounts") - - // Node settings - Hepto.PersistentFlags().IntVar(&config.Node.Port, "discovery-port", 7123, "TCP port used for discovering the cluster") - Hepto.PersistentFlags().StringVar(&config.Node.Name, "name", "", "Hepto node name") - Hepto.PersistentFlags().StringSliceVar(&config.Node.Anchors, "anchors", []string{}, "List of cluster anchors") - Hepto.PersistentFlags().StringVar(&config.Node.Role, "role", "node", "Node role inside the cluster") -} -- GitLab