Skip to content

Commit 2a8dac1

Browse files
committed
Output a warning for label image labels instead of erroring
This change ignore errors during container runtime due to large image labels and instead outputs warning. This is necessary as certain image building tools like buildpacks may have large labels in the images which need not be passed to the container. Signed-off-by: Sambhav Kothari <[email protected]>
1 parent 10d9d1a commit 2a8dac1

3 files changed

Lines changed: 23 additions & 4 deletions

File tree

cmd/ctr/commands/run/run.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/containerd/containerd/cmd/ctr/commands"
3030
"github.com/containerd/containerd/cmd/ctr/commands/tasks"
3131
"github.com/containerd/containerd/containers"
32+
clabels "github.com/containerd/containerd/labels"
3233
"github.com/containerd/containerd/namespaces"
3334
"github.com/containerd/containerd/oci"
3435
gocni "github.com/containerd/go-cni"
@@ -254,7 +255,13 @@ func fullID(ctx context.Context, c containerd.Container) string {
254255
func buildLabels(cmdLabels, imageLabels map[string]string) map[string]string {
255256
labels := make(map[string]string)
256257
for k, v := range imageLabels {
257-
labels[k] = v
258+
if err := clabels.Validate(k, v); err == nil {
259+
labels[k] = v
260+
} else {
261+
// In case the image label is invalid, we output a warning and skip adding it to the
262+
// container.
263+
logrus.WithError(err).Warnf("unable to add image label with key %s to the container", k)
264+
}
258265
}
259266
// labels from the command line will override image and the initial image config labels
260267
for k, v := range cmdLabels {

pkg/cri/server/helpers.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/containerd/containerd"
2727
"github.com/containerd/containerd/containers"
2828
"github.com/containerd/containerd/errdefs"
29+
clabels "github.com/containerd/containerd/labels"
2930
criconfig "github.com/containerd/containerd/pkg/cri/config"
3031
containerstore "github.com/containerd/containerd/pkg/cri/store/container"
3132
imagestore "github.com/containerd/containerd/pkg/cri/store/image"
@@ -36,6 +37,7 @@ import (
3637
"github.com/containerd/containerd/runtime/linux/runctypes"
3738
runcoptions "github.com/containerd/containerd/runtime/v2/runc/options"
3839
"github.com/containerd/typeurl"
40+
"github.com/sirupsen/logrus"
3941

4042
runhcsoptions "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options"
4143
imagedigest "github.com/opencontainers/go-digest"
@@ -285,8 +287,15 @@ func filterLabel(k, v string) string {
285287
// buildLabel builds the labels from config to be passed to containerd
286288
func buildLabels(configLabels, imageConfigLabels map[string]string, containerType string) map[string]string {
287289
labels := make(map[string]string)
290+
288291
for k, v := range imageConfigLabels {
289-
labels[k] = v
292+
if err := clabels.Validate(k, v); err == nil {
293+
labels[k] = v
294+
} else {
295+
// In case the image label is invalid, we output a warning and skip adding it to the
296+
// container.
297+
logrus.WithError(err).Warnf("unable to add image label with key %s to the container", k)
298+
}
290299
}
291300
// labels from the CRI request (config) will override labels in the image config
292301
for k, v := range configLabels {

pkg/cri/server/helpers_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package server
1919
import (
2020
"context"
2121
"os"
22+
"strings"
2223
"testing"
2324
"time"
2425

@@ -119,8 +120,9 @@ func TestGetRepoDigestAndTag(t *testing.T) {
119120

120121
func TestBuildLabels(t *testing.T) {
121122
imageConfigLabels := map[string]string{
122-
"a": "z",
123-
"d": "y",
123+
"a": "z",
124+
"d": "y",
125+
"long-label": strings.Repeat("example", 10000),
124126
}
125127
configLabels := map[string]string{
126128
"a": "b",
@@ -132,6 +134,7 @@ func TestBuildLabels(t *testing.T) {
132134
assert.Equal(t, "d", newLabels["c"])
133135
assert.Equal(t, "y", newLabels["d"])
134136
assert.Equal(t, containerKindSandbox, newLabels[containerKindLabel])
137+
assert.NotContains(t, newLabels, "long-label")
135138

136139
newLabels["a"] = "e"
137140
assert.Empty(t, configLabels[containerKindLabel], "should not add new labels into original label")

0 commit comments

Comments
 (0)