This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
96 lines (76 loc) · 2.38 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"github.com/alexellis/faas-containerd/config"
"github.com/alexellis/faas-containerd/handlers"
"github.com/openfaas/faas-provider/proxy"
"github.com/containerd/containerd"
bootstrap "github.com/openfaas/faas-provider"
"github.com/openfaas/faas-provider/types"
)
var (
Version string
GitCommit string
)
func main() {
Start()
}
// Start faas-containerd
func Start() {
config, providerConfig, err := config.ReadFromEnv(types.OsEnv{})
if err != nil {
panic(err)
}
log.Printf("faas-containerd starting..\tVersion: %s\tCommit: %s\tService Timeout: %s\n", Version, GitCommit, config.WriteTimeout.String())
wd, err := os.Getwd()
if err != nil {
panic(err)
}
writeHostsErr := ioutil.WriteFile(path.Join(wd, "hosts"),
[]byte(`127.0.0.1 localhost`), 0644)
if writeHostsErr != nil {
log.Fatalln(fmt.Errorf("cannot write hosts file: %s", writeHostsErr).Error())
}
writeResolvErr := ioutil.WriteFile(path.Join(wd, "resolv.conf"),
[]byte(`nameserver 8.8.8.8`), 0644)
if writeResolvErr != nil {
log.Fatalln(fmt.Errorf("cannot write resolv.conf file: %s", writeResolvErr).Error())
}
cni, err := handlers.InitNetwork()
if err != nil {
panic(err)
}
client, err := containerd.New(providerConfig.Sock)
if err != nil {
panic(err)
}
defer client.Close()
invokeResolver := handlers.NewInvokeResolver(client)
bootstrapHandlers := types.FaaSHandlers{
FunctionProxy: proxy.NewHandlerFunc(*config, invokeResolver),
DeleteHandler: handlers.MakeDeleteHandler(client, cni),
DeployHandler: handlers.MakeDeployHandler(client, cni),
FunctionReader: handlers.MakeReadHandler(client),
ReplicaReader: handlers.MakeReplicaReaderHandler(client),
ReplicaUpdater: handlers.MakeReplicaUpdateHandler(client, cni),
UpdateHandler: handlers.MakeUpdateHandler(client, cni),
HealthHandler: func(w http.ResponseWriter, r *http.Request) {},
InfoHandler: handlers.MakeInfoHandler(Version, GitCommit),
ListNamespaceHandler: listNamespaces(),
}
log.Printf("Listening on TCP port: %d\n", *config.TCPPort)
bootstrap.Serve(&bootstrapHandlers, config)
}
func listNamespaces() func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
list := []string{""}
out, _ := json.Marshal(list)
w.Write(out)
}
}