Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add label support #184

Merged
merged 3 commits into from
Oct 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@ Or only Windows:
- "node.platform.os == windows"
```

### Labels

Labels can be applied through a map which may be consumed by the back-end scheduler such as Docker Swarm or Kubernetes.

For example:

```yaml
labels:
kafka.topic: topic1
canary: true
```

### YAML reference

The possible entries for functions are documented below:
Expand All @@ -192,6 +204,9 @@ functions:
environment:
env1: value1
env2: "value2"
labels:
label1: value1
label2: "value2"
constraints:
- "com.hdd == ssd"
```
Expand Down
51 changes: 45 additions & 6 deletions commands/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
update bool
constraints []string
secrets []string
labelOpts []string
)

func init() {
Expand All @@ -39,6 +40,9 @@ func init() {

// Setup flags that are used only by this command (variables defined above)
deployCmd.Flags().StringArrayVarP(&envvarOpts, "env", "e", []string{}, "Set one or more environment variables (ENVVAR=VALUE)")

deployCmd.Flags().StringArrayVarP(&labelOpts, "label", "l", []string{}, "Set one or more label (LABEL=VALUE)")

deployCmd.Flags().BoolVar(&replace, "replace", true, "Replace any existing function")
deployCmd.Flags().BoolVar(&update, "update", false, "Update existing functions")

Expand All @@ -62,6 +66,7 @@ var deployCmd = &cobra.Command{
[--handler HANDLER_DIR]
[--fprocess PROCESS]
[--env ENVVAR=VALUE ...]
[--label LABEL=VALUE ...]
[--replace=false]
[--update=false]
[--constraint PLACEMENT_CONSTRAINT ...]
Expand All @@ -75,6 +80,7 @@ the "--yaml" flag (which may contain multiple function definitions), or directly
via flags. Note: --replace and --update are mutually exclusive.`,
Example: ` faas-cli deploy -f https://domain/path/myfunctions.yml
faas-cli deploy -f ./samples.yml
faas-cli deploy -f ./samples.yml --label canary=true
faas-cli deploy -f ./samples.yml --filter "*gif*" --secret dockerhuborg
faas-cli deploy -f ./samples.yml --regex "fn[0-9]_.*"
faas-cli deploy -f ./samples.yml --replace=false
Expand Down Expand Up @@ -136,9 +142,22 @@ func runDeploy(cmd *cobra.Command, args []string) {
log.Fatalln(err)
}

labelMap := map[string]string{}
if function.Labels != nil {
labelMap = *function.Labels
}

labelArgumentMap, labelErr := parseMap(labelOpts, "label")
if labelErr != nil {
fmt.Printf("Error parsing labels: %v\n", labelErr)
os.Exit(1)
}

allLabels := mergeMap(labelMap, labelArgumentMap)

allEnvironment := mergeMap(function.Environment, fileEnvironment)

proxy.DeployFunction(function.FProcess, services.Provider.GatewayURL, function.Name, function.Image, function.Language, replace, allEnvironment, services.Provider.Network, constraints, update, secrets)
proxy.DeployFunction(function.FProcess, services.Provider.GatewayURL, function.Name, function.Image, function.Language, replace, allEnvironment, services.Provider.Network, constraints, update, secrets, allLabels)
}
} else {
if len(image) == 0 {
Expand All @@ -150,14 +169,34 @@ func runDeploy(cmd *cobra.Command, args []string) {
return
}

envvars, err := parseEnvvars(envvarOpts)
envvars, err := parseMap(envvarOpts, "env")
if err != nil {
fmt.Printf("Error parsing envvars: %v\n", err)
os.Exit(1)
}

proxy.DeployFunction(fprocess, gateway, functionName, image, language, replace, envvars, network, constraints, update, secrets)
labelMap, labelErr := parseMap(labelOpts, "label")
if labelErr != nil {
fmt.Printf("Error parsing labels: %v\n", labelErr)
os.Exit(1)
}

proxy.DeployFunction(fprocess, gateway, functionName, image, language, replace, envvars, network, constraints, update, secrets, labelMap)
}
}

func buildLabelMap(labelOpts []string) map[string]string {
labelMap := map[string]string{}
for _, opt := range labelOpts {
if !strings.Contains(opt, "=") {
fmt.Println("Error - label option does not contain a value")
} else {
index := strings.Index(opt, "=")

labelMap[opt[0:index]] = opt[index+1:]
}
}
return labelMap
}

func readFiles(files []string) (map[string]string, error) {
Expand All @@ -181,18 +220,18 @@ func readFiles(files []string) (map[string]string, error) {
return envs, nil
}

func parseEnvvars(envvars []string) (map[string]string, error) {
func parseMap(envvars []string, keyName string) (map[string]string, error) {
result := make(map[string]string)
for _, envvar := range envvars {
s := strings.SplitN(strings.TrimSpace(envvar), "=", 2)
envvarName := s[0]
envvarValue := s[1]

if !(len(envvarName) > 0) {
return nil, fmt.Errorf("Empty envvar name: [%s]", envvar)
return nil, fmt.Errorf("Empty %s name: [%s]", keyName, envvar)
}
if !(len(envvarValue) > 0) {
return nil, fmt.Errorf("Empty envvar value: [%s]", envvar)
return nil, fmt.Errorf("Empty %s value: [%s]", keyName, envvar)
}

result[envvarName] = envvarValue
Expand Down
5 changes: 4 additions & 1 deletion proxy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import (
)

// DeployFunction call FaaS server to deploy a new function
func DeployFunction(fprocess string, gateway string, functionName string, image string, language string, replace bool, envVars map[string]string, network string, constraints []string, update bool, secrets []string) {
func DeployFunction(fprocess string, gateway string, functionName string, image string,
language string, replace bool, envVars map[string]string, network string,
constraints []string, update bool, secrets []string, labels map[string]string) {

// Need to alter Gateway to allow nil/empty string as fprocess, to avoid this repetition.
var fprocessTemplate string
Expand Down Expand Up @@ -47,6 +49,7 @@ func DeployFunction(fprocess string, gateway string, functionName string, image
EnvVars: envVars,
Constraints: constraints,
Secrets: secrets, // TODO: allow registry auth to be specified or read from local Docker credentials store
Labels: &labels,
}

reqBytes, _ := json.Marshal(&req)
Expand Down
2 changes: 2 additions & 0 deletions proxy/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func Test_DeployFunction(t *testing.T) {
[]string{},
false,
[]string{},
map[string]string{},
)
})

Expand Down Expand Up @@ -64,6 +65,7 @@ func Test_DeployFunction_Not2xx(t *testing.T) {
[]string{},
false,
[]string{},
map[string]string{},
)
})

Expand Down
5 changes: 5 additions & 0 deletions stack/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ type Function struct {

Constraints *[]string `yaml:"constraints"`

// EnvironmentFile is a list of files to import and override environmental variables.
// These are overriden in order.
EnvironmentFile []string `yaml:"environment_file"`

Labels *map[string]string `yaml:"labels"`
}

// EnvironmentFile represents external file for environment data
type EnvironmentFile struct {
Environment map[string]string `yaml:"environment"`
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.