-
Notifications
You must be signed in to change notification settings - Fork 688
/
main.go
207 lines (173 loc) · 6.33 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package watt
import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/datawire/ambassador/cmd/watt/aggregator"
"github.com/datawire/ambassador/cmd/watt/invoker"
"github.com/datawire/ambassador/cmd/watt/thingconsul"
"github.com/datawire/ambassador/cmd/watt/thingkube"
"github.com/datawire/ambassador/cmd/watt/watchapi"
"github.com/datawire/ambassador/pkg/k8s"
"github.com/datawire/ambassador/pkg/kates"
"github.com/datawire/ambassador/pkg/limiter"
"github.com/datawire/ambassador/pkg/supervisor"
"github.com/datawire/dlib/dlog"
)
type wattFlags struct {
kubernetesNamespace string
initialSources []string
initialFieldSelector string
initialLabelSelector string
watchHooks []string
notifyReceivers []string
listenNetwork string
listenAddress string
legacyListenPort int
interval time.Duration
showVersion bool
}
func Main(ctx context.Context, Version string, args ...string) error {
var flags wattFlags
rootCmd := &cobra.Command{
Use: "watt",
Short: "watt - watch all the things",
SilenceErrors: true, // we'll handle it after .ExecuteContext() returns
SilenceUsage: true, // our FlagErrorFunc will handle it
}
rootCmd.Flags().StringVarP(&flags.kubernetesNamespace, "namespace", "n", "", "namespace to watch (default: all)")
rootCmd.Flags().StringSliceVarP(&flags.initialSources, "source", "s", []string{}, "configure an initial static source")
rootCmd.Flags().StringVar(&flags.initialFieldSelector, "fields", "", "configure an initial field selector string")
rootCmd.Flags().StringVar(&flags.initialLabelSelector, "labels", "", "configure an initial label selector string")
rootCmd.Flags().StringSliceVarP(&flags.watchHooks, "watch", "w", []string{}, "configure watch hook(s)")
rootCmd.Flags().StringSliceVar(&flags.notifyReceivers, "notify", []string{},
"invoke the program with the given arguments as a receiver")
rootCmd.Flags().DurationVarP(&flags.interval, "interval", "i", 250*time.Millisecond,
"configure the rate limit interval")
rootCmd.Flags().BoolVarP(&flags.showVersion, "version", "", false, "display version information")
rootCmd.Flags().StringVar(&flags.listenNetwork, "listen-network", "tcp", "network for the snapshot server to listen on")
rootCmd.Flags().StringVar(&flags.listenAddress, "listen-address", ":7000", "address (on --listen-network) for the snapshot server to listen on")
rootCmd.Flags().IntVarP(&flags.legacyListenPort, "port", "p", 0, "configure the snapshot server port")
rootCmd.Flags().MarkHidden("port")
rootCmd.RunE = func(cmd *cobra.Command, _ []string) error {
return runWatt(cmd.Context(), Version, flags)
}
rootCmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
if err == nil {
return nil
}
fmt.Fprintf(os.Stderr, "%s\nSee '%s --help'.\n", err, cmd.CommandPath())
os.Exit(2)
return nil
})
rootCmd.SetArgs(args)
return rootCmd.ExecuteContext(ctx)
}
func runWatt(ctx context.Context, Version string, flags wattFlags) error {
if flags.showVersion {
fmt.Println("watt", Version)
return nil
}
if flags.legacyListenPort != 0 {
flags.listenAddress = fmt.Sprintf(":%v", flags.legacyListenPort)
}
if len(flags.initialSources) == 0 {
return errors.New("no initial sources configured")
}
// XXX: we don't need to create this here anymore
client, err := k8s.NewClient(nil)
if err != nil {
return err
}
kubeAPIWatcher := client.Watcher()
/*for idx := range initialSources {
initialSources[idx] = kubeAPIWatcher.Canonical(initialSources[idx])
}*/
dlog.Printf(ctx, "starting watt...")
// The aggregator sends the current consul resolver set to the
// consul watch manager.
aggregatorToConsulwatchmanCh := make(chan []watchapi.ConsulWatchSpec, 100)
// The aggregator sends the current k8s watch set to the
// kubernetes watch manager.
aggregatorToKubewatchmanCh := make(chan []watchapi.KubernetesWatchSpec, 100)
apiServerAuthority := flags.listenAddress
if strings.HasPrefix(apiServerAuthority, ":") {
apiServerAuthority = "localhost" + apiServerAuthority
}
invokerObj := invoker.NewInvoker(apiServerAuthority, flags.notifyReceivers)
limiter := limiter.NewComposite(limiter.NewUnlimited(), limiter.NewInterval(flags.interval), flags.interval)
crdYAML, err := ioutil.ReadFile("/opt/ambassador/etc/crds.yaml")
if err != nil {
return err
}
crdObjs, err := kates.ParseManifests(string(crdYAML))
if err != nil {
return err
}
validator, err := kates.NewValidator(nil, crdObjs)
if err != nil {
return err
}
aggregator := aggregator.NewAggregator(invokerObj.Snapshots, aggregatorToKubewatchmanCh, aggregatorToConsulwatchmanCh,
flags.initialSources, aggregator.ExecWatchHook(flags.watchHooks), limiter, validator)
kubebootstrap := thingkube.NewKubeBootstrap(
flags.kubernetesNamespace, // namespace
flags.initialSources, // kinds
flags.initialFieldSelector, // fieldSelector
flags.initialLabelSelector, // labelSelector
[]chan<- thingkube.K8sEvent{aggregator.KubernetesEvents}, // notify
kubeAPIWatcher, // kubeAPIWatcher
)
consulwatchman := thingconsul.NewConsulWatchMan(
aggregator.ConsulEvents,
aggregatorToConsulwatchmanCh,
)
kubewatchman := thingkube.NewKubeWatchMan(
client, // k8s client
aggregator.KubernetesEvents, // eventsCh
aggregatorToKubewatchmanCh, // watchesCh
)
apiServer := invoker.NewAPIServer(
flags.listenNetwork,
flags.listenAddress,
invokerObj,
)
s := supervisor.WithContext(ctx)
s.Supervise(&supervisor.Worker{
Name: "kubebootstrap",
Work: kubebootstrap.Work,
})
s.Supervise(&supervisor.Worker{
Name: "consulwatchman",
Work: consulwatchman.Work,
})
s.Supervise(&supervisor.Worker{
Name: "kubewatchman",
Work: kubewatchman.Work,
})
s.Supervise(&supervisor.Worker{
Name: "aggregator",
Work: aggregator.Work,
})
s.Supervise(&supervisor.Worker{
Name: "invoker",
Work: invokerObj.Work,
})
s.Supervise(&supervisor.Worker{
Name: "api",
Work: apiServer.Work,
})
if errs := s.Run(); len(errs) > 0 {
msgs := []string{}
for _, err := range errs {
msgs = append(msgs, err.Error())
}
return fmt.Errorf("ERROR(s): %s", strings.Join(msgs, "\n "))
}
return nil
}