Skip to content

Commit f480363

Browse files
ampantDean-Coakley
andcommitted
Update pkg/apis/configuration/v1alpha1/types.go
Co-Authored-By: Dean Coakley <[email protected]>
1 parent 48ef449 commit f480363

File tree

7 files changed

+141
-84
lines changed

7 files changed

+141
-84
lines changed

docs/virtualserver-and-virtualserverroute.md

+19
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,25 @@ tls:
221221
| `buffers` | Configures the buffers used for reading a response from the upstream server for a single connection. The buffers field configures the buffers used for reading a response from the upstream server for a single connection: number: 4 size: 8K . See the [proxy_buffers](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_buffers) directive for additional information. | `buffers` | No |
222222
| `buffer-size` | Sets the size of the buffer used for reading the first part of a response received from the upstream server. See the [proxy_buffer_size](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_buffer_size) directive. The default is set in the `proxy-buffer-size` ConfigMap key. | `string` | No |
223223

224+
### Upstream.Buffers
225+
Buffers defines Buffer Configuration for an Upstream. The buffers field configures the buffers used for reading a response from the upstream server for a single connection. For example
226+
227+
```yaml
228+
name: tea
229+
service: tea-svc
230+
port: 80
231+
buffering: true
232+
buffers:
233+
number: 4
234+
size: 8K
235+
buffer-size: 16k
236+
```
237+
238+
| Field | Description | Type | Required |
239+
| ----- | ----------- | ---- | -------- |
240+
| `number` | Configures the number of buffers. The default is set in the `proxy-buffers` ConfigMap key. The default is 8 | `int` | Yes |
241+
| `size` | Configures the size of a buffer. The default is set in the `proxy-buffers` ConfigMap key. Sizes can be specified in bytes, kilobytes (suffixes k and K) or megabytes (suffixes m and M), for example, “1024”, “8k”, “1m”. The default is 4k | `string` | Yes |
242+
224243
### Upstream.TLS
225244

226245
| Field | Description | Type | Required |

internal/configs/virtualserver.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,8 @@ func generateString(s string, defaultS string) string {
391391
return s
392392
}
393393

