2018-08-05 22:43:45 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 The Kubernetes 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 backendprotocol
|
|
|
|
|
|
|
|
import (
|
2021-08-21 20:42:00 +00:00
|
|
|
networking "k8s.io/api/networking/v1"
|
2020-08-08 23:31:02 +00:00
|
|
|
"k8s.io/klog/v2"
|
2018-08-05 22:43:45 +00:00
|
|
|
|
|
|
|
"k8s.io/ingress-nginx/internal/ingress/annotations/parser"
|
2023-07-22 03:32:07 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/errors"
|
2018-08-05 22:43:45 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/resolver"
|
|
|
|
)
|
|
|
|
|
2023-08-31 07:36:48 +00:00
|
|
|
var validProtocols = []string{"auto_http", "http", "https", "grpc", "grpcs", "fcgi"}
|
2023-07-22 03:32:07 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
http = "HTTP"
|
|
|
|
backendProtocolAnnotation = "backend-protocol"
|
2018-08-05 22:43:45 +00:00
|
|
|
)
|
|
|
|
|
2023-07-22 03:32:07 +00:00
|
|
|
var backendProtocolConfig = parser.Annotation{
|
|
|
|
Group: "backend",
|
|
|
|
Annotations: parser.AnnotationFields{
|
|
|
|
backendProtocolAnnotation: {
|
|
|
|
Validator: parser.ValidateOptions(validProtocols, false, true),
|
|
|
|
Scope: parser.AnnotationScopeLocation,
|
|
|
|
Risk: parser.AnnotationRiskLow, // Low, as it allows just a set of options
|
|
|
|
Documentation: `this annotation can be used to define which protocol should
|
|
|
|
be used to communicate with backends`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-08-05 22:43:45 +00:00
|
|
|
type backendProtocol struct {
|
2023-07-22 03:32:07 +00:00
|
|
|
r resolver.Resolver
|
|
|
|
annotationConfig parser.Annotation
|
2018-08-05 22:43:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewParser creates a new backend protocol annotation parser
|
|
|
|
func NewParser(r resolver.Resolver) parser.IngressAnnotation {
|
2023-07-22 03:32:07 +00:00
|
|
|
return backendProtocol{
|
|
|
|
r: r,
|
|
|
|
annotationConfig: backendProtocolConfig,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a backendProtocol) GetDocumentation() parser.AnnotationFields {
|
|
|
|
return a.annotationConfig.Annotations
|
2018-08-05 22:43:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseAnnotations parses the annotations contained in the ingress
|
|
|
|
// rule used to indicate the backend protocol.
|
2019-06-09 22:49:59 +00:00
|
|
|
func (a backendProtocol) Parse(ing *networking.Ingress) (interface{}, error) {
|
2018-08-05 22:43:45 +00:00
|
|
|
if ing.GetAnnotations() == nil {
|
2023-07-22 03:32:07 +00:00
|
|
|
return http, nil
|
2018-08-05 22:43:45 +00:00
|
|
|
}
|
|
|
|
|
2023-07-22 03:32:07 +00:00
|
|
|
proto, err := parser.GetStringAnnotation(backendProtocolAnnotation, ing, a.annotationConfig.Annotations)
|
2018-08-05 22:43:45 +00:00
|
|
|
if err != nil {
|
2023-07-22 03:32:07 +00:00
|
|
|
if errors.IsValidationError(err) {
|
|
|
|
klog.Warningf("validation error %s. Using HTTP as protocol", err)
|
|
|
|
}
|
|
|
|
return http, nil
|
2018-08-05 22:43:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return proto, nil
|
|
|
|
}
|
2023-07-22 03:32:07 +00:00
|
|
|
|
|
|
|
func (a backendProtocol) Validate(anns map[string]string) error {
|
|
|
|
maxrisk := parser.StringRiskToRisk(a.r.GetSecurityConfiguration().AnnotationsRiskLevel)
|
|
|
|
return parser.CheckAnnotationRisk(anns, maxrisk, backendProtocolConfig.Annotations)
|
|
|
|
}
|