Skip to content

Commit

Permalink
Set config creation time without mutating (#524)
Browse files Browse the repository at this point in the history
* Set config creation time without mutating

* only set creation time if set

* drop creationTime from buildLayer entirely
  • Loading branch information
imjasonh authored Dec 16, 2021
1 parent 33fa766 commit 8135bf2
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions pkg/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ func appFilename(importpath string) string {
// owner: BUILTIN/Users group: BUILTIN/Users ($sddlValue="O:BUG:BU")
const userOwnerAndGroupSID = "AQAAgBQAAAAkAAAAAAAAAAAAAAABAgAAAAAABSAAAAAhAgAAAQIAAAAAAAUgAAAAIQIAAA=="

func tarBinary(name, binary string, creationTime v1.Time, platform *v1.Platform) (*bytes.Buffer, error) {
func tarBinary(name, binary string, platform *v1.Platform) (*bytes.Buffer, error) {
buf := bytes.NewBuffer(nil)
tw := tar.NewWriter(buf)
defer tw.Close()
Expand All @@ -402,8 +402,7 @@ func tarBinary(name, binary string, creationTime v1.Time, platform *v1.Platform)
// Use a fixed Mode, so that this isn't sensitive to the directory and umask
// under which it was created. Additionally, windows can only set 0222,
// 0444, or 0666, none of which are executable.
Mode: 0555,
ModTime: creationTime.Time,
Mode: 0555,
}); err != nil {
return nil, fmt.Errorf("writing dir %q: %w", dir, err)
}
Expand All @@ -425,8 +424,7 @@ func tarBinary(name, binary string, creationTime v1.Time, platform *v1.Platform)
// Use a fixed Mode, so that this isn't sensitive to the directory and umask
// under which it was created. Additionally, windows can only set 0222,
// 0444, or 0666, none of which are executable.
Mode: 0555,
ModTime: creationTime.Time,
Mode: 0555,
}
if platform.OS == "windows" {
// This magic value is for some reason needed for Windows to be
Expand Down Expand Up @@ -714,6 +712,7 @@ func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, pl
History: v1.History{
Author: "ko",
CreatedBy: "ko build " + ref.String(),
Created: g.kodataCreationTime,
Comment: "kodata contents, at $KO_DATA_PATH",
},
})
Expand All @@ -734,6 +733,7 @@ func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, pl
Layer: binaryLayer,
History: v1.History{
Author: "ko",
Created: g.creationTime,
CreatedBy: "ko build " + ref.String(),
Comment: "go build output, at " + appPath,
},
Expand Down Expand Up @@ -771,19 +771,16 @@ func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, pl
cfg.Config.Labels[k] = v
}

empty := v1.Time{}
if g.creationTime != empty {
cfg.Created = g.creationTime
}

image, err := mutate.ConfigFile(withApp, cfg)
if err != nil {
return nil, err
}

empty := v1.Time{}
if g.creationTime != empty {
image, err = mutate.CreatedAt(image, g.creationTime)
if err != nil {
return nil, err
}
}

si := signed.Image(image)

if g.sbom != nil {
Expand All @@ -805,7 +802,7 @@ func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, pl

func buildLayer(appPath, file string, platform *v1.Platform) (v1.Layer, error) {
// Construct a tarball with the binary and produce a layer.
binaryLayerBuf, err := tarBinary(appPath, file, v1.Time{}, platform)
binaryLayerBuf, err := tarBinary(appPath, file, platform)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 8135bf2

Please sign in to comment.