-
Notifications
You must be signed in to change notification settings - Fork 433
/
main_setup.go
125 lines (111 loc) · 3.94 KB
/
main_setup.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
package setuputils
import (
"context"
"flag"
"sync"
"time"
"github.com/solo-io/gloo/pkg/utils/settingsutil"
"github.com/solo-io/gloo/pkg/utils/usage"
"github.com/solo-io/gloo/pkg/version"
v1 "github.com/solo-io/gloo/projects/gloo/pkg/api/v1"
"github.com/solo-io/go-utils/contextutils"
"github.com/solo-io/go-utils/kubeutils"
"github.com/solo-io/reporting-client/pkg/client"
"github.com/solo-io/reporting-client/pkg/signature"
"github.com/solo-io/solo-kit/pkg/api/v1/clients"
"github.com/solo-io/solo-kit/pkg/api/v1/clients/factory"
"github.com/solo-io/solo-kit/pkg/api/v1/clients/kube"
"github.com/solo-io/solo-kit/pkg/api/v1/resources/core"
"go.uber.org/zap"
_ "k8s.io/client-go/plugin/pkg/client/auth"
)
type SetupOpts struct {
LoggerName string
// logged as the version of Gloo currently executing
Version string
SetupFunc SetupFunc
ExitOnError bool
CustomCtx context.Context
// optional - if present, add these values in each JSON log line in the gloo pod.
// By default, we already log the gloo version.
LoggingPrefixVals []interface{}
// optional - if present, report usage with the payload this discovers
// should really only provide it in very intentional places- in the gloo pod, and in glooctl
// otherwise, we'll provide redundant copies of the usage data
UsageReporter client.UsagePayloadReader
}
var once sync.Once
func Main(opts SetupOpts) error {
// prevent panic if multiple flag.Parse called concurrently
once.Do(func() {
flag.Parse()
})
ctx := opts.CustomCtx
if ctx == nil {
ctx = context.Background()
}
ctx = contextutils.WithLogger(ctx, opts.LoggerName)
loggingContext := append([]interface{}{"version", opts.Version}, opts.LoggingPrefixVals...)
ctx = contextutils.WithLoggerValues(ctx, loggingContext...)
if opts.UsageReporter != nil {
go func() {
signatureManager := signature.NewSignatureManager()
errs := StartReportingUsage(opts.CustomCtx, opts.UsageReporter, opts.LoggerName, signatureManager)
for err := range errs {
contextutils.LoggerFrom(ctx).Warnw("Error while reporting usage", zap.Error(err))
}
}()
}
settingsClient, err := kubeOrFileSettingsClient(ctx, setupNamespace, setupDir)
if err != nil {
return err
}
if err := settingsClient.Register(); err != nil {
return err
}
emitter := v1.NewSetupEmitter(settingsClient)
settingsRef := core.ResourceRef{Namespace: setupNamespace, Name: setupName}
eventLoop := v1.NewSetupEventLoop(emitter, NewSetupSyncer(settingsRef, opts.SetupFunc))
errs, err := eventLoop.Run([]string{setupNamespace}, clients.WatchOpts{
Ctx: ctx,
RefreshRate: time.Second,
})
if err != nil {
return err
}
for err := range errs {
if opts.ExitOnError {
contextutils.LoggerFrom(ctx).Fatalf("error in setup: %v", err)
}
contextutils.LoggerFrom(ctx).Errorf("error in setup: %v", err)
}
return nil
}
func kubeOrFileSettingsClient(ctx context.Context, setupNamespace, settingsDir string) (v1.SettingsClient, error) {
if settingsDir != "" {
contextutils.LoggerFrom(ctx).Infow("using filesystem for settings", zap.String("directory", settingsDir))
return v1.NewSettingsClient(&factory.FileResourceClientFactory{
RootDir: settingsDir,
})
}
cfg, err := kubeutils.GetConfig("", "")
if err != nil {
return nil, err
}
return v1.NewSettingsClient(&factory.KubeResourceClientFactory{
Crd: v1.SettingsCrd,
Cfg: cfg,
SharedCache: kube.NewKubeCache(ctx),
NamespaceWhitelist: []string{setupNamespace},
SkipCrdCreation: settingsutil.GetSkipCrdCreation(),
})
}
// does not block the current goroutine
func StartReportingUsage(ctx context.Context, usagePayloadReader client.UsagePayloadReader, product string, signatureManager signature.SignatureManager) <-chan error {
usageClient := client.NewUsageClient(
usage.ReportingServiceUrl,
usagePayloadReader,
usage.BuildProductMetadata(product, version.Version),
signatureManager)
return usageClient.StartReportingUsage(ctx, usage.ReportingPeriod)
}