Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added observability into build and extraction. #1037

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions cmd/build-helpers/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,11 @@ func extractSourcePackage(ctx context.Context, sourcePackageNamespace, sourcePac
}

r := tar.NewReader(bytes.NewReader(bs))
err = sourceimage.ExtractTar(targetPath, sourcePath, r)
count, err := sourceimage.ExtractTar(targetPath, sourcePath, r)
if err != nil {
return errors.Wrap(err, "failed to extract tar")
}
log.Printf("Extracted %d files\n", count)
return nil
}

Expand All @@ -173,7 +174,12 @@ func extractSourceImage(sourceImage, sourcePath, targetPath string) error {
}

log.Printf("Extracting contents from: %s to %s\n", sourcePath, targetPath)
return sourceimage.ExtractImage(targetPath, sourcePath, image)
count, err := sourceimage.ExtractImage(targetPath, sourcePath, image)
if err != nil {
return err
}
log.Printf("Extracted %d files\n", count)
return nil
}

func extractSourceImageWithRetry(retryDuration time.Duration, sourceImage, sourcePath, targetPath string) error {
Expand Down
5 changes: 3 additions & 2 deletions pkg/kf/sourcepackages/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"log"
"os"
"path/filepath"
"time"

v1alpha1 "github.com/google/kf/v2/pkg/apis/kf/v1alpha1"
cv1alpha1 "github.com/google/kf/v2/pkg/client/kf/clientset/versioned/typed/kf/v1alpha1"
Expand Down Expand Up @@ -160,7 +161,7 @@ func (p *sourcePackagesClient) UploadSourcePath(
// Upload the data.
logger := logging.FromContext(ctx).With(zap.Namespace("source upload"))
logger.Infof("Uploading directory %s (size=%0.2fKiB)", sourcePath, float64(fi.Size()/1024.0))

uploadStart := time.Now()
requestURI := fmt.Sprintf(
"/apis/upload.kf.dev/v1alpha1/proxy/namespaces/%s/%s",
app.Namespace,
Expand All @@ -173,7 +174,7 @@ func (p *sourcePackagesClient) UploadSourcePath(
return fmt.Errorf("failed to upload source directory: %v", err)
}

logger.Info("Successfully uploaded directory")
logger.Infof("Successfully uploaded source in %0.2f seconds", time.Since(uploadStart).Seconds())

// Success!
return nil
Expand Down
5 changes: 2 additions & 3 deletions pkg/kf/testutil/acceptance/acceptance_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,8 @@ func useCachedImage(t *testing.T, dir, acceptanceImage string) map[string]string
image, err := remote.Image(imageRef, remote.WithAuthFromKeychain(authn.DefaultKeychain))
testutil.AssertNil(t, "remote.Image", err)

testutil.AssertNil(t, "sourceImage.ExtractImage",
sourceimage.ExtractImage(dir, sourceimage.DefaultSourcePath, image),
)
_, err = sourceimage.ExtractImage(dir, sourceimage.DefaultSourcePath, image)
testutil.AssertNil(t, "sourceImage.ExtractImage", err)

files, err := ioutil.ReadDir(dir)
testutil.AssertNil(t, "ioutil.ReadDir", err)
Expand Down
12 changes: 7 additions & 5 deletions pkg/sourceimage/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

// ExtractImage extracts the files and directories from the given sourcePath in
// image to targetPath on the current system.
func ExtractImage(targetPath, sourcePath string, image v1.Image) error {
func ExtractImage(targetPath, sourcePath string, image v1.Image) (int, error) {
tarReader := mutate.Extract(image)
defer tarReader.Close()
r := tar.NewReader(tarReader)
Expand All @@ -39,19 +39,21 @@ func ExtractImage(targetPath, sourcePath string, image v1.Image) error {
//
// Special files are skipped, and all files are converted to their absolute
// path equivalents to avoid traversal attacks.
func ExtractTar(targetPath, sourcePath string, tarReader *tar.Reader) error {
func ExtractTar(targetPath, sourcePath string, tarReader *tar.Reader) (int, error) {
fileCount := 0
for {
header, err := tarReader.Next()

switch {
case err == io.EOF:
return nil
return fileCount, nil
case err != nil:
return err
return fileCount, err
default:
if err := extract(targetPath, sourcePath, header, tarReader); err != nil {
return err
return fileCount, err
}
fileCount++
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/sourceimage/extract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ func TestExtractTar(t *testing.T) {
defer os.RemoveAll(temp)

r := tar.NewReader(tarBytes)
tarErr := ExtractTar(temp, "/home", r)
count, tarErr := ExtractTar(temp, "/home", r)
testutil.AssertNil(t, "tar err", tarErr)
testutil.AssertEqual(t, "file count", 8, count)

AssertFile(t, temp, "foo/readme.txt", 0600, []byte("some-text"))
AssertFile(t, temp, "foo/prog", 0700, []byte("ELF"))
Expand Down