Skip to content

Commit

Permalink
more metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
dskiff committed Apr 27, 2024
1 parent 46aa1b0 commit a0068a3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
46 changes: 40 additions & 6 deletions pkg/build/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"context"
"fmt"
"strings"
"time"

"github.com/google/go-containerregistry/pkg/authn"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"github.com/google/go-containerregistry/pkg/v1/types"
)

type TargetType int
Expand Down Expand Up @@ -43,6 +46,8 @@ type BuildSpec struct {
BaseRef string
InjectLayer BuildSpecInjectLayer
Target BuildSpecTarget

Author string
}

type BuildContext struct {
Expand All @@ -59,34 +64,49 @@ func Build(ctx BuildContext, spec BuildSpec) error {
return fmt.Errorf("failed to retrieve base image: %w", err)
}

newLayer, err := createLayerFromFolder(ctx, spec.InjectLayer)
mediaType, err := getMediaType(baseImage)
if err != nil {
return fmt.Errorf("failed to get media type: %w", err)
}

newLayer, err := createLayerFromFolder(ctx, spec.InjectLayer, tarball.WithMediaType(mediaType))
if err != nil {
return fmt.Errorf("failed to create layer from source: %w", err)
}

newImage, err := mutate.AppendLayers(baseImage, newLayer)
unixEpoch := time.Unix(0, 0)
newImage, err := mutate.Append(baseImage, mutate.Addendum{
Layer: newLayer,
MediaType: mediaType,
History: v1.History{
Created: v1.Time{Time: unixEpoch},
Author: "github.com/dskiff/tko",
CreatedBy: "tko build",
},
})
if err != nil {
return fmt.Errorf("failed to append layer to base image: %w", err)
}

newImage, err = mutateConfig(newImage, spec.InjectLayer)
newImage, err = mutateConfig(newImage, spec)
if err != nil {
return fmt.Errorf("failed to mutate config: %w", err)
}

return publish(ctx, newImage, spec.Target)
}

func mutateConfig(img v1.Image, layer BuildSpecInjectLayer) (v1.Image, error) {
func mutateConfig(img v1.Image, spec BuildSpec) (v1.Image, error) {
imgCfg, err := img.ConfigFile()
if err != nil {
return nil, err
}
imgCfg = imgCfg.DeepCopy()

imgCfg.Config.WorkingDir = layer.DestinationPath
imgCfg.Config.Entrypoint = []string{layer.Entrypoint}
imgCfg.Config.WorkingDir = spec.InjectLayer.DestinationPath
imgCfg.Config.Entrypoint = []string{spec.InjectLayer.Entrypoint}
imgCfg.Config.Cmd = nil
imgCfg.Author = spec.Author

return mutate.ConfigFile(img, imgCfg)
}
Expand Down Expand Up @@ -114,3 +134,17 @@ func ParseTargetType(str string) (TargetType, error) {
return -1, fmt.Errorf("invalid target type: %s", str)
}
}

func getMediaType(base v1.Image) (types.MediaType, error) {
mt, err := base.MediaType()
if err != nil {
return "", err
}
switch mt {
case types.OCIManifestSchema1:
return types.OCILayer, nil
case types.DockerManifestSchema2:
return types.DockerLayer, nil
}
return "", fmt.Errorf("unsupported base media type: %s", mt)
}
6 changes: 5 additions & 1 deletion pkg/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

type BuildCmd struct {
BaseRef string `short:"b" help:"Base image reference" env:"TKO_BASE_IMAGE" default:"ubuntu:jammy@latest"`
BaseRef string `short:"b" help:"Base image reference" env:"TKO_BASE_IMAGE" default:"ubuntu:jammy"`

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

Expand All @@ -25,6 +25,8 @@ type BuildCmd struct {
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"`

Author string `short:"a" help:"Author of the build" env:"TKO_AUTHOR" default:"github.com/dskiff/tko"`

Verbose bool `short:"v" help:"Enable verbose output"`
}

Expand Down Expand Up @@ -57,6 +59,8 @@ func (b *BuildCmd) Run(cliCtx *CliCtx) error {
Repo: b.TargetRepo,
Type: targetType,
},

Author: b.Author,
}

out, err := yaml.Marshal(cfg)
Expand Down

0 comments on commit a0068a3

Please sign in to comment.