From 03efa41ef32af16b8db24bfdeb0f16734a8c1fc3 Mon Sep 17 00:00:00 2001 From: Sukhil Suresh Date: Mon, 27 Apr 2020 16:23:21 -0400 Subject: [PATCH] Resolve issue #356 Improve logging for the prepare step Before ``` $ logs -image tutorial-image -build 1 [prepare] prepare:fetch.go:88: Successfully cloned "https://github.com/spring-projects/spring-petclinic" @ "82cb521d636b282340378d80a6307a08e3d4a4c4" in path "/workspace" ``` After: ``` $ logs -image tutorial-image -build 5 [prepare] Loading secrets for "https://index.docker.io/v1/" from secret "tutorial-registry-credentials" [prepare] Successfully cloned "https://github.com/spring-projects/spring-petclinic" @ "400e3028f48a6c23f5156f6598dd10cb5e6a2849" in path "/workspace" ``` Signed-off-by: Sukhil Suresh --- cmd/build-init/main.go | 4 ++-- cmd/rebase/main.go | 8 +++++--- pkg/dockercreds/parse_annoted_secrets.go | 7 +++++-- pkg/dockercreds/parse_annoted_secrets_test.go | 10 +++++++++- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/cmd/build-init/main.go b/cmd/build-init/main.go index 8fba8b288..91b3799d6 100644 --- a/cmd/build-init/main.go +++ b/cmd/build-init/main.go @@ -55,9 +55,9 @@ const ( func main() { flag.Parse() - logger := log.New(os.Stdout, "prepare:", log.Lshortfile) + logger := log.New(os.Stdout, "", 0) - creds, err := dockercreds.ParseMountedAnnotatedSecrets(buildSecretsDir, dockerCredentials) + creds, err := dockercreds.ParseMountedAnnotatedSecrets(buildSecretsDir, dockerCredentials, logger) if err != nil { logger.Fatal(err) } diff --git a/cmd/rebase/main.go b/cmd/rebase/main.go index eeded40a5..420d153b7 100644 --- a/cmd/rebase/main.go +++ b/cmd/rebase/main.go @@ -2,6 +2,7 @@ package main import ( "flag" + "log" "os" "github.com/buildpacks/imgutil/remote" @@ -34,16 +35,17 @@ func init() { func main() { flag.Parse() tags := flag.Args() + logger := log.New(os.Stdout, "", 0) - cmd.Exit(rebase(tags)) + cmd.Exit(rebase(tags, logger)) } -func rebase(tags []string) error { +func rebase(tags []string, logger *log.Logger) error { if len(tags) < 1 { return cmd.FailCode(cmd.CodeInvalidArgs, "must provide one or more image tags") } - keychain, err := dockercreds.ParseMountedAnnotatedSecrets(buildSecretsDir, dockerCredentials) + keychain, err := dockercreds.ParseMountedAnnotatedSecrets(buildSecretsDir, dockerCredentials, logger) if err != nil { return cmd.FailErrCode(err, cmd.CodeInvalidArgs) } diff --git a/pkg/dockercreds/parse_annoted_secrets.go b/pkg/dockercreds/parse_annoted_secrets.go index e2b16d219..5e65e74a5 100644 --- a/pkg/dockercreds/parse_annoted_secrets.go +++ b/pkg/dockercreds/parse_annoted_secrets.go @@ -1,14 +1,16 @@ package dockercreds import ( + "log" "strings" "github.com/google/go-containerregistry/pkg/authn" - "github.com/pivotal/kpack/pkg/secret" "github.com/pkg/errors" + + "github.com/pivotal/kpack/pkg/secret" ) -func ParseMountedAnnotatedSecrets(volumeName string, secrets []string) (DockerCreds, error) { +func ParseMountedAnnotatedSecrets(volumeName string, secrets []string, logger *log.Logger) (DockerCreds, error) { var dockerCreds = DockerCreds{} for _, s := range secrets { splitSecret := strings.Split(s, "=") @@ -18,6 +20,7 @@ func ParseMountedAnnotatedSecrets(volumeName string, secrets []string) (DockerCr secretName := splitSecret[0] domain := splitSecret[1] + logger.Printf("Loading secrets for %q from secret %q", domain, secretName) auth, err := secret.ReadBasicAuthSecret(volumeName, secretName) if err != nil { return nil, err diff --git a/pkg/dockercreds/parse_annoted_secrets_test.go b/pkg/dockercreds/parse_annoted_secrets_test.go index 5f37622a5..7ea7a4281 100644 --- a/pkg/dockercreds/parse_annoted_secrets_test.go +++ b/pkg/dockercreds/parse_annoted_secrets_test.go @@ -1,7 +1,9 @@ package dockercreds_test import ( + "bytes" "io/ioutil" + "log" "os" "path" "testing" @@ -44,10 +46,16 @@ func testParseAnnotatedSecrets(t *testing.T, when spec.G, it spec.S) { when("ParseMountedAnnotatedSecrets", func() { it("parses the volume mounted creds", func() { + + logger := log.New(&bytes.Buffer{}, "", 0) + creds, err := dockercreds.ParseMountedAnnotatedSecrets(testDir, []string{ "gcr-creds=gcr.io", - "dockerhub-creds=index.docker.io"}) + "dockerhub-creds=index.docker.io", + }, + logger, + ) require.NoError(t, err) assert.Equal(t, dockercreds.DockerCreds{