-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
122 lines (101 loc) · 3.58 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
package main
import (
"context"
"fmt"
"github.com/giantswarm/microerror"
"github.com/giantswarm/microkit/command"
microserver "github.com/giantswarm/microkit/server"
"github.com/giantswarm/micrologger"
"github.com/giantswarm/versionbundle"
"github.com/spf13/viper"
"github.com/giantswarm/etcd-kubernetes-resources-count-exporter/flag"
"github.com/giantswarm/etcd-kubernetes-resources-count-exporter/pkg/project"
"github.com/giantswarm/etcd-kubernetes-resources-count-exporter/server"
"github.com/giantswarm/etcd-kubernetes-resources-count-exporter/service"
)
var (
f = flag.New()
)
func main() {
err := mainError()
if err != nil {
panic(fmt.Sprintf("%#v\n", err))
}
}
func mainError() error {
var err error
ctx := context.Background()
logger, err := micrologger.New(micrologger.Config{})
if err != nil {
return microerror.Mask(err)
}
// We define a server factory to create the custom server once all command
// line flags are parsed and all microservice configuration is sorted out.
serverFactory := func(v *viper.Viper) microserver.Server {
// Create a new custom service which implements business logic.
var newService *service.Service
{
c := service.Config{
Flag: f,
Logger: logger,
Viper: v,
Description: project.Description(),
GitCommit: project.GitSHA(),
ProjectName: project.Name(),
Source: project.Source(),
Version: project.Version(),
}
newService, err = service.New(c)
if err != nil {
panic(fmt.Sprintf("%#v", microerror.Mask(err)))
}
go newService.Boot(ctx)
}
// Create a new custom server which bundles our endpoints.
var newServer microserver.Server
{
c := server.Config{
Logger: logger,
Service: newService,
Viper: v,
ProjectName: project.Name(),
}
newServer, err = server.New(c)
if err != nil {
panic(fmt.Sprintf("%#v", microerror.Mask(err)))
}
}
return newServer
}
// Create a new microkit command which manages our custom microservice.
var newCommand command.Command
{
c := command.Config{
Logger: logger,
ServerFactory: serverFactory,
Description: project.Description(),
GitCommit: project.GitSHA(),
Name: project.Name(),
Source: project.Source(),
Version: project.Version(),
VersionBundles: []versionbundle.Bundle{project.NewVersionBundle()},
}
newCommand, err = command.New(c)
if err != nil {
return microerror.Mask(err)
}
}
daemonCommand := newCommand.DaemonCommand().CobraCommand()
daemonCommand.PersistentFlags().StringArray(f.Service.Etcd.Endpoints, []string{"https://127.0.0.1:2379"}, "endpoint to connect to etcd. Can be repeated multiple times for multiple endpoints. Default: https://127.0.0.1:2379")
daemonCommand.PersistentFlags().String(f.Service.Etcd.CaCertPath, "", "path of the CA certificate file for tls validation.")
daemonCommand.PersistentFlags().String(f.Service.Etcd.KeyPath, "", "path of the client key file for tls authentication.")
daemonCommand.PersistentFlags().String(f.Service.Etcd.CertPath, "", "path of the client certificate file for tls authentication.")
daemonCommand.PersistentFlags().Int(f.Service.Etcd.DialTimeout, 10, "dial timeout in seconds for connecting to etcd.")
daemonCommand.PersistentFlags().String(f.Service.Etcd.Prefix, "", "prefix used to store k8s data in etcd as specified in the '--etcd-prefix' flag of API server.")
daemonCommand.PersistentFlags().String(f.Service.Events.Prefix, "", "prefix used to store event data in etcd")
err = newCommand.CobraCommand().Execute()
if err != nil {
return microerror.Mask(err)
}
return nil
}