forked from openfaas/faas-swarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
89 lines (70 loc) · 3.1 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
// Copyright (c) Alex Ellis 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package main
import (
"context"
"log"
"time"
"github.com/openfaas/faas-provider/logs"
"github.com/openfaas/faas-provider/proxy"
"github.com/docker/docker/client"
handlers "github.com/neuroforgede/nf-faas-docker/handlers"
"github.com/neuroforgede/nf-faas-docker/types"
"github.com/neuroforgede/nf-faas-docker/version"
bootstrap "github.com/openfaas/faas-provider"
bootTypes "github.com/openfaas/faas-provider/types"
)
func main() {
var err error
dockerClient, err := client.NewEnvClient()
if err != nil {
log.Fatalf("Error with Docker client: %s.", err.Error())
}
handlers.InitGlobalConfig()
dockerVersion, versionErr := dockerClient.ServerVersion(context.Background())
if versionErr != nil {
log.Fatalf("Error with Docker server: %s", versionErr.Error())
}
log.Printf("Docker API version: %s, %s\n", dockerVersion.APIVersion, dockerVersion.Version)
// How many times to reschedule a function.
maxRestarts := uint64(5)
// Delay between container restarts
restartDelay := time.Second * 5
readConfig := types.ReadConfig{}
osEnv := bootTypes.OsEnv{}
cfg, err := readConfig.Read(osEnv)
if err != nil {
log.Fatalf("Error reading config: %s", err.Error())
}
log.Printf("HTTP Read Timeout: %s\n", cfg.FaaSConfig.GetReadTimeout())
log.Printf("HTTP Write Timeout: %s\n", cfg.FaaSConfig.WriteTimeout)
funcProxyHandler := handlers.NewFunctionLookup(dockerClient, cfg.DNSRoundRobin)
dockerConfig, dockerConfigErr := handlers.ParseDockerConfig()
if dockerConfigErr != nil {
dockerConfig = handlers.DefaultDockerConfig()
log.Printf("failed to parse docker config. defaulting to empty")
}
bootstrapHandlers := bootTypes.FaaSHandlers{
DeleteHandler: handlers.DeleteHandler(dockerClient),
DeployHandler: handlers.DeployHandler(dockerConfig, dockerClient, maxRestarts, restartDelay),
FunctionReader: handlers.FunctionReader(true, dockerClient),
FunctionProxy: proxy.NewHandlerFunc(cfg.FaaSConfig, funcProxyHandler),
ReplicaReader: handlers.ReplicaReader(dockerClient),
ReplicaUpdater: handlers.ReplicaUpdater(dockerClient),
UpdateHandler: handlers.UpdateHandler(dockerConfig, dockerClient, maxRestarts, restartDelay),
HealthHandler: handlers.Health(),
InfoHandler: handlers.MakeInfoHandler(version.BuildVersion(), version.GitCommit),
SecretHandler: handlers.MakeSecretsHandler(dockerClient),
LogHandler: logs.NewLogHandlerFunc(handlers.NewLogRequester(dockerClient), cfg.FaaSConfig.WriteTimeout),
ListNamespaceHandler: handlers.NamespaceLister(),
}
bootstrapConfig := bootTypes.FaaSConfig{
ReadTimeout: cfg.FaaSConfig.GetReadTimeout(),
WriteTimeout: cfg.FaaSConfig.WriteTimeout,
TCPPort: cfg.FaaSConfig.TCPPort,
EnableBasicAuth: cfg.FaaSConfig.EnableBasicAuth,
SecretMountPath: cfg.FaaSConfig.SecretMountPath,
}
log.Printf("Basic authentication: %v\n", bootstrapConfig.EnableBasicAuth)
bootstrap.Serve(&bootstrapHandlers, &bootstrapConfig)
}