forked from openfaas/faas-netes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (93 loc) · 4.22 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
// 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 (
"flag"
"log"
"os"
"time"
"github.com/openfaas/faas-provider/proxy"
"k8s.io/client-go/kubernetes"
"github.com/openfaas-incubator/openfaas-operator/pkg/signals"
"github.com/openfaas/faas-netes/handlers"
"github.com/openfaas/faas-netes/k8s"
"github.com/openfaas/faas-netes/types"
"github.com/openfaas/faas-netes/version"
bootstrap "github.com/openfaas/faas-provider"
"github.com/openfaas/faas-provider/logs"
bootTypes "github.com/openfaas/faas-provider/types"
kubeinformers "k8s.io/client-go/informers"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
var kubeconfig string
var masterURL string
flag.StringVar(&kubeconfig, "kubeconfig", "",
"Path to a kubeconfig. Only required if out-of-cluster.")
flag.StringVar(&masterURL, "master", "",
"The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.")
flag.Parse()
clientCmdConfig, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfig)
if err != nil {
log.Fatalf("Error building kubeconfig: %s", err.Error())
}
clientset, err := kubernetes.NewForConfig(clientCmdConfig)
if err != nil {
log.Fatalf("Error building Kubernetes clientset: %s", err.Error())
}
functionNamespace := "default"
if namespace, exists := os.LookupEnv("function_namespace"); exists {
functionNamespace = namespace
}
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)
log.Printf("HTTPProbe: %v\n", cfg.HTTPProbe)
log.Printf("SetNonRootUser: %v\n", cfg.SetNonRootUser)
deployConfig := k8s.DeploymentConfig{
RuntimeHTTPPort: 8080,
HTTPProbe: cfg.HTTPProbe,
SetNonRootUser: cfg.SetNonRootUser,
ReadinessProbe: &k8s.ProbeConfig{
InitialDelaySeconds: int32(cfg.ReadinessProbeInitialDelaySeconds),
TimeoutSeconds: int32(cfg.ReadinessProbeTimeoutSeconds),
PeriodSeconds: int32(cfg.ReadinessProbePeriodSeconds),
},
LivenessProbe: &k8s.ProbeConfig{
InitialDelaySeconds: int32(cfg.LivenessProbeInitialDelaySeconds),
TimeoutSeconds: int32(cfg.LivenessProbeTimeoutSeconds),
PeriodSeconds: int32(cfg.LivenessProbePeriodSeconds),
},
ImagePullPolicy: cfg.ImagePullPolicy,
}
factory := k8s.NewFunctionFactory(clientset, deployConfig)
defaultResync := time.Second * 5
kubeInformerOpt := kubeinformers.WithNamespace(functionNamespace)
kubeInformerFactory := kubeinformers.NewSharedInformerFactoryWithOptions(clientset, defaultResync, kubeInformerOpt)
// set up signals so we handle the first shutdown signal gracefully
stopCh := signals.SetupSignalHandler()
endpointsInformer := kubeInformerFactory.Core().V1().Endpoints()
go kubeInformerFactory.Start(stopCh)
lister := endpointsInformer.Lister()
functionLookup := k8s.NewFunctionLookup(functionNamespace, lister)
bootstrapHandlers := bootTypes.FaaSHandlers{
FunctionProxy: proxy.NewHandlerFunc(cfg.FaaSConfig, functionLookup),
DeleteHandler: handlers.MakeDeleteHandler(functionNamespace, clientset),
DeployHandler: handlers.MakeDeployHandler(functionNamespace, factory),
FunctionReader: handlers.MakeFunctionReader(functionNamespace, clientset),
ReplicaReader: handlers.MakeReplicaReader(functionNamespace, clientset),
ReplicaUpdater: handlers.MakeReplicaUpdater(functionNamespace, clientset),
UpdateHandler: handlers.MakeUpdateHandler(functionNamespace, factory),
HealthHandler: handlers.MakeHealthHandler(),
InfoHandler: handlers.MakeInfoHandler(version.BuildVersion(), version.GitCommit),
SecretHandler: handlers.MakeSecretHandler(functionNamespace, clientset),
LogHandler: logs.NewLogHandlerFunc(k8s.NewLogRequestor(clientset, functionNamespace), cfg.FaaSConfig.WriteTimeout),
ListNamespaceHandler: handlers.MakeNamespacesLister(functionNamespace, clientset),
}
bootstrap.Serve(&bootstrapHandlers, &cfg.FaaSConfig)
}