forked from projectcalico/typha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
typha-client.go
106 lines (89 loc) · 3.13 KB
/
typha-client.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
// Copyright (c) 2017-2018 Tigera, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"time"
log "github.com/sirupsen/logrus"
"math/rand"
"runtime"
"github.com/docopt/docopt-go"
"context"
"github.com/projectcalico/libcalico-go/lib/backend/api"
"github.com/projectcalico/typha/pkg/buildinfo"
"github.com/projectcalico/typha/pkg/config"
"github.com/projectcalico/typha/pkg/logutils"
"github.com/projectcalico/typha/pkg/syncclient"
"github.com/projectcalico/typha/pkg/syncproto"
)
const usage = `Test client for Typha, Calico's fan-out proxy.
Usage:
typha-client [options]
Options:
--version Print the version and exit.
--server=<ADDR> Set the server to connect to [default: localhost:5473].
--type=<TYPE> Use a particular syncer type.
`
type syncerCallbacks struct {
updateCount int
}
func (s *syncerCallbacks) OnStatusUpdated(status api.SyncStatus) {
log.WithField("status", status).Info("Status received")
}
func (s *syncerCallbacks) OnUpdates(updates []api.Update) {
s.updateCount += len(updates)
log.WithField("numUpdates", len(updates)).WithField("total", s.updateCount).Info("Updates received")
}
func main() {
// Go's RNG is not seeded by default. Do that now.
rand.Seed(time.Now().UTC().UnixNano())
// Set up logging.
logutils.ConfigureEarlyLogging()
logutils.ConfigureLogging(&config.Config{
LogSeverityScreen: "info",
DebugDisableLogDropping: true,
})
// Parse command-line args.
version := "Version: " + buildinfo.GitVersion + "\n" +
"Full git commit ID: " + buildinfo.GitRevision + "\n" +
"Build date: " + buildinfo.BuildDate + "\n"
arguments, err := docopt.Parse(usage, nil, true, version, false)
if err != nil {
println(usage)
log.Fatalf("Failed to parse usage, exiting: %v", err)
}
buildInfoLogCxt := log.WithFields(log.Fields{
"version": buildinfo.GitVersion,
"buildDate": buildinfo.BuildDate,
"gitCommit": buildinfo.GitRevision,
"GOMAXPROCS": runtime.GOMAXPROCS(0),
})
buildInfoLogCxt.Info("Typha starting up")
log.Infof("Command line arguments: %v", arguments)
callbacks := &syncerCallbacks{}
addr := arguments["--server"].(string)
var syncerType syncproto.SyncerType
if t, ok := arguments["--type"].(string); ok {
syncerType = syncproto.SyncerType(t)
}
options := &syncclient.Options{
SyncerType: syncerType,
}
client := syncclient.New(addr, buildinfo.GitVersion, "test-host", "some info", callbacks, options)
err = client.Start(context.Background())
if err != nil {
log.WithError(err).Panic("Client failed")
}
client.Finished.Wait()
log.Panic("Client failed")
}