-
Notifications
You must be signed in to change notification settings - Fork 223
/
Copy pathnew_function.go
372 lines (293 loc) · 10.6 KB
/
new_function.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright (c) Alex Ellis 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package commands
import (
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/openfaas/faas-cli/builder"
"github.com/openfaas/faas-cli/stack"
"github.com/spf13/cobra"
)
var (
appendFile string
list bool
quiet bool
memoryLimit string
cpuLimit string
memoryRequest string
cpuRequest string
)
func init() {
newFunctionCmd.Flags().StringVar(&language, "lang", "", "Language or template to use")
newFunctionCmd.Flags().StringVarP(&gateway, "gateway", "g", defaultGateway, "Gateway URL to store in YAML stack file")
newFunctionCmd.Flags().StringVar(&handlerDir, "handler", "", "directory the handler will be written to")
newFunctionCmd.Flags().StringVarP(&imagePrefix, "prefix", "p", "", "Set prefix for the function image")
newFunctionCmd.Flags().StringVar(&memoryLimit, "memory-limit", "", "Set a limit for the memory")
newFunctionCmd.Flags().StringVar(&cpuLimit, "cpu-limit", "", "Set a limit for the CPU")
newFunctionCmd.Flags().StringVar(&memoryRequest, "memory-request", "", "Set a request or the memory")
newFunctionCmd.Flags().StringVar(&cpuRequest, "cpu-request", "", "Set a request value for the CPU")
newFunctionCmd.Flags().BoolVar(&list, "list", false, "List available languages")
newFunctionCmd.Flags().StringVarP(&appendFile, "append", "a", "", "Append to existing YAML file")
newFunctionCmd.Flags().BoolVarP(&quiet, "quiet", "q", false, "Skip template notes")
faasCmd.AddCommand(newFunctionCmd)
}
// newFunctionCmd displays newFunction information
var newFunctionCmd = &cobra.Command{
Use: "new FUNCTION_NAME --lang=FUNCTION_LANGUAGE [--gateway=http://host:port] | --list | --append=STACK_FILE)",
Short: "Create a new template in the current folder with the name given as name",
Long: `The new command creates a new function based upon hello-world in the given
language or type in --list for a list of languages available.`,
Example: ` faas-cli new chatbot --lang node
faas-cli new chatbot --lang node --append stack.yml
faas-cli new text-parser --lang python --quiet
faas-cli new text-parser --lang python --gateway http://mydomain:8080
faas-cli new --list`,
PreRunE: preRunNewFunction,
RunE: runNewFunction,
}
// validateFunctionName provides least-common-denominator validation - i.e. only allows valid Kubernetes services names
func validateFunctionName(functionName string) error {
// Regex for RFC-1123 validation:
// k8s.io/kubernetes/pkg/util/validation/validation.go
var validDNS = regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?$`)
if matched := validDNS.MatchString(functionName); !matched {
return fmt.Errorf(`function name can only contain a-z, 0-9 and dashes`)
}
return nil
}
// preRunNewFunction validates args & flags
func preRunNewFunction(cmd *cobra.Command, args []string) error {
if list {
return nil
}
language, _ = validateLanguageFlag(language)
if len(language) == 0 && len(args) < 1 {
cmd.Help()
os.Exit(0)
}
if len(language) == 0 {
return fmt.Errorf("you must supply a function language with the --lang flag")
}
if len(args) < 1 {
return fmt.Errorf(`please provide a name for the function`)
}
functionName = args[0]
if err := validateFunctionName(functionName); err != nil {
return err
}
return nil
}
func runNewFunction(cmd *cobra.Command, args []string) error {
if list {
var availableTemplates []string
templateFolders, err := os.ReadDir(templateDirectory)
if err != nil {
return fmt.Errorf(`no language templates were found.
Download templates:
faas-cli template pull download the default templates
faas-cli template store list view the template store
faas-cli template store pull NAME download the default templates
faas-cli new --lang NAME Attempt to download NAME from the template store`)
}
for _, file := range templateFolders {
if file.IsDir() {
availableTemplates = append(availableTemplates, file.Name())
}
}
fmt.Printf("Languages available as templates:\n%s\n", printAvailableTemplates(availableTemplates))
return nil
}
if !stack.IsValidTemplate(language) {
envTemplateRepoStore := os.Getenv(templateStoreURLEnvironment)
storeURL := getTemplateStoreURL(templateStoreURL, envTemplateRepoStore, DefaultTemplatesStore)
templatesInfo, err := getTemplateInfo(storeURL)
if err != nil {
return fmt.Errorf("error while getting templates info: %s", err)
}
var templateInfo *TemplateInfo
for _, info := range templatesInfo {
if info.TemplateName == language {
templateInfo = &info
break
}
}
if templateInfo == nil {
return fmt.Errorf("template: \"%s\" was not found in the templates folder or in the store", language)
}
templateName := templateInfo.TemplateName
if err := pullTemplate(templateInfo.Repository, templateName); err != nil {
return fmt.Errorf("error while pulling template: %s", err)
}
}
var fileName, outputMsg string
appendMode := len(appendFile) > 0
if appendMode {
if (strings.HasSuffix(appendFile, ".yml") || strings.HasSuffix(appendFile, ".yaml")) == false {
return fmt.Errorf("when appending to a stack the suffix should be .yml or .yaml")
}
if _, statErr := os.Stat(appendFile); statErr != nil {
return fmt.Errorf("unable to find file: %s - %s", appendFile, statErr.Error())
}
var duplicateError error
duplicateError = duplicateFunctionName(functionName, appendFile)
if duplicateError != nil {
return duplicateError
}
fileName = appendFile
outputMsg = fmt.Sprintf("Stack file updated: %s\n", fileName)
} else {
gateway = getGatewayURL(gateway, defaultGateway, gateway, os.Getenv(openFaaSURLEnvironment))
fileName = functionName + ".yml"
outputMsg = fmt.Sprintf("Stack file written: %s\n", fileName)
}
if len(handlerDir) == 0 {
handlerDir = functionName
}
if _, err := os.Stat(handlerDir); err == nil {
return fmt.Errorf("folder: %s already exists", handlerDir)
}
_, err := os.Stat(fileName)
if err == nil && appendMode == false {
return fmt.Errorf("file: %s already exists", fileName)
}
if err := os.Mkdir(handlerDir, 0700); err != nil {
return fmt.Errorf("folder: could not create %s : %s", handlerDir, err)
}
fmt.Printf("Folder: %s created.\n", handlerDir)
if err := updateGitignore(); err != nil {
return fmt.Errorf("got unexpected error while updating .gitignore file: %s", err)
}
pathToTemplateYAML := fmt.Sprintf("./template/%s/template.yml", language)
if _, err := os.Stat(pathToTemplateYAML); err != nil && os.IsNotExist(err) {
return err
}
langTemplate, err := stack.ParseYAMLForLanguageTemplate(pathToTemplateYAML)
if err != nil {
return fmt.Errorf("error reading language template: %s", err.Error())
}
templateHandlerFolder := "function"
if len(langTemplate.HandlerFolder) > 0 {
templateHandlerFolder = langTemplate.HandlerFolder
}
fromTemplateHandler := filepath.Join("template", language, templateHandlerFolder)
// Create function directory from template.
builder.CopyFiles(fromTemplateHandler, handlerDir)
printLogo()
fmt.Printf("\nFunction created in folder: %s\n", handlerDir)
imageName := fmt.Sprintf("%s:latest", functionName)
imagePrefixVal := getPrefixValue()
if imagePrefixVal = strings.TrimSpace(imagePrefixVal); len(imagePrefixVal) > 0 {
imageName = fmt.Sprintf("%s/%s", imagePrefixVal, imageName)
}
function := stack.Function{
Name: functionName,
Handler: "./" + handlerDir,
Language: language,
Image: imageName,
}
if len(memoryLimit) > 0 || len(cpuLimit) > 0 {
function.Limits = &stack.FunctionResources{
CPU: cpuLimit,
Memory: memoryLimit,
}
}
if len(memoryRequest) > 0 || len(cpuRequest) > 0 {
function.Requests = &stack.FunctionResources{
CPU: cpuRequest,
Memory: memoryRequest,
}
}
yamlContent := prepareYAMLContent(appendMode, gateway, &function)
f, err := os.OpenFile("./"+fileName, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return fmt.Errorf("could not open file '%s' %s", fileName, err)
}
_, stackWriteErr := f.Write([]byte(yamlContent))
if stackWriteErr != nil {
return fmt.Errorf("error writing stack file %s", stackWriteErr)
}
fmt.Print(outputMsg)
if !quiet {
languageTemplate, _ := stack.LoadLanguageTemplate(language)
if languageTemplate.WelcomeMessage != "" {
fmt.Printf("\nNotes:\n")
fmt.Printf("%s\n", languageTemplate.WelcomeMessage)
}
}
return nil
}
func getPrefixValue() string {
prefix := ""
if len(imagePrefix) > 0 {
return imagePrefix
}
if val, ok := os.LookupEnv("OPENFAAS_PREFIX"); ok && len(val) > 0 {
prefix = val
}
return prefix
}
func prepareYAMLContent(appendMode bool, gateway string, function *stack.Function) (yamlContent string) {
yamlContent = ` ` + function.Name + `:
lang: ` + function.Language + `
handler: ` + function.Handler + `
image: ` + function.Image + `
`
if function.Requests != nil && (len(function.Requests.CPU) > 0 || len(function.Requests.Memory) > 0) {
yamlContent += " requests:\n"
if len(function.Requests.CPU) > 0 {
yamlContent += ` cpu: ` + function.Requests.CPU + "\n"
}
if len(function.Requests.Memory) > 0 {
yamlContent += ` memory: ` + function.Requests.Memory + "\n"
}
}
if function.Limits != nil && (len(function.Limits.CPU) > 0 || len(function.Limits.Memory) > 0) {
yamlContent += " limits:\n"
if len(function.Limits.CPU) > 0 {
yamlContent += ` cpu: ` + function.Limits.CPU + "\n"
}
if len(function.Limits.Memory) > 0 {
yamlContent += ` memory: ` + function.Limits.Memory + "\n"
}
}
yamlContent += "\n"
if !appendMode {
yamlContent = `version: ` + defaultSchemaVersion + `
provider:
name: openfaas
gateway: ` + gateway + `
functions:
` + yamlContent
}
return yamlContent
}
func printAvailableTemplates(availableTemplates []string) string {
var result string
sort.Slice(availableTemplates, func(i, j int) bool {
return availableTemplates[i] < availableTemplates[j]
})
for _, template := range availableTemplates {
result += fmt.Sprintf("- %s\n", template)
}
return result
}
func duplicateFunctionName(functionName string, appendFile string) error {
fileBytes, readErr := os.ReadFile(appendFile)
if readErr != nil {
return fmt.Errorf("unable to read %s to append, %s", appendFile, readErr)
}
services, parseErr := stack.ParseYAMLData(fileBytes, "", "", envsubst)
if parseErr != nil {
return fmt.Errorf("Error parsing %s yml file", appendFile)
}
if _, exists := services.Functions[functionName]; exists {
return fmt.Errorf(`
Function %s already exists in %s file.
Cannot have duplicate function names in same yaml file`, functionName, appendFile)
}
return nil
}