forked from openshift/origin
/
expose.go
170 lines (147 loc) · 4.88 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
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
package cmd
import (
"fmt"
"io"
"strings"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
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 = `
Expose containers internally as services or externally via routes
There is also the ability to expose a deployment configuration, replication controller, service, or pod
as a new service on a specified port. If no labels are specified, the new object will re-use the
labels from the object it exposes.`
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 and route name
$ %[1]s expose service nginx -l name=myroute --name=fromdowntown
// Create a route and specify a hostname
$ %[1]s expose service nginx --hostname=www.example.com
// Expose a deployment configuration as a service and use the specified port
$ %[1]s expose dc ruby-hello-world --port=8080 --generator=service/v1`
)
// 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.Short = "Expose a replicated application as a service or route"
cmd.Long = exposeLong
cmd.Example = fmt.Sprintf(exposeExample, fullName)
cmd.Flags().Set("generator", "route/v1")
cmd.Flag("generator").Usage = "The name of the API generator to use. Default is 'route/v1'."
cmd.Flag("generator").DefValue = "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)
}
cmd.Flags().String("hostname", "", "Set a hostname for the new route")
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" {
if len(cmdutil.GetFlagString(cmd, "hostname")) > 0 {
return fmt.Errorf("cannot use --hostname without generating a route")
}
return nil
}
if err := validateFlags(cmd); err != nil {
return err
}
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 == kapi.ProtocolTCP {
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
}
// validateFlags filters out flags that are not supposed to be used
// when generating a route
func validateFlags(cmd *cobra.Command) error {
invalidFlags := []string{}
if len(cmdutil.GetFlagString(cmd, "type")) != 0 {
invalidFlags = append(invalidFlags, "--type")
}
if len(cmdutil.GetFlagString(cmd, "selector")) != 0 {
invalidFlags = append(invalidFlags, "--selector")
}
if len(cmdutil.GetFlagString(cmd, "container-port")) != 0 {
invalidFlags = append(invalidFlags, "--container-port")
}
if len(cmdutil.GetFlagString(cmd, "target-port")) != 0 {
invalidFlags = append(invalidFlags, "--target-port")
}
if len(cmdutil.GetFlagString(cmd, "public-ip")) != 0 {
invalidFlags = append(invalidFlags, "--public-ip")
}
if cmdutil.GetFlagInt(cmd, "port") != -1 {
invalidFlags = append(invalidFlags, "--port")
}
if cmdutil.GetFlagBool(cmd, "create-external-load-balancer") {
invalidFlags = append(invalidFlags, "--create-external-load-balancer")
}
msg := ""
switch len(invalidFlags) {
case 0:
return nil
case 1:
msg = invalidFlags[0]
default:
commaSeparated, last := invalidFlags[:len(invalidFlags)-1], invalidFlags[len(invalidFlags)-1]
msg = fmt.Sprintf("%s or %s", strings.Join(commaSeparated, ","), last)
}
return fmt.Errorf("cannot use %s when generating a route", msg)
}