Files
fission-src/pkg/fission-cli/cmd/httptrigger/parse.go
T
Ta-Ching ChenandGitHub 49d60b19f3 Add Ingress host, path and annotations support (#1325)
For each ingress controller, the format of ingress host,
path and annotations are different. To support different
kinds of controller, this PR adds new ingress config field
to http trigger spec. A user can set annotations, host and
path based on the type of underlying ingress controller
with CLI.

Command example:

fission route create --name foo \
    --url /foo/{bar} --function foofn --createingress \
    --ingressannotation "nginx.ingress.kubernetes.io/ssl-redirect=false" \
    --ingressannotation "nginx.ingress.kubernetes.io/use-regex=true" \
    --ingressrule "*=/foo/*"
2019-09-26 16:54:16 +08:00

119 lines
3.1 KiB
Go

/*
Copyright 2019 The Fission 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 httptrigger
import (
"fmt"
"strings"
fv1 "github.com/fission/fission/pkg/apis/fission.io/v1"
)
// GetIngressConfig returns an IngressConfig based on user inputs; return error if any.
func GetIngressConfig(annotations []string, rule string, fallbackRelativeURL string, oldIngressConfig *fv1.IngressConfig) (*fv1.IngressConfig, error) {
removeAnns, anns, err := getIngressAnnotations(annotations)
if err != nil {
return nil, err
}
isEmptyRule, host, path, err := getIngressHostRule(rule, fallbackRelativeURL)
if err != nil {
return nil, err
}
if oldIngressConfig == nil {
if isEmptyRule { // assign default value
host = "*"
path = fallbackRelativeURL
}
return &fv1.IngressConfig{
Annotations: anns,
Host: host,
Path: path,
}, nil
}
if removeAnns {
oldIngressConfig.Annotations = nil
} else if len(anns) > 0 {
if oldIngressConfig.Annotations == nil {
oldIngressConfig.Annotations = make(map[string]string, len(anns))
}
for k, v := range anns {
oldIngressConfig.Annotations[k] = v
}
}
if isEmptyRule {
// an empty rule means no new rule was given,
// leave host and path intact except when host
// or path is empty.
if len(oldIngressConfig.Host) == 0 {
oldIngressConfig.Host = "*"
}
if len(oldIngressConfig.Path) == 0 {
oldIngressConfig.Path = fallbackRelativeURL
}
} else {
oldIngressConfig.Host = host
oldIngressConfig.Path = path
}
return oldIngressConfig, nil
}
func getIngressAnnotations(annotations []string) (remove bool, anns map[string]string, err error) {
if len(annotations) == 0 {
return false, nil, nil
}
anns = make(map[string]string)
for _, ann := range annotations {
if ann == "-" {
// remove all annotations
return true, nil, nil
}
v := strings.Split(ann, "=")
if len(v) != 2 {
return false, nil, fmt.Errorf("illegal ingress annotation: %v", ann)
}
key, val := v[0], v[1]
anns[key] = val
}
return false, anns, nil
}
func getIngressHostRule(rule string, fallbackPath string) (empty bool, host string, path string, err error) {
if len(fallbackPath) == 0 {
return false, "", "", fmt.Errorf("fallback url cannot be empty")
}
if len(rule) == 0 {
return true, "", "", nil
}
if rule == "-" {
return false, "*", fallbackPath, nil
}
v := strings.Split(rule, "=")
if len(v) != 2 {
return false, "", "", fmt.Errorf("illegal ingress rule: %v", rule)
}
if len(v[0]) == 0 || len(v[1]) == 0 {
return false, "", "", fmt.Errorf("host (%v) or path (%v) cannot be empty", v[0], v[1])
}
return false, v[0], v[1], nil
}