-
Notifications
You must be signed in to change notification settings - Fork 524
/
create.go
350 lines (287 loc) · 12.2 KB
/
create.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
/*
Copyright 2023 The Nuclio Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package command
import (
"context"
"encoding/json"
"github.com/nuclio/nuclio/pkg/common"
"github.com/nuclio/nuclio/pkg/platform"
"github.com/nuclio/nuclio/pkg/platform/kube/ingress"
"github.com/nuclio/errors"
"github.com/spf13/cobra"
)
type createCommandeer struct {
cmd *cobra.Command
rootCommandeer *RootCommandeer
}
func newCreateCommandeer(ctx context.Context, rootCommandeer *RootCommandeer) *createCommandeer {
commandeer := &createCommandeer{
rootCommandeer: rootCommandeer,
}
cmd := &cobra.Command{
Use: "create",
Aliases: []string{"cre"},
Short: "Create resources",
}
createProjectCommand := newCreateProjectCommandeer(ctx, commandeer).cmd
createFunctionEventCommand := newCreateFunctionEventCommandeer(ctx, commandeer).cmd
createAPIGatewayCommand := newCreateAPIGatewayCommandeer(ctx, commandeer).cmd
cmd.AddCommand(
createProjectCommand,
createFunctionEventCommand,
createAPIGatewayCommand,
)
commandeer.cmd = cmd
return commandeer
}
type createProjectCommandeer struct {
*createCommandeer
projectConfig platform.ProjectConfig
}
func newCreateProjectCommandeer(ctx context.Context, createCommandeer *createCommandeer) *createProjectCommandeer {
commandeer := &createProjectCommandeer{
createCommandeer: createCommandeer,
}
cmd := &cobra.Command{
Use: "project name",
Aliases: []string{"proj", "prj"},
Short: "Create a new project",
Long: `Create a new Nuclio project.`,
RunE: func(cmd *cobra.Command, args []string) error {
// if we got positional arguments
if len(args) != 1 {
return errors.New("Project create requires an identifier")
}
// initialize root
if err := createCommandeer.rootCommandeer.initialize(true); err != nil {
return errors.Wrap(err, "Failed to initialize root")
}
commandeer.projectConfig.Meta.Name = args[0]
commandeer.projectConfig.Meta.Namespace = createCommandeer.rootCommandeer.namespace
if err := createCommandeer.rootCommandeer.platform.CreateProject(ctx, &platform.CreateProjectOptions{
ProjectConfig: &commandeer.projectConfig,
}); err != nil {
return err
}
commandeer.rootCommandeer.loggerInstance.InfoWith("Project created",
"Name",
commandeer.projectConfig.Meta.Name,
"Namespace",
commandeer.projectConfig.Meta.Namespace)
return nil
},
}
cmd.Flags().StringVar(&commandeer.projectConfig.Spec.Description, "description", "", "Project description")
cmd.Flags().StringVar(&commandeer.projectConfig.Spec.Owner, "owner", "", "Project owner")
commandeer.cmd = cmd
return commandeer
}
type createAPIGatewayCommandeer struct {
*createCommandeer
apiGatewayConfig platform.APIGatewayConfig
project string
host string
description string
path string
authenticationMode string
basicAuthUsername string
basicAuthPassword string
function string
canaryFunction string
canaryPercentage int
encodedAttributes string
encodedExtraLables string
encodedCanaryExtraLables string
}
func newCreateAPIGatewayCommandeer(ctx context.Context, createCommandeer *createCommandeer) *createAPIGatewayCommandeer {
commandeer := &createAPIGatewayCommandeer{
createCommandeer: createCommandeer,
}
cmd := &cobra.Command{
Use: "apigateway name",
Aliases: []string{"agw"},
Short: "Create api gateways",
RunE: func(cmd *cobra.Command, args []string) error {
// if we got positional arguments
if len(args) != 1 {
return errors.New("Api gateway create requires an identifier")
}
// initialize root
if err := createCommandeer.rootCommandeer.initialize(true); err != nil {
return errors.Wrap(err, "Failed to initialize root")
}
// decode the JSON attributes
if err := json.Unmarshal([]byte(commandeer.encodedAttributes),
&commandeer.apiGatewayConfig); err != nil {
return errors.Wrap(err, "Failed to decode a function's event attributes")
}
commandeer.apiGatewayConfig.Meta.Name = args[0]
commandeer.apiGatewayConfig.Meta.Namespace = createCommandeer.rootCommandeer.namespace
if commandeer.project != "" {
commandeer.apiGatewayConfig.Meta.Labels = map[string]string{
common.NuclioResourceLabelKeyProjectName: commandeer.project,
}
}
// enrich api gateway spec with commandeer input
commandeer.apiGatewayConfig.Spec.Host = commandeer.host
commandeer.apiGatewayConfig.Spec.Description = commandeer.description
commandeer.apiGatewayConfig.Spec.Path = commandeer.path
// enrich authentication mode
if commandeer.authenticationMode != "" {
commandeer.apiGatewayConfig.Spec.AuthenticationMode = ingress.AuthenticationMode(commandeer.authenticationMode)
} else {
commandeer.apiGatewayConfig.Spec.AuthenticationMode = ingress.AuthenticationModeNone
}
// enrich basic-auth spec if it was specified
if commandeer.apiGatewayConfig.Spec.AuthenticationMode == ingress.AuthenticationModeBasicAuth {
if commandeer.basicAuthUsername == "" || commandeer.basicAuthPassword == "" {
return errors.New("Basic auth username and password must be specified")
}
commandeer.apiGatewayConfig.Spec.Authentication = &platform.APIGatewayAuthenticationSpec{
BasicAuth: &platform.BasicAuth{
Username: commandeer.basicAuthUsername,
Password: commandeer.basicAuthPassword,
},
}
}
// validate a primary function was specified
if commandeer.function == "" {
return errors.New("A primary function must be specified")
}
// decode the JSON extra labels
extraLabels := map[string]string{}
if err := json.Unmarshal([]byte(commandeer.encodedExtraLables),
&extraLabels); err != nil {
return errors.Wrap(err, "Failed to decode a function's extra labels")
}
commandeer.apiGatewayConfig.Spec.Upstreams = []platform.APIGatewayUpstreamSpec{
{
Kind: platform.APIGatewayUpstreamKindNuclioFunction,
NuclioFunction: &platform.NuclioFunctionAPIGatewaySpec{
Name: commandeer.function,
},
ExtraLabels: extraLabels,
},
}
if commandeer.canaryFunction != "" {
if commandeer.canaryPercentage == 0 {
return errors.New("Canary function percentage must be specified")
}
canaryExtraLabels := map[string]string{}
if commandeer.encodedCanaryExtraLables == "" {
// decode the canary's JSON extra labels
if err := json.Unmarshal([]byte(commandeer.encodedExtraLables),
&canaryExtraLabels); err != nil {
return errors.Wrap(err, "Failed to decode a function's extra labels")
}
}
canaryUpstream := platform.APIGatewayUpstreamSpec{
Kind: platform.APIGatewayUpstreamKindNuclioFunction,
NuclioFunction: &platform.NuclioFunctionAPIGatewaySpec{
Name: commandeer.canaryFunction,
},
Percentage: commandeer.canaryPercentage,
ExtraLabels: canaryExtraLabels,
}
commandeer.apiGatewayConfig.Spec.Upstreams = append(commandeer.apiGatewayConfig.Spec.Upstreams, canaryUpstream)
}
commandeer.apiGatewayConfig.Status.State = platform.APIGatewayStateWaitingForProvisioning
if err := createCommandeer.rootCommandeer.platform.CreateAPIGateway(ctx,
&platform.CreateAPIGatewayOptions{
APIGatewayConfig: &commandeer.apiGatewayConfig,
}); err != nil {
return err
}
commandeer.rootCommandeer.loggerInstance.InfoWith("API gateway created",
"Name",
commandeer.apiGatewayConfig.Meta.Name,
"Namespace",
commandeer.apiGatewayConfig.Meta.Namespace)
return nil
},
}
cmd.Flags().StringVar(&commandeer.project, "project", "project", "The project the api gateway should be created in")
cmd.Flags().StringVar(&commandeer.host, "host", "", "Api gateway host address")
cmd.Flags().StringVar(&commandeer.description, "description", "", "Api gateway description")
cmd.Flags().StringVar(&commandeer.path, "path", "", "Api gateway path (the URI that'll be concatenated to the host as an endpoint)")
cmd.Flags().StringVar(&commandeer.authenticationMode, "authentication-mode", "", "Api gateway authentication mode. ['none', 'basicAuth', 'accessKey']")
cmd.Flags().StringVar(&commandeer.basicAuthUsername, "basic-auth-username", "", "The basic-auth username")
cmd.Flags().StringVar(&commandeer.basicAuthPassword, "basic-auth-password", "", "The basic-auth password")
cmd.Flags().StringVar(&commandeer.function, "function", "", "The api gateway primary function")
cmd.Flags().StringVar(&commandeer.canaryFunction, "canary-function", "", "The api gateway canary function")
cmd.Flags().IntVar(&commandeer.canaryPercentage, "canary-percentage", 0, "The canary function percentage")
cmd.Flags().StringVar(&commandeer.encodedAttributes, "attrs", "{}", "JSON-encoded attributes for the api gateway (overrides all the rest)")
cmd.Flags().StringVar(&commandeer.encodedExtraLables, "labels", "{}", "JSON-encoded custom labels for the api gateway")
cmd.Flags().StringVar(&commandeer.encodedExtraLables, "canary-labels", "{}", "JSON-encoded custom labels for canary upstream of the api gateway")
commandeer.cmd = cmd
return commandeer
}
type createFunctionEventCommandeer struct {
*createCommandeer
functionEventConfig platform.FunctionEventConfig
encodedAttributes string
functionName string
}
func newCreateFunctionEventCommandeer(ctx context.Context, createCommandeer *createCommandeer) *createFunctionEventCommandeer {
commandeer := &createFunctionEventCommandeer{
createCommandeer: createCommandeer,
}
cmd := &cobra.Command{
Use: "functionevent name",
Aliases: []string{"fe"},
Short: "Create function events",
RunE: func(cmd *cobra.Command, args []string) error {
// if we got positional arguments
if len(args) != 1 {
return errors.New("Function event create requires an identifier")
}
if commandeer.functionName == "" {
return errors.New("Function event must belong to a function")
}
// initialize root
if err := createCommandeer.rootCommandeer.initialize(true); err != nil {
return errors.Wrap(err, "Failed to initialize root")
}
commandeer.functionEventConfig.Meta.Name = args[0]
commandeer.functionEventConfig.Meta.Namespace = createCommandeer.rootCommandeer.namespace
commandeer.functionEventConfig.Meta.Labels = map[string]string{
common.NuclioResourceLabelKeyFunctionName: commandeer.functionName,
}
// decode the JSON attributes
if err := json.Unmarshal([]byte(commandeer.encodedAttributes),
&commandeer.functionEventConfig.Spec.Attributes); err != nil {
return errors.Wrap(err, "Failed to decode a function's event attributes")
}
if err := createCommandeer.rootCommandeer.platform.CreateFunctionEvent(ctx,
&platform.CreateFunctionEventOptions{
FunctionEventConfig: commandeer.functionEventConfig,
}); err != nil {
return err
}
commandeer.rootCommandeer.loggerInstance.InfoWith("Function event created",
"Name",
commandeer.functionEventConfig.Meta.Name,
"Namespace",
commandeer.functionEventConfig.Meta.Namespace)
return nil
},
}
cmd.Flags().StringVar(&commandeer.functionName, "function", "", "function this event belongs to")
cmd.Flags().StringVar(&commandeer.functionEventConfig.Spec.DisplayName, "display-name", "", "display name, if different than name (optional)")
cmd.Flags().StringVar(&commandeer.functionEventConfig.Spec.TriggerName, "trigger-name", "", "trigger name to invoke (optional)")
cmd.Flags().StringVar(&commandeer.functionEventConfig.Spec.TriggerKind, "trigger-kind", "", "trigger kind to invoke (optional)")
cmd.Flags().StringVar(&commandeer.functionEventConfig.Spec.Body, "body", "", "body content to invoke the function with")
cmd.Flags().StringVar(&commandeer.encodedAttributes, "attrs", "{}", "JSON-encoded attributes for the function event")
commandeer.cmd = cmd
return commandeer
}