/
main.go
170 lines (154 loc) · 4.08 KB
/
main.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
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/fission/fission-workflows/pkg/apiserver"
"github.com/fission/fission-workflows/pkg/apiserver/fission"
"github.com/fission/fission-workflows/pkg/version"
"github.com/golang/protobuf/ptypes/empty"
"github.com/gorilla/handlers"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"google.golang.org/grpc"
)
func main() {
app := cli.NewApp()
app.Author = "Erwin van Eyk"
app.Email = "erwin@platform9.com"
app.Version = version.Version
app.EnableBashCompletion = true
app.Usage = "A Fission environment that functions as a proxy between Fission and Fission Workflows."
app.Description = app.Usage
app.HideVersion = true
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "version, v",
},
cli.IntFlag{
Name: "verbosity",
Value: 1,
Usage: "CLI verbosity (0 is quiet, 1 is the default, 2 is verbose.)",
},
cli.StringFlag{
Name: "target, t",
Value: "fission-workflows",
Usage: "Address of Fission Workflows to proxy traffic to (do not add a scheme)",
},
cli.IntFlag{
Name: "port, p",
Value: 80,
Usage: "Port to serve the proxy at.",
},
cli.BoolFlag{
Name: "test",
Usage: "Ensure that the proxy can reach the workflows before serving the proxy.",
},
cli.DurationFlag{
Name: "timeout",
Usage: "The default timeout assigned to workflow invocations coming from the Fission proxy",
Value: 5 * time.Minute,
},
}
app.Action = commandContext(func(cliCtx Context) error {
// Print version if asked
if cliCtx.IsSet("version") {
fmt.Println(version.VersionInfo().JSON())
return nil
}
// Ensure that context is canceled upon termination signals
ctx, cancelFn := context.WithCancel(cliCtx)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill, syscall.SIGTERM)
go func() {
for sig := range c {
logrus.Debugf("Received signal: %v", sig)
go func() {
time.Sleep(30 * time.Second)
fmt.Println("Deadline exceeded; forcing shutdown.")
os.Exit(1)
}()
cancelFn()
break
}
}()
// Establish connection with Fission Workflows apiserver
target := cliCtx.String("target")
conn, err := grpc.DialContext(ctx, target, grpc.WithInsecure())
if err != nil {
logrus.Fatalf("Failed to establish connection to '%s': %v", target, err)
}
logrus.Infof("Established gRPC connection to '%s'", target)
// Setup proxy
proxy := fission.NewEnvironmentProxyServer(apiserver.NewClient(conn), cliCtx.Duration("timeout"))
// Test proxy
if cliCtx.Bool("test") {
adminClient := apiserver.NewAdminAPIClient(conn)
statusCtx, cancelFn := context.WithTimeout(ctx, 10*time.Second)
_, err := adminClient.Status(statusCtx, &empty.Empty{})
if err != nil {
logrus.Fatalf("Failed to reach workflows deployment: %v", err)
}
cancelFn()
}
// Setup proxy server
proxyAddr := fmt.Sprintf(":%d", cliCtx.Int("port"))
proxySrv := &http.Server{Addr: proxyAddr}
mux := http.NewServeMux()
proxy.RegisterServer(mux)
proxySrv.Handler = handlers.LoggingHandler(os.Stdout, mux)
// Serve...
go func() {
<-ctx.Done()
logrus.Debugf("Shutting down server...")
proxySrv.Shutdown(ctx)
}()
logrus.Infof("Serving proxy at: %s", proxyAddr)
if err := proxySrv.ListenAndServe(); err != nil {
if err == http.ErrServerClosed {
logrus.Info(err)
} else {
logrus.Error(err)
}
}
return nil
})
app.Run(os.Args)
}
func commandContext(fn func(c Context) error) func(c *cli.Context) error {
return func(c *cli.Context) error {
switch c.GlobalInt("verbosity") {
case 0:
logrus.SetLevel(logrus.ErrorLevel)
case 1:
logrus.SetLevel(logrus.InfoLevel)
default:
fallthrough
case 2:
logrus.SetLevel(logrus.DebugLevel)
}
return fn(Context{c})
}
}
type Context struct {
*cli.Context
}
func (c Context) Deadline() (deadline time.Time, ok bool) {
return
}
func (c Context) Done() <-chan struct{} {
return nil
}
func (c Context) Err() error {
return nil
}
func (c Context) Value(key interface{}) interface{} {
if s, ok := key.(string); ok {
return c.Generic(s)
}
return nil
}