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/*"
This commit is contained in:
@@ -547,20 +547,41 @@ type (
|
||||
|
||||
// HTTPTriggerSpec is for router to expose user functions at the given URL path.
|
||||
HTTPTriggerSpec struct {
|
||||
// NOT USED NOW
|
||||
Host string `json:"-"` //`json:"host"`
|
||||
// TODO: remove this field since we have IngressConfig already
|
||||
// Deprecated: the original idea of this field is not for setting Ingress.
|
||||
// Since we have IngressConfig now, remove Host after couple releases.
|
||||
Host string `json:"host"`
|
||||
|
||||
// RelativeURL is the exposed URL for external client to access a function with.
|
||||
RelativeURL string `json:"relativeurl"`
|
||||
|
||||
// If CreateIngress is true, router will create a ingress definition.
|
||||
CreateIngress bool `json:"createingress"`
|
||||
|
||||
// HTTP method to access a function.
|
||||
Method string `json:"method"`
|
||||
|
||||
// FunctionReference is a reference to the target function.
|
||||
FunctionReference FunctionReference `json:"functionref"`
|
||||
|
||||
// If CreateIngress is true, router will create a ingress definition.
|
||||
CreateIngress bool `json:"createingress"`
|
||||
|
||||
// TODO: make IngressConfig a independent Fission resource
|
||||
// IngressConfig for router to set up Ingress.
|
||||
IngressConfig IngressConfig `json:"ingressconfig"`
|
||||
}
|
||||
|
||||
// IngressConfig is for router to set up Ingress.
|
||||
IngressConfig struct {
|
||||
// Annotations will be add to metadata when creating Ingress.
|
||||
Annotations map[string]string `json:"annotations"`
|
||||
|
||||
// Path is for path matching. The format of path
|
||||
// depends on what ingress controller you used.
|
||||
Path string `json:"path"`
|
||||
|
||||
// Host is for ingress controller to apply rules. If
|
||||
// host is empty or "*", the rule applies to all
|
||||
// inbound HTTP traffic.
|
||||
Host string `json:"host"`
|
||||
}
|
||||
|
||||
// KubernetesWatchTriggerSpec
|
||||
|
||||
@@ -32,6 +32,8 @@ const (
|
||||
ErrorUnsupportedType = iota
|
||||
ErrorInvalidValue
|
||||
ErrorInvalidObject
|
||||
|
||||
totalAnnotationSizeLimitB int = 256 * (1 << 10) // 256 kB
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -444,6 +446,58 @@ func (spec HTTPTriggerSpec) Validate() error {
|
||||
}
|
||||
}
|
||||
|
||||
result = multierror.Append(result, spec.IngressConfig.Validate())
|
||||
|
||||
return result.ErrorOrNil()
|
||||
}
|
||||
|
||||
func (config IngressConfig) Validate() error {
|
||||
result := &multierror.Error{}
|
||||
|
||||
// Details for how to validate Ingress host rule,
|
||||
// see https://github.com/kubernetes/kubernetes/blob/release-1.16/pkg/apis/networking/validation/validation.go
|
||||
|
||||
if len(config.Path) > 0 {
|
||||
if !strings.HasPrefix(config.Path, "/") {
|
||||
result = multierror.Append(result, MakeValidationErr(ErrorInvalidValue, "HTTPTriggerSpec.IngressConfig.IngressRule.Path", config.Path, "must be an absolute path"))
|
||||
}
|
||||
|
||||
_, err := regexp.CompilePOSIX(config.Path)
|
||||
if err != nil {
|
||||
result = multierror.Append(result, MakeValidationErr(ErrorInvalidValue, "HTTPTriggerSpec.IngressConfig.IngressRule.Path", config.Path, "must be a valid regex"))
|
||||
}
|
||||
}
|
||||
|
||||
// In Ingress, to accept requests from all host, the host field will
|
||||
// be an empty string instead of "*" shown in kubectl. The router replaces
|
||||
// the asterisk with "" when creating/updateing the Ingress, so here we
|
||||
// skip the check if the Host is equal to "*".
|
||||
if len(config.Host) > 0 && config.Host != "*" {
|
||||
if strings.Contains(config.Host, "*") {
|
||||
for _, msg := range validation.IsWildcardDNS1123Subdomain(config.Host) {
|
||||
result = multierror.Append(result, MakeValidationErr(ErrorInvalidValue, "HTTPTriggerSpec.IngressConfig.IngressRule.Host", config.Host, msg))
|
||||
}
|
||||
}
|
||||
for _, msg := range validation.IsDNS1123Subdomain(config.Host) {
|
||||
result = multierror.Append(result, MakeValidationErr(ErrorInvalidValue, "HTTPTriggerSpec.IngressConfig.IngressRule.Host", config.Host, msg))
|
||||
}
|
||||
}
|
||||
|
||||
// Details for how to validate annotations,
|
||||
// see https://github.com/kubernetes/kubernetes/blob/512eccac1f1d72d6d1cb304bc565c50d1f2e295e/staging/src/k8s.io/apimachinery/pkg/api/validation/objectmeta.go#L46
|
||||
|
||||
var totalSize int64
|
||||
for k, v := range config.Annotations {
|
||||
for _, msg := range validation.IsQualifiedName(strings.ToLower(k)) {
|
||||
result = multierror.Append(result, MakeValidationErr(ErrorInvalidValue, "HTTPTriggerSpec.IngressConfig.Annotations.key", k, msg))
|
||||
}
|
||||
totalSize += (int64)(len(k)) + (int64)(len(v))
|
||||
}
|
||||
if totalSize > (int64)(totalAnnotationSizeLimitB) {
|
||||
msg := fmt.Sprintf("must have at most %v characters", totalSize)
|
||||
result = multierror.Append(result, MakeValidationErr(ErrorInvalidValue, "HTTPTriggerSpec.IngressConfig.Annotations.value", totalAnnotationSizeLimitB, msg))
|
||||
}
|
||||
|
||||
return result.ErrorOrNil()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user