forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
route.go
454 lines (395 loc) · 15.4 KB
/
route.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package create
import (
"fmt"
"io"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime/schema"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/resource"
cmdutil "github.com/openshift/origin/pkg/cmd/util"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
routeapi "github.com/openshift/origin/pkg/route/apis/route"
fileutil "github.com/openshift/origin/pkg/util/file"
)
var (
routeLong = templates.LongDesc(`
Expose containers externally via secured routes
Three types of secured routes are supported: edge, passthrough, and reencrypt.
If you wish to create unsecured routes, see "%[1]s expose -h"`)
)
// NewCmdCreateRoute is a macro command to create a secured route.
func NewCmdCreateRoute(fullName string, f *clientcmd.Factory, out, errOut io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "route",
Short: "Expose containers externally via secured routes",
Long: fmt.Sprintf(routeLong, fullName),
Run: kcmdutil.DefaultSubCommandRun(errOut),
}
cmd.AddCommand(NewCmdCreateEdgeRoute(fullName, f, out))
cmd.AddCommand(NewCmdCreatePassthroughRoute(fullName, f, out))
cmd.AddCommand(NewCmdCreateReencryptRoute(fullName, f, out))
return cmd
}
var (
edgeRouteLong = templates.LongDesc(`
Create a route that uses edge TLS termination
Specify the service (either just its name or using type/name syntax) that the
generated route should expose via the --service flag.`)
edgeRouteExample = templates.Examples(`
# Create an edge route named "my-route" that exposes frontend service.
%[1]s create route edge my-route --service=frontend
# Create an edge route that exposes the frontend service and specify a path.
# If the route name is omitted, the service name will be re-used.
%[1]s create route edge --service=frontend --path /assets`)
)
// NewCmdCreateEdgeRoute is a macro command to create an edge route.
func NewCmdCreateEdgeRoute(fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "edge [NAME] --service=SERVICE",
Short: "Create a route that uses edge TLS termination",
Long: edgeRouteLong,
Example: fmt.Sprintf(edgeRouteExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
err := CreateEdgeRoute(f, out, cmd, args)
kcmdutil.CheckErr(err)
},
}
kcmdutil.AddValidateFlags(cmd)
kcmdutil.AddPrinterFlags(cmd)
kcmdutil.AddDryRunFlag(cmd)
cmd.Flags().String("hostname", "", "Set a hostname for the new route")
cmd.Flags().String("port", "", "Name of the service port or number of the container port the route will route traffic to")
cmd.Flags().String("insecure-policy", "", "Set an insecure policy for the new route")
cmd.Flags().String("service", "", "Name of the service that the new route is exposing")
cmd.MarkFlagRequired("service")
cmd.Flags().String("path", "", "Path that the router watches to route traffic to the service.")
cmd.Flags().String("cert", "", "Path to a certificate file.")
cmd.MarkFlagFilename("cert")
cmd.Flags().String("key", "", "Path to a key file.")
cmd.MarkFlagFilename("key")
cmd.Flags().String("ca-cert", "", "Path to a CA certificate file.")
cmd.MarkFlagFilename("ca-cert")
cmd.Flags().String("wildcard-policy", "", "Sets the WilcardPolicy for the hostname, the default is \"None\". valid values are \"None\" and \"Subdomain\"")
return cmd
}
// CreateEdgeRoute implements the behavior to run the create edge route command.
func CreateEdgeRoute(f *clientcmd.Factory, out io.Writer, cmd *cobra.Command, args []string) error {
kc, err := f.ClientSet()
if err != nil {
return err
}
routeClient, err := f.OpenshiftInternalRouteClient()
if err != nil {
return err
}
ns, _, err := f.DefaultNamespace()
if err != nil {
return err
}
serviceName, err := resolveServiceName(f, kcmdutil.GetFlagString(cmd, "service"))
if err != nil {
return err
}
routeName, err := resolveRouteName(args)
if err != nil {
return err
}
route, err := cmdutil.UnsecuredRoute(kc, ns, routeName, serviceName, kcmdutil.GetFlagString(cmd, "port"), false)
if err != nil {
return err
}
wildcardpolicy := kcmdutil.GetFlagString(cmd, "wildcard-policy")
if len(wildcardpolicy) > 0 {
route.Spec.WildcardPolicy = routeapi.WildcardPolicyType(wildcardpolicy)
}
route.Spec.Host = kcmdutil.GetFlagString(cmd, "hostname")
route.Spec.Path = kcmdutil.GetFlagString(cmd, "path")
route.Spec.TLS = new(routeapi.TLSConfig)
route.Spec.TLS.Termination = routeapi.TLSTerminationEdge
cert, err := fileutil.LoadData(kcmdutil.GetFlagString(cmd, "cert"))
if err != nil {
return err
}
route.Spec.TLS.Certificate = string(cert)
key, err := fileutil.LoadData(kcmdutil.GetFlagString(cmd, "key"))
if err != nil {
return err
}
route.Spec.TLS.Key = string(key)
caCert, err := fileutil.LoadData(kcmdutil.GetFlagString(cmd, "ca-cert"))
if err != nil {
return err
}
route.Spec.TLS.CACertificate = string(caCert)
insecurePolicy := kcmdutil.GetFlagString(cmd, "insecure-policy")
if len(insecurePolicy) > 0 {
route.Spec.TLS.InsecureEdgeTerminationPolicy = routeapi.InsecureEdgeTerminationPolicyType(insecurePolicy)
}
dryRun := kcmdutil.GetFlagBool(cmd, "dry-run")
actualRoute := route
if !dryRun {
actualRoute, err = routeClient.Route().Routes(ns).Create(route)
if err != nil {
return err
}
}
mapper, typer := f.Object()
resourceMapper := &resource.Mapper{
ObjectTyper: typer,
RESTMapper: mapper,
ClientMapper: resource.ClientMapperFunc(f.ClientForMapping),
}
info, err := resourceMapper.InfoForObject(actualRoute, []schema.GroupVersionKind{{Group: ""}})
if err != nil {
return err
}
shortOutput := kcmdutil.GetFlagString(cmd, "output") == "name"
kcmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, dryRun, "created")
return nil
}
var (
passthroughRouteLong = templates.LongDesc(`
Create a route that uses passthrough TLS termination
Specify the service (either just its name or using type/name syntax) that the
generated route should expose via the --service flag.`)
passthroughRouteExample = templates.Examples(`
# Create a passthrough route named "my-route" that exposes the frontend service.
%[1]s create route passthrough my-route --service=frontend
# Create a passthrough route that exposes the frontend service and specify
# a hostname. If the route name is omitted, the service name will be re-used.
%[1]s create route passthrough --service=frontend --hostname=www.example.com`)
)
// NewCmdCreatePassthroughRoute is a macro command to create a passthrough route.
func NewCmdCreatePassthroughRoute(fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "passthrough [NAME] --service=SERVICE",
Short: "Create a route that uses passthrough TLS termination",
Long: passthroughRouteLong,
Example: fmt.Sprintf(passthroughRouteExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
err := CreatePassthroughRoute(f, out, cmd, args)
kcmdutil.CheckErr(err)
},
}
kcmdutil.AddValidateFlags(cmd)
kcmdutil.AddPrinterFlags(cmd)
kcmdutil.AddDryRunFlag(cmd)
cmd.Flags().String("hostname", "", "Set a hostname for the new route")
cmd.Flags().String("port", "", "Name of the service port or number of the container port the route will route traffic to")
cmd.Flags().String("insecure-policy", "", "Set an insecure policy for the new route")
cmd.Flags().String("service", "", "Name of the service that the new route is exposing")
cmd.MarkFlagRequired("service")
cmd.Flags().String("wildcard-policy", "", "Sets the WilcardPolicy for the hostname, the default is \"None\". valid values are \"None\" and \"Subdomain\"")
return cmd
}
// CreatePassthroughRoute implements the behavior to run the create passthrough route command.
func CreatePassthroughRoute(f *clientcmd.Factory, out io.Writer, cmd *cobra.Command, args []string) error {
kc, err := f.ClientSet()
if err != nil {
return err
}
routeClient, err := f.OpenshiftInternalRouteClient()
if err != nil {
return err
}
ns, _, err := f.DefaultNamespace()
if err != nil {
return err
}
serviceName, err := resolveServiceName(f, kcmdutil.GetFlagString(cmd, "service"))
if err != nil {
return err
}
routeName, err := resolveRouteName(args)
if err != nil {
return err
}
route, err := cmdutil.UnsecuredRoute(kc, ns, routeName, serviceName, kcmdutil.GetFlagString(cmd, "port"), false)
if err != nil {
return err
}
wildcardpolicy := kcmdutil.GetFlagString(cmd, "wildcard-policy")
if len(wildcardpolicy) > 0 {
route.Spec.WildcardPolicy = routeapi.WildcardPolicyType(wildcardpolicy)
}
route.Spec.Host = kcmdutil.GetFlagString(cmd, "hostname")
route.Spec.TLS = new(routeapi.TLSConfig)
route.Spec.TLS.Termination = routeapi.TLSTerminationPassthrough
insecurePolicy := kcmdutil.GetFlagString(cmd, "insecure-policy")
if len(insecurePolicy) > 0 {
route.Spec.TLS.InsecureEdgeTerminationPolicy = routeapi.InsecureEdgeTerminationPolicyType(insecurePolicy)
}
dryRun := kcmdutil.GetFlagBool(cmd, "dry-run")
actualRoute := route
if !dryRun {
actualRoute, err = routeClient.Route().Routes(ns).Create(route)
if err != nil {
return err
}
}
mapper, typer := f.Object()
resourceMapper := &resource.Mapper{
ObjectTyper: typer,
RESTMapper: mapper,
ClientMapper: resource.ClientMapperFunc(f.ClientForMapping),
}
info, err := resourceMapper.InfoForObject(actualRoute, []schema.GroupVersionKind{{Group: ""}})
if err != nil {
return err
}
shortOutput := kcmdutil.GetFlagString(cmd, "output") == "name"
kcmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, dryRun, "created")
return nil
}
var (
reencryptRouteLong = templates.LongDesc(`
Create a route that uses reencrypt TLS termination
Specify the service (either just its name or using type/name syntax) that the
generated route should expose via the --service flag. A destination CA certificate
is needed for reencrypt routes, specify one with the --dest-ca-cert flag.`)
reencryptRouteExample = templates.Examples(`
# Create a route named "my-route" that exposes the frontend service.
%[1]s create route reencrypt my-route --service=frontend --dest-ca-cert cert.cert
# Create a reencrypt route that exposes the frontend service and re-use
# the service name as the route name.
%[1]s create route reencrypt --service=frontend --dest-ca-cert cert.cert`)
)
// NewCmdCreateReencryptRoute is a macro command to create a reencrypt route.
func NewCmdCreateReencryptRoute(fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "reencrypt [NAME] --dest-ca-cert=FILENAME --service=SERVICE",
Short: "Create a route that uses reencrypt TLS termination",
Long: reencryptRouteLong,
Example: fmt.Sprintf(reencryptRouteExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
err := CreateReencryptRoute(f, out, cmd, args)
kcmdutil.CheckErr(err)
},
}
kcmdutil.AddValidateFlags(cmd)
kcmdutil.AddPrinterFlags(cmd)
kcmdutil.AddDryRunFlag(cmd)
cmd.Flags().String("hostname", "", "Set a hostname for the new route")
cmd.Flags().String("port", "", "Name of the service port or number of the container port the route will route traffic to")
cmd.Flags().String("insecure-policy", "", "Set an insecure policy for the new route")
cmd.Flags().String("service", "", "Name of the service that the new route is exposing")
cmd.MarkFlagRequired("service")
cmd.Flags().String("path", "", "Path that the router watches to route traffic to the service.")
cmd.Flags().String("cert", "", "Path to a certificate file.")
cmd.MarkFlagFilename("cert")
cmd.Flags().String("key", "", "Path to a key file.")
cmd.MarkFlagFilename("key")
cmd.Flags().String("ca-cert", "", "Path to a CA certificate file.")
cmd.MarkFlagFilename("ca-cert")
cmd.Flags().String("dest-ca-cert", "", "Path to a CA certificate file, used for securing the connection from the router to the destination.")
cmd.MarkFlagRequired("dest-ca-cert")
cmd.MarkFlagFilename("dest-ca-cert")
cmd.Flags().String("wildcard-policy", "", "Sets the WildcardPolicy for the hostname, the default is \"None\". valid values are \"None\" and \"Subdomain\"")
return cmd
}
// CreateReencryptRoute implements the behavior to run the create reencrypt route command.
func CreateReencryptRoute(f *clientcmd.Factory, out io.Writer, cmd *cobra.Command, args []string) error {
kc, err := f.ClientSet()
if err != nil {
return err
}
routeClient, err := f.OpenshiftInternalRouteClient()
if err != nil {
return err
}
ns, _, err := f.DefaultNamespace()
if err != nil {
return err
}
serviceName, err := resolveServiceName(f, kcmdutil.GetFlagString(cmd, "service"))
if err != nil {
return err
}
routeName, err := resolveRouteName(args)
if err != nil {
return err
}
route, err := cmdutil.UnsecuredRoute(kc, ns, routeName, serviceName, kcmdutil.GetFlagString(cmd, "port"), false)
if err != nil {
return err
}
wildcardpolicy := kcmdutil.GetFlagString(cmd, "wildcard-policy")
if len(wildcardpolicy) > 0 {
route.Spec.WildcardPolicy = routeapi.WildcardPolicyType(wildcardpolicy)
}
route.Spec.Host = kcmdutil.GetFlagString(cmd, "hostname")
route.Spec.Path = kcmdutil.GetFlagString(cmd, "path")
route.Spec.TLS = new(routeapi.TLSConfig)
route.Spec.TLS.Termination = routeapi.TLSTerminationReencrypt
cert, err := fileutil.LoadData(kcmdutil.GetFlagString(cmd, "cert"))
if err != nil {
return err
}
route.Spec.TLS.Certificate = string(cert)
key, err := fileutil.LoadData(kcmdutil.GetFlagString(cmd, "key"))
if err != nil {
return err
}
route.Spec.TLS.Key = string(key)
caCert, err := fileutil.LoadData(kcmdutil.GetFlagString(cmd, "ca-cert"))
if err != nil {
return err
}
route.Spec.TLS.CACertificate = string(caCert)
destCACert, err := fileutil.LoadData(kcmdutil.GetFlagString(cmd, "dest-ca-cert"))
if err != nil {
return err
}
route.Spec.TLS.DestinationCACertificate = string(destCACert)
insecurePolicy := kcmdutil.GetFlagString(cmd, "insecure-policy")
if len(insecurePolicy) > 0 {
route.Spec.TLS.InsecureEdgeTerminationPolicy = routeapi.InsecureEdgeTerminationPolicyType(insecurePolicy)
}
dryRun := kcmdutil.GetFlagBool(cmd, "dry-run")
actualRoute := route
if !dryRun {
actualRoute, err = routeClient.Route().Routes(ns).Create(route)
if err != nil {
return err
}
}
mapper, typer := f.Object()
resourceMapper := &resource.Mapper{
ObjectTyper: typer,
RESTMapper: mapper,
ClientMapper: resource.ClientMapperFunc(f.ClientForMapping),
}
info, err := resourceMapper.InfoForObject(actualRoute, []schema.GroupVersionKind{{Group: ""}})
if err != nil {
return err
}
shortOutput := kcmdutil.GetFlagString(cmd, "output") == "name"
kcmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, dryRun, "created")
return nil
}
func resolveServiceName(f *clientcmd.Factory, resource string) (string, error) {
if len(resource) == 0 {
return "", fmt.Errorf("you need to provide a service name via --service")
}
mapper, _ := f.Object()
rType, name, err := cmdutil.ResolveResource(kapi.Resource("services"), resource, mapper)
if err != nil {
return "", err
}
if rType != kapi.Resource("services") {
return "", fmt.Errorf("cannot expose %v as routes", rType)
}
return name, nil
}
func resolveRouteName(args []string) (string, error) {
switch len(args) {
case 0:
case 1:
return args[0], nil
default:
return "", fmt.Errorf("multiple names provided. Please specify at most one")
}
return "", nil
}