From cd0563eae21eb432fb1617bc42e1648085959e54 Mon Sep 17 00:00:00 2001 From: Robert Maticevic Date: Tue, 10 Dec 2024 14:44:34 +0100 Subject: [PATCH] MINOR: httpchk: add support for host param --- config-parser/parsers/option-httpchk.go | 11 +++++++++-- config-parser/tests/integration/backend_data_test.go | 4 ++++ config-parser/tests/integration/backend_test.go | 1 + config-parser/tests/integration/defaults_data_test.go | 4 ++++ config-parser/tests/integration/defaults_test.go | 1 + config-parser/tests/option-httpchk_generated_test.go | 1 + config-parser/types/types.go | 2 ++ configuration/configuration.go | 2 ++ models/httpchk_params.go | 4 ++++ models/httpchk_params_compare.go | 8 ++++++++ models/httpchk_params_compare_test.go | 4 ++-- specification/build/haproxy_spec.yaml | 2 ++ specification/models/configuration/misc.yaml | 2 ++ test/configuration_test.go | 2 +- test/expected/structured.json | 4 +++- 15 files changed, 46 insertions(+), 6 deletions(-) diff --git a/config-parser/parsers/option-httpchk.go b/config-parser/parsers/option-httpchk.go index ec36be82..20420c81 100644 --- a/config-parser/parsers/option-httpchk.go +++ b/config-parser/parsers/option-httpchk.go @@ -59,13 +59,16 @@ func (s *OptionHttpchk) Parse(line string, parts []string, comment string) (stri Version: parts[4], Comment: comment, } - default: // > 5 + case 6: s.data = &types.OptionHttpchk{ Method: parts[2], URI: parts[3], - Version: strings.Join(parts[4:], " "), + Version: parts[4], + Host: parts[5], Comment: comment, } + default: // > 6 + return "", &errors.ParseError{Parser: "option httpchk", Line: line} } return "", nil } @@ -90,6 +93,10 @@ func (s *OptionHttpchk) Result() ([]common.ReturnResultLine, error) { sb.WriteString(" ") sb.WriteString(s.data.Version) } + if s.data.Host != "" { + sb.WriteString(" ") + sb.WriteString(s.data.Host) + } return []common.ReturnResultLine{ { Data: sb.String(), diff --git a/config-parser/tests/integration/backend_data_test.go b/config-parser/tests/integration/backend_data_test.go index 7d3383f7..cc889aeb 100644 --- a/config-parser/tests/integration/backend_data_test.go +++ b/config-parser/tests/integration/backend_data_test.go @@ -1057,6 +1057,10 @@ const backend_optionhttpchkmethoduriversion = ` backend test option httpchk ` +const backend_optionhttpchkmethoduriversionhos = ` +backend test + option httpchk +` const backend_optionmysqlcheck = ` backend test option mysql-check diff --git a/config-parser/tests/integration/backend_test.go b/config-parser/tests/integration/backend_test.go index 3f3d78b8..7df16fb6 100644 --- a/config-parser/tests/integration/backend_test.go +++ b/config-parser/tests/integration/backend_test.go @@ -694,6 +694,7 @@ func TestWholeConfigsSectionsBackend(t *testing.T) { {"backend_optionhttpchkOPTIONSHTTP11rnHost", backend_optionhttpchkOPTIONSHTTP11rnHost}, {"backend_optionhttpchkmethoduri", backend_optionhttpchkmethoduri}, {"backend_optionhttpchkmethoduriversion", backend_optionhttpchkmethoduriversion}, + {"backend_optionhttpchkmethoduriversionhos", backend_optionhttpchkmethoduriversionhos}, {"backend_optionhttpchkuri", backend_optionhttpchkuri}, {"backend_optionhttprestrictreqhdrnamesdel", backend_optionhttprestrictreqhdrnamesdel}, {"backend_optionhttprestrictreqhdrnamespre", backend_optionhttprestrictreqhdrnamespre}, diff --git a/config-parser/tests/integration/defaults_data_test.go b/config-parser/tests/integration/defaults_data_test.go index 4a7be79d..11f9dbe1 100644 --- a/config-parser/tests/integration/defaults_data_test.go +++ b/config-parser/tests/integration/defaults_data_test.go @@ -1033,6 +1033,10 @@ const defaults_optionhttpchkmethoduriversion = ` defaults test option httpchk ` +const defaults_optionhttpchkmethoduriversionhos = ` +defaults test + option httpchk +` const defaults_uniqueidformatXocicpfifpTsrtpid = ` defaults test unique-id-format %{+X}o_%ci:%cp_%fi:%fp_%Ts_%rt:%pid diff --git a/config-parser/tests/integration/defaults_test.go b/config-parser/tests/integration/defaults_test.go index 738e2e41..d2774152 100644 --- a/config-parser/tests/integration/defaults_test.go +++ b/config-parser/tests/integration/defaults_test.go @@ -681,6 +681,7 @@ func TestWholeConfigsSectionsDefaults(t *testing.T) { {"defaults_optionhttpchkOPTIONSHTTP11rnHost", defaults_optionhttpchkOPTIONSHTTP11rnHost}, {"defaults_optionhttpchkmethoduri", defaults_optionhttpchkmethoduri}, {"defaults_optionhttpchkmethoduriversion", defaults_optionhttpchkmethoduriversion}, + {"defaults_optionhttpchkmethoduriversionhos", defaults_optionhttpchkmethoduriversionhos}, {"defaults_optionhttpchkuri", defaults_optionhttpchkuri}, {"defaults_optionhttprestrictreqhdrnamesdel", defaults_optionhttprestrictreqhdrnamesdel}, {"defaults_optionhttprestrictreqhdrnamespre", defaults_optionhttprestrictreqhdrnamespre}, diff --git a/config-parser/tests/option-httpchk_generated_test.go b/config-parser/tests/option-httpchk_generated_test.go index 7595cf22..d3724c17 100644 --- a/config-parser/tests/option-httpchk_generated_test.go +++ b/config-parser/tests/option-httpchk_generated_test.go @@ -31,6 +31,7 @@ func TestOptionHttpchk(t *testing.T) { "option httpchk ": true, "option httpchk ": true, "option httpchk ": true, + "option httpchk ": true, "---": false, "--- ---": false, } diff --git a/config-parser/types/types.go b/config-parser/types/types.go index 48792b79..1af96424 100644 --- a/config-parser/types/types.go +++ b/config-parser/types/types.go @@ -782,11 +782,13 @@ type OptionForwardFor struct { //test:ok:option httpchk //test:ok:option httpchk //test:ok:option httpchk +//test:ok:option httpchk type OptionHttpchk struct { NoOption bool Method string URI string Version string + Host string Comment string } diff --git a/configuration/configuration.go b/configuration/configuration.go index c13c368f..421448da 100644 --- a/configuration/configuration.go +++ b/configuration/configuration.go @@ -761,6 +761,7 @@ func (s *SectionParser) getHttpchkData() (bool, interface{}) { Method: d.Method, URI: d.URI, Version: d.Version, + Host: d.Host, }) return true, "httpchk" } @@ -2093,6 +2094,7 @@ func (s *SectionObject) getHTTPChkData() (common.ParserData, error) { Method: params.Method, Version: params.Version, URI: params.URI, + Host: params.Host, }, nil } diff --git a/models/httpchk_params.go b/models/httpchk_params.go index 4499f0cc..4d7b8eeb 100644 --- a/models/httpchk_params.go +++ b/models/httpchk_params.go @@ -34,6 +34,10 @@ import ( // // swagger:model httpchk_params type HttpchkParams struct { + + // host + Host string `json:"host,omitempty"` + // method // Enum: ["HEAD","PUT","POST","GET","TRACE","PATCH","DELETE","CONNECT","OPTIONS"] // +kubebuilder:validation:Enum="HEAD","PUT","POST","GET","TRACE","PATCH","DELETE","CONNECT","OPTIONS"; diff --git a/models/httpchk_params_compare.go b/models/httpchk_params_compare.go index b88ac1c6..8e2adbef 100644 --- a/models/httpchk_params_compare.go +++ b/models/httpchk_params_compare.go @@ -24,6 +24,10 @@ package models // // opts ...Options are ignored in this method func (s HttpchkParams) Equal(t HttpchkParams, opts ...Options) bool { + if s.Host != t.Host { + return false + } + if s.Method != t.Method { return false } @@ -47,6 +51,10 @@ func (s HttpchkParams) Equal(t HttpchkParams, opts ...Options) bool { // opts ...Options are ignored in this method func (s HttpchkParams) Diff(t HttpchkParams, opts ...Options) map[string][]interface{} { diff := make(map[string][]interface{}) + if s.Host != t.Host { + diff["Host"] = []interface{}{s.Host, t.Host} + } + if s.Method != t.Method { diff["Method"] = []interface{}{s.Method, t.Method} } diff --git a/models/httpchk_params_compare_test.go b/models/httpchk_params_compare_test.go index 2a607ba7..79474281 100644 --- a/models/httpchk_params_compare_test.go +++ b/models/httpchk_params_compare_test.go @@ -170,7 +170,7 @@ func TestHttpchkParamsDiffFalse(t *testing.T) { for _, sample := range samples { result := sample.a.Diff(sample.b) - if len(result) != 3 { + if len(result) != 4 { json := jsoniter.ConfigCompatibleWithStandardLibrary a, err := json.Marshal(&sample.a) if err != nil { @@ -180,7 +180,7 @@ func TestHttpchkParamsDiffFalse(t *testing.T) { if err != nil { t.Errorf(err.Error()) } - t.Errorf("Expected HttpchkParams to be different in 3 cases, but it is not (%d) %s %s", len(result), a, b) + t.Errorf("Expected HttpchkParams to be different in 4 cases, but it is not (%d) %s %s", len(result), a, b) } } } diff --git a/specification/build/haproxy_spec.yaml b/specification/build/haproxy_spec.yaml index 090a5fac..46c96fc7 100644 --- a/specification/build/haproxy_spec.yaml +++ b/specification/build/haproxy_spec.yaml @@ -9963,6 +9963,8 @@ definitions: title: Default Server httpchk_params: properties: + host: + type: string method: enum: - HEAD diff --git a/specification/models/configuration/misc.yaml b/specification/models/configuration/misc.yaml index dd95982a..ab75068a 100644 --- a/specification/models/configuration/misc.yaml +++ b/specification/models/configuration/misc.yaml @@ -139,6 +139,8 @@ httpchk_params: pattern: '^[^ ]*$' version: type: string + host: + type: string redispatch: type: object required: diff --git a/test/configuration_test.go b/test/configuration_test.go index c5384fbc..98bf559a 100644 --- a/test/configuration_test.go +++ b/test/configuration_test.go @@ -717,7 +717,7 @@ backend test log-tag bla option http-keep-alive option forwardfor header X-Forwarded-For - option httpchk HEAD / + option httpchk HEAD / HTTP/1.1 www option tcpka option srvtcpka option checkcache diff --git a/test/expected/structured.json b/test/expected/structured.json index 20dae66b..a48d4a2e 100644 --- a/test/expected/structured.json +++ b/test/expected/structured.json @@ -552,7 +552,9 @@ "http_send_name_header": "X-My-Awesome-Header", "httpchk_params": { "method": "HEAD", - "uri": "/" + "uri": "/", + "version": "HTTP/1.1", + "host": "www" }, "id": 456, "ignore_persist_list": [