-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
106 lines (94 loc) · 4.14 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
package main
import (
"errors"
"flag"
"fmt"
"github.com/Peripli/istio-broker-proxy/pkg/router"
"istio.io/istio/pkg/log"
"net/url"
)
var producerInterceptor router.ProducerInterceptor
var consumerInterceptor router.ConsumerInterceptor
var routerConfig router.Config
var serviceNamePrefix string
var networkProfile string
var configStore string
var logLevel int
var version string
func newConfigStore(configStoreURL string) (router.ConfigStore, error) {
uri, err := url.Parse(configStoreURL)
if err != nil {
return nil, err
}
switch uri.Scheme {
case "k8s":
return router.NewInClusterConfigStore(), nil
case "file":
return router.NewFileConfigStore(uri.Path), nil
default:
return nil, errors.New("Invalid schema for config store:" + uri.Scheme)
}
}
func newConfigStoreOrFail(configStoreURL string) router.ConfigStore {
store, err := newConfigStore(configStoreURL)
if err != nil {
panic(err)
}
return store
}
func main() {
SetupConfiguration()
flag.Parse()
configureLogging()
engine := router.SetupRouterWithVersion(configureInterceptor(newConfigStoreOrFail), routerConfig, version)
engine.Run(fmt.Sprintf(":%d", routerConfig.Port))
}
func configureInterceptor(configStoreFactory func(configStoreUrl string) router.ConfigStore) router.ServiceBrokerInterceptor {
if networkProfile == "" {
panic("networkProfile not configured")
}
log.Infof("Running on port %d\n", routerConfig.Port)
var interceptor router.ServiceBrokerInterceptor
if consumerInterceptor.ConsumerID != "" {
consumerInterceptor.ServiceNamePrefix = serviceNamePrefix
consumerInterceptor.NetworkProfile = networkProfile
consumerInterceptor.ConfigStore = configStoreFactory(configStore)
interceptor = consumerInterceptor
} else if producerInterceptor.ProviderID != "" {
producerInterceptor.ServiceNamePrefix = serviceNamePrefix
producerInterceptor.NetworkProfile = networkProfile
producerInterceptor.ConfigStore = configStoreFactory(configStore)
err := producerInterceptor.WriteIstioConfigFiles(routerConfig.Port)
if err != nil {
panic(fmt.Sprintf("unable to write istio-broker provider side configuration file: %v", err))
}
interceptor = producerInterceptor
} else {
interceptor = router.NewNoOpInterceptor()
}
return interceptor
}
func configureLogging() {
options := log.DefaultOptions()
options.SetOutputLevel(log.DefaultScopeName, log.Level(logLevel))
options.SetStackTraceLevel(log.DefaultScopeName, log.FatalLevel)
log.Configure(options)
outLevel, _ := options.GetOutputLevel(log.DefaultScopeName)
log.Infof("Log level is set to %#v", outLevel)
}
// SetupConfiguration sets up the configuration (e.g. initializing the available command line parameters)
func SetupConfiguration() {
flag.IntVar(&logLevel, "logLevel", 4, "Log level (0=None, 1=Fatal, 2=Error, 3=Warn, 4=Info, 5=Debug")
flag.StringVar(&producerInterceptor.SystemDomain, "systemdomain", "", "system domain of the landscape")
flag.StringVar(&producerInterceptor.ProviderID, "providerId", "", "The subject alternative name of the provider for which the service has a certificate")
flag.IntVar(&producerInterceptor.LoadBalancerPort, "loadBalancerPort", 9000, "port of the load balancer of the landscape")
flag.StringVar(&configStore, "configStore", "k8s://", "URL to store the istio configuration files. Use 'k8s://' to store the configuration to kubernetes")
flag.StringVar(&producerInterceptor.IPAddress, "ipAddress", "127.0.0.1", "IP address of ingress")
flag.StringVar(&producerInterceptor.PlanMetaData, "planMetaData", "{}", "Metadata which is added to each service")
flag.StringVar(&networkProfile, "networkProfile", "", "Network profile e.g. urn:local.test:public")
flag.StringVar(&consumerInterceptor.ConsumerID, "consumerId", "", "The subject alternative name of the consumer for which the service has a certificate")
flag.StringVar(&routerConfig.ForwardURL, "forwardUrl", "", "url for forwarding incoming requests")
flag.BoolVar(&routerConfig.SkipVerifyTLS, "skipVerifyTLS", false, "Do not verify the certificate of the forwardUrl")
flag.IntVar(&routerConfig.Port, "port", router.DefaultPort, "Server listen port")
flag.StringVar(&serviceNamePrefix, "serviceNamePrefix", "", "Service name prefix")
}