Skip to content

Commit

Permalink
Merge branch 'main' into 317_labels_to_volumes
Browse files Browse the repository at this point in the history
  • Loading branch information
dfreilich authored Jan 27, 2021
2 parents f3717c9 + ddaff10 commit 62d3ce9
Show file tree
Hide file tree
Showing 21 changed files with 170 additions and 56 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.pack/
/pack
out/
benchmarks.test
*.out

# Jetbrains Goland
.idea/
Expand All @@ -10,4 +12,5 @@ artifacts/
.DS_Store

# Travis unencrypted file
.travis/key.pem
.travis/key.pem

17 changes: 13 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ acceptance-all:

clean:
@echo "> Cleaning workspace..."
@$(RMRF) .$/out || (exit 0)
@$(RMRF) .$/out benchmarks.test || (exit 0)

verify: verify-format lint

Expand All @@ -137,9 +137,18 @@ prepare-for-pr: tidy verify test
echo "-----------------\n" &&\
exit 0)

benchmark: out
@echo "> Running Benchmarks"
$(GOCMD) test -run=^$ -bench=. -benchtime=1s -benchmem -memprofile=./out/bench_mem.out -cpuprofile=./out/bench_cpu.out -tags=benchmarks ./benchmarks/ -v
# NOTE: You can analyze the results, using go tool pprof. For instance, you can start a server to see a graph of the cpu usage by running
# go tool pprof -http=":8082" out/bench_cpu.out. Alternatively, you can run go tool pprof, and in the ensuing cli, run
# commands like top10 or web to dig down into the cpu and memory usage
# For more, see https://blog.golang.org/pprof

# NOTE: Windows doesn't support `-p`
out:
@mkdir out
mkdir out$/tests
@mkdir out || (exit 0)
mkdir out$/tests || (exit 0)


.PHONY: clean build format imports lint test unit acceptance prepare-for-pr verify verify-format
.PHONY: clean build format imports lint test unit acceptance prepare-for-pr verify verify-format benchmark
85 changes: 85 additions & 0 deletions benchmarks/build_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// +build benchmarks

package benchmarks

import (
"bytes"
"fmt"
"path/filepath"
"testing"

dockerCli "github.com/docker/docker/client"
"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/buildpacks/pack"
"github.com/buildpacks/pack/internal/commands"
cfg "github.com/buildpacks/pack/internal/config"
ilogging "github.com/buildpacks/pack/internal/logging"
h "github.com/buildpacks/pack/testhelpers"
)

var (
baseImg = "some-org/" + h.RandString(10)
trustedImg = baseImg + "-trusted-"
builder = "cnbs/sample-builder:bionic"
mockAppPath = filepath.Join("..", "acceptance", "testdata", "mock_app")
)

func BenchmarkBuild(b *testing.B) {
dockerClient, err := dockerCli.NewClientWithOpts(dockerCli.FromEnv, dockerCli.WithVersion("1.38"))
if err != nil {
b.Error(errors.Wrap(err, "creating docker client"))
}

if err = h.PullImageWithAuth(dockerClient, builder, ""); err != nil {
b.Error(errors.Wrapf(err, "pulling builder %s", builder))
}

cmd := createCmd(b, dockerClient)

b.Run("with Untrusted Builder", func(b *testing.B) {
for i := 0; i < b.N; i++ {
// perform the operation we're analyzing
cmd.SetArgs([]string{fmt.Sprintf("%s%d", baseImg, i), "-p", mockAppPath, "-B", builder})
if err = cmd.Execute(); err != nil {
b.Error(errors.Wrapf(err, "running build #%d", i))
}
}
})

b.Run("with Trusted Builder", func(b *testing.B) {
for i := 0; i < b.N; i++ {
// perform the operation we're analyzing
cmd.SetArgs([]string{fmt.Sprintf("%s%d", trustedImg, i), "-p", mockAppPath, "-B", builder, "--trust-builder"})
if err = cmd.Execute(); err != nil {
b.Error(errors.Wrapf(err, "running build #%d", i))
}
}
})

// Cleanup
for i := 0; i < b.N; i++ {
if err = h.DockerRmi(dockerClient, fmt.Sprintf("%s%d", baseImg, i)); err != nil {
b.Error(errors.Wrapf(err, "deleting image #%d", i))
}

if err = h.DockerRmi(dockerClient, fmt.Sprintf("%s%d", trustedImg, i)); err != nil {
b.Error(errors.Wrapf(err, "deleting image #%d", i))
}
}

if err = h.DockerRmi(dockerClient, builder); err != nil {
b.Error(errors.Wrapf(err, "deleting builder %s", builder))
}
}

