Skip to content

Commit b988fc9

Browse files
sambhavktock
authored andcommitted
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]> (cherry picked from commit 2a8dac1) Signed-off-by: Kohei Tokunaga <[email protected]>
1 parent 62853e6 commit b988fc9

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"
@@ -252,7 +253,13 @@ func fullID(ctx context.Context, c containerd.Container) string {
252253
func buildLabels(cmdLabels, imageLabels map[string]string) map[string]string {
253254
labels := make(map[string]string)
254255
for k, v := range imageLabels {
255-
labels[k] = v
256+
if err := clabels.Validate(k, v); err == nil {
257+
labels[k] = v
258+
} else {
259+
// In case the image label is invalid, we output a warning and skip adding it to the
260+
// container.
261+
logrus.WithError(err).Warnf("unable to add image label with key %s to the container", k)
262+
}
256263
}
257264
// labels from the command line will override image and the initial image config labels
258265
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
runhcsoptions "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options"
2727
"github.com/containerd/containerd"
2828
"github.com/containerd/containerd/containers"
29+
clabels "github.com/containerd/containerd/labels"
2930
"github.com/containerd/containerd/plugin"
3031
"github.com/containerd/containerd/reference/docker"
3132
"github.com/containerd/containerd/runtime/linux/runctypes"
@@ -34,6 +35,7 @@ import (
3435
imagedigest "github.com/opencontainers/go-digest"
3536
"github.com/pelletier/go-toml"
3637
"github.com/pkg/errors"
38+
"github.com/sirupsen/logrus"
3739
"golang.org/x/net/context"
3840
runtime "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
3941

@@ -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
"io/ioutil"
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)