forked from openshift/origin
/
expose.go
105 lines (91 loc) · 2.58 KB
/
expose.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
package cmd
import (
"fmt"
"io"
kcmd "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd"
cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
"github.com/spf13/cobra"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
)
const (
exposeLong = `Take a replicated application and expose it as a route.
Looks up a service by name and derives a route from it.`
exposeExample = `// Create a route based on service nginx. The new route will re-use nginx's labels.
$ %[1]s expose service nginx
// Create a route and specify your own label.
$ %[1]s expose service nginx --labels name=myroute`
)
// NewCmdExpose is a wrapper for the Kubernetes cli expose command
func NewCmdExpose(fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
cmd := kcmd.NewCmdExposeService(f.Factory, out)
cmd.Long = exposeLong
cmd.Example = fmt.Sprintf(exposeExample, fullName)
cmd.Flags().Set("generator", "route/v1")
cmd.Run = func(cmd *cobra.Command, args []string) {
err := validate(cmd, f, args)
cmdutil.CheckErr(err)
err = kcmd.RunExpose(f.Factory, out, cmd, args)
cmdutil.CheckErr(err)
}
return cmd
}
// validate adds one layer of validation prior to calling the upstream
// expose command. Used only for validating services that are about
// to be exposed as routes.
func validate(cmd *cobra.Command, f *clientcmd.Factory, args []string) error {
if cmdutil.GetFlagString(cmd, "generator") != "route/v1" {
return nil
}
namespace, err := f.DefaultNamespace()
if err != nil {
return err
}
_, kc, err := f.Clients()
if err != nil {
return err
}
mapper, typer := f.Object()
r := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()).
ContinueOnError().
NamespaceParam(namespace).DefaultNamespace().
ResourceTypeOrNameArgs(false, args...).
Flatten().
Do()
err = r.Err()
if err != nil {
return err
}
mapping, err := r.ResourceMapping()
if err != nil {
return err
}
infos, err := r.Infos()
if err != nil {
return err
}
if len(infos) > 1 {
return fmt.Errorf("multiple resources provided: %v", args)
}
info := infos[0]
switch mapping.Kind {
case "Service":
svc, err := kc.Services(info.Namespace).Get(info.Name)
if err != nil {
return err
}
supportsTCP := false
for _, port := range svc.Spec.Ports {
if port.Protocol == "TCP" {
supportsTCP = true
break
}
}
if !supportsTCP {
return fmt.Errorf("service %s doesn't support TCP", info.Name)
}
default:
return fmt.Errorf("cannot expose a %s as a route", mapping.Kind)
}
return nil
}