Skip to content

Commit

Permalink
Resolve issue #356
Browse files Browse the repository at this point in the history
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 <ssuresh@pivotal.io>
  • Loading branch information
sukhil-suresh committed Apr 27, 2020
1 parent 4082690 commit 03efa41
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions cmd/build-init/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 5 additions & 3 deletions cmd/rebase/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"flag"
"log"
"os"

"github.com/buildpacks/imgutil/remote"
Expand Down Expand Up @@ -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)
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/dockercreds/parse_annoted_secrets.go
Original file line number Diff line number Diff line change
@@ -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, "=")
Expand All @@ -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
Expand Down
10 changes: 9 additions & 1 deletion pkg/dockercreds/parse_annoted_secrets_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package dockercreds_test

import (
"bytes"
"io/ioutil"
"log"
"os"
"path"
"testing"
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 03efa41

Please sign in to comment.