forked from projectcontour/contour
/
contour.go
191 lines (166 loc) · 6.05 KB
/
contour.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
// Copyright © 2017 Heptio
// 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"
"flag"
"fmt"
"io"
"net"
"net/http"
"os"
"path/filepath"
"time"
kingpin "gopkg.in/alecthomas/kingpin.v2"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"github.com/gorilla/handlers"
"github.com/heptio/contour/internal/contour"
"github.com/heptio/contour/internal/envoy"
"github.com/heptio/contour/internal/grpc"
"github.com/heptio/contour/internal/json"
"github.com/heptio/contour/internal/k8s"
"github.com/heptio/contour/internal/log/stdlog"
"github.com/heptio/contour/internal/workgroup"
)
const (
V1_API_ADDRESS = "127.0.0.1:8000" // v1 JSON RPC
V2_API_ADDRESS = "127.0.0.1:8001" // v2 gRPC
)
// this is necessary due to #113 wherein glog neccessitates a call to flag.Parse
// before any logging statements can be invoked. (See also https://github.com/golang/glog/blob/master/glog.go#L679)
// unsure why this seemingly unnecessary prerequisite is in place but there must be some sane reason.
func init() {
flag.Parse()
}
func main() {
logger := stdlog.New(os.Stdout, os.Stderr, 0)
t := &contour.Translator{
Logger: logger.WithPrefix("translator"),
}
app := kingpin.New("contour", "Heptio Contour Kubernetes ingress controller.")
bootstrap := app.Command("bootstrap", "Generate bootstrap configuration.")
var config envoy.ConfigWriter
path := bootstrap.Arg("path", "Configuration file.").Required().String()
bootstrap.Flag("admin-address", "Envoy admin interface address").StringVar(&config.AdminAddress)
bootstrap.Flag("admin-port", "Envoy admin interface port").IntVar(&config.AdminPort)
serve := app.Command("serve", "Serve xDS API traffic")
inCluster := serve.Flag("incluster", "use in cluster configuration.").Bool()
kubeconfig := serve.Flag("kubeconfig", "path to kubeconfig (if not in running inside a cluster)").Default(filepath.Join(os.Getenv("HOME"), ".kube", "config")).String()
debug := serve.Flag("debug", "enable v1 REST API request logging.").Bool()
// translator configuration
serve.Flag("envoy-http-address", "Envoy HTTP listener address").StringVar(&t.HTTPAddress)
serve.Flag("envoy-https-address", "Envoy HTTPS listener address").StringVar(&t.HTTPSAddress)
serve.Flag("envoy-http-port", "Envoy HTTP listener port").IntVar(&t.HTTPPort)
serve.Flag("envoy-https-port", "Envoy HTTPS listener port").IntVar(&t.HTTPSPort)
serve.Flag("use-proxy-protocol", "Use PROXY protocol for all listeners").BoolVar(&t.UseProxyProto)
args := os.Args[1:]
switch kingpin.MustParse(app.Parse(args)) {
default:
app.Usage(args)
os.Exit(2)
case bootstrap.FullCommand():
writeBootstrapConfig(&config, *path)
case serve.FullCommand():
logger.Infof("args: %v", args)
var g workgroup.Group
// buffer notifications to t to ensure they are handled sequentially.
buf := k8s.NewBuffer(&g, t, logger, 128)
// REST v1 support
ds := json.DataSource{
Logger: logger.WithPrefix("DataSource"),
}
client := newClient(*kubeconfig, *inCluster)
k8s.WatchServices(&g, client, logger, &ds, buf)
k8s.WatchEndpoints(&g, client, logger, &ds, buf)
k8s.WatchIngress(&g, client, logger, &ds, buf)
k8s.WatchSecrets(&g, client, logger, buf) // don't deliver to &ds, the rest api doesn't know how to process secrets
g.Add(func(stop <-chan struct{}) {
logger := logger.WithPrefix("JSONAPI")
api := json.NewAPI(logger, &ds)
if *debug {
// enable request logging if --debug enabled
api = handlers.LoggingHandler(os.Stdout, api)
}
srv := &http.Server{
Handler: api,
Addr: V1_API_ADDRESS,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
go srv.ListenAndServe() // run server in another goroutine
logger.Infof("started, listening on %v", srv.Addr)
defer logger.Infof("stopped")
<-stop // wait for stop signal
srv.Shutdown(context.Background()) // shutdown and wait for server to exit
})
g.Add(func(stop <-chan struct{}) {
logger := logger.WithPrefix("gRPCAPI")
l, err := net.Listen("tcp", V2_API_ADDRESS)
if err != nil {
logger.Errorf("could not listen on %s: %v", V2_API_ADDRESS, err)
return // TODO(dfc) should return the error not log it
}
s := grpc.NewAPI(logger, t)
logger.Infof("started")
defer logger.Infof("stopped")
s.Serve(l)
})
g.Run()
}
}
type configWriter interface {
WriteJSON(io.Writer) error
WriteYAML(io.Writer) error
}
// writeBootstrapConfig writes a bootstrap configuration to the supplied path.
// If the path ends in .json, the configuration file will be in v1 JSON format.
// If the path ends in .yaml, the configuration file will be in v2 YAML format.
func writeBootstrapConfig(config configWriter, path string) {
f, err := os.Create(path)
check(err)
switch filepath.Ext(path) {
case ".json":
fmt.Fprintf(os.Stderr, "WARNING: JSON support is deprecated in Contour 0.3 and will be removed in Contour 0.4")
err = config.WriteJSON(f)
check(err)
case ".yaml":
err = config.WriteYAML(f)
check(err)
default:
f.Close()
check(fmt.Errorf("path %s must end in one of .json or .yaml", path))
}
check(f.Close())
}
func newClient(kubeconfig string, inCluster bool) *kubernetes.Clientset {
var err error
var config *rest.Config
if kubeconfig != "" && !inCluster {
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
check(err)
} else {
config, err = rest.InClusterConfig()
check(err)
}
client, err := kubernetes.NewForConfig(config)
check(err)
return client
}
func check(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}