394-
func generateBuffers(s conf_v1alpha1.Buffers, defaultS string) string {
395-
if (conf_v1alpha1.Buffers{}) == s {
394+
func generateBuffers(s *conf_v1alpha1.UpstreamBuffers, defaultS string) string {
395+
if s == nil {
396396
return defaultS
397397
}
398398
return fmt.Sprintf("%v %v", s.Number, s.Size)

internal/configs/virtualserver_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/nginxinc/kubernetes-ingress/internal/configs/version2"
99
"github.com/nginxinc/kubernetes-ingress/internal/nginx"
10+
"github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/v1alpha1"
1011
conf_v1alpha1 "github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/v1alpha1"
1112
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1213
)
@@ -937,6 +938,29 @@ func TestGenerateString(t *testing.T) {
937938
}
938939
}
939940

941+
func TestGenerateBuffer(t *testing.T) {
942+
tests := []struct {
943+
inputS *v1alpha1.UpstreamBuffers
944+
expected string
945+
}{
946+
{
947+
inputS: nil,
948+
expected: "8 4k",
949+
},
950+
{
951+
inputS: &v1alpha1.UpstreamBuffers{Number: 8, Size: "16K"},
952+
expected: "8 16K",
953+
},
954+
}
955+
956+
for _, test := range tests {
957+
result := generateBuffers(test.inputS, "8 4k")
958+
if result != test.expected {
959+
t.Errorf("generateBuffer() return %v but expected %v", result, test.expected)
960+
}
961+
}
962+
}
963+
940964
func TestGenerateLocation(t *testing.T) {
941965
cfgParams := ConfigParams{
942966
ProxyConnectTimeout: "30s",

pkg/apis/configuration/v1alpha1/types.go

+23-23
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,29 @@ type VirtualServerSpec struct {
2525

2626
// Upstream defines an upstream.
2727
type Upstream struct {
28-
Name string `json:"name"`
29-
Service string `json:"service"`
30-
Port uint16 `json:"port"`
31-
LBMethod string `json:"lb-method"`
32-
FailTimeout string `json:"fail-timeout"`
33-
MaxFails *int `json:"max-fails"`
34-
MaxConns *int `json:"max-conns"`
35-
Keepalive *int `json:"keepalive"`
36-
ProxyConnectTimeout string `json:"connect-timeout"`
37-
ProxyReadTimeout string `json:"read-timeout"`
38-
ProxySendTimeout string `json:"send-timeout"`
39-
ProxyNextUpstream string `json:"next-upstream"`
40-
ProxyNextUpstreamTimeout string `json:"next-upstream-timeout"`
41-
ProxyNextUpstreamTries int `json:"next-upstream-tries"`
42-
ProxyBuffering *bool `json:"buffering"`
43-
ProxyBuffers Buffers `json:"buffers"`
44-
ProxyBufferSize string `json:"buffersize"`
45-
ClientMaxBodySize string `json:"client-max-body-size"`
46-
TLS UpstreamTLS `json:"tls"`
47-
HealthCheck *HealthCheck `json:"healthCheck"`
48-
}
49-
50-
//Buffers defines Buffer Configuration for an Upstream
28+
Name string `json:"name"`
29+
Service string `json:"service"`
30+
Port uint16 `json:"port"`
31+
LBMethod string `json:"lb-method"`
32+
FailTimeout string `json:"fail-timeout"`
33+
MaxFails *int `json:"max-fails"`
34+
MaxConns *int `json:"max-conns"`
35+
Keepalive *int `json:"keepalive"`
36+
ProxyConnectTimeout string `json:"connect-timeout"`
37+
ProxyReadTimeout string `json:"read-timeout"`
38+
ProxySendTimeout string `json:"send-timeout"`
39+
ProxyNextUpstream string `json:"next-upstream"`
40+
ProxyNextUpstreamTimeout string `json:"next-upstream-timeout"`
41+
ProxyNextUpstreamTries int `json:"next-upstream-tries"`
42+
ProxyBuffering *bool `json:"buffering"`
43+
ProxyBuffers *UpstreamBuffers `json:"buffers"`
44+
ProxyBufferSize string `json:"buffer-size"`
45+
ClientMaxBodySize string `json:"client-max-body-size"`
46+
TLS UpstreamTLS `json:"tls"`
47+
HealthCheck *HealthCheck `json:"healthCheck"`
48+
}
49+
50+
//UpstreamBuffers defines Buffer Configuration for an Upstream
5151
type UpstreamBuffers struct {
5252
Number int `json:"number"`
5353
Size string `json:"size"`

pkg/apis/configuration/v1alpha1/zz_generated.deepcopy.go

+21-17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/configuration/validation/validation.go

+25-21
Original file line numberDiff line numberDiff line change
@@ -96,54 +96,58 @@ func validateTime(time string, fieldPath *field.Path) field.ErrorList {
9696
}
9797

9898
// http://nginx.org/en/docs/syntax.html
99-
const sizeFmt = `\d+[kKmMgG]?`
100-
const sizeErrMsg = "must consist of numeric characters followed by a valid size suffix. 'k|K|m|M|g|G"
99+
const offsetFmt = `\d+[kKmMgG]?`
100+
const offsetErrMsg = "must consist of numeric characters followed by a valid size suffix. 'k|K|m|M|g|G"
101101

102-
var sizeRegexp = regexp.MustCompile("^" + sizeFmt + "$")
102+
var offsetRegexp = regexp.MustCompile("^" + offsetFmt + "$")
103103

104-
func validateSize(size string, fieldPath *field.Path) field.ErrorList {
104+
func validateOffset(size string, fieldPath *field.Path) field.ErrorList {
105105
allErrs := field.ErrorList{}
106106

107107
if size == "" {
108108
return allErrs
109109
}
110110

111-
if !sizeRegexp.MatchString(size) {
112-
msg := validation.RegexError(sizeErrMsg, sizeFmt, "16", "32k", "64M")
111+
if !offsetRegexp.MatchString(size) {
112+
msg := validation.RegexError(offsetErrMsg, offsetFmt, "16", "32k", "64M")
113113
return append(allErrs, field.Invalid(fieldPath, size, msg))
114114
}
115115

116116
return allErrs
117117
}
118118

119-
func validateBuffer(buff v1alpha1.Buffers, fieldPath *field.Path) field.ErrorList {
119+
const sizeFmt = `\d+[kKmM]?`
120+
const sizeErrMsg = "must consist of numeric characters followed by a valid size suffix. 'k|K|m|M"
121+
122+
var sizeRegexp = regexp.MustCompile("^" + sizeFmt + "$")
123+
124+
func validateBuffer(buff *v1alpha1.UpstreamBuffers, fieldPath *field.Path) field.ErrorList {
120125
allErrs := field.ErrorList{}
121126

122-
if (v1alpha1.Buffers{}) == buff {
127+
if buff == nil {
123128
return allErrs
124129
}
125130

126131
if buff.Number <= 0 {
127132
return append(allErrs, field.Invalid(fieldPath, buff.Number, "must be positive"))
128133
}
129134

130-
if buff.Size == "" {
131-
return append(allErrs, field.Invalid(fieldPath, buff.Size, "cannot be empty"))
132-
}
133-
134-
if buff.Size == "4k" || buff.Size == "8k" {
135-
return allErrs
136-
}
137-
return append(allErrs, field.Invalid(fieldPath, buff.Size, "must be 4k or 8k"))
135+
allErrs = validateSize(buff.Size, fieldPath)
136+
return allErrs
138137
}
139138

140-
func validateBufSize(size string, fieldPath *field.Path) field.ErrorList {
139+
func validateSize(size string, fieldPath *field.Path) field.ErrorList {
141140
allErrs := field.ErrorList{}
142141

143-
if size == "" || size == "4k" || size == "8k" {
142+
if size == "" {
144143
return allErrs
145144
}
146-
return append(allErrs, field.Invalid(fieldPath, size, "must be 4k or 8k"))
145+
146+
if !sizeRegexp.MatchString(size) {
147+
msg := validation.RegexError(sizeErrMsg, sizeFmt, "16", "32k", "64M")
148+
return append(allErrs, field.Invalid(fieldPath, size, msg))
149+
}
150+
return allErrs
147151
}
148152

149153
func validateUpstreamLBMethod(lBMethod string, fieldPath *field.Path, isPlus bool) field.ErrorList {
@@ -359,10 +363,10 @@ func validateUpstreams(upstreams []v1alpha1.Upstream, fieldPath *field.Path, isP
359363
allErrs = append(allErrs, validatePositiveIntOrZeroFromPointer(u.MaxFails, idxPath.Child("max-fails"))...)
360364
allErrs = append(allErrs, validatePositiveIntOrZeroFromPointer(u.Keepalive, idxPath.Child("keepalive"))...)
361365
allErrs = append(allErrs, validatePositiveIntOrZeroFromPointer(u.MaxConns, idxPath.Child("max-conns"))...)
362-
allErrs = append(allErrs, validateSize(u.ClientMaxBodySize, idxPath.Child("client-max-body-size"))...)
366+
allErrs = append(allErrs, validateOffset(u.ClientMaxBodySize, idxPath.Child("client-max-body-size"))...)
363367
allErrs = append(allErrs, validateUpstreamHealthCheck(u.HealthCheck, idxPath.Child("healthCheck"))...)
364368
allErrs = append(allErrs, validateBuffer(u.ProxyBuffers, idxPath.Child("buffers"))...)
365-
allErrs = append(allErrs, validateBufSize(u.ProxyBufferSize, idxPath.Child("buffer-size"))...)
369+
allErrs = append(allErrs, validateSize(u.ProxyBufferSize, idxPath.Child("buffer-size"))...)
366370

367371
for _, msg := range validation.IsValidPortNum(int(u.Port)) {
368372
allErrs = append(allErrs, field.Invalid(idxPath.Child("port"), u.Port, msg))

pkg/apis/configuration/validation/validation_test.go

+27-21
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,9 @@ func TestValidateUpstreamsFails(t *testing.T) {
328328
Name: "upstream1",
329329
Service: "test-1",
330330
Port: 80,
331-
ProxyBuffers: v1alpha1.Buffers{
331+
ProxyBuffers: &v1alpha1.UpstreamBuffers{
332332
Number: -1,
333-
Size: "1k",
333+
Size: "1G",
334334
},
335335
},
336336
},
@@ -345,7 +345,7 @@ func TestValidateUpstreamsFails(t *testing.T) {
345345
Name: "upstream1",
346346
Service: "test-1",
347347
Port: 80,
348-
ProxyBufferSize: "1k",
348+
ProxyBufferSize: "1G",
349349
},
350350
},
351351
expectedUpstreamNames: map[string]sets.Empty{
@@ -1689,53 +1689,59 @@ func TestValidateTime(t *testing.T) {
16891689
}
16901690
}
16911691

1692-
func TestValidateSize(t *testing.T) {
1692+
func TestValidateOffset(t *testing.T) {
16931693
var validInput = []string{"", "1", "10k", "11m", "1K", "100M"}
16941694
for _, test := range validInput {
1695-
allErrs := validateSize(test, field.NewPath("size-field"))
1695+
allErrs := validateOffset(test, field.NewPath("size-field"))
16961696
if len(allErrs) != 0 {
1697-
t.Errorf("validateSize(%q) returned an error for valid input", test)
1697+
t.Errorf("validateOffset(%q) returned an error for valid input", test)
16981698
}
16991699
}
17001700

17011701
var invalidInput = []string{"55mm", "2mG", "6kb", "-5k", "1L"}
17021702
for _, test := range invalidInput {
1703-
allErrs := validateSize(test, field.NewPath("size-field"))
1703+
allErrs := validateOffset(test, field.NewPath("size-field"))
17041704
if len(allErrs) == 0 {
1705-
t.Errorf("validateSize(%q) didn't return error for invalid input.", test)
1705+
t.Errorf("validateOffset(%q) didn't return error for invalid input.", test)
17061706
}
17071707
}
17081708
}
17091709

17101710
func TestValidateBuffer(t *testing.T) {
1711-
validbuff := v1alpha1.Buffers{Number: 8, Size: "8k"}
1712-
allErrs := validateBuffer(validbuff, field.NewPath("proxy_buffers"))
1711+
validbuff := &v1alpha1.UpstreamBuffers{Number: 8, Size: "8k"}
1712+
allErrs := validateBuffer(validbuff, field.NewPath("buffers-field"))
17131713

17141714
if len(allErrs) != 0 {
17151715
t.Errorf("validateBuffer returned errors %v valid input %v", allErrs, validbuff)
17161716
}
17171717

1718-
invalidbuff := v1alpha1.Buffers{Number: -8, Size: "15k"}
1719-
allErr := validateBuffer(invalidbuff, field.NewPath("proxy_buffers"))
1720-
if len(allErr) == 0 {
1721-
t.Errorf("validateBuffer(%q) didn't return error for invalid input.", invalidbuff)
1718+
invalidbuff := &v1alpha1.UpstreamBuffers{Number: -8, Size: "15G"}
1719+
allErrs = validateBuffer(invalidbuff, field.NewPath("buffers-field"))
1720+
if len(allErrs) == 0 {
1721+
t.Errorf("validateBuffer didn't return error %v for invalid input %v.", allErrs, invalidbuff)
1722+
}
1723+
1724+
invalidbuff = &v1alpha1.UpstreamBuffers{Number: 8, Size: "15G"}
1725+
allErrs = validateBuffer(invalidbuff, field.NewPath("buffers-field"))
1726+
if len(allErrs) == 0 {
1727+
t.Errorf("validateBuffer didn't return error %v for invalid input %v.", allErrs, invalidbuff)
17221728
}
17231729
}
17241730

1725-
func TestValidateBufSize(t *testing.T) {
1726-
var validInput = []string{"", "4k", "8k"}
1731+
func TestValidateSize(t *testing.T) {
1732+
var validInput = []string{"", "4k", "8K", "16m", "32M"}
17271733
for _, test := range validInput {
1728-
allErrs := validateBufSize(test, field.NewPath("proxy_buffer_size"))
1734+
allErrs := validateSize(test, field.NewPath("size-field"))
17291735
if len(allErrs) != 0 {
1730-
t.Errorf("validateBufSize(%q) returned an error for valid input", test)
1736+
t.Errorf("validateSize(%q) returned an error for valid input", test)
17311737
}
17321738
}
17331739

1734-
var invalidInput = []string{"55mm", "2mG", "6kb", "-5k", "1L"}
1740+
var invalidInput = []string{"55mm", "2mG", "6kb", "-5k", "1L", "5G"}
17351741
for _, test := range invalidInput {
1736-
allErrs := validateBufSize(test, field.NewPath("proxy_buffer_size"))
1742+
allErrs := validateSize(test, field.NewPath("size-field"))
17371743
if len(allErrs) == 0 {
1738-
t.Errorf("validateBufSize(%q) didn't return error for invalid input.", test)
1744+
t.Errorf("validateSize(%q) didn't return error for invalid input.", test)
17391745
}
17401746
}
17411747
}

0 commit comments

Comments
 (0)