Skip to content

Commit

Permalink
create a constant for pushing image and use that to parse error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
tejal29 committed Jun 22, 2020
1 parent aa43689 commit 670da61
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 9 deletions.
3 changes: 2 additions & 1 deletion pkg/skaffold/build/cache/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/tag"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/color"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
sErrors "github.com/GoogleContainerTools/skaffold/pkg/skaffold/errors"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
)

Expand Down Expand Up @@ -76,7 +77,7 @@ func (c *cache) Build(ctx context.Context, out io.Writer, tags tag.ImageTags, ar
case needsPushing:
color.Green.Fprintln(out, "Found. Pushing")
if err := result.Push(ctx, out, c); err != nil {
return nil, fmt.Errorf("pushing image: %w", err)
return nil, fmt.Errorf("%s: %w", sErrors.PushImageErrPrefix, err)
}

default:
Expand Down
5 changes: 3 additions & 2 deletions pkg/skaffold/docker/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/sirupsen/logrus"

sErrors "github.com/GoogleContainerTools/skaffold/pkg/skaffold/errors"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
)
Expand Down Expand Up @@ -247,7 +248,7 @@ func (l *localDaemon) Push(ctx context.Context, out io.Writer, ref string) (stri
RegistryAuth: registryAuth,
})
if err != nil {
return "", fmt.Errorf("could not push image %q: %w", ref, err)
return "", fmt.Errorf("%s %q: %w", sErrors.PushImageErrPrefix, ref, err)
}
defer rc.Close()

Expand All @@ -266,7 +267,7 @@ func (l *localDaemon) Push(ctx context.Context, out io.Writer, ref string) (stri
}

if err := streamDockerMessages(out, rc, auxCallback); err != nil {
return "", fmt.Errorf("could not push image %q: %w", ref, err)
return "", fmt.Errorf("%s %q: %w", sErrors.PushImageErrPrefix, ref, err)
}

if digest == "" {
Expand Down
4 changes: 4 additions & 0 deletions pkg/skaffold/errors/buildProblems.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
)

const (
PushImageErrPrefix = "could not push image"
)

var (
// for testing
getConfigForCurrentContext = config.GetConfigForCurrentKubectx
Expand Down
5 changes: 3 additions & 2 deletions pkg/skaffold/errors/err_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package errors

import (
"fmt"
"regexp"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
Expand All @@ -37,12 +38,12 @@ type problem struct {
// Build Problems are Errors in build phase
var knownBuildProblems = map[proto.StatusCode]problem{
proto.StatusCode_BUILD_PUSH_ACCESS_DENIED: {
regexp: re(".* pushing image: denied: .*"),
regexp: re(fmt.Sprintf(".* %s.* denied: .*", PushImageErrPrefix)),
description: "Build Failed. No push access to specified image repository",
suggestion: suggestBuildPushAccessDeniedAction,
},
proto.StatusCode_BUILD_PROJECT_NOT_FOUND: {
regexp: re("build failed: pushing image: unknown: Project"),
regexp: re(fmt.Sprintf("build failed: %s.* unknown: Project", PushImageErrPrefix)),
description: "Build Failed",
suggestion: func(config.SkaffoldOptions) string { return "Check your GCR project." },
},
Expand Down
8 changes: 4 additions & 4 deletions pkg/skaffold/errors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,28 @@ func TestShowAIError(t *testing.T) {
description: "Push access denied when neither default repo or global config is defined",
opts: config.SkaffoldOptions{},
context: &config.ContextConfig{},
err: fmt.Errorf("skaffold build failed: pushing image: denied: push access to resource"),
err: fmt.Errorf("skaffold build failed: could not push image: denied: push access to resource"),
expected: "Build Failed. No push access to specified image repository. Trying running with `--default-repo` flag.",
},
{
description: "Push access denied when default repo is defined",
opts: config.SkaffoldOptions{DefaultRepo: stringOrUndefined("gcr.io/test")},
context: &config.ContextConfig{},
err: fmt.Errorf("skaffold build failed: pushing image: denied: push access to resource"),
err: fmt.Errorf("skaffold build failed: could not push image image1 : denied: push access to resource"),
expected: "Build Failed. No push access to specified image repository. Check your `--default-repo` value or try `gcloud auth configure-docker`.",
},
{
description: "Push access denied when global repo is defined",
opts: config.SkaffoldOptions{},
context: &config.ContextConfig{DefaultRepo: "docker.io/global"},
err: fmt.Errorf("skaffold build failed: pushing image: denied: push access to resource"),
err: fmt.Errorf("skaffold build failed: could not push image: denied: push access to resource"),
expected: "Build Failed. No push access to specified image repository. Check your default-repo setting in skaffold config or try `docker login`.",
},
{
description: "unknown project error",
opts: config.SkaffoldOptions{},
context: &config.ContextConfig{DefaultRepo: "docker.io/global"},
err: fmt.Errorf("build failed: pushing image: unknown: Project"),
err: fmt.Errorf("build failed: could not push image: unknown: Project"),
expected: "Build Failed. Check your GCR project.",
},
{
Expand Down

0 comments on commit 670da61

Please sign in to comment.