-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
124 lines (102 loc) · 3.27 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
package main
import (
"context"
"flag"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/openfaas-incubator/vcenter-connector/pkg/events"
ofsdk "github.com/openfaas-incubator/connector-sdk/types"
"github.com/openfaas/faas-provider/auth"
"github.com/openfaas/openfaas-cloud/sdk"
)
const (
topicDelimiter = ","
)
func main() {
var gatewayURL string
var vcenterURL string
var vcUser string
var vcPass string
var vcUserSecret string
var vcPasswordSecret string
var insecure bool
// TODO: add option to configure log verbosity
flag.StringVar(&gatewayURL, "gateway", "http://127.0.0.1:8080", "URL for OpenFaaS gateway")
flag.StringVar(&vcenterURL, "vcenter", "http://127.0.0.1:8989/sdk", "URL for vCenter")
flag.StringVar(&vcUser, "vc-user", "", "User to connect to vCenter")
flag.StringVar(&vcPass, "vc-pass", "", "Password to connect to vCenter")
flag.StringVar(&vcUserSecret, "vc-user-secret-name", "", "Secret file to use for username")
flag.StringVar(&vcPasswordSecret, "vc-pass-secret-name", "", "Secret file to use for password")
flag.BoolVar(&insecure, "insecure", false, "use an insecure connection to vCenter (default false)")
flag.Parse()
if len(vcenterURL) == 0 {
log.Fatal("vcenterURL not provided")
}
if len(vcUserSecret) > 0 {
val, err := sdk.ReadSecret(vcUserSecret)
if err != nil {
panic(err.Error())
}
vcUser = val
}
if len(vcPasswordSecret) > 0 {
val, err := sdk.ReadSecret(vcPasswordSecret)
if err != nil {
panic(err.Error())
}
vcPass = val
}
vcenterClient, err := events.NewVCenterClient(context.Background(), vcUser, vcPass, vcenterURL, insecure)
if err != nil {
log.Fatalf("could not connect to vCenter: %v", err)
}
// OpenFaaS credentials for the connector
var credentials *auth.BasicAuthCredentials
if val, ok := os.LookupEnv("basic_auth"); ok && len(val) > 0 {
if val == "true" || val == "1" {
reader := auth.ReadBasicAuthFromDisk{}
if val, ok := os.LookupEnv("secret_mount_path"); ok && len(val) > 0 {
reader.SecretMountPath = os.Getenv("secret_mount_path")
}
res, err := reader.Read()
if err != nil {
log.Fatalf("could not read credentials: %v", err)
}
credentials = res
}
}
// OpenFaaS connector SDK controller configuration
ofconfig := ofsdk.ControllerConfig{
GatewayURL: gatewayURL,
TopicAnnotationDelimiter: topicDelimiter,
RebuildInterval: time.Second * 10,
UpstreamTimeout: time.Second * 15,
AsyncFunctionInvocation: true, // don't block when invoking long-running/heavy functions, higher throughput
PrintSync: true,
}
ofcontroller := ofsdk.NewController(credentials, &ofconfig)
responseHandler := events.NewEventReceiver()
ofcontroller.Subscribe(responseHandler)
ofcontroller.BeginMapBuilder()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// signal handling
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGTERM, os.Interrupt)
go func() {
s := <-sigCh
log.Printf("got signal: %v, cleaning up...", s)
cancel()
// give subroutines some time to finish their work
<-time.Tick(3 * time.Second)
os.Exit(0)
}()
// blocks until eventStream returns
err = events.Stream(ctx, vcenterClient.Client, ofcontroller)
if err != nil {
log.Fatalf("could not bind events: %v", err)
}
}