|
| 1 | +package global |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/haproxytech/client-native/v3/models" |
| 7 | + |
| 8 | + "github.com/haproxytech/kubernetes-ingress/pkg/annotations/common" |
| 9 | + "github.com/haproxytech/kubernetes-ingress/pkg/store" |
| 10 | + "github.com/haproxytech/kubernetes-ingress/pkg/utils" |
| 11 | +) |
| 12 | + |
| 13 | +var logger = utils.GetLogger() |
| 14 | + |
| 15 | +type HTTPConnectionMode struct { |
| 16 | + name string |
| 17 | + defaults *models.Defaults |
| 18 | +} |
| 19 | + |
| 20 | +func NewHTTPConnectionMode(n string, d *models.Defaults) *HTTPConnectionMode { |
| 21 | + return &HTTPConnectionMode{name: n, defaults: d} |
| 22 | +} |
| 23 | + |
| 24 | +func (a *HTTPConnectionMode) GetName() string { |
| 25 | + return a.name |
| 26 | +} |
| 27 | + |
| 28 | +// processAlternativeAnnotations process connection mode annotations |
| 29 | +// Deprecated: this function can be removed when `http-server-close` and `http-keep-alive` become obsolete |
| 30 | +func (a *HTTPConnectionMode) processAlternativeAnnotations(httpConnectionMode string, annotations ...map[string]string) bool { |
| 31 | + if httpConnectionMode != "" { |
| 32 | + return false |
| 33 | + } |
| 34 | + var mode string |
| 35 | + alternativeAnnotations := []string{"http-server-close", "http-keep-alive"} |
| 36 | + for _, annotation := range alternativeAnnotations { |
| 37 | + value := common.GetValue(annotation, annotations...) |
| 38 | + if value == "" { |
| 39 | + continue |
| 40 | + } |
| 41 | + enabled, err := utils.GetBoolValue(value, annotation) |
| 42 | + if err != nil { |
| 43 | + logger.Error(err.Error()) |
| 44 | + continue |
| 45 | + } |
| 46 | + logger.Warningf("annotation [%s] is DEPRECATED, use [http-connection-mode: \"%s\"] instead", annotation, annotation) |
| 47 | + if enabled { |
| 48 | + mode = annotation |
| 49 | + } |
| 50 | + } |
| 51 | + if mode != "" { |
| 52 | + a.defaults.HTTPConnectionMode = mode |
| 53 | + return true |
| 54 | + } |
| 55 | + return false |
| 56 | +} |
| 57 | + |
| 58 | +func (a *HTTPConnectionMode) Process(k store.K8s, annotations ...map[string]string) error { |
| 59 | + input := common.GetValue(a.GetName(), annotations...) |
| 60 | + |
| 61 | + // this block can be removed when annotations `http-server-close` and `http-keep-alive` become obsolete |
| 62 | + if a.processAlternativeAnnotations(input, annotations...) { |
| 63 | + return nil |
| 64 | + } |
| 65 | + |
| 66 | + switch input { |
| 67 | + case |
| 68 | + "", |
| 69 | + "http-keep-alive", |
| 70 | + "http-server-close", |
| 71 | + "httpclose": |
| 72 | + break |
| 73 | + default: |
| 74 | + return fmt.Errorf("invalid http-connection-mode value '%s'", input) |
| 75 | + } |
| 76 | + a.defaults.HTTPConnectionMode = input |
| 77 | + return nil |
| 78 | +} |
0 commit comments