forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate.go
93 lines (80 loc) · 2.12 KB
/
generate.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
package generator
import (
"fmt"
"strconv"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/kubectl"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/intstr"
"github.com/openshift/origin/pkg/route/api"
)
// RouteGenerator generates routes from a given set of parameters
type RouteGenerator struct{}
// RouteGenerator implements the kubectl.Generator interface for routes
var _ kubectl.Generator = RouteGenerator{}
// ParamNames returns the parameters required for generating a route
func (RouteGenerator) ParamNames() []kubectl.GeneratorParam {
return []kubectl.GeneratorParam{
{"labels", false},
{"default-name", true},
{"target-port", false},
{"name", false},
{"hostname", false},
{"path", false},
}
}
// Generate accepts a set of parameters and maps them into a new route
func (RouteGenerator) Generate(genericParams map[string]interface{}) (runtime.Object, error) {
var (
labels map[string]string
err error
)
params := map[string]string{}
for key, value := range genericParams {
strVal, isString := value.(string)
if !isString {
return nil, fmt.Errorf("expected string, saw %v for '%s'", value, key)
}
params[key] = strVal
}
labelString, found := params["labels"]
if found && len(labelString) > 0 {
labels, err = kubectl.ParseLabels(labelString)
if err != nil {
return nil, err
}
}
name, found := params["name"]
if !found || len(name) == 0 {
name, found = params["default-name"]
if !found || len(name) == 0 {
return nil, fmt.Errorf("'name' is a required parameter.")
}
}
route := &api.Route{
ObjectMeta: kapi.ObjectMeta{
Name: name,
Labels: labels,
},
Spec: api.RouteSpec{
Host: params["hostname"],
Path: params["path"],
To: kapi.ObjectReference{
Name: params["default-name"],
},
},
}
portString := params["target-port"]
if len(portString) > 0 {
var targetPort intstr.IntOrString
if port, err := strconv.Atoi(portString); err == nil {
targetPort = intstr.FromInt(port)
} else {
targetPort = intstr.FromString(portString)
}
route.Spec.Port = &api.RoutePort{
TargetPort: targetPort,
}
}
return route, nil
}