From b2be10f341569adeca574b067c29a27172802220 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20Fern=C3=A1ndez=20Mart=C3=ADnez?= Date: Thu, 3 Dec 2020 16:31:11 +0100 Subject: [PATCH] Adapting REGEXP to docker requirements According to the `docker` CLI, when running a container with a wrong name, the following output is shown: ``` > docker run -it --name '$$' alpine docker: Error response from daemon: Invalid container name ($$), only [a-zA-Z0-9][a-zA-Z0-9_.-] are allowed. ``` Concatenating `container:` to the expected regular expression `[a-zA-Z0-9][a-zA-Z0-9_.-]` helps us rejecting wrong values. --- pkg/skaffold/schema/validation/validation.go | 2 +- .../schema/validation/validation_test.go | 45 ++++++++++++++++++- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/pkg/skaffold/schema/validation/validation.go b/pkg/skaffold/schema/validation/validation.go index 172af5b0ca8..b8c67aea4e9 100644 --- a/pkg/skaffold/schema/validation/validation.go +++ b/pkg/skaffold/schema/validation/validation.go @@ -189,7 +189,7 @@ func validateDockerNetworkMode(artifacts []*latest.Artifact) (errs []error) { if mode == "none" || mode == "bridge" || mode == "host" { continue } - containerRegExp := regexp.MustCompile("container:.+") + containerRegExp := regexp.MustCompile("^container:[a-zA-Z0-9][a-zA-Z0-9_.-]*$") if containerRegExp.MatchString(mode) { continue } diff --git a/pkg/skaffold/schema/validation/validation_test.go b/pkg/skaffold/schema/validation/validation_test.go index 086796e07d6..4462958e590 100644 --- a/pkg/skaffold/schema/validation/validation_test.go +++ b/pkg/skaffold/schema/validation/validation_test.go @@ -309,13 +309,54 @@ func TestValidateNetworkMode(t *testing.T) { shouldErr: true, }, { - description: "container's network stack", + description: "wrong container's network stack '-not-valid'", artifacts: []*latest.Artifact{ { ImageName: "image/container", ArtifactType: latest.ArtifactType{ DockerArtifact: &latest.DockerArtifact{ - NetworkMode: "Container:unique-identifier", + NetworkMode: "Container:-not-valid", + }, + }, + }, + }, + shouldErr: true, + }, + { + description: "wrong container's network stack 'fussball'", + artifacts: []*latest.Artifact{ + { + ImageName: "image/container", + ArtifactType: latest.ArtifactType{ + DockerArtifact: &latest.DockerArtifact{ + NetworkMode: "Container:fußball", + }, + }, + }, + }, + shouldErr: true, + }, + { + description: "container's network stack 'unique'", + artifacts: []*latest.Artifact{ + { + ImageName: "image/container", + ArtifactType: latest.ArtifactType{ + DockerArtifact: &latest.DockerArtifact{ + NetworkMode: "Container:unique", + }, + }, + }, + }, + }, + { + description: "container's network stack 'unique-id.123'", + artifacts: []*latest.Artifact{ + { + ImageName: "image/container", + ArtifactType: latest.ArtifactType{ + DockerArtifact: &latest.DockerArtifact{ + NetworkMode: "Container:unique-id.123", }, }, },