Skip to content

Commit

Permalink
refactor: removed gw command (#627)
Browse files Browse the repository at this point in the history
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 <richard.case@outlook.com>
Co-authored-by: Balazs Nadasdi <balazs@weave.works>
  • Loading branch information
richardcase and yitsushi authored Mar 4, 2023
1 parent d51af02 commit fc3739d
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 124 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ require (
github.com/urfave/cli/v2 v2.24.4
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
Expand Down
8 changes: 4 additions & 4 deletions internal/command/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
116 changes: 0 additions & 116 deletions internal/command/gw/gw.go

This file was deleted.

5 changes: 1 addition & 4 deletions internal/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
50 changes: 50 additions & 0 deletions internal/command/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit fc3739d

Please sign in to comment.