From b4817bd3479abbb942fd69d4f33f8058fb500e89 Mon Sep 17 00:00:00 2001 From: Richard Case Date: Sat, 11 Feb 2023 15:15:12 +0000 Subject: [PATCH] refactor: removed `gw` command The separate `gw` command has been removed and its functionality incorporated into the `run` command. The HTTP gateway is then enabled using `--enable-http`. NOTE: the code to start the HTTP gateway remains the same. We will need to refactor it in the future to take into account the the TLS flags. Signed-off-by: Richard Case --- go.mod | 1 + go.sum | 1 + internal/command/flags/flags.go | 8 +-- internal/command/gw/gw.go | 116 -------------------------------- internal/command/root.go | 5 +- internal/command/run/run.go | 50 ++++++++++++++ internal/config/config.go | 2 + 7 files changed, 59 insertions(+), 124 deletions(-) delete mode 100644 internal/command/gw/gw.go diff --git a/go.mod b/go.mod index 44c571b1..1bf7ed43 100644 --- a/go.mod +++ b/go.mod @@ -55,6 +55,7 @@ require ( github.com/urfave/cli/v2 v2.24.3 github.com/weaveworks-liquidmetal/flintlock/api v0.0.0-20230211152005-2177e42d0ee6 github.com/weaveworks-liquidmetal/flintlock/client v0.0.0-20230211152005-2177e42d0ee6 + github.com/grpc-ecosystem/grpc-gateway v1.16.0 github.com/yitsushi/file-tailor v1.0.0 gopkg.in/yaml.v2 v2.4.0 sigs.k8s.io/yaml v1.3.0 diff --git a/go.sum b/go.sum index efbb4746..31cceebb 100644 --- a/go.sum +++ b/go.sum @@ -531,6 +531,7 @@ github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92Bcuy github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= +github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.0 h1:1JYBfzqrWPcCclBwxFCPAou9n+q86mfnu7NAeHfte7A= github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.0/go.mod h1:YDZoGHuwE+ov0c8smSH49WLF3F2LaWnYYuDVd+EWrc0= diff --git a/internal/command/flags/flags.go b/internal/command/flags/flags.go index 860b6648..8a81bb34 100644 --- a/internal/command/flags/flags.go +++ b/internal/command/flags/flags.go @@ -55,10 +55,10 @@ func AddGRPCServerFlagsToCommand(cmd *cobra.Command, cfg *config.Config) { // AddGWServerFlagsToCommand will add gRPC HTTP gateway flags to the supplied command. func AddGWServerFlagsToCommand(cmd *cobra.Command, cfg *config.Config) { - cmd.Flags().StringVar(&cfg.GRPCAPIEndpoint, - grpcEndpointFlag, - defaults.GRPCAPIEndpoint, - "The address of the gRPC server to act as a gateway for.") + cmd.Flags().BoolVar(&cfg.EnableHTTPGateway, + "enable-http", + false, + "Should the API be exposed via HTTP.") cmd.Flags().StringVar(&cfg.HTTPAPIEndpoint, httpEndpointFlag, diff --git a/internal/command/gw/gw.go b/internal/command/gw/gw.go deleted file mode 100644 index 4c2948ad..00000000 --- a/internal/command/gw/gw.go +++ /dev/null @@ -1,116 +0,0 @@ -package gw - -import ( - "context" - "fmt" - "net/http" - "os" - "os/signal" - "sync" - - "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" - "github.com/spf13/cobra" - mvmv1 "github.com/weaveworks-liquidmetal/flintlock/api/services/microvm/v1alpha1" - cmdflags "github.com/weaveworks-liquidmetal/flintlock/internal/command/flags" - "github.com/weaveworks-liquidmetal/flintlock/internal/config" - "github.com/weaveworks-liquidmetal/flintlock/internal/version" - "github.com/weaveworks-liquidmetal/flintlock/pkg/flags" - "github.com/weaveworks-liquidmetal/flintlock/pkg/log" - "google.golang.org/grpc" - "google.golang.org/grpc/credentials/insecure" -) - -// NewCommand creates a new cobra command for running the gRPC HTTP gateway. -func NewCommand(cfg *config.Config) *cobra.Command { - cmd := &cobra.Command{ - Use: "gw", - Short: "Start serving the HTTP gateway for the flintlock gRPC API", - PreRunE: func(c *cobra.Command, _ []string) error { - flags.BindCommandToViper(c) - - logger := log.GetLogger(c.Context()) - logger.Infof( - "flintlockd, version=%s, built_on=%s, commit=%s", - version.Version, - version.BuildDate, - version.CommitHash, - ) - - return nil - }, - RunE: func(c *cobra.Command, _ []string) error { - return runGWServer(c.Context(), cfg) - }, - } - - cmdflags.AddGWServerFlagsToCommand(cmd, cfg) - - return cmd -} - -func runGWServer(ctx context.Context, cfg *config.Config) error { - logger := log.GetLogger(ctx) - logger.Info("flintlockd grpc api gateway starting") - - sigChan := make(chan os.Signal, 1) - signal.Notify(sigChan, os.Interrupt) - - wg := &sync.WaitGroup{} - ctx, cancel := context.WithCancel(log.WithLogger(ctx, logger)) - - wg.Add(1) - - go func() { - defer wg.Done() - - if err := serveAPI(ctx, cfg); err != nil { - logger.Errorf("failed serving api: %v", err) - } - }() - - <-sigChan - logger.Debug("shutdown signal received, waiting for work to finish") - - cancel() - wg.Wait() - - logger.Info("all work finished, exiting") - - return nil -} - -func serveAPI(ctx context.Context, cfg *config.Config) error { - logger := log.GetLogger(ctx) - mux := runtime.NewServeMux() - - opts := []grpc.DialOption{ - grpc.WithTransportCredentials(insecure.NewCredentials()), - } - - if err := mvmv1.RegisterMicroVMHandlerFromEndpoint(ctx, mux, cfg.GRPCAPIEndpoint, opts); err != nil { - return fmt.Errorf("could not register microvm server: %w", err) - } - - server := &http.Server{ - Addr: cfg.HTTPAPIEndpoint, - Handler: mux, - } - - go func() { - <-ctx.Done() - logger.Infof("shutting down the http gateway server") - - //nolint: contextcheck // Intentional. - if err := server.Shutdown(context.Background()); err != nil { - logger.Errorf("failed to shutdown http gateway server: %v", err) - } - }() - - logger.Debugf("starting http server listening on endpoint %s", cfg.HTTPAPIEndpoint) - - if err := server.ListenAndServe(); err != nil { - return fmt.Errorf("listening and serving http api: %w", err) - } - - return nil -} diff --git a/internal/command/root.go b/internal/command/root.go index 8c1e873e..1e0f4941 100644 --- a/internal/command/root.go +++ b/internal/command/root.go @@ -6,7 +6,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" - "github.com/weaveworks-liquidmetal/flintlock/internal/command/gw" + "github.com/weaveworks-liquidmetal/flintlock/internal/command/run" "github.com/weaveworks-liquidmetal/flintlock/internal/config" "github.com/weaveworks-liquidmetal/flintlock/internal/version" @@ -72,9 +72,6 @@ func addRootSubCommands(cmd *cobra.Command, cfg *config.Config) error { cmd.AddCommand(runCmd) cmd.AddCommand(versionCommand()) - gwCmd := gw.NewCommand(cfg) - cmd.AddCommand(gwCmd) - return nil } diff --git a/internal/command/run/run.go b/internal/command/run/run.go index 167ead9e..efbd8ecf 100644 --- a/internal/command/run/run.go +++ b/internal/command/run/run.go @@ -16,6 +16,7 @@ import ( grpc_mw "github.com/grpc-ecosystem/go-grpc-middleware" grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth" grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus" + "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/spf13/cobra" mvmv1 "github.com/weaveworks-liquidmetal/flintlock/api/services/microvm/v1alpha1" @@ -27,6 +28,7 @@ import ( "github.com/weaveworks-liquidmetal/flintlock/pkg/flags" "github.com/weaveworks-liquidmetal/flintlock/pkg/log" "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/reflection" ) @@ -63,6 +65,7 @@ func NewCommand(cfg *config.Config) (*cobra.Command, error) { cmdflags.AddContainerDFlagsToCommand(cmd, cfg) cmdflags.AddFirecrackerFlagsToCommand(cmd, cfg) cmdflags.AddDebugFlagsToCommand(cmd, cfg) + cmdflags.AddGWServerFlagsToCommand(cmd, cfg) if err := cmdflags.AddNetworkFlagsToCommand(cmd, cfg); err != nil { return nil, fmt.Errorf("adding network flags to run command: %w", err) @@ -109,6 +112,17 @@ func runServer(ctx context.Context, cfg *config.Config) error { }() } + if cfg.EnableHTTPGateway { + wg.Add(1) + + go func() { + defer wg.Done() + if err := serveHTTP(ctx, cfg); err != nil { + logger.Errorf("failed serving http api: %v", err) + } + }() + } + if !cfg.DisableReconcile { wg.Add(1) @@ -271,3 +285,39 @@ func runPProf(ctx context.Context, cfg *config.Config) error { return nil } + +func serveHTTP(ctx context.Context, cfg *config.Config) error { + logger := log.GetLogger(ctx) + mux := runtime.NewServeMux() + + opts := []grpc.DialOption{ + grpc.WithTransportCredentials(insecure.NewCredentials()), + } + + if err := mvmv1.RegisterMicroVMHandlerFromEndpoint(ctx, mux, cfg.GRPCAPIEndpoint, opts); err != nil { + return fmt.Errorf("could not register microvm server: %w", err) + } + + server := &http.Server{ + Addr: cfg.HTTPAPIEndpoint, + Handler: mux, + } + + go func() { + <-ctx.Done() + logger.Infof("shutting down the http gateway server") + + //nolint: contextcheck // Intentional. + if err := server.Shutdown(context.Background()); err != nil { + logger.Errorf("failed to shutdown http gateway server: %v", err) + } + }() + + logger.Debugf("starting http server listening on endpoint %s", cfg.HTTPAPIEndpoint) + + if err := server.ListenAndServe(); err != nil { + return fmt.Errorf("listening and serving http api: %w", err) + } + + return nil +} diff --git a/internal/config/config.go b/internal/config/config.go index 021fa07d..ae1b8763 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -16,6 +16,8 @@ type Config struct { GRPCAPIEndpoint string // HTTPAPIEndpoint is the endpoint for the HTTP proxy for the gRPC service HTTPAPIEndpoint string + // EnableHTTPGateway indicates that the HTTP gateway should be started + EnableHTTPGateway bool // FirecrackerBin is the firecracker binary to use. FirecrackerBin string // FirecrackerDetatch indicates if the child firecracker processes should be detached from their parent.