forked from openfaas/faas-cli
/
store_deploy.go
130 lines (107 loc) · 4.78 KB
/
store_deploy.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
// Copyright (c) OpenFaaS Project 2018. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package commands
import (
"fmt"
"log"
"os"
"github.com/spf13/cobra"
)
func init() {
// Setup flags that are used by multiple commands (variables defined in faas.go)
storeDeployCmd.Flags().StringVarP(&gateway, "gateway", "g", defaultGateway, "Gateway URL starting with http(s)://")
storeDeployCmd.Flags().StringVar(&network, "network", "", "Name of the network")
storeDeployCmd.Flags().StringVar(&functionName, "name", "", "Name of the deployed function (overriding name from the store)")
// Setup flags that are used only by deploy command (variables defined above)
storeDeployCmd.Flags().StringArrayVarP(&storeDeployFlags.envvarOpts, "env", "e", []string{}, "Adds one or more environment variables to the defined ones by store (ENVVAR=VALUE)")
storeDeployCmd.Flags().StringArrayVarP(&storeDeployFlags.labelOpts, "label", "l", []string{}, "Set one or more label (LABEL=VALUE)")
storeDeployCmd.Flags().BoolVar(&storeDeployFlags.replace, "replace", false, "Replace any existing function")
storeDeployCmd.Flags().BoolVar(&storeDeployFlags.update, "update", true, "Update existing functions")
storeDeployCmd.Flags().StringArrayVar(&storeDeployFlags.constraints, "constraint", []string{}, "Apply a constraint to the function")
storeDeployCmd.Flags().StringArrayVar(&storeDeployFlags.secrets, "secret", []string{}, "Give the function access to a secure secret")
storeDeployCmd.Flags().BoolVarP(&storeDeployFlags.sendRegistryAuth, "send-registry-auth", "a", false, "send registryAuth from Docker credentials manager with the request")
storeDeployCmd.Flags().StringArrayVarP(&storeDeployFlags.annotationOpts, "annotation", "", []string{}, "Set one or more annotation (ANNOTATION=VALUE)")
// Set bash-completion.
_ = storeDeployCmd.Flags().SetAnnotation("handler", cobra.BashCompSubdirsInDir, []string{})
storeCmd.AddCommand(storeDeployCmd)
}
var storeDeployCmd = &cobra.Command{
Use: `deploy (FUNCTION_NAME|FUNCTION_TITLE)
[--name FUNCTION_NAME]
[--gateway GATEWAY_URL]
[--network NETWORK_NAME]
[--env ENVVAR=VALUE ...]
[--label LABEL=VALUE ...]
[--annotation ANNOTATION=VALUE ...]
[--replace=false]
[--update=true]
[--constraint PLACEMENT_CONSTRAINT ...]
[--secret "SECRET_NAME"]
[--url STORE_URL]`,
Short: "Deploy OpenFaaS functions from a store",
Long: `Same as faas-cli deploy except that function is pre-loaded with arguments from the store`,
Example: ` faas-cli store deploy figlet
faas-cli store deploy figlet \
--gateway=http://127.0.0.1:8080 \
--env=MYVAR=myval`,
RunE: runStoreDeploy,
}
func runStoreDeploy(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("please provide the function name")
}
storeItems, err := storeList(storeAddress)
if err != nil {
return err
}
requestedStoreFn := args[0]
item := storeFindFunction(requestedStoreFn, storeItems)
if item == nil {
return fmt.Errorf("function '%s' not found", requestedStoreFn)
}
// Add the store environment variables to the provided ones from cmd
if item.Environment != nil {
for k, v := range item.Environment {
env := fmt.Sprintf("%s=%s", k, v)
storeDeployFlags.envvarOpts = append(storeDeployFlags.envvarOpts, env)
}
}
// Add the store labels to the provided ones from cmd
if item.Labels != nil {
for k, v := range item.Labels {
label := fmt.Sprintf("%s=%s", k, v)
storeDeployFlags.labelOpts = append(storeDeployFlags.labelOpts, label)
}
}
if item.Annotations != nil {
for k, v := range item.Annotations {
annotation := fmt.Sprintf("%s=%s", k, v)
storeDeployFlags.annotationOpts = append(storeDeployFlags.annotationOpts, annotation)
}
}
// Use the network from manifest if not changed by user
if !cmd.Flag("network").Changed {
network = item.Network
}
itemName := item.Name
if functionName != "" {
itemName = functionName
}
var registryAuth string
if storeDeployFlags.sendRegistryAuth {
dockerConfig := configFile{}
err := readDockerConfig(&dockerConfig)
if err != nil {
log.Printf("Unable to read the docker config - %v\n", err.Error())
}
registryAuth = getRegistryAuth(&dockerConfig, item.Image)
}
gateway = getGatewayURL(gateway, defaultGateway, "", os.Getenv(openFaaSURLEnvironment))
statusCode, err := deployImage(item.Image, item.Fprocess, itemName, registryAuth, storeDeployFlags, tlsInsecure, item.ReadOnlyRootFilesystem)
if badStatusCode(statusCode) {
failedStatusCode := map[string]int{itemName: statusCode}
err := deployFailed(failedStatusCode)
return err
}
return err
}