kube-proxy: make iptables buffer-writing cleaner

This commit is contained in:
Tim Hockin
2021-11-04 00:31:19 -07:00
parent f558554ce0
commit f662170ff7
6 changed files with 158 additions and 146 deletions

View File

@@ -468,23 +468,35 @@ func GetClusterIPByFamily(ipFamily v1.IPFamily, service *v1.Service) string {
return ""
}
// WriteLine join all words with spaces, terminate with newline and write to buff.
func WriteLine(buf *bytes.Buffer, words ...string) {
type LineBuffer struct {
b bytes.Buffer
}
// Write joins all words with spaces, terminates with newline and writes to buf.
func (buf *LineBuffer) Write(words ...string) {
// We avoid strings.Join for performance reasons.
for i := range words {
buf.WriteString(words[i])
buf.b.WriteString(words[i])
if i < len(words)-1 {
buf.WriteByte(' ')
buf.b.WriteByte(' ')
} else {
buf.WriteByte('\n')
buf.b.WriteByte('\n')
}
}
}
// WriteBytesLine write bytes to buffer, terminate with newline
func WriteBytesLine(buf *bytes.Buffer, bytes []byte) {
buf.Write(bytes)
buf.WriteByte('\n')
// WriteBytes writes bytes to buffer, and terminates with newline.
func (buf *LineBuffer) WriteBytes(bytes []byte) {
buf.b.Write(bytes)
buf.b.WriteByte('\n')
}
func (buf *LineBuffer) Reset() {
buf.b.Reset()
}
func (buf *LineBuffer) Bytes() []byte {
return buf.b.Bytes()
}
// RevertPorts is closing ports in replacementPortsMap but not in originalPortsMap. In other words, it only

View File

@@ -17,7 +17,6 @@ limitations under the License.
package util
import (
"bytes"
"context"
"fmt"
"math/rand"
@@ -1171,13 +1170,13 @@ func TestWriteLine(t *testing.T) {
expected: "test1 test2 test3\n",
},
}
testBuffer := bytes.NewBuffer(nil)
testBuffer := LineBuffer{}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
testBuffer.Reset()
WriteLine(testBuffer, testCase.words...)
if !strings.EqualFold(testBuffer.String(), testCase.expected) {
t.Fatalf("write word is %v\n expected: %s, got: %s", testCase.words, testCase.expected, testBuffer.String())
testBuffer.Write(testCase.words...)
if want, got := testCase.expected, string(testBuffer.Bytes()); !strings.EqualFold(want, got) {
t.Fatalf("write word is %v\n expected: %s, got: %s", testCase.words, want, got)
}
})
}
@@ -1201,13 +1200,13 @@ func TestWriteBytesLine(t *testing.T) {
},
}
testBuffer := bytes.NewBuffer(nil)
testBuffer := LineBuffer{}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
testBuffer.Reset()
WriteBytesLine(testBuffer, testCase.bytes)
if !strings.EqualFold(testBuffer.String(), testCase.expected) {
t.Fatalf("write word is %v\n expected: %s, got: %s", testCase.bytes, testCase.expected, testBuffer.String())
testBuffer.WriteBytes(testCase.bytes)
if want, got := testCase.expected, string(testBuffer.Bytes()); !strings.EqualFold(want, got) {
t.Fatalf("write bytes is %v\n expected: %s, got: %s", testCase.bytes, want, got)
}
})
}
@@ -1244,12 +1243,12 @@ func TestWriteCountLines(t *testing.T) {
expected: 100000,
},
}
testBuffer := bytes.NewBuffer(nil)
testBuffer := LineBuffer{}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
testBuffer.Reset()
for i := 0; i < testCase.expected; i++ {
WriteLine(testBuffer, randSeq())
testBuffer.Write(randSeq())
}
n := CountBytesLines(testBuffer.Bytes())
if n != testCase.expected {