ingress-nginx-helm/internal/ingress/annotations/mirror/main.go
Gerald Pape db49b9da6f
Fix mirror-target values without path separator and port (#9889)
* Remove variables with $ before feeding into url.Parse

Signed-off-by: Gerald Pape <gerald@giantswarm.io>

* Do not render invalid request mirroring config

Signed-off-by: Gerald Pape <gerald@giantswarm.io>

* Remove additional note from docs again

Signed-off-by: Gerald Pape <gerald@giantswarm.io>

* Include quotes in e2e test for mirror proxy_pass

---------

Signed-off-by: Gerald Pape <gerald@giantswarm.io>
2023-06-11 11:59:47 -07:00

108 lines
2.4 KiB
Go

/*
Copyright 2019 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 mirror
import (
"fmt"
"strings"
networking "k8s.io/api/networking/v1"
"k8s.io/ingress-nginx/internal/ingress/annotations/parser"
"k8s.io/ingress-nginx/internal/ingress/resolver"
)
// Config returns the mirror to use in a given location
type Config struct {
Source string `json:"source"`
RequestBody string `json:"requestBody"`
Target string `json:"target"`
Host string `json:"host"`
}
// Equal tests for equality between two Configuration types
func (m1 *Config) Equal(m2 *Config) bool {
if m1 == m2 {
return true
}
if m1 == nil || m2 == nil {
return false
}
if m1.Source != m2.Source {
return false
}
if m1.RequestBody != m2.RequestBody {
return false
}
if m1.Target != m2.Target {
return false
}
if m1.Host != m2.Host {
return false
}
return true
}
type mirror struct {
r resolver.Resolver
}
// NewParser creates a new mirror configuration annotation parser
func NewParser(r resolver.Resolver) parser.IngressAnnotation {
return mirror{r}
}
// ParseAnnotations parses the annotations contained in the ingress
// rule used to configure mirror
func (a mirror) Parse(ing *networking.Ingress) (interface{}, error) {
config := &Config{
Source: fmt.Sprintf("/_mirror-%v", ing.UID),
}
var err error
config.RequestBody, err = parser.GetStringAnnotation("mirror-request-body", ing)
if err != nil || config.RequestBody != "off" {
config.RequestBody = "on"
}
config.Target, err = parser.GetStringAnnotation("mirror-target", ing)
if err != nil {
config.Target = ""
config.Source = ""
}
config.Host, err = parser.GetStringAnnotation("mirror-host", ing)
if err != nil {
if config.Target != "" {
target := strings.Split(config.Target, "$")
url, err := parser.StringToURL(target[0])
if err != nil {
config.Host = ""
} else {
config.Host = url.Hostname()
}
}
}
return config, nil
}