/* 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 ( networking "k8s.io/api/networking/v1" "k8s.io/klog/v2" "k8s.io/ingress-nginx/internal/ingress/annotations/parser" "k8s.io/ingress-nginx/internal/ingress/errors" "k8s.io/ingress-nginx/internal/ingress/resolver" ) var validProtocols = []string{"auto_http", "http", "https", "grpc", "grpcs", "fcgi"} const ( http = "HTTP" backendProtocolAnnotation = "backend-protocol" ) 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`, }, }, } type backendProtocol struct { r resolver.Resolver annotationConfig parser.Annotation } // NewParser creates a new backend protocol annotation parser func NewParser(r resolver.Resolver) parser.IngressAnnotation { return backendProtocol{ r: r, annotationConfig: backendProtocolConfig, } } func (a backendProtocol) GetDocumentation() parser.AnnotationFields { return a.annotationConfig.Annotations } // ParseAnnotations parses the annotations contained in the ingress // rule used to indicate the backend protocol. func (a backendProtocol) Parse(ing *networking.Ingress) (interface{}, error) { if ing.GetAnnotations() == nil { return http, nil } proto, err := parser.GetStringAnnotation(backendProtocolAnnotation, ing, a.annotationConfig.Annotations) if err != nil { if errors.IsValidationError(err) { klog.Warningf("validation error %s. Using HTTP as protocol", err) } return http, nil } return proto, nil } func (a backendProtocol) Validate(anns map[string]string) error { maxrisk := parser.StringRiskToRisk(a.r.GetSecurityConfiguration().AnnotationsRiskLevel) return parser.CheckAnnotationRisk(anns, maxrisk, backendProtocolConfig.Annotations) }