Merge pull request #6124 from samj1912/large-labels

Output a warning for label image labels instead of erroring
This commit is contained in:
Phil Estes 2021-10-19 08:36:50 -04:00 committed by GitHub
commit 9c488e5dae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 4 deletions

View File

@ -29,6 +29,7 @@ import (
"github.com/containerd/containerd/cmd/ctr/commands" "github.com/containerd/containerd/cmd/ctr/commands"
"github.com/containerd/containerd/cmd/ctr/commands/tasks" "github.com/containerd/containerd/cmd/ctr/commands/tasks"
"github.com/containerd/containerd/containers" "github.com/containerd/containerd/containers"
clabels "github.com/containerd/containerd/labels"
"github.com/containerd/containerd/namespaces" "github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/oci" "github.com/containerd/containerd/oci"
gocni "github.com/containerd/go-cni" gocni "github.com/containerd/go-cni"
@ -254,7 +255,13 @@ func fullID(ctx context.Context, c containerd.Container) string {
func buildLabels(cmdLabels, imageLabels map[string]string) map[string]string { func buildLabels(cmdLabels, imageLabels map[string]string) map[string]string {
labels := make(map[string]string) labels := make(map[string]string)
for k, v := range imageLabels { for k, v := range imageLabels {
labels[k] = v if err := clabels.Validate(k, v); err == nil {
labels[k] = v
} else {
// In case the image label is invalid, we output a warning and skip adding it to the
// container.
logrus.WithError(err).Warnf("unable to add image label with key %s to the container", k)
}
} }
// labels from the command line will override image and the initial image config labels // labels from the command line will override image and the initial image config labels
for k, v := range cmdLabels { for k, v := range cmdLabels {

View File

@ -26,6 +26,7 @@ import (
"github.com/containerd/containerd" "github.com/containerd/containerd"
"github.com/containerd/containerd/containers" "github.com/containerd/containerd/containers"
"github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/errdefs"
clabels "github.com/containerd/containerd/labels"
criconfig "github.com/containerd/containerd/pkg/cri/config" criconfig "github.com/containerd/containerd/pkg/cri/config"
containerstore "github.com/containerd/containerd/pkg/cri/store/container" containerstore "github.com/containerd/containerd/pkg/cri/store/container"
imagestore "github.com/containerd/containerd/pkg/cri/store/image" imagestore "github.com/containerd/containerd/pkg/cri/store/image"
@ -36,6 +37,7 @@ import (
"github.com/containerd/containerd/runtime/linux/runctypes" "github.com/containerd/containerd/runtime/linux/runctypes"
runcoptions "github.com/containerd/containerd/runtime/v2/runc/options" runcoptions "github.com/containerd/containerd/runtime/v2/runc/options"
"github.com/containerd/typeurl" "github.com/containerd/typeurl"
"github.com/sirupsen/logrus"
runhcsoptions "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options" runhcsoptions "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options"
imagedigest "github.com/opencontainers/go-digest" imagedigest "github.com/opencontainers/go-digest"
@ -285,8 +287,15 @@ func filterLabel(k, v string) string {
// buildLabel builds the labels from config to be passed to containerd // buildLabel builds the labels from config to be passed to containerd
func buildLabels(configLabels, imageConfigLabels map[string]string, containerType string) map[string]string { func buildLabels(configLabels, imageConfigLabels map[string]string, containerType string) map[string]string {
labels := make(map[string]string) labels := make(map[string]string)
for k, v := range imageConfigLabels { for k, v := range imageConfigLabels {
labels[k] = v if err := clabels.Validate(k, v); err == nil {
labels[k] = v
} else {
// In case the image label is invalid, we output a warning and skip adding it to the
// container.
logrus.WithError(err).Warnf("unable to add image label with key %s to the container", k)
}
} }
// labels from the CRI request (config) will override labels in the image config // labels from the CRI request (config) will override labels in the image config
for k, v := range configLabels { for k, v := range configLabels {

View File

@ -19,6 +19,7 @@ package server
import ( import (
"context" "context"
"os" "os"
"strings"
"testing" "testing"
"time" "time"
@ -119,8 +120,9 @@ func TestGetRepoDigestAndTag(t *testing.T) {
func TestBuildLabels(t *testing.T) { func TestBuildLabels(t *testing.T) {
imageConfigLabels := map[string]string{ imageConfigLabels := map[string]string{
"a": "z", "a": "z",
"d": "y", "d": "y",
"long-label": strings.Repeat("example", 10000),
} }
configLabels := map[string]string{ configLabels := map[string]string{
"a": "b", "a": "b",
@ -132,6 +134,7 @@ func TestBuildLabels(t *testing.T) {
assert.Equal(t, "d", newLabels["c"]) assert.Equal(t, "d", newLabels["c"])
assert.Equal(t, "y", newLabels["d"]) assert.Equal(t, "y", newLabels["d"])
assert.Equal(t, containerKindSandbox, newLabels[containerKindLabel]) assert.Equal(t, containerKindSandbox, newLabels[containerKindLabel])
assert.NotContains(t, newLabels, "long-label")
newLabels["a"] = "e" newLabels["a"] = "e"
assert.Empty(t, configLabels[containerKindLabel], "should not add new labels into original label") assert.Empty(t, configLabels[containerKindLabel], "should not add new labels into original label")