Skip to content

Commit 0217c3e

Browse files
fabianonunesoktalz
authored andcommitted
MEDIUM: merge HTTPConnectionMode annotations into one.
This commit creates the annotation `http-connection-mode` to replace the way to set the HTTP connection mode and deprecates `http-keep-alive` and `http-server-close`. Both the `http-keep-alive` and `http-server-close` annotations change the value of `defaults.HTTPConnectionMode`. But since the `http-keep-alive` annotation is evaluated last, it overwrites whatever value `http-server-close` has previously set, even if `http-keep-alive` is empty, undefined, or `'false'`. It's better to merge these annotations into one since they are mutually exclusive. After all, if you set `http-keep-alive: 'false'`, you won't get the expected result: no options will be set and the default will be used, **which is keep-alive**. Also, a third alternative is not supported via annotations (httpclose). The new annotation takes precedence over the old ones.
1 parent 18b47de commit 0217c3e

File tree

3 files changed

+79
-10
lines changed

3 files changed

+79
-10
lines changed

pkg/annotations/annotations.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ func (a annImpl) Global(g *models.Global, l *models.LogTargets) []Annotation {
7171

7272
func (a annImpl) Defaults(d *models.Defaults) []Annotation {
7373
return []Annotation{
74-
global.NewOption("http-server-close", d),
75-
global.NewOption("http-keep-alive", d),
7674
global.NewOption("dontlognull", d),
7775
global.NewOption("logasap", d),
7876
global.NewTimeout("timeout-http-request", d),
@@ -85,6 +83,7 @@ func (a annImpl) Defaults(d *models.Defaults) []Annotation {
8583
global.NewTimeout("timeout-tunnel", d),
8684
global.NewTimeout("timeout-http-keep-alive", d),
8785
global.NewLogFormat("log-format", d),
86+
global.NewHTTPConnectionMode("http-connection-mode", d),
8887
}
8988
}
9089

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
}

pkg/annotations/global/option.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ func (a *Option) Process(k store.K8s, annotations ...map[string]string) error {
3030
input := common.GetValue(a.GetName(), annotations...)
3131
if input == "" {
3232
switch a.name {
33-
case "http-server-close", "http-keep-alive":
34-
a.defaults.HTTPConnectionMode = ""
3533
case "dontlognull":
3634
a.defaults.Dontlognull = ""
3735
case "logasap":
@@ -48,10 +46,6 @@ func (a *Option) Process(k store.K8s, annotations ...map[string]string) error {
4846
}
4947
if enabled {
5048
switch a.name {
51-
case "http-server-close":
52-
a.defaults.HTTPConnectionMode = "http-server-close"
53-
case "http-keep-alive":
54-
a.defaults.HTTPConnectionMode = "http-keep-alive"
5549
case "dontlognull":
5650
a.defaults.Dontlognull = "enabled"
5751
case "logasap":
@@ -61,8 +55,6 @@ func (a *Option) Process(k store.K8s, annotations ...map[string]string) error {
6155
}
6256
} else {
6357
switch a.name {
64-
case "http-server-close", "http-keep-alive":
65-
a.defaults.HTTPConnectionMode = ""
6658
case "dontlognull":
6759
a.defaults.Dontlognull = "disabled"
6860
case "logasap":

0 commit comments

Comments
 (0)