forked from solo-io/gloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_setup.go
122 lines (110 loc) · 3.66 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
package setuputils
import (
"context"
"flag"
"fmt"
"sync"
"time"
"github.com/solo-io/gloo/projects/gloo/pkg/defaults"
"github.com/gogo/protobuf/types"
"github.com/solo-io/gloo/pkg/utils/settingsutil"
"github.com/solo-io/gloo/pkg/version"
v1 "github.com/solo-io/gloo/projects/gloo/pkg/api/v1"
check "github.com/solo-io/go-checkpoint"
"github.com/solo-io/go-utils/contextutils"
"github.com/solo-io/go-utils/kubeutils"
"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"
"github.com/solo-io/solo-kit/pkg/errors"
_ "k8s.io/client-go/plugin/pkg/client/auth"
)
type SetupOpts struct {
LoggingPrefix string
SetupFunc SetupFunc
ExitOnError bool
CustomCtx context.Context
}
var once sync.Once
func Main(opts SetupOpts) error {
loggingPrefix := opts.LoggingPrefix
check.NewUsageClient().Start(loggingPrefix, version.Version)
// 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, loggingPrefix)
settingsClient, err := kubeOrFileSettingsClient(ctx, setupNamespace, setupDir)
if err != nil {
return err
}
if err := settingsClient.Register(); err != nil {
return err
}
if err := writeDefaultSettings(setupNamespace, setupName, settingsClient); 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 != "" {
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(),
})
}
// TODO(ilackarms): remove this or move it to a test package, only use settings watch for production gloo
func writeDefaultSettings(defaultNamespace, name string, cli v1.SettingsClient) error {
settings := &v1.Settings{
ConfigSource: &v1.Settings_KubernetesConfigSource{
KubernetesConfigSource: &v1.Settings_KubernetesCrds{},
},
ArtifactSource: &v1.Settings_KubernetesArtifactSource{
KubernetesArtifactSource: &v1.Settings_KubernetesConfigmaps{},
},
SecretSource: &v1.Settings_KubernetesSecretSource{
KubernetesSecretSource: &v1.Settings_KubernetesSecrets{},
},
BindAddr: fmt.Sprintf("0.0.0.0:%v", defaults.GlooXdsPort),
RefreshRate: types.DurationProto(time.Minute),
DevMode: true,
DiscoveryNamespace: defaultNamespace,
Metadata: core.Metadata{Namespace: defaultNamespace, Name: name},
}
if _, err := cli.Write(settings, clients.WriteOpts{}); err != nil && !errors.IsExist(err) {
return errors.Wrapf(err, "failed to create default settings")
}
return nil
}