/
main.go
236 lines (215 loc) · 5.96 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/fission/fission-workflows/cmd/fission-workflows-bundle/bundle"
"github.com/fission/fission-workflows/pkg/fes/backend/nats"
"github.com/fission/fission-workflows/pkg/util"
natsio "github.com/nats-io/go-nats"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
func main() {
ctx, cancelFn := context.WithCancel(context.Background())
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill, syscall.SIGTERM)
go func() {
for sig := range c {
fmt.Println("Received signal: ", sig)
go func() {
time.Sleep(30 * time.Second)
fmt.Println("Deadline exceeded; forcing shutdown.")
os.Exit(0)
}()
cancelFn()
break
}
}()
cliApp := createCli()
cliApp.Action = func(c *cli.Context) error {
setupLogging(c)
policy, err := bundle.ParseSchedulerConfig(c)
if err != nil {
logrus.Fatal("Error while initializing workflows: ", err)
}
proxyConfig, err := bundle.ParseFissionProxyConfig(c)
if err != nil {
logrus.Fatal("Error while parsing Fission Proxy: ", err)
}
return bundle.Run(ctx, &bundle.Options{
NATS: parseNatsOptions(c),
Fission: parseFissionOptions(c),
Scheduler: policy,
InternalRuntime: c.Bool("internal"),
InvocationController: c.Bool("controller") || c.Bool("invocation-controller"),
WorkflowController: c.Bool("controller") || c.Bool("workflow-controller"),
AdminAPI: c.Bool("api") || c.Bool("api-admin"),
WorkflowAPI: c.Bool("api") || c.Bool("api-workflow"),
InvocationAPI: c.Bool("api") || c.Bool("api-workflow-invocation"),
HTTPGateway: c.Bool("api") || c.Bool("api-http"),
Metrics: c.Bool("metrics"),
Debug: c.Bool("debug"),
FissionProxy: proxyConfig,
})
}
cliApp.Run(os.Args)
}
func setupLogging(c *cli.Context) {
if c.Bool("debug") {
logrus.SetLevel(logrus.DebugLevel)
} else {
logrus.SetLevel(logrus.InfoLevel)
}
}
func parseFissionOptions(c *cli.Context) *bundle.FissionOptions {
if !c.Bool("fission") {
return nil
}
return &bundle.FissionOptions{
ExecutorAddress: c.String("fission-executor"),
ControllerAddr: c.String("fission-controller"),
RouterAddr: c.String("fission-router"),
}
}
func parseNatsOptions(c *cli.Context) *nats.Config {
if !c.Bool("nats") {
return nil
}
client := c.String("nats-client")
if client == "" {
client = fmt.Sprintf("workflow-bundle-%s", util.UID())
}
return &nats.Config{
URL: c.String("nats-url"),
Cluster: c.String("nats-cluster"),
Client: client,
AutoReconnect: true,
}
}
func createCli() *cli.App {
cliApp := cli.NewApp()
cliApp.Flags = append([]cli.Flag{
// Generic
cli.BoolFlag{
Name: "d, debug",
EnvVar: "WORKFLOW_DEBUG",
},
// NATS
cli.StringFlag{
Name: "nats-url",
Usage: "URL to the data store used by the NATS event store.",
Value: natsio.DefaultURL, // http://nats-streaming.fission
EnvVar: "ES_NATS_URL",
},
cli.StringFlag{
Name: "nats-cluster",
Usage: "Cluster name used for the NATS event store (if needed)",
Value: "test-cluster", // mqtrigger
EnvVar: "ES_NATS_CLUSTER",
},
cli.StringFlag{
Name: "nats-client",
Usage: "Client name used for the NATS event store. By default it will generate a unique clientID.",
EnvVar: "ES_NATS_CLIENT",
},
cli.BoolFlag{
Name: "nats",
Usage: "Use NATS as the event store",
},
// Fission Environment Proxy
cli.BoolFlag{
Name: "fission.proxy, fission-proxy",
Usage: "Enable Fission environment as a proxy",
},
cli.DurationFlag{
Name: "fission.proxy.timeout",
Usage: "The default timeout assigned to workflow invocations coming from the Fission proxy",
Value: 5 * time.Minute,
},
cli.StringFlag{
Name: "fission.proxy.addr",
Usage: "The timeout assigned to workflow invocations coming from the Fission proxy",
Value: ":8888",
},
// Fission Function Runtime
cli.BoolFlag{
Name: "fission",
Usage: "Use Fission as a function environment",
},
cli.StringFlag{
Name: "fission-executor",
Usage: "Address of the Fission executor to optimize executions",
Value: "http://executor.fission",
EnvVar: "FNENV_FISSION_EXECUTOR",
},
cli.StringFlag{
Name: "fission-controller",
Usage: "Address of the Fission controller for resolving functions",
Value: "http://controller.fission",
EnvVar: "FNENV_FISSION_CONTROLLER",
},
cli.StringFlag{
Name: "fission-router",
Usage: "Address of the Fission router for executing functions",
Value: "http://router.fission",
EnvVar: "FNENV_FISSION_ROUTER",
},
// Components
cli.BoolFlag{
Name: "internal",
Usage: "Use internal function runtime",
},
cli.BoolFlag{
Name: "controller",
Usage: "Run the controller with all components",
},
cli.BoolFlag{
Name: "workflow-controller",
Usage: "Run the workflow controller",
},
cli.BoolFlag{
Name: "invocation-controller",
Usage: "Run the invocation controller",
},
cli.BoolFlag{
Name: "api-http",
Usage: "Serve the http apis of the apis",
},
cli.BoolFlag{
Name: "api-workflow-invocation",
Usage: "Serve the workflow invocation gRPC api",
},
cli.BoolFlag{
Name: "api-workflow",
Usage: "Serve the workflow gRPC api",
},
cli.BoolFlag{
Name: "api-admin",
Usage: "Serve the admin gRPC api",
},
cli.BoolFlag{
Name: "metrics",
Usage: "Serve prometheus metrics",
},
cli.BoolFlag{
Name: "api",
Usage: "Shortcut for serving all APIs over both gRPC and HTTP",
},
// Scheduler
cli.StringFlag{
Name: bundle.FlagSchedulerPolicy,
Usage: "Policy to use for the scheduler (prewarm-all, prewarm-horizon, horizon)",
Value: "horizon",
},
cli.DurationFlag{
Name: bundle.FlagSchedulerColdStartDuration,
Usage: "The static cold start duration to assume when using prewarm schedulers",
Value: 1 * time.Second,
},
})
return cliApp
}