-
Notifications
You must be signed in to change notification settings - Fork 2k
Add Safer Proxy_Buffer Config #8088
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AlexFenlon
wants to merge
14
commits into
main
Choose a base branch
from
feat/safe-buffer-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
252ee58
add helper function to enable proxy_buffers if proxy_buffer_size is e…
AlexFenlon 4d5a6d3
add a safe way to enable proxy_buffers and proxy_buffer_size
AlexFenlon 36ad41a
add `nginx.org/proxy-busy-buffers-size` annotation to ingress
AlexFenlon 0952f77
add `proxy-busy-buffer-size` to configmap, improve validation
AlexFenlon 5bea282
Merge remote-tracking branch 'refs/remotes/origin/main' into feat/saf…
AlexFenlon de8c064
rename ProxyBusyBufferSize to ProxyBusyBuffersSize to be more accurate
AlexFenlon ca2e16b
Add ProxyBusyBuffersSize to VirtualServer as `busy-buffers-size`
AlexFenlon 4162d5d
make validation better
AlexFenlon 7457a49
Merge branch 'main' into feat/safe-buffer-config
AlexFenlon 1bf7b14
make validation better
AlexFenlon 9cffde2
Merge remote-tracking branch 'origin/feat/safe-buffer-config' into fe…
AlexFenlon 10999ff
Update tests
AlexFenlon b9394c0
Update tests, improve validation
AlexFenlon 58fe7cd
fix validation
AlexFenlon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,11 @@ | |
package commonhelpers | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/nginx/kubernetes-ingress/internal/validation" | ||
) | ||
|
||
// MakeSecretPath will return the path to the secret with the base secrets | ||
|
@@ -27,3 +31,192 @@ func MakeOnOffFromBool(b *bool) string { | |
func BoolToPointerBool(b bool) *bool { | ||
return &b | ||
} | ||
|
||
// MakeProxyBuffers generates nginx proxy buffer configuration with validation | ||
func MakeProxyBuffers(proxyBuffers, proxyBufferSize, proxyBusyBuffersSize string) string { | ||
var parts []string | ||
|
||
proxyBufferSize = validation.NormalizeBufferSize(proxyBufferSize) | ||
proxyBusyBuffersSize = validation.NormalizeBufferSize(proxyBusyBuffersSize) | ||
|
||
proxyBufferSize, _ = capBufferLimits(proxyBufferSize, 0) | ||
|
||
if proxyBufferSize != "" && proxyBuffers == "" { | ||
count := 4 | ||
if proxyBusyBuffersSize != "" { | ||
bufferSizeBytes := validation.ParseSize(proxyBufferSize) | ||
if bufferSizeBytes > 0 { | ||
minBuffers := int((validation.ParseSize(proxyBusyBuffersSize) + bufferSizeBytes) / bufferSizeBytes) | ||
if minBuffers > count { | ||
count = minBuffers | ||
} | ||
} | ||
} | ||
proxyBuffers = fmt.Sprintf("%d %s", count, proxyBufferSize) | ||
parts = append(parts, fmt.Sprintf("proxy_buffer_size %s", proxyBufferSize), fmt.Sprintf("proxy_buffers %s", proxyBuffers)) | ||
Comment on lines
+45
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be refactored to make the code more readable, i.e. a function named after what we want to do |
||
} else if proxyBuffers != "" { | ||
originalBuffers := proxyBuffers | ||
proxyBuffers, proxyBufferSize = correctBufferConfig(proxyBuffers, proxyBufferSize) | ||
parts = append(parts, fmt.Sprintf("proxy_buffers %s", proxyBuffers)) | ||
if proxyBufferSize != "" { | ||
parts = append(parts, fmt.Sprintf("proxy_buffer_size %s", proxyBufferSize)) | ||
} | ||
|
||
if proxyBusyBuffersSize == "" && originalBuffers != proxyBuffers { | ||
proxyBusyBuffersSize = defaultBusyBufferSize(proxyBuffers) | ||
} | ||
} | ||
|
||
parts = addBusyBufferSizeConfig(parts, proxyBuffers, proxyBufferSize, proxyBusyBuffersSize) | ||
|
||
if len(parts) == 0 { | ||
return "" | ||
} | ||
return strings.Join(parts, ";\n\t\t") + ";" | ||
} | ||
|
||
func defaultBusyBufferSize(proxyBuffers string) string { | ||
fields := strings.Fields(proxyBuffers) | ||
if len(fields) >= 2 { | ||
// Return the individual buffer size as the default busy buffer size | ||
return fields[1] | ||
} | ||
return "" | ||
} | ||
|
||
// correctBufferConfig applies corrections to proxy buffer configuration | ||
func correctBufferConfig(proxyBuffers, proxyBufferSize string) (string, string) { | ||
fields := strings.Fields(strings.TrimSpace(proxyBuffers)) | ||
if len(fields) < 2 { | ||
return proxyBuffers, proxyBufferSize | ||
} | ||
|
||
count, _ := strconv.Atoi(fields[0]) | ||
|
||
// Capping buffer count and individual buffer size | ||
fields[1], count = capBufferLimits(fields[1], count) | ||
proxyBuffers = fmt.Sprintf("%d %s", count, fields[1]) | ||
|
||
if proxyBufferSize == "" { | ||
proxyBufferSize = fields[1] | ||
} else { | ||
// Validate user-defined proxy_buffer_size | ||
bufferSizeBytes := validation.ParseSize(proxyBufferSize) | ||
if bufferSizeBytes <= 0 { | ||
// Invalid → fallback to individual buffer size | ||
proxyBufferSize = fields[1] | ||
} else { | ||
// Valid → cap it if needed, but don't downgrade | ||
proxyBufferSize, _ = capBufferLimits(proxyBufferSize, 0) | ||
} | ||
} | ||
|
||
return proxyBuffers, proxyBufferSize | ||
} | ||
|
||
// capBufferLimits applies limits to buffer configurations to prevent issues | ||
func capBufferLimits(bufferSize string, bufferCount int) (string, int) { | ||
const maxReasonableBufferSize = 1000 * 1024 * 1024 // 1000MB in bytes | ||
const maxReasonableBufferCount = 1024 | ||
const minReasonableBufferCount = 2 | ||
|
||
cappedSize := bufferSize | ||
cappedCount := bufferCount | ||
|
||
// Cap buffer size | ||
if bufferSize != "" { | ||
bufferSizeBytes := validation.ParseSize(bufferSize) | ||
if bufferSizeBytes > maxReasonableBufferSize { | ||
cappedSize = validation.FormatSize(maxReasonableBufferSize) | ||
} | ||
} | ||
|
||
// Cap buffer count | ||
if bufferCount > maxReasonableBufferCount { | ||
cappedCount = maxReasonableBufferCount | ||
} else if bufferCount < minReasonableBufferCount { | ||
cappedCount = minReasonableBufferCount | ||
} | ||
|
||
return cappedSize, cappedCount | ||
} | ||
|
||
// addBusyBufferSizeConfig manages busy buffer size configuration and adds it to the parts slice | ||
func addBusyBufferSizeConfig(parts []string, proxyBuffers, proxyBufferSize, proxyBusyBuffersSize string) []string { | ||
// Always ensure we have a valid busy buffer size when we have any buffer configuration | ||
if len(parts) > 0 && proxyBuffers != "" { | ||
if proxyBusyBuffersSize == "" { | ||
proxyBusyBuffersSize = processBusyBufferSize(proxyBuffers, proxyBufferSize, "") | ||
} else { | ||
proxyBusyBuffersSize = processBusyBufferSize(proxyBuffers, proxyBufferSize, proxyBusyBuffersSize) | ||
} | ||
|
||
if proxyBusyBuffersSize != "" { | ||
parts = append(parts, fmt.Sprintf("proxy_busy_buffers_size %s", proxyBusyBuffersSize)) | ||
} | ||
} else if proxyBusyBuffersSize != "" { | ||
// Handle case where only busy buffer size is provided without buffer configuration | ||
parts = append(parts, fmt.Sprintf("proxy_busy_buffers_size %s", proxyBusyBuffersSize)) | ||
} | ||
|
||
return parts | ||
} | ||
|
||
// If proxyBusyBuffersSize is empty, it calculates a safe value | ||
// If proxyBusyBuffersSize is provided, it validates and corrects it | ||
func processBusyBufferSize(proxyBuffers, proxyBufferSize, proxyBusyBuffersSize string) string { | ||
fields := strings.Fields(proxyBuffers) | ||
if len(fields) < 2 { | ||
if proxyBusyBuffersSize == "" { | ||
return "" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we not return an error here? |
||
} | ||
return proxyBusyBuffersSize | ||
} | ||
|
||
count, _ := strconv.Atoi(fields[0]) | ||
individualSize := validation.ParseSize(fields[1]) | ||
|
||
if proxyBusyBuffersSize == "" { | ||
proxyBufferSize, _ = capBufferLimits(proxyBufferSize, 0) | ||
} | ||
bufferSize := validation.ParseSize(proxyBufferSize) | ||
|
||
maxSize := int64(count)*individualSize - individualSize | ||
minSize := individualSize | ||
if bufferSize > minSize { | ||
minSize = bufferSize | ||
} | ||
|
||
if maxSize <= 0 || minSize >= maxSize { | ||
safeSize := individualSize | ||
if maxSize > individualSize { | ||
safeSize = maxSize - 1024 // Leave 1k margin for safety | ||
if safeSize < individualSize { | ||
safeSize = individualSize | ||
} | ||
} | ||
return validation.FormatSize(safeSize) | ||
} | ||
|
||
// If no busy buffer size provided, calculate a safe one | ||
if proxyBusyBuffersSize == "" { | ||
// proxy_buffer_size + (2 * individual_buffer_size) | ||
recommendedSize := minSize + 2*individualSize | ||
if recommendedSize >= maxSize { | ||
return validation.FormatSize(minSize) | ||
} | ||
return validation.FormatSize(recommendedSize) | ||
} | ||
|
||
busySize := validation.ParseSize(proxyBusyBuffersSize) | ||
|
||
if busySize >= maxSize { | ||
return validation.FormatSize(maxSize) | ||
} | ||
|
||
if busySize < minSize { | ||
return validation.FormatSize(minSize) | ||
} | ||
|
||
return proxyBusyBuffersSize | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
given the existing naming convension, shouldn't this be