forked from openfaas/faas-netes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
257 lines (207 loc) · 8.61 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// 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-netes/handlers"
"github.com/openfaas/faas-netes/k8s"
clientset "github.com/openfaas/faas-netes/pkg/client/clientset/versioned"
informers "github.com/openfaas/faas-netes/pkg/client/informers/externalversions"
"github.com/openfaas/faas-netes/pkg/controller"
"github.com/openfaas/faas-netes/pkg/server"
"github.com/openfaas/faas-netes/pkg/signals"
"github.com/openfaas/faas-netes/types"
version "github.com/openfaas/faas-netes/version"
faasProvider "github.com/openfaas/faas-provider"
"github.com/openfaas/faas-provider/logs"
"github.com/openfaas/faas-provider/proxy"
providertypes "github.com/openfaas/faas-provider/types"
kubeinformers "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
glog "k8s.io/klog"
// required to authenticate against GKE clusters
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
// required for updating and validating the CRD clientset
_ "k8s.io/code-generator/cmd/client-gen/generators"
)
var pullPolicyOptions = map[string]bool{
"Always": true,
"IfNotPresent": true,
"Never": true,
}
func main() {
var kubeconfig string
var masterURL string
var operator bool
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.BoolVar(&operator, "operator", false, "Use the operator mode instead of faas-netes")
flag.Parse()
if !operator {
runController(kubeconfig, masterURL)
} else {
runOperator(kubeconfig, masterURL)
}
}
// runController runs the faas-netes imperative controller
func runController(kubeconfig, masterURL string) {
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 := providertypes.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)
sha, release := version.GetReleaseInfo()
log.Printf("Starting operator. Version: %s\tcommit: %s", release, sha)
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 := providertypes.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),
}
faasProvider.Serve(&bootstrapHandlers, &cfg.FaaSConfig)
}
// runOperator runs the CRD Operator
func runOperator(kubeconfig, masterURL string) {
setupLogging()
sha, release := version.GetReleaseInfo()
glog.Infof("Starting operator. Version: %s\tcommit: %s", release, sha)
// set up signals so we handle the first shutdown signal gracefully
stopCh := signals.SetupSignalHandler()
cfg, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfig)
if err != nil {
glog.Fatalf("Error building kubeconfig: %s", err.Error())
}
kubeClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
glog.Fatalf("Error building Kubernetes clientset: %s", err.Error())
}
faasClient, err := clientset.NewForConfig(cfg)
if err != nil {
glog.Fatalf("Error building OpenFaaS clientset: %s", err.Error())
}
readConfig := types.ReadConfig{}
osEnv := providertypes.OsEnv{}
config, err := readConfig.Read(osEnv)
if err != nil {
panic(err)
}
deployConfig := k8s.DeploymentConfig{
RuntimeHTTPPort: 8080,
HTTPProbe: config.HTTPProbe,
SetNonRootUser: config.SetNonRootUser,
ReadinessProbe: &k8s.ProbeConfig{
InitialDelaySeconds: int32(config.ReadinessProbeInitialDelaySeconds),
TimeoutSeconds: int32(config.ReadinessProbeTimeoutSeconds),
PeriodSeconds: int32(config.ReadinessProbePeriodSeconds),
},
LivenessProbe: &k8s.ProbeConfig{
InitialDelaySeconds: int32(config.LivenessProbeInitialDelaySeconds),
TimeoutSeconds: int32(config.LivenessProbeTimeoutSeconds),
PeriodSeconds: int32(config.LivenessProbePeriodSeconds),
},
ImagePullPolicy: config.ImagePullPolicy,
}
factory := controller.NewFunctionFactory(kubeClient, deployConfig)
functionNamespace := "openfaas-fn"
if namespace, exists := os.LookupEnv("function_namespace"); exists {
functionNamespace = namespace
}
if !pullPolicyOptions[config.ImagePullPolicy] {
glog.Fatalf("Invalid image_pull_policy configured: %s", config.ImagePullPolicy)
}
// the sync interval does not affect the scale to/from zero feature
// auto-scaling is does via the HTTP API that acts on the deployment Spec.Replicas
defaultResync := time.Minute * 5
kubeInformerOpt := kubeinformers.WithNamespace(functionNamespace)
kubeInformerFactory := kubeinformers.NewSharedInformerFactoryWithOptions(kubeClient, defaultResync, kubeInformerOpt)
faasInformerOpt := informers.WithNamespace(functionNamespace)
faasInformerFactory := informers.NewSharedInformerFactoryWithOptions(faasClient, defaultResync, faasInformerOpt)
endpointsInformer := kubeInformerFactory.Core().V1().Endpoints()
deploymentInformer := kubeInformerFactory.Apps().V1().Deployments()
log.Printf("Waiting for cache sync in main")
kubeInformerFactory.WaitForCacheSync(stopCh)
log.Printf("Cache sync done")
ctrl := controller.NewController(
kubeClient,
faasClient,
kubeInformerFactory,
faasInformerFactory,
factory,
)
srv := server.New(faasClient, kubeClient, endpointsInformer, deploymentInformer)
go faasInformerFactory.Start(stopCh)
go kubeInformerFactory.Start(stopCh)
go srv.Start()
if err = ctrl.Run(1, stopCh); err != nil {
glog.Fatalf("Error running controller: %s", err.Error())
}
}
func setupLogging() {
klogFlags := flag.NewFlagSet("klog", flag.ExitOnError)
glog.InitFlags(klogFlags)
// Sync the glog and klog flags.
flag.CommandLine.VisitAll(func(f1 *flag.Flag) {
f2 := klogFlags.Lookup(f1.Name)
if f2 != nil {
value := f1.Value.String()
f2.Value.Set(value)
}
})
}