Skip to content

Commit

Permalink
make chown configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
dskiff committed Apr 27, 2024
1 parent f7f8e69 commit 9a3d40d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
4 changes: 4 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ build:
entrypoint: /entrypoint
destination-path: /destination
destination-chown: false
target-repo: repo/target
author: me
Expand Down Expand Up @@ -47,6 +48,7 @@ build:

assert.Equal(t, "/entrypoint", cli.Build.Entrypoint)
assert.Equal(t, "/destination", cli.Build.DestinationPath)
assert.Equal(t, false, cli.Build.DestinationChown)
assert.Equal(t, "repo/target", cli.Build.TargetRepo)

assert.Equal(t, "me", cli.Build.Author)
Expand Down Expand Up @@ -76,6 +78,7 @@ func TestBuildArgs(t *testing.T) {
"-p", "custom-os/arch-variant",
"-e", "/entrypoint",
"-d", "/destination",
"--destination-chown=false",
"-t", "repo/target",
"--author", "me",
"-A", "label1=value1",
Expand All @@ -94,6 +97,7 @@ func TestBuildArgs(t *testing.T) {

assert.Equal(t, "/entrypoint", cli.Build.Entrypoint)
assert.Equal(t, "/destination", cli.Build.DestinationPath)
assert.Equal(t, false, cli.Build.DestinationChown)
assert.Equal(t, "repo/target", cli.Build.TargetRepo)

assert.Equal(t, "me", cli.Build.Author)
Expand Down
16 changes: 9 additions & 7 deletions pkg/build/layers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import (
var unixEpoch = time.Unix(0, 0)

func createLayerFromFolder(ctx BuildContext, layer BuildSpecInjectLayer, opts ...tarball.LayerOption) (v1.Layer, error) {
tarPath, err := createTarFromFolder(ctx, layer.SourcePath, layer.DestinationPath)
tarPath, err := createTarFromFolder(ctx, layer.SourcePath, layer.DestinationPath, layer.DestinationChown)
if err != nil {
return nil, err
}

return tarball.LayerFromFile(tarPath, opts...)
}

func createTarFromFolder(ctx BuildContext, srcPath, dstPath string) (string, error) {
func createTarFromFolder(ctx BuildContext, srcPath, dstPath string, chown bool) (string, error) {
tarFile, err := createTempFile(ctx)
if err != nil {
return "", err
Expand Down Expand Up @@ -53,17 +53,19 @@ func createTarFromFolder(ctx BuildContext, srcPath, dstPath string) (string, err
return err
}
header.Name = filepath.Join(dstPath, relPath)

header.AccessTime = unixEpoch
header.ChangeTime = unixEpoch
header.ModTime = unixEpoch
header.Uid = 0
header.Gid = 0
header.Gname = "root"
header.Uname = "root"
header.PAXRecords = nil
header.Xattrs = nil

if chown {
header.Uid = 0
header.Gid = 0
header.Gname = "root"
header.Uname = "root"
}

log.Println("adding file:", header.Name)

// Write file header
Expand Down
7 changes: 4 additions & 3 deletions pkg/build/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ func (p Platform) String() string {
type BuildSpecInjectLayer struct {
Platform Platform

SourcePath string
DestinationPath string
Entrypoint string
SourcePath string
DestinationPath string
DestinationChown bool
Entrypoint string
}

type BuildSpecTarget struct {
Expand Down
16 changes: 9 additions & 7 deletions pkg/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ type BuildCmd struct {

Platform string `short:"p" help:"Platform to build for" env:"TKO_PLATFORM" default:"linux/amd64"`

SourcePath string `arg:"" name:"path" help:"Path to artifacts to embed" type:"path" env:"TKO_SOURCE_PATH"`
DestinationPath string `short:"d" help:"Path to embed artifacts in" env:"TKO_DEST_PATH" default:"/tko-app"`
Entrypoint string `short:"e" help:"Entrypoint for the embedded artifacts" env:"TKO_ENTRYPOINT" default:"/tko-app/app"`
SourcePath string `arg:"" name:"path" help:"Path to artifacts to embed" type:"path" env:"TKO_SOURCE_PATH"`
DestinationPath string `short:"d" help:"Path to embed artifacts in" env:"TKO_DEST_PATH" default:"/tko-app"`
DestinationChown bool `help:"Whether to chown the destination path to root:root" default:"true"`
Entrypoint string `short:"e" help:"Entrypoint for the embedded artifacts" env:"TKO_ENTRYPOINT" default:"/tko-app/app"`

TargetRepo string `short:"t" help:"Target repository" env:"TKO_TARGET_REPO" required:"true"`
TargetType string `short:"T" help:"Target type" env:"TKO_TARGET_TYPE" default:"REMOTE" enum:"REMOTE,LOCAL_DAEMON,LOCAL_FILE"`
Expand Down Expand Up @@ -62,10 +63,11 @@ func (b *BuildCmd) Run(cliCtx *CliCtx) error {
cfg := build.BuildSpec{
BaseRef: b.BaseRef,
InjectLayer: build.BuildSpecInjectLayer{
Platform: platform,
SourcePath: b.SourcePath,
DestinationPath: b.DestinationPath,
Entrypoint: b.Entrypoint,
Platform: platform,
SourcePath: b.SourcePath,
DestinationPath: b.DestinationPath,
DestinationChown: b.DestinationChown,
Entrypoint: b.Entrypoint,
},
Target: build.BuildSpecTarget{
Repo: b.TargetRepo,
Expand Down

0 comments on commit 9a3d40d

Please sign in to comment.