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

chore: upgrade ko #9043

Merged
merged 5 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
234 changes: 104 additions & 130 deletions go.mod

Large diffs are not rendered by default.

1,379 changes: 227 additions & 1,152 deletions go.sum

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions pkg/skaffold/build/buildpacks/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ func rewriteLifecycleStatusCode(lce error) error {

func mapLifecycleStatusCode(code int) string {
switch code {
case lifecycle.CodeFailed:
case lifecycle.CodeForFailed:
return "buildpacks lifecycle failed"
case lifecycle.CodeInvalidArgs:
case lifecycle.CodeForInvalidArgs:
return "lifecycle reported invalid arguments"
case lifecycle.CodeIncompatiblePlatformAPI:
case lifecycle.CodeForIncompatiblePlatformAPI:
return "incompatible version of Platform API"
case lifecycle.CodeIncompatibleBuildpackAPI:
case lifecycle.CodeForIncompatibleBuildpackAPI:
return "incompatible version of Buildpacks API"
default:
// we should never see CodeRebaseError or CodeLaunchError
Expand Down
8 changes: 4 additions & 4 deletions pkg/skaffold/build/buildpacks/lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ func TestLifecycleStatusCode(t *testing.T) {
code int
expected string
}{
{lifecycle.CodeFailed, "buildpacks lifecycle failed"},
{lifecycle.CodeInvalidArgs, "lifecycle reported invalid arguments"},
{lifecycle.CodeIncompatiblePlatformAPI, "incompatible version of Platform API"},
{lifecycle.CodeIncompatibleBuildpackAPI, "incompatible version of Buildpacks API"},
{lifecycle.CodeForFailed, "buildpacks lifecycle failed"},
{lifecycle.CodeForInvalidArgs, "lifecycle reported invalid arguments"},
{lifecycle.CodeForIncompatiblePlatformAPI, "incompatible version of Platform API"},
{lifecycle.CodeForIncompatibleBuildpackAPI, "incompatible version of Buildpacks API"},

{0, "lifecycle failed with status code 0"},
}
Expand Down
23 changes: 14 additions & 9 deletions pkg/skaffold/debug/cnb.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import (
"fmt"
"strings"

"github.com/buildpacks/lifecycle/api"
cnbl "github.com/buildpacks/lifecycle/launch"
cnb "github.com/buildpacks/lifecycle/platform"
cnb "github.com/buildpacks/lifecycle/platform/files"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shell "github.com/kballard/go-shellquote"

"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/debug/types"
Expand Down Expand Up @@ -170,15 +171,17 @@ func adjustCommandLine(m cnb.BuildMetadata, ic ImageConfiguration) (ImageConfigu

if p, clArgs, found := findCNBProcess(ic, m); found {
// Direct: p.Command is the command and p.Args are the arguments
pCommand := p.Command.Entries[0]
pArgs := append(p.Command.Entries[1:], p.Args...)
if p.Direct {
// Detect and unwrap `/bin/sh -c ...`-style command lines.
// For example, GCP Buildpacks turn Procfiles into `/bin/bash -c ...`
if len(p.Args) >= 2 && isShDashC(p.Command, p.Args[0]) {
p.Command = p.Args[1]
p.Args = p.Args[2:]
if len(pArgs) >= 2 && isShDashC(pCommand, pArgs[0]) {
pCommand = pArgs[1]
pArgs = pArgs[2:]
// and fall through to script type below
} else {
args := append([]string{p.Command}, p.Args...)
args := append([]string{pCommand}, pArgs...)
args = append(args, clArgs...)
ic.Entrypoint = []string{cnbLauncher}
ic.Arguments = args
Expand All @@ -189,14 +192,14 @@ func adjustCommandLine(m cnb.BuildMetadata, ic ImageConfiguration) (ImageConfigu
}
// Script type: split p.Command, pass it through the transformer, and then reassemble in the rewriter.
ic.Entrypoint = []string{cnbLauncher}
if args, err := shell.Split(p.Command); err == nil {
if args, err := shell.Split(pCommand); err == nil {
ic.Arguments = args
} else {
ic.Arguments = []string{p.Command}
ic.Arguments = []string{pCommand}
}
return ic, func(transformed []string) []string {
// reassemble back into a script with arguments
result := append([]string{shJoin(transformed)}, p.Args...)
result := append([]string{shJoin(transformed)}, pArgs...)
result = append(result, clArgs...)
return result
}
Expand Down Expand Up @@ -239,7 +242,9 @@ func findCNBProcess(ic ImageConfiguration, m cnb.BuildMetadata) (cnbl.Process, [
// determine process-type
processType := "web" // default buildpacks process type
platformAPI := ic.Env["CNB_PLATFORM_API"]
if platformAPI == "0.4" {
v, _ := api.NewVersion(platformAPI)

if v != nil && v.AtLeast("0.4") {
// Platform API 0.4-style /cnb/process/xxx
if !strings.HasPrefix(ic.Entrypoint[0], cnbProcessLauncherPrefix) {
return cnbl.Process{}, nil, false
Expand Down
27 changes: 15 additions & 12 deletions pkg/skaffold/debug/cnb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import (
"encoding/json"
"testing"

"github.com/buildpacks/lifecycle/api"
"github.com/buildpacks/lifecycle/launch"
cnb "github.com/buildpacks/lifecycle/platform"
cnb "github.com/buildpacks/lifecycle/platform/files"
"github.com/google/go-cmp/cmp"

"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/debug/types"
Expand Down Expand Up @@ -71,8 +72,8 @@ func TestHasCNBLauncherEntrypoint(t *testing.T) {
func TestFindCNBProcess(t *testing.T) {
// metadata with default process type `web`
md := cnb.BuildMetadata{Processes: []launch.Process{
{Type: "web", Command: "webProcess arg1 arg2", Args: []string{"posArg1", "posArg2"}},
{Type: "diag", Command: "diagProcess"},
{Type: "web", Command: launch.NewRawCommand([]string{"webProcess arg1 arg2"}), Args: []string{"posArg1", "posArg2"}},
{Type: "diag", Command: launch.NewRawCommand([]string{"diagProcess"})},
}}
tests := []struct {
description string
Expand Down Expand Up @@ -104,8 +105,8 @@ func TestFindCNBProcess(t *testing.T) {
func TestAdjustCommandLine(t *testing.T) {
// metadata with default process type `web`
md := cnb.BuildMetadata{Processes: []launch.Process{
{Type: "web", Command: "webProcess arg1 arg2", Args: []string{"posArg1", "posArg2"}},
{Type: "diag", Command: "diagProcess", Args: []string{"posArg1", "posArg2"}, Direct: true},
{Type: "web", Command: launch.NewRawCommand([]string{"webProcess arg1 arg2"}), Args: []string{"posArg1", "posArg2"}},
{Type: "diag", Command: launch.NewRawCommand([]string{"diagProcess"}), Args: []string{"posArg1", "posArg2"}, Direct: true},
}}
tests := []struct {
description string
Expand Down Expand Up @@ -202,22 +203,24 @@ func TestAdjustCommandLine(t *testing.T) {

func TestUpdateForCNBImage(t *testing.T) {
// metadata with default process type `web`
apiVersion := &api.Version{Major: 0, Minor: 12}
md := cnb.BuildMetadata{Processes: []launch.Process{

// script-style process with positional arguments equiv to `sh -c "webProcess arg1 arg2" posArg1 posArg2`
{Type: "web", Command: "webProcess arg1 arg2", Args: []string{"posArg1", "posArg2"}},
{Type: "diag", Command: "diagProcess"},
{Type: "web", Command: launch.NewRawCommand([]string{"webProcess arg1 arg2"}).WithPlatformAPI(apiVersion), Args: []string{"posArg1", "posArg2"}},
{Type: "diag", Command: launch.NewRawCommand([]string{"diagProcess"}).WithPlatformAPI(apiVersion)},
// direct process will exec `command cmdArg1`
{Type: "direct", Command: "command", Args: []string{"cmdArg1"}, Direct: true},
{Type: "sh-c", Command: "/bin/sh", Args: []string{"-c", "command arg1 arg2"}, Direct: true},
{Type: "direct", Command: launch.NewRawCommand([]string{"command"}).WithPlatformAPI(apiVersion), Args: []string{"cmdArg1"}, Direct: true},
{Type: "sh-c", Command: launch.NewRawCommand([]string{"/bin/sh"}).WithPlatformAPI(apiVersion), Args: []string{"-c", "command arg1 arg2"}, Direct: true},
// Google Buildpacks turns Procfiles into `/bin/bash -c cmdline`
{Type: "bash-c", Command: "/bin/bash", Args: []string{"-c", "command arg1 arg2"}, Direct: true},
{Type: "bash-c", Command: launch.NewRawCommand([]string{"/bin/bash"}).WithPlatformAPI(apiVersion), Args: []string{"-c", "command arg1 arg2"}, Direct: true},
}}
mdMarshalled, _ := json.Marshal(&md)
mdJSON := string(mdMarshalled)
// metadata with no default process type
mdnd := cnb.BuildMetadata{Processes: []launch.Process{
{Type: "diag", Command: "diagProcess"},
{Type: "direct", Command: "command", Args: []string{"cmdArg1"}, Direct: true},
{Type: "diag", Command: launch.NewRawCommand([]string{"diagProcess"}).WithPlatformAPI(apiVersion)},
{Type: "direct", Command: launch.NewRawCommand([]string{"command"}).WithPlatformAPI(apiVersion), Args: []string{"cmdArg1"}, Direct: true},
}}
mdndMarshalled, _ := json.Marshal(&mdnd)
mdndJSON := string(mdndMarshalled)
Expand Down
4 changes: 2 additions & 2 deletions pkg/skaffold/deploy/docker/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func (d *Deployer) setupDebugging(ctx context.Context, out io.Writer, artifact g
}

// create the volume used by the init container
v, err := d.client.VolumeCreate(ctx, volume.VolumeCreateBody{
v, err := d.client.VolumeCreate(ctx, volume.CreateOptions{
Labels: labels,
})
if err != nil {
Expand Down Expand Up @@ -305,7 +305,7 @@ func (d *Deployer) setupDebugging(ctx context.Context, out io.Writer, artifact g
return bindings, nil
}

func (d *Deployer) createMount(v types.Volume, labels map[string]string) mount.Mount {
func (d *Deployer) createMount(v volume.Volume, labels map[string]string) mount.Mount {
return mount.Mount{
Type: mount.TypeVolume,
Source: v.Name,
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/docker/debugger/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
)

var (
SupportVolumeMount = volume.VolumeCreateBody{Name: debug.DebuggingSupportFilesVolume}
SupportVolumeMount = volume.CreateOptions{Name: debug.DebuggingSupportFilesVolume}
TransformImage = transformImage // For testing
)

Expand Down
3 changes: 2 additions & 1 deletion pkg/skaffold/docker/docker_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/moby/buildkit/frontend/dockerfile/command"
"github.com/moby/buildkit/frontend/dockerfile/parser"
Expand Down Expand Up @@ -97,7 +98,7 @@ func validate(path string) bool {

// validate each node contains valid dockerfile directive
for _, child := range res.AST.Children {
_, ok := command.Commands[child.Value]
_, ok := command.Commands[strings.ToLower(child.Value)]
if !ok {
return false
}
Expand Down
19 changes: 12 additions & 7 deletions pkg/skaffold/docker/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ type LocalDaemon interface {
Push(ctx context.Context, out io.Writer, ref string) (string, error)
Pull(ctx context.Context, out io.Writer, ref string, platform v1.Platform) error
Load(ctx context.Context, out io.Writer, input io.Reader, ref string) (string, error)
Run(ctx context.Context, out io.Writer, opts ContainerCreateOpts) (<-chan container.ContainerWaitOKBody, <-chan error, string, error)
Run(ctx context.Context, out io.Writer, opts ContainerCreateOpts) (<-chan container.WaitResponse, <-chan error, string, error)
Delete(ctx context.Context, out io.Writer, id string) error
Tag(ctx context.Context, image, ref string) error
TagWithImageID(ctx context.Context, ref string, imageID string) (string, error)
Expand All @@ -105,7 +105,7 @@ type LocalDaemon interface {
Prune(ctx context.Context, images []string, pruneChildren bool) ([]string, error)
DiskUsage(ctx context.Context) (uint64, error)
RawClient() client.CommonAPIClient
VolumeCreate(ctx context.Context, opts volume.VolumeCreateBody) (types.Volume, error)
VolumeCreate(ctx context.Context, opts volume.CreateOptions) (volume.Volume, error)
VolumeRemove(ctx context.Context, id string) error
Stop(ctx context.Context, id string, stopTimeout *time.Duration) error
Remove(ctx context.Context, id string) error
Expand Down Expand Up @@ -163,7 +163,7 @@ func (l *localDaemon) Close() error {
return l.apiClient.Close()
}

func (l *localDaemon) VolumeCreate(ctx context.Context, opts volume.VolumeCreateBody) (types.Volume, error) {
func (l *localDaemon) VolumeCreate(ctx context.Context, opts volume.CreateOptions) (volume.Volume, error) {
return l.apiClient.VolumeCreate(ctx, opts)
}

Expand Down Expand Up @@ -205,7 +205,7 @@ func (l *localDaemon) ContainerExists(ctx context.Context, name string) bool {

// Delete stops, removes, and prunes a running container
func (l *localDaemon) Delete(ctx context.Context, out io.Writer, id string) error {
if err := l.apiClient.ContainerStop(ctx, id, nil); err != nil {
if err := l.apiClient.ContainerStop(ctx, id, container.StopOptions{}); err != nil {
log.Entry(ctx).Debugf("unable to stop running container: %s", err.Error())
}
if err := l.apiClient.ContainerRemove(ctx, id, types.ContainerRemoveOptions{}); err != nil {
Expand All @@ -219,7 +219,7 @@ func (l *localDaemon) Delete(ctx context.Context, out io.Writer, id string) erro
}

// Run creates a container from a given image reference, and returns a wait channel and the container ID.
func (l *localDaemon) Run(ctx context.Context, out io.Writer, opts ContainerCreateOpts) (<-chan container.ContainerWaitOKBody, <-chan error, string, error) {
func (l *localDaemon) Run(ctx context.Context, out io.Writer, opts ContainerCreateOpts) (<-chan container.WaitResponse, <-chan error, string, error) {
if opts.ContainerConfig == nil {
return nil, nil, "", fmt.Errorf("cannot call Run with empty container config")
}
Expand Down Expand Up @@ -617,7 +617,7 @@ func (l *localDaemon) ImageList(ctx context.Context, ref string) ([]types.ImageS
}

func (l *localDaemon) DiskUsage(ctx context.Context) (uint64, error) {
usage, err := l.apiClient.DiskUsage(ctx)
usage, err := l.apiClient.DiskUsage(ctx, types.DiskUsageOptions{})
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -719,7 +719,12 @@ func (l *localDaemon) Prune(ctx context.Context, images []string, pruneChildren
}

func (l *localDaemon) Stop(ctx context.Context, id string, stopTimeout *time.Duration) error {
if err := l.apiClient.ContainerStop(ctx, id, stopTimeout); err != nil {
var so container.StopOptions
if stopTimeout != nil {
seconds := int(stopTimeout.Seconds())
so.Timeout = &seconds
ericzzzzzzz marked this conversation as resolved.
Show resolved Hide resolved
}
if err := l.apiClient.ContainerStop(ctx, id, so); err != nil {
log.Entry(ctx).Debugf("unable to stop running container: %s", err.Error())
return err
}
Expand Down
21 changes: 12 additions & 9 deletions pkg/skaffold/docker/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (

v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/moby/buildkit/frontend/dockerfile/command"
"github.com/moby/buildkit/frontend/dockerfile/dockerfile2llb"
"github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/moby/buildkit/frontend/dockerfile/parser"
"github.com/moby/buildkit/frontend/dockerfile/shell"
Expand Down Expand Up @@ -133,7 +132,7 @@ func ExtractOnlyCopyCommands(absDockerfilePath string) ([]FromTo, error) {
workdir := "/"
envs := make([]string, 0)
for _, node := range res.AST.Children {
switch node.Value {
switch strings.ToLower(node.Value) {
case command.Add, command.Copy:
cpCmd, err := readCopyCommand(node, envs, workdir)
if err != nil {
Expand All @@ -158,7 +157,7 @@ func filterUnusedBuildArgs(dockerFile io.Reader, buildArgs map[string]*string) (
}
m := make(map[string]*string)
for _, n := range res.AST.Children {
if n.Value != command.Arg {
if strings.ToLower(n.Value) != command.Arg {
continue
}
k := strings.SplitN(n.Next.Value, "=", 2)[0]
Expand All @@ -176,7 +175,7 @@ func expandBuildArgs(nodes []*parser.Node, buildArgs map[string]*string) error {
}

for i, node := range nodes {
if node.Value != command.Arg {
if strings.ToLower(node.Value) != command.Arg {
continue
}

Expand All @@ -194,7 +193,7 @@ func expandBuildArgs(nodes []*parser.Node, buildArgs map[string]*string) error {

for _, node := range nodes[i+1:] {
// Stop replacements if an arg is redefined with the same key
if node.Value == command.Arg && strings.Split(node.Next.Value, "=")[0] == key {
if strings.ToLower(node.Value) == command.Arg && strings.Split(node.Next.Value, "=")[0] == key {
break
}

Expand Down Expand Up @@ -259,7 +258,7 @@ func extractCopyCommands(ctx context.Context, nodes []*parser.Node, onlyLastImag
workdir := "/"
envs := make([]string, 0)
for _, node := range nodes {
switch node.Value {
switch strings.ToLower(node.Value) {
case command.From:
from := fromInstruction(node)
if from.as != "" {
Expand Down Expand Up @@ -367,7 +366,7 @@ func expandOnbuildInstructions(ctx context.Context, nodes []*parser.Node, cfg Co
var expandedNodes []*parser.Node
n := 0
for m, node := range nodes {
if node.Value == command.From {
if strings.ToLower(node.Value) == command.From {
from := fromInstruction(node)

// onbuild should immediately follow the from command
Expand Down Expand Up @@ -477,12 +476,16 @@ func resolveDir(cwd, targetDir string) string {
}

func validateParsedDockerfile(r io.Reader, res *parser.Result) error {
b, err := io.ReadAll(r)
if err != nil {
return err
}
// skip validation for dockerfiles using explicit `syntax` directive
if _, _, _, usesSyntax := dockerfile2llb.DetectSyntax(r); usesSyntax {
if _, _, _, usesSyntax := parser.DetectSyntax(b); usesSyntax {
return nil
}
// instructions.Parse will check for malformed Dockerfile
_, _, err := instructions.Parse(res.AST)
_, _, err = instructions.Parse(res.AST)
return err
}

Expand Down
7 changes: 6 additions & 1 deletion testutil/fake_image_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"sync/atomic"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/registry"
"github.com/docker/docker/client"
reg "github.com/docker/docker/registry"
Expand Down Expand Up @@ -287,7 +288,11 @@ func (f *FakeAPIClient) ImageList(ctx context.Context, ops types.ImageListOption
return rt, nil
}

func (f *FakeAPIClient) DiskUsage(ctx context.Context) (types.DiskUsage, error) {
func (f *FakeAPIClient) ImageHistory(ctx context.Context, image string) ([]image.HistoryResponseItem, error) {
return nil, nil
}

func (f *FakeAPIClient) DiskUsage(ctx context.Context, ops types.DiskUsageOptions) (types.DiskUsage, error) {
// if DUFails is positive faile first DUFails errors and then return ok
// if negative, return ok first DUFails times and then fail the rest
if f.DUFails > 0 {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.