This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
/
serve.go
219 lines (186 loc) · 7.18 KB
/
serve.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package entrypoints
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net"
"net/http"
_ "net/http/pprof" // Required to serve application.
"strings"
"github.com/pkg/errors"
"google.golang.org/grpc/credentials"
"github.com/lyft/flyteadmin/pkg/common"
"github.com/lyft/flytestdlib/logger"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
flyteService "github.com/lyft/flyteidl/gen/pb-go/flyteidl/service"
"github.com/lyft/flyteadmin/pkg/config"
"github.com/lyft/flyteadmin/pkg/rpc/adminservice"
"github.com/spf13/cobra"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/lyft/flytestdlib/contextutils"
"github.com/lyft/flytestdlib/promutils/labeled"
"google.golang.org/grpc"
)
type ServingOptions struct {
Secure bool
// Optional Arguments, Should be provided to enable secure mode
CertFile string
KeyFile string
}
var serviceOpts = ServingOptions{}
// serveCmd represents the serve command
var serveCmd = &cobra.Command{
Use: "serve",
Short: "Launches the Flyte admin server",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
cfg := config.GetConfig()
if serviceOpts.Secure {
return serveGatewaySecure(ctx, cfg, serviceOpts)
}
return serveGatewayInsecure(ctx, cfg)
},
}
func init() {
// Command information
RootCmd.AddCommand(serveCmd)
serveCmd.Flags().BoolVarP(&serviceOpts.Secure, "secure", "s", false, "Use ssl")
serveCmd.Flags().StringVarP(&serviceOpts.CertFile, "cert-file", "c", "", "Path of file that contains x509 certificate")
serveCmd.Flags().StringVarP(&serviceOpts.KeyFile, "key-file", "k", "", "Path of file that contains x509 client key")
// Set Keys
labeled.SetMetricKeys(contextutils.AppNameKey, contextutils.ProjectKey, contextutils.DomainKey,
contextutils.ExecIDKey, contextutils.WorkflowIDKey, contextutils.NodeIDKey, contextutils.TaskIDKey,
contextutils.TaskTypeKey, common.RuntimeTypeKey, common.RuntimeVersionKey)
}
// Creates a new gRPC Server with all the configuration
func newGRPCServer(_ context.Context, cfg *config.Config, opts ...grpc.ServerOption) (*grpc.Server, error) {
serverOpts := []grpc.ServerOption{
grpc.StreamInterceptor(grpc_prometheus.StreamServerInterceptor),
grpc.UnaryInterceptor(grpc_prometheus.UnaryServerInterceptor),
}
serverOpts = append(serverOpts, opts...)
grpcServer := grpc.NewServer(serverOpts...)
grpc_prometheus.Register(grpcServer)
flyteService.RegisterAdminServiceServer(grpcServer, adminservice.NewAdminServer(cfg.KubeConfig, cfg.Master))
return grpcServer, nil
}
func newHTTPServer(ctx context.Context, cfg *config.Config, grpcConnectionOpts []grpc.DialOption, grpcAddress string) (*http.ServeMux, error) {
// Register the server that will serve HTTP/REST Traffic
mux := http.NewServeMux()
// Register healthcheck
mux.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) {
// A very simple health check.
w.WriteHeader(http.StatusOK)
})
// Register OpenAPI endpoint
// This endpoint will serve the OpenAPI2 spec generated by the swagger protoc plugin, and bundled by go-bindata
mux.HandleFunc("/api/v1/openapi", func(w http.ResponseWriter, r *http.Request) {
swaggerBytes, err := flyteService.Asset("admin.swagger.json")
if err != nil {
logger.Warningf(ctx, "Err %v", err)
w.WriteHeader(http.StatusFailedDependency)
} else {
w.WriteHeader(http.StatusOK)
_, err := w.Write(swaggerBytes)
if err != nil {
logger.Errorf(ctx, "failed to write openAPI information, error: %s", err.Error())
}
}
})
// Register the actual Server that will service gRPC traffic
gwmux := runtime.NewServeMux(runtime.WithMarshalerOption("application/octet-stream", &runtime.ProtoMarshaller{}))
err := flyteService.RegisterAdminServiceHandlerFromEndpoint(ctx, gwmux, grpcAddress, grpcConnectionOpts)
if err != nil {
return nil, errors.Wrap(err, "error registering admin service")
}
mux.Handle("/", gwmux)
return mux, nil
}
func serveGatewayInsecure(ctx context.Context, cfg *config.Config) error {
logger.Infof(ctx, "Serving FlyteAdmin Insecure")
grpcServer, err := newGRPCServer(ctx, cfg)
if err != nil {
return errors.Wrap(err, "failed to create GRPC server")
}
logger.Infof(ctx, "Serving GRPC Traffic on: %s", cfg.GetGrpcHostAddress())
lis, err := net.Listen("tcp", cfg.GetGrpcHostAddress())
if err != nil {
return errors.Wrapf(err, "failed to listen on GRPC port: %s", cfg.GetGrpcHostAddress())
}
go func() {
err := grpcServer.Serve(lis)
logger.Fatalf(ctx, "Failed to create GRPC Server, Err: ", err)
}()
logger.Infof(ctx, "Starting HTTP/1 Gateway server on %s", cfg.GetHostAddress())
httpServer, err := newHTTPServer(ctx, cfg, []grpc.DialOption{grpc.WithInsecure()}, cfg.GetGrpcHostAddress())
if err != nil {
return err
}
err = http.ListenAndServe(cfg.GetHostAddress(), httpServer)
if err != nil {
return errors.Wrapf(err, "failed to Start HTTP Server")
}
return nil
}
// grpcHandlerFunc returns an http.Handler that delegates to grpcServer on incoming gRPC
// connections or otherHandler otherwise.
// See https://github.com/philips/grpc-gateway-example/blob/master/cmd/serve.go for reference
func grpcHandlerFunc(grpcServer *grpc.Server, otherHandler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// This is a partial recreation of gRPC's internal checks
if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
logger.Infof(context.TODO(), "Received GRPC request, %s, %s, %s, %d, %d", r.RequestURI, r.Header, r.Method, r.ProtoMajor, r.ProtoMinor)
grpcServer.ServeHTTP(w, r)
} else {
logger.Infof(context.TODO(), "Received regular request - %s, %s, %s, %d, %d", r.RequestURI, r.Header, r.Method, r.ProtoMajor, r.ProtoMinor)
otherHandler.ServeHTTP(w, r)
}
})
}
func serveGatewaySecure(ctx context.Context, cfg *config.Config, opts ServingOptions) error {
// This support single cert right now?
var err error
cert, err := tls.LoadX509KeyPair(opts.CertFile, opts.KeyFile)
if err != nil {
return err
}
certPool := x509.NewCertPool()
data, err := ioutil.ReadFile(opts.CertFile)
if err != nil {
return errors.Wrapf(err, "failed to read server cert file: %s", opts.CertFile)
}
if ok := certPool.AppendCertsFromPEM([]byte(data)); !ok {
return fmt.Errorf("failed to load certificate into the pool")
}
grpcServer, err := newGRPCServer(ctx, cfg, grpc.Creds(credentials.NewClientTLSFromCert(certPool, cfg.GetHostAddress())))
if err != nil {
return errors.Wrap(err, "failed to create GRPC server")
}
dialCreds := credentials.NewTLS(&tls.Config{
ServerName: cfg.GetHostAddress(),
RootCAs: certPool,
})
httpServer, err := newHTTPServer(ctx, cfg, []grpc.DialOption{grpc.WithTransportCredentials(dialCreds)}, cfg.GetHostAddress())
if err != nil {
return err
}
conn, err := net.Listen("tcp", cfg.GetHostAddress())
if err != nil {
panic(err)
}
srv := &http.Server{
Addr: cfg.GetHostAddress(),
Handler: grpcHandlerFunc(grpcServer, httpServer),
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
NextProtos: []string{"h2"},
},
}
err = srv.Serve(tls.NewListener(conn, srv.TLSConfig))
if err != nil {
return errors.Wrapf(err, "failed to Start HTTP/2 Server")
}
return nil
}