forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
239 lines (208 loc) · 7.08 KB
/
process.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
package cmd
import (
"fmt"
"io"
"reflect"
"strings"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
kcmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/golang/glog"
"github.com/spf13/cobra"
"github.com/openshift/origin/pkg/cmd/cli/describe"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
configapi "github.com/openshift/origin/pkg/config/api"
"github.com/openshift/origin/pkg/template"
"github.com/openshift/origin/pkg/template/api"
)
// injectUserVars injects user specified variables into the Template
func injectUserVars(cmd *cobra.Command, t *api.Template) {
values := util.StringList{}
values.Set(kcmdutil.GetFlagString(cmd, "value"))
for _, keypair := range values {
p := strings.SplitN(keypair, "=", 2)
if len(p) != 2 {
glog.Errorf("Invalid parameter assignment '%s'", keypair)
continue
}
if v := template.GetParameterByName(t, p[0]); v != nil {
v.Value = p[1]
v.Generate = ""
template.AddParameter(t, *v)
} else {
glog.Errorf("Unknown parameter name '%s'", p[0])
}
}
}
const (
process_long = `Process template into a list of resources specified in filename or stdin
JSON and YAML formats are accepted.`
process_example = ` // Convert template.json file into resource list
$ %[1]s process -f template.json
// Process template while passing a user-defined label
$ %[1]s process -f template.json -l name=mytemplate
// Convert stored template into resource list
$ %[1]s process foo
// Convert template.json into resource list
$ cat template.json | %[1]s process -f -`
)
// NewCmdProcess implements the OpenShift cli process command
func NewCmdProcess(fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "process (TEMPLATE | -f FILENAME) [-v=KEY=VALUE]",
Short: "Process template into list of resources",
Long: process_long,
Example: fmt.Sprintf(process_example, fullName),
Run: func(cmd *cobra.Command, args []string) {
err := RunProcess(f, out, cmd, args)
kcmdutil.CheckErr(err)
},
}
cmd.Flags().StringP("filename", "f", "", "Filename or URL to file to read a template")
cmd.Flags().StringP("value", "v", "", "Specify a list of key-value pairs (eg. -v FOO=BAR,BAR=FOO) to set/override parameter values")
cmd.Flags().BoolP("parameters", "", false, "Do not process but only print available parameters")
cmd.Flags().StringP("labels", "l", "", "Label to set in all resources for this template")
cmd.Flags().StringP("output", "o", "", "Output format. One of: describe|json|yaml|template|templatefile.")
cmd.Flags().Bool("raw", false, "If true output the processed template instead of the template's objects. Implied by -o describe")
cmd.Flags().String("output-version", "", "Output the formatted object with the given version (default api-version).")
cmd.Flags().StringP("template", "t", "", "Template string or path to template file to use when -o=template or -o=templatefile. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]")
return cmd
}
// RunProject contains all the necessary functionality for the OpenShift cli process command
func RunProcess(f *clientcmd.Factory, out io.Writer, cmd *cobra.Command, args []string) error {
storedTemplate := ""
if len(args) > 0 {
storedTemplate = args[0]
}
filename := kcmdutil.GetFlagString(cmd, "filename")
if len(storedTemplate) == 0 && len(filename) == 0 {
return kcmdutil.UsageError(cmd, "Must pass a filename or name of stored template")
}
namespace, err := f.DefaultNamespace()
if err != nil {
return err
}
mapper, typer := f.Object()
client, _, err := f.Clients()
if err != nil {
return err
}
var (
templateObj *api.Template
mapping *meta.RESTMapping
)
if len(storedTemplate) > 0 {
templateObj, err = client.Templates(namespace).Get(storedTemplate)
if err != nil {
if errors.IsNotFound(err) {
return fmt.Errorf("template %q could not be found", storedTemplate)
}
return err
}
version, kind, err := mapper.VersionAndKindForResource("template")
if mapping, err = mapper.RESTMapping(kind, version); err != nil {
return err
}
} else {
obj, err := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()).
NamespaceParam(namespace).RequireNamespace().
FilenameParam(filename).
Do().
Object()
if err != nil {
return err
}
var ok bool
templateObj, ok = obj.(*api.Template)
if !ok {
return fmt.Errorf("cannot convert input to Template: ", reflect.TypeOf(obj))
}
templateObj.CreationTimestamp = util.Now()
version, kind, err := kapi.Scheme.ObjectVersionAndKind(templateObj)
if err != nil {
return err
}
if mapping, err = mapper.RESTMapping(kind, version); err != nil {
return err
}
}
if cmd.Flag("value").Changed {
injectUserVars(cmd, templateObj)
}
// If 'parameters' flag is set it does not do processing but only print
// the template parameters to console for inspection.
if kcmdutil.GetFlagBool(cmd, "parameters") == true {
err = describe.PrintTemplateParameters(templateObj.Parameters, out)
if err != nil {
return err
}
return nil
}
label := kcmdutil.GetFlagString(cmd, "labels")
if len(label) != 0 {
lbl, err := kubectl.ParseLabels(label)
if err != nil {
return err
}
if templateObj.ObjectLabels == nil {
templateObj.ObjectLabels = make(map[string]string)
}
for key, value := range lbl {
templateObj.ObjectLabels[key] = value
}
}
// TODO: use AsVersionedObjects to generate the runtime.Objects, because
// some objects may not exist in the destination version but they should
// still be transformed.
obj, err := client.TemplateConfigs(namespace).Create(templateObj)
if err != nil {
return err
}
outputVersion := kcmdutil.OutputVersion(cmd, mapping.APIVersion)
raw := kcmdutil.GetFlagBool(cmd, "raw")
outputFormat := kcmdutil.GetFlagString(cmd, "output")
if len(outputFormat) == 0 {
outputFormat = "json"
}
if outputFormat == "describe" {
s, err := (&describe.TemplateDescriber{
MetadataAccessor: meta.NewAccessor(),
ObjectTyper: kapi.Scheme,
ObjectDescriber: nil,
}).DescribeTemplate(obj)
if err != nil {
return err
}
fmt.Fprintf(out, s)
return nil
}
// use generic output
var result runtime.Object
switch {
case raw:
result = obj
// legacy support - when using older api versions, output a Config
case kapi.PreV1Beta3(outputVersion):
result = &configapi.Config{
ListMeta: kapi.ListMeta{},
Items: obj.Objects,
}
// display the processed template instead of the objects
default:
result = &kapi.List{
ListMeta: kapi.ListMeta{},
Items: obj.Objects,
}
}
p, _, err := kubectl.GetPrinter(outputFormat, "")
if err != nil {
return err
}
p = kubectl.NewVersionedPrinter(p, kapi.Scheme, outputVersion)
return p.PrintObj(result, out)
}