Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: removed gw command #627

Merged
merged 2 commits into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
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