func createCmd(b *testing.B, docker *dockerCli.Client) *cobra.Command {
outBuf := bytes.Buffer{}
logger := ilogging.NewLogWithWriters(&outBuf, &outBuf)
packClient, err := pack.NewClient(pack.WithLogger(logger), pack.WithDockerClient(docker), pack.WithExperimental(true))
if err != nil {
b.Error(errors.Wrap(err, "creating packClient"))
}
return commands.Build(logger, cfg.Config{}, packClient)
}
13 changes: 13 additions & 0 deletions internal/build/phase_config_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package build
import (
"fmt"
"io"
"strings"

"github.com/docker/docker/api/types/container"

"github.com/buildpacks/pack/internal/style"
"github.com/buildpacks/pack/logging"
)

Expand Down Expand Up @@ -59,6 +61,17 @@ func NewPhaseConfigProvider(name string, lifecycleExec *LifecycleExecution, ops

provider.ctrConf.Cmd = append([]string{"/cnb/lifecycle/" + name}, provider.ctrConf.Cmd...)

lifecycleExec.logger.Debugf("Running the %s on OS %s with:", style.Symbol(provider.Name()), style.Symbol(provider.os))
lifecycleExec.logger.Debug("Container Settings:")
lifecycleExec.logger.Debugf(" Args: %s", style.Symbol(strings.Join(provider.ctrConf.Cmd, " ")))
lifecycleExec.logger.Debugf(" System Envs: %s", style.Symbol(strings.Join(provider.ctrConf.Env, " ")))
lifecycleExec.logger.Debugf(" Image: %s", style.Symbol(provider.ctrConf.Image))
lifecycleExec.logger.Debugf(" User: %s", style.Symbol(provider.ctrConf.User))
lifecycleExec.logger.Debugf(" Labels: %s", style.Symbol(fmt.Sprintf("%s", provider.ctrConf.Labels)))

lifecycleExec.logger.Debug("Host Settings:")
lifecycleExec.logger.Debugf(" Binds: %s", style.Symbol(strings.Join(provider.hostConf.Binds, " ")))
lifecycleExec.logger.Debugf(" Network Mode: %s", style.Symbol(string(provider.hostConf.NetworkMode)))
return provider
}

Expand Down
39 changes: 39 additions & 0 deletions internal/build/phase_config_provider_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package build_test

import (
"bytes"
"math/rand"
"testing"
"time"

ifakes "github.com/buildpacks/imgutil/fakes"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/strslice"
"github.com/docker/docker/client"
"github.com/heroku/color"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"

"github.com/buildpacks/pack/internal/build"
"github.com/buildpacks/pack/internal/build/fakes"
ilogging "github.com/buildpacks/pack/internal/logging"
"github.com/buildpacks/pack/logging"
h "github.com/buildpacks/pack/testhelpers"
)
Expand Down Expand Up @@ -275,5 +278,41 @@ func testPhaseConfigProvider(t *testing.T, when spec.G, it spec.S) {
h.AssertEq(t, isType, true)
})
})

