-
Notifications
You must be signed in to change notification settings - Fork 14
/
discovery.go
113 lines (93 loc) · 2.87 KB
/
discovery.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
// Copyright 2019 The etcd Authors
//
// 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 (
"context"
"fmt"
"log"
"net/http"
"net/url"
"os"
"time"
handling "go.etcd.io/discoveryserver/http"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
func fail(err string) {
log.Print(err)
pflag.PrintDefaults()
os.Exit(2) // default go flag error code
}
func mustHostOnlyURL(givenUrl string) string {
u, err := url.Parse(givenUrl)
if err != nil {
fail(fmt.Sprintf("Invalid url given: %v", err))
}
if len(u.Path) != 0 && u.Path != "/" {
fail(fmt.Sprintf("Expected url without path (%v)", u.Path))
}
if u.RawQuery != "" {
fail(fmt.Sprintf("Expected url without query (?%v)", u.RawQuery))
}
if u.Fragment != "" {
fail(fmt.Sprintf("Expected url without fragment (%v)", u.Fragment))
}
if u.Host == "" {
fail(fmt.Sprint("Expected hostname (none given)"))
}
return u.Scheme + "://" + u.Host
}
func init() {
viper.SetEnvPrefix("disc")
viper.AutomaticEnv()
pflag.StringP("etcd", "e", "http://127.0.0.1:2379", "etcd endpoint location")
pflag.StringP("host", "h", "https://discovery.etcd.io", "discovery url prefix")
pflag.StringP("addr", "a", ":8087", "web service address")
pflag.StringP("minage", "m", "168h", "min age of a token for garbage collection")
pflag.StringP("gcfreq", "g", "5m", "garbage collection frequency")
viper.BindPFlag("etcd", pflag.Lookup("etcd"))
viper.BindPFlag("host", pflag.Lookup("host"))
viper.BindPFlag("addr", pflag.Lookup("addr"))
viper.BindPFlag("minage", pflag.Lookup("minage"))
viper.BindPFlag("gcfreq", pflag.Lookup("gcfreq"))
pflag.Parse()
}
func main() {
log.SetFlags(0)
etcdHost := mustHostOnlyURL(viper.GetString("etcd"))
discHost := mustHostOnlyURL(viper.GetString("host"))
minAge, err := time.ParseDuration(viper.GetString("minage"))
if err != nil {
panic(err)
}
gcFreq, err := time.ParseDuration(viper.GetString("gcfreq"))
if err != nil {
panic(err)
}
webAddr := viper.GetString("addr")
state := handling.Setup(context.Background(), etcdHost, discHost)
go func() {
c := time.Tick(gcFreq)
for range c {
log.Printf("garbage collection running")
state.GarbageCollect(minAge, 10*minAge)
}
}()
log.Printf("discovery server started with etcd %q and host %q", etcdHost, discHost)
log.Printf("discovery serving on %s", webAddr)
err = http.ListenAndServe(webAddr, nil)
if err != nil {
panic(err)
}
}