diff --git a/configuration/backend_test.go b/configuration/backend_test.go index fd864eee..a4f95b39 100644 --- a/configuration/backend_test.go +++ b/configuration/backend_test.go @@ -382,6 +382,9 @@ func TestGetBackend(t *testing.T) { t.Errorf("%v: Compression.Types[1] wrong: %v", b.Name, b.Compression.Types[1]) } } + if b.Compression.Direction != "both" { + t.Errorf("%v: Compression.Direction not both: %v", b.Name, b.Compression.Direction) + } } if b.Checkcache != "enabled" { t.Errorf("%v: Checkcache not enabled: %v", b.Name, b.Checkcache) @@ -726,7 +729,8 @@ func TestCreateEditDeleteBackend(t *testing.T) { }, AcceptInvalidHTTPResponse: "enabled", Compression: &models.Compression{ - Offload: true, + Offload: true, + Direction: "both", }, LogHealthChecks: "enabled", Checkcache: "enabled", diff --git a/configuration/configuration.go b/configuration/configuration.go index d4ecdbc2..7e0969c9 100644 --- a/configuration/configuration.go +++ b/configuration/configuration.go @@ -1178,6 +1178,15 @@ func (s *SectionParser) compression() interface{} { } } + data, err = s.get("compression direction", false) + if err == nil { + d, ok := data.(*types.StringC) + if ok && d != nil { + compressionFound = true + compression.Direction = d.Value + } + } + if compressionFound { return compression } @@ -2612,6 +2621,16 @@ func (s *SectionObject) compression(field reflect.Value) error { if err != nil { return err } + + err = s.set("compression direction", nil) + if err != nil { + // compression direction does not exist on Frontends + var setErr error + if errors.As(parser_errors.ErrAttributeNotFound, &setErr) { + return nil + } + return err + } return nil } compression, ok := field.Elem().Interface().(models.Compression) @@ -2637,6 +2656,12 @@ func (s *SectionObject) compression(field reflect.Value) error { return err } } + if len(compression.Direction) > 0 { + err = s.set("compression direction", &types.StringC{Value: compression.Direction}) + if err != nil { + return err + } + } return nil } diff --git a/configuration/configuration_test.go b/configuration/configuration_test.go index 5d74d24a..ad5f2a4a 100644 --- a/configuration/configuration_test.go +++ b/configuration/configuration_test.go @@ -675,6 +675,7 @@ backend test no option accept-invalid-http-response no option h1-case-adjust-bogus-server compression type application/json text/plain + compression direction both srvtcpka-cnt 10 srvtcpka-idle 10s srvtcpka-intvl 10 diff --git a/go.mod b/go.mod index 97b5ea29..8d72d96b 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/google/go-cmp v0.6.0 github.com/google/renameio v1.0.1 github.com/google/uuid v1.6.0 - github.com/haproxytech/config-parser/v5 v5.1.0 + github.com/haproxytech/config-parser/v5 v5.1.1-0.20240220100901-1145ec548975 github.com/json-iterator/go v1.1.12 github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 github.com/mitchellh/mapstructure v1.5.0 diff --git a/go.sum b/go.sum index fa0f6978..5f9c1257 100644 --- a/go.sum +++ b/go.sum @@ -34,8 +34,8 @@ github.com/google/renameio v1.0.1 h1:Lh/jXZmvZxb0BBeSY5VKEfidcbcbenKjZFzM/q0fSeU github.com/google/renameio v1.0.1/go.mod h1:t/HQoYBZSsWSNK35C6CO/TpPLDVWvxOHboWUAweKUpk= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/haproxytech/config-parser/v5 v5.1.0 h1:gdifNXscsx4x++GQWw9Oj3JDpJMgSLtCiXgdQqxsYQU= -github.com/haproxytech/config-parser/v5 v5.1.0/go.mod h1:iy8nBB1eopwYbyeh3FQpjxZUxfcIDyTV9bW0F1t+cVA= +github.com/haproxytech/config-parser/v5 v5.1.1-0.20240220100901-1145ec548975 h1:CL/3SEvayajAyRHHOmKLsqiwZ8SVwAstCbKEvuxNknI= +github.com/haproxytech/config-parser/v5 v5.1.1-0.20240220100901-1145ec548975/go.mod h1:iy8nBB1eopwYbyeh3FQpjxZUxfcIDyTV9bW0F1t+cVA= github.com/haproxytech/go-logger v1.1.0 h1:HgGtYaI1ApkvbQdsm7f9AzQQoxTB7w37criTflh7IQE= github.com/haproxytech/go-logger v1.1.0/go.mod h1:OekUd8HCb7ubxMplzHUPBTHNxZmddOWfOjWclZsqIeM= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= diff --git a/models/compression.go b/models/compression.go index ee34e05e..011fa4be 100644 --- a/models/compression.go +++ b/models/compression.go @@ -39,6 +39,11 @@ type Compression struct { // algorithms Algorithms []string `json:"algorithms,omitempty"` + // direction + // Enum: [request response both] + // +kubebuilder:validation:Enum=request;response;both; + Direction string `json:"direction,omitempty"` + // offload Offload bool `json:"offload,omitempty"` @@ -54,6 +59,10 @@ func (m *Compression) Validate(formats strfmt.Registry) error { res = append(res, err) } + if err := m.validateDirection(formats); err != nil { + res = append(res, err) + } + if len(res) > 0 { return errors.CompositeValidationError(res...) } @@ -96,6 +105,51 @@ func (m *Compression) validateAlgorithms(formats strfmt.Registry) error { return nil } +var compressionTypeDirectionPropEnum []interface{} + +func init() { + var res []string + if err := json.Unmarshal([]byte(`["request","response","both"]`), &res); err != nil { + panic(err) + } + for _, v := range res { + compressionTypeDirectionPropEnum = append(compressionTypeDirectionPropEnum, v) + } +} + +const ( + + // CompressionDirectionRequest captures enum value "request" + CompressionDirectionRequest string = "request" + + // CompressionDirectionResponse captures enum value "response" + CompressionDirectionResponse string = "response" + + // CompressionDirectionBoth captures enum value "both" + CompressionDirectionBoth string = "both" +) + +// prop value enum +func (m *Compression) validateDirectionEnum(path, location string, value string) error { + if err := validate.EnumCase(path, location, value, compressionTypeDirectionPropEnum, true); err != nil { + return err + } + return nil +} + +func (m *Compression) validateDirection(formats strfmt.Registry) error { + if swag.IsZero(m.Direction) { // not required + return nil + } + + // value enum + if err := m.validateDirectionEnum("direction", "body", m.Direction); err != nil { + return err + } + + return nil +} + // ContextValidate validates this compression based on context it is used func (m *Compression) ContextValidate(ctx context.Context, formats strfmt.Registry) error { return nil diff --git a/models/compression_compare.go b/models/compression_compare.go index 55f7a142..a89ce5b8 100644 --- a/models/compression_compare.go +++ b/models/compression_compare.go @@ -37,6 +37,10 @@ func (s Compression) Equal(t Compression, opts ...Options) bool { return false } + if s.Direction != t.Direction { + return false + } + if s.Offload != t.Offload { return false } @@ -69,6 +73,10 @@ func (s Compression) Diff(t Compression, opts ...Options) map[string][]interface diff["Algorithms"] = []interface{}{s.Algorithms, t.Algorithms} } + if s.Direction != t.Direction { + diff["Direction"] = []interface{}{s.Direction, t.Direction} + } + if s.Offload != t.Offload { diff["Offload"] = []interface{}{s.Offload, t.Offload} } diff --git a/models/compression_compare_test.go b/models/compression_compare_test.go index 3ee2dcac..7f2258d5 100644 --- a/models/compression_compare_test.go +++ b/models/compression_compare_test.go @@ -171,7 +171,7 @@ func TestCompressionDiffFalse(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 { @@ -181,7 +181,7 @@ func TestCompressionDiffFalse(t *testing.T) { if err != nil { t.Errorf(err.Error()) } - t.Errorf("Expected Compression to be different in 3 cases, but it is not (%d) %s %s", len(result), a, b) + t.Errorf("Expected Compression 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 a641ce17..06ac2b47 100644 --- a/specification/build/haproxy_spec.yaml +++ b/specification/build/haproxy_spec.yaml @@ -9852,6 +9852,13 @@ definitions: type: string type: array x-omitempty: true + direction: + enum: + - request + - response + - both + type: string + x-omitempty: true offload: type: boolean types: diff --git a/specification/models/configuration/misc.yaml b/specification/models/configuration/misc.yaml index 13e125e5..b2144f33 100644 --- a/specification/models/configuration/misc.yaml +++ b/specification/models/configuration/misc.yaml @@ -310,6 +310,10 @@ compression: type: string offload: type: boolean + direction: + type: string + enum: [request, response, both] + x-omitempty: true stats_http_request: type: object required: