Merge pull request #7618 from changweige/enlarge-limit-key
image/label: print more characters of label keys
This commit is contained in:
commit
6c8c427166
@ -24,15 +24,18 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
maxSize = 4096
|
maxSize = 4096
|
||||||
|
// maximum length of key portion of error message if len of key + len of value > maxSize
|
||||||
|
keyMaxLen = 64
|
||||||
)
|
)
|
||||||
|
|
||||||
// Validate a label's key and value are under 4096 bytes
|
// Validate a label's key and value are under 4096 bytes
|
||||||
func Validate(k, v string) error {
|
func Validate(k, v string) error {
|
||||||
if (len(k) + len(v)) > maxSize {
|
total := len(k) + len(v)
|
||||||
if len(k) > 10 {
|
if total > maxSize {
|
||||||
k = k[:10]
|
if len(k) > keyMaxLen {
|
||||||
|
k = k[:keyMaxLen]
|
||||||
}
|
}
|
||||||
return fmt.Errorf("label key and value greater than maximum size (%d bytes), key: %s: %w", maxSize, k, errdefs.ErrInvalidArgument)
|
return fmt.Errorf("label key and value length (%d bytes) greater than maximum size (%d bytes), key: %s: %w", total, maxSize, k, errdefs.ErrInvalidArgument)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/containerd/containerd/errdefs"
|
"github.com/containerd/containerd/errdefs"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestValidLabels(t *testing.T) {
|
func TestValidLabels(t *testing.T) {
|
||||||
@ -51,3 +52,29 @@ func TestInvalidLabels(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLongKey(t *testing.T) {
|
||||||
|
key := strings.Repeat("s", keyMaxLen+1)
|
||||||
|
value := strings.Repeat("v", maxSize-len(key))
|
||||||
|
|
||||||
|
err := Validate(key, value)
|
||||||
|
assert.Equal(t, err, nil)
|
||||||
|
|
||||||
|
key = strings.Repeat("s", keyMaxLen+12)
|
||||||
|
value = strings.Repeat("v", maxSize-len(key)+1)
|
||||||
|
|
||||||
|
err = Validate(key, value)
|
||||||
|
assert.ErrorIs(t, err, errdefs.ErrInvalidArgument)
|
||||||
|
|
||||||
|
key = strings.Repeat("s", keyMaxLen-1)
|
||||||
|
value = strings.Repeat("v", maxSize-len(key))
|
||||||
|
|
||||||
|
err = Validate(key, value)
|
||||||
|
assert.Equal(t, err, nil)
|
||||||
|
|
||||||
|
key = strings.Repeat("s", keyMaxLen-1)
|
||||||
|
value = strings.Repeat("v", maxSize-len(key)-1)
|
||||||
|
|
||||||
|
err = Validate(key, value)
|
||||||
|
assert.Equal(t, err, nil)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user