when("verbose", func() {
it("prints debug information about the phase", func() {
var outBuf bytes.Buffer
logger := ilogging.NewLogWithWriters(&outBuf, &outBuf, ilogging.WithVerbose())

docker, err := client.NewClientWithOpts(client.FromEnv, client.WithVersion("1.38"))
h.AssertNil(t, err)

defaultBuilder, err := fakes.NewFakeBuilder()
h.AssertNil(t, err)

opts := build.LifecycleOptions{
AppPath: "some-app-path",
Builder: defaultBuilder,
}

lifecycleExec, err := build.NewLifecycleExecution(logger, docker, opts)
h.AssertNil(t, err)

_ = build.NewPhaseConfigProvider(
"some-name",
lifecycleExec,
build.WithRoot(),
)

h.AssertContains(t, outBuf.String(), "Running the 'some-name' on OS")
h.AssertContains(t, outBuf.String(), "Args: '/cnb/lifecycle/some-name'")
h.AssertContains(t, outBuf.String(), "System Envs: 'CNB_PLATFORM_API=0.4'")
h.AssertContains(t, outBuf.String(), "Image: 'some-builder-name'")
h.AssertContains(t, outBuf.String(), "User:")
h.AssertContains(t, outBuf.String(), "Labels: 'map[author:pack]'")
h.AssertContainsMatch(t, outBuf.String(), `Binds: \'\S+:\S+layers \S+:\S+workspace'`)
h.AssertContains(t, outBuf.String(), "Network Mode: ''")
})
})
})
}
2 changes: 1 addition & 1 deletion internal/commands/add_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func AddBuildpackRegistry(logger logging.Logger, cfg config.Config, cfgPath stri
Use: "add-registry <name> <url>",
Args: cobra.ExactArgs(2),
Hidden: true,
Short: prependExperimental("Add buildpack registry to your pack config file"),
Short: "Add buildpack registry to your pack config file",
Example: "pack add-registry my-registry https://github.com/buildpacks/my-registry",
Long: "A Buildpack Registry is a (still experimental) place to publish, store, and discover buildpacks. " +
"Users can add buildpacks registries using add-registry, and publish/yank buildpacks from it, as well as use those buildpacks when building applications.",
Expand Down
8 changes: 3 additions & 5 deletions internal/commands/buildpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ func NewBuildpackCommand(logger logging.Logger, cfg config.Config, client PackCl
}

cmd.AddCommand(BuildpackPackage(logger, cfg, client, packageConfigReader))
if cfg.Experimental {
cmd.AddCommand(BuildpackPull(logger, cfg, client))
cmd.AddCommand(BuildpackRegister(logger, cfg, client))
cmd.AddCommand(BuildpackYank(logger, cfg, client))
}
cmd.AddCommand(BuildpackPull(logger, cfg, client))
cmd.AddCommand(BuildpackRegister(logger, cfg, client))
cmd.AddCommand(BuildpackYank(logger, cfg, client))

AddHelpFlag(cmd, "buildpack")
return cmd
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/buildpack_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func BuildpackPull(logger logging.Logger, cfg config.Config, client PackClient)
cmd := &cobra.Command{
Use: "pull <uri>",
Args: cobra.ExactArgs(1),
Short: prependExperimental("Pull a buildpack from a registry and store it locally"),
Short: "Pull a buildpack from a registry and store it locally",
Example: "pack buildpack pull example/[email protected]",
RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
registry, err := config.GetRegistry(cfg, flags.BuildpackRegistry)
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/buildpack_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func BuildpackRegister(logger logging.Logger, cfg config.Config, client PackClie
cmd := &cobra.Command{
Use: "register <image>",
Args: cobra.ExactArgs(1),
Short: prependExperimental("Register a buildpack to a registry"),
Short: "Register a buildpack to a registry",
Example: "pack register my-buildpack",
RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
registry, err := config.GetRegistry(cfg, flags.BuildpackRegistry)
Expand Down
17 changes: 1 addition & 16 deletions internal/commands/buildpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,7 @@ func testBuildpackCommand(t *testing.T, when spec.G, it spec.S) {
h.AssertNil(t, cmd.Execute())
output := outBuf.String()
h.AssertContains(t, output, "Interact with buildpacks")
h.AssertContains(t, output, "Usage:")
h.AssertContains(t, output, "package")
for _, command := range []string{"register", "yank", "pull"} {
h.AssertNotContains(t, output, command)
}
})

it("only shows experimental commands if in the config", func() {
cmd = commands.NewBuildpackCommand(logger, config.Config{Experimental: true}, mockClient, fakes.NewFakePackageConfigReader())
cmd.SetOut(logging.GetWriterForLevel(logger, logging.InfoLevel))
cmd.SetArgs([]string{})
h.AssertNil(t, cmd.Execute())
output := outBuf.String()
h.AssertContains(t, output, "Interact with buildpacks")
h.AssertContains(t, output, "Usage:")
for _, command := range []string{"package", "register", "yank", "pull"} {
for _, command := range []string{"Usage", "package", "register", "yank", "pull"} {
h.AssertContains(t, output, command)
}
})
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/buildpack_yank.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func BuildpackYank(logger logging.Logger, cfg config.Config, client PackClient)
cmd := &cobra.Command{
Use: "yank <buildpack-id-and-version>",
Args: cobra.ExactArgs(1),
Short: prependExperimental("Yank a buildpack from a registry"),
Short: "Yank a buildpack from a registry",
Example: "pack yank [email protected]",
RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
buildpackIDVersion := args[0]
Expand Down
4 changes: 0 additions & 4 deletions internal/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ func multiValueHelp(name string) string {
return fmt.Sprintf("\nRepeat for each %s in order,\n or supply once by comma-separated list", name)
}

func prependExperimental(short string) string {
return fmt.Sprintf("(%s) %s", style.Warn("experimental"), short)
}

func getMirrors(config config.Config) map[string][]string {
mirrors := map[string][]string{}
for _, ri := range config.RunImages {
Expand Down
5 changes: 1 addition & 4 deletions internal/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@ func NewConfigCommand(logger logging.Logger, cfg config.Config, cfgPath string,
cmd.AddCommand(ConfigDefaultBuilder(logger, cfg, cfgPath, client))
cmd.AddCommand(ConfigExperimental(logger, cfg, cfgPath))
cmd.AddCommand(ConfigPullPolicy(logger, cfg, cfgPath))
cmd.AddCommand(ConfigRegistries(logger, cfg, cfgPath))
cmd.AddCommand(ConfigRunImagesMirrors(logger, cfg, cfgPath))
cmd.AddCommand(ConfigTrustedBuilder(logger, cfg, cfgPath))

if cfg.Experimental {
cmd.AddCommand(ConfigRegistries(logger, cfg, cfgPath))
}

AddHelpFlag(cmd, "config")
return cmd
}
Expand Down
5 changes: 1 addition & 4 deletions internal/commands/config_registries.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func ConfigRegistries(logger logging.Logger, cfg config.Config, cfgPath string)
cmd := &cobra.Command{
Use: "registries",
Aliases: []string{"registry", "registreis"},
Short: prependExperimental("List, add and remove registries"),
Short: "List, add and remove registries",
Long: bpRegistryExplanation + "\nYou can use the attached commands to list, add, and remove registries from your config",
RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
listRegistries(args, logger, cfg)
Expand All @@ -38,22 +38,19 @@ func ConfigRegistries(logger logging.Logger, cfg config.Config, cfgPath string)

listCmd := generateListCmd("registries", logger, cfg, listRegistries)
listCmd.Example = "pack config registries list"
listCmd.Short = prependExperimental(listCmd.Short)
listCmd.Long = bpRegistryExplanation + "List Registries saved in the pack config.\n\nShow the registries that are either added by default or have been explicitly added by using `pack config registries add`"
cmd.AddCommand(listCmd)

addCmd := generateAdd("registries", logger, cfg, cfgPath, addRegistry)
addCmd.Args = cobra.ExactArgs(2)
addCmd.Example = "pack config registries add my-registry https://github.com/buildpacks/my-registry"
addCmd.Short = prependExperimental(addCmd.Short)
addCmd.Long = bpRegistryExplanation + "Users can add registries from the config by using registries remove, and publish/yank buildpacks from it, as well as use those buildpacks when building applications."
addCmd.Flags().BoolVar(&setDefault, "default", false, "Set this buildpack registry as the default")
addCmd.Flags().StringVar(&registryType, "type", "github", "Type of buildpack registry [git|github]")
cmd.AddCommand(addCmd)

rmCmd := generateRemove("registries", logger, cfg, cfgPath, removeRegistry)
rmCmd.Example = "pack config registries remove myregistry"
rmCmd.Short = prependExperimental(rmCmd.Short)
rmCmd.Long = bpRegistryExplanation + "Users can remove registries from the config by using `pack config registries remove <registry>`"
cmd.AddCommand(rmCmd)

Expand Down
2 changes: 1 addition & 1 deletion internal/commands/config_registries_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func ConfigRegistriesDefault(logger logging.Logger, cfg config.Config, cfgPath s
cmd := &cobra.Command{
Use: "default <name>",
Args: cobra.MaximumNArgs(1),
Short: prependExperimental("Set default registry"),
Short: "Set default registry",
Example: "pack config registries default myregistry",
Long: bpRegistryExplanation + "\n\nYou can use this command to list, set, and unset a default registry, which will be used when looking for buildpacks:" +
"* To list your default registry, run `pack config registries default`.\n" +
Expand Down
8 changes: 0 additions & 8 deletions internal/commands/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,6 @@ func testConfigCommand(t *testing.T, when spec.G, it spec.S) {
h.AssertContains(t, output, command)
}
})

it("doesn't print experimental commands if experimental not enabled", func() {
command = commands.NewConfigCommand(logger, config.Config{}, configPath, mockClient)
command.SetArgs([]string{})
h.AssertNil(t, command.Execute())
output := outBuf.String()
h.AssertNotContains(t, output, "registries")
})
})
}

Expand Down
2 changes: 1 addition & 1 deletion internal/commands/list_registries.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func ListBuildpackRegistries(logger logging.Logger, cfg config.Config) *cobra.Co
Use: "list-registries",
Args: cobra.NoArgs,
Hidden: true,
Short: prependExperimental("List buildpack registries"),
Short: "List buildpack registries",
Example: "pack list-registries",
RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
deprecationWarning(logger, "list-registries", "config registries list")
Expand Down
Loading

0 comments on commit 62d3ce9

Please sign in to comment.