Skip to content

Commit

Permalink
Merge pull request #17 from dskiff/dskiff/annotations
Browse files Browse the repository at this point in the history
label -> annotation
  • Loading branch information
dskiff committed Apr 27, 2024
2 parents 10ff8a9 + b22a9bc commit f7f8e69
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 57 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ jobs:
- name: Run against local docker daemon
run: |
go run . build \
-l "org.opencontainers.image.revision=${{ github.sha }}" \
-l "org.opencontainers.image.version=dev-${{ github.sha }}" \
-t "test/repo" \
--annotations "org.opencontainers.image.revision=${{ github.sha }}" \
--annotations "org.opencontainers.image.version=dev-${{ github.sha }}" \
--target-repo "test/repo" \
"./dist/tko_linux_amd64_v1"
env:
TKO_TARGET_TYPE: LOCAL_DAEMON
10 changes: 5 additions & 5 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ jobs:
- name: Publish (latest)
run: |
tko build \
-l "org.opencontainers.image.revision=${{ github.sha }}" \
-l "org.opencontainers.image.version=dev-${{ github.sha }}" \
-t "ghcr.io/dskiff/tko:latest" \
-e "/bin/bash" \
--annotations "org.opencontainers.image.revision=${{ github.sha }}" \
--annotations "org.opencontainers.image.version=dev-${{ github.sha }}" \
--target-repo "ghcr.io/dskiff/tko:latest" \
--entrypoint "/bin/bash" \
"./dist/tko_linux_amd64_v1"
env:
TKO_BASE_IMAGE: debian:bookworm-slim@sha256:155280b00ee0133250f7159b567a07d7cd03b1645714c3a7458b2287b0ca83cb
TKO_BASE_REF: debian:bookworm-slim@sha256:155280b00ee0133250f7159b567a07d7cd03b1645714c3a7458b2287b0ca83cb
GITHUB_TOKEN: ${{ github.token }}
18 changes: 9 additions & 9 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ jobs:
- name: Publish (bin)
run: |
tko build \
-b "scratch" \
-t "ghcr.io/dskiff/tko:bin" \
-l "org.opencontainers.image.revision=${{ github.sha }}" \
-l "org.opencontainers.image.version=${{ github.ref_name }}" \
--annotations "org.opencontainers.image.revision=${{ github.sha }}" \
--annotations "org.opencontainers.image.version=${{ github.ref_name }}" \
--base-ref "scratch" \
--target-repo "ghcr.io/dskiff/tko:bin" \
"./dist/tko_linux_amd64_v1"
env:
GITHUB_TOKEN: ${{ github.token }}

- name: Publish (version)
run: |
tko build \
-l "org.opencontainers.image.revision=${{ github.sha }}" \
-l "org.opencontainers.image.version=${{ github.ref_name }}" \
-t "ghcr.io/dskiff/tko:${{ github.ref_name }}" \
-e "/bin/bash" \
--annotations "org.opencontainers.image.revision=${{ github.sha }}" \
--annotations "org.opencontainers.image.version=${{ github.ref_name }}" \
--target-repo "ghcr.io/dskiff/tko:${{ github.ref_name }}" \
--entrypoint "/bin/bash" \
"./dist/tko_linux_amd64_v1"
env:
TKO_BASE_IMAGE: debian:bookworm-slim@sha256:155280b00ee0133250f7159b567a07d7cd03b1645714c3a7458b2287b0ca83cb
TKO_BASE_REF: debian:bookworm-slim@sha256:155280b00ee0133250f7159b567a07d7cd03b1645714c3a7458b2287b0ca83cb
GITHUB_TOKEN: ${{ github.token }}
2 changes: 1 addition & 1 deletion .tko.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
build:
destination-path: /usr/local/bin
entrypoint: /usr/local/bin/tko
default-labels:
default-annotations:
org.opencontainers.image.title: tko
org.opencontainers.image.url: github.com/dskiff/tko
org.opencontainers.image.source: github.com/dskiff/tko
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ You can also provide any of the supported parameters using a `.tko.yml` (or `.tk
build:
base-ref: ubuntu:jammy@sha256:6d7b5d3317a71adb5e175640150e44b8b9a9401a7dd394f44840626aff9fa94d
author: my name
labels:
default-annotations:
org.opencontainers.image.source: github.com/my-org/my-project
```

Expand All @@ -70,7 +70,7 @@ build:
- run: tko build "./out"
env:
TKO_BASE_IMAGE: ubuntu:jammy@sha256:6d7b5d3317a71adb5e175640150e44b8b9a9401a7dd394f44840626aff9fa94d
TKO_BASE_REF: ubuntu:jammy@sha256:6d7b5d3317a71adb5e175640150e44b8b9a9401a7dd394f44840626aff9fa94d
GITHUB_TOKEN: ${{ github.token }}
```

Expand All @@ -87,7 +87,7 @@ build:
- run: tko build "./dist"
env:
TKO_BASE_IMAGE: ubuntu:jammy@sha256:6d7b5d3317a71adb5e175640150e44b8b9a9401a7dd394f44840626aff9fa94d
TKO_BASE_REF: ubuntu:jammy@sha256:6d7b5d3317a71adb5e175640150e44b8b9a9401a7dd394f44840626aff9fa94d
GITHUB_TOKEN: ${{ github.token }}
```

Expand Down
30 changes: 15 additions & 15 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ build:
target-repo: repo/target
author: me
default-labels:
default-annotations:
label1: value1
label2: value2
labels:
annotations:
label3: value3
label4: value4
Expand All @@ -50,10 +50,10 @@ build:
assert.Equal(t, "repo/target", cli.Build.TargetRepo)

assert.Equal(t, "me", cli.Build.Author)
assert.Equal(t, "value1", cli.Build.DefaultLabels["label1"])
assert.Equal(t, "value2", cli.Build.DefaultLabels["label2"])
assert.Equal(t, "value3", cli.Build.Labels["label3"])
assert.Equal(t, "value4", cli.Build.Labels["label4"])
assert.Equal(t, "value1", cli.Build.DefaultAnnotations["label1"])
assert.Equal(t, "value2", cli.Build.DefaultAnnotations["label2"])
assert.Equal(t, "value3", cli.Build.Annotations["label3"])
assert.Equal(t, "value4", cli.Build.Annotations["label4"])

assert.Equal(t, "/tmp-dir", cli.Build.Tmp)
assert.Equal(t, true, cli.Build.Verbose)
Expand All @@ -77,11 +77,11 @@ func TestBuildArgs(t *testing.T) {
"-e", "/entrypoint",
"-d", "/destination",
"-t", "repo/target",
"-a", "me",
"-L", "label1=value1",
"-L", "label2=value2",
"-l", "label3=value3",
"-l", "label4=value4",
"--author", "me",
"-A", "label1=value1",
"-A", "label2=value2",
"-a", "label3=value3",
"-a", "label4=value4",
"-T", "REMOTE",
"-v",
"--tmp", "/tmp-dir",
Expand All @@ -97,10 +97,10 @@ func TestBuildArgs(t *testing.T) {
assert.Equal(t, "repo/target", cli.Build.TargetRepo)

assert.Equal(t, "me", cli.Build.Author)
assert.Equal(t, "value1", cli.Build.DefaultLabels["label1"])
assert.Equal(t, "value2", cli.Build.DefaultLabels["label2"])
assert.Equal(t, "value3", cli.Build.Labels["label3"])
assert.Equal(t, "value4", cli.Build.Labels["label4"])
assert.Equal(t, "value1", cli.Build.DefaultAnnotations["label1"])
assert.Equal(t, "value2", cli.Build.DefaultAnnotations["label2"])
assert.Equal(t, "value3", cli.Build.Annotations["label3"])
assert.Equal(t, "value4", cli.Build.Annotations["label4"])

assert.Equal(t, "/tmp-dir", cli.Build.Tmp)
assert.Equal(t, true, cli.Build.Verbose)
Expand Down
7 changes: 7 additions & 0 deletions pkg/build/layers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package build

import (
"archive/tar"
"fmt"
"io"
"log"
"os"
Expand Down Expand Up @@ -43,6 +44,10 @@ func createTarFromFolder(ctx BuildContext, srcPath, dstPath string) (string, err
return err
}

if header.Typeflag == tar.TypeSymlink || header.Typeflag == tar.TypeLink {
return fmt.Errorf("links are not supported: %s", fi.Name())
}

relPath, err := filepath.Rel(srcPath, file)
if err != nil {
return err
Expand All @@ -56,6 +61,8 @@ func createTarFromFolder(ctx BuildContext, srcPath, dstPath string) (string, err
header.Gid = 0
header.Gname = "root"
header.Uname = "root"
header.PAXRecords = nil
header.Xattrs = nil

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

Expand Down
15 changes: 8 additions & 7 deletions pkg/build/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ type BuildSpec struct {
InjectLayer BuildSpecInjectLayer
Target BuildSpecTarget

Author string
Labels map[string]string
Author string
Annotations map[string]string
}

type BuildContext struct {
Expand Down Expand Up @@ -111,13 +111,14 @@ func mutateConfig(img v1.Image, spec BuildSpec, metadata BaseImageMetadata) (v1.
imgCfg.Container = ""
imgCfg.DockerVersion = ""

if imgCfg.Config.Labels == nil {
imgCfg.Config.Labels = map[string]string{}
}
imgCfg.Config.Labels = map[string]string{}
imgCfg.Config.Labels["org.opencontainers.image.base.name"] = metadata.name
imgCfg.Config.Labels["org.opencontainers.image.base.digest"] = metadata.imageDigest

for k, v := range spec.Labels {
if metadata.imageDigest != "" {
imgCfg.Config.Labels["org.opencontainers.image.base.digest"] = metadata.imageDigest
}

for k, v := range spec.Annotations {
imgCfg.Config.Labels[k] = v
}

Expand Down
24 changes: 12 additions & 12 deletions 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"`
BaseRef string `short:"b" help:"Base image reference" env:"TKO_BASE_REF" default:"ubuntu:jammy"`

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

Expand All @@ -25,9 +25,9 @@ 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,LOCAL_FILE"`

Author string `short:"a" help:"Author of the build" env:"TKO_AUTHOR" default:"github.com/dskiff/tko"`
DefaultLabels map[string]string `short:"L" help:"Default labels to apply to the image" env:"TKO_DEFAULT_LABELS" default:"" mapsep:"," sep:"="`
Labels map[string]string `short:"l" help:"Additional labels to apply to the image. Can override default-labels." env:"LABELS" default:"" mapsep:"," sep:"="`
Author string `help:"Author of the build" env:"TKO_AUTHOR" default:"github.com/dskiff/tko"`
DefaultAnnotations map[string]string `short:"A" help:"Default annotations to apply to the image" env:"TKO_DEFAULT_ANNOTATIONS" default:"" mapsep:"," sep:"="`
Annotations map[string]string `short:"a" help:"Additional annotations to apply to the image. Can override default-annotations." env:"TKO_ANNOTATIONS" default:"" mapsep:"," sep:"="`

Tmp string `help:"Path where tko can write temporary files. Defaults to golang's tmp logic." env:"TKO_TMP" default:""`
Verbose bool `short:"v" help:"Enable verbose output"`
Expand All @@ -50,13 +50,13 @@ func (b *BuildCmd) Run(cliCtx *CliCtx) error {
github.Keychain,
)

// Labels would ideally be merged by kong, but this works too
labels := make(map[string]string)
for k, v := range b.DefaultLabels {
labels[k] = v
// Annotations would ideally be merged by kong, but this works too
annotations := make(map[string]string)
for k, v := range b.DefaultAnnotations {
annotations[k] = v
}
for k, v := range b.Labels {
labels[k] = v
for k, v := range b.Annotations {
annotations[k] = v
}

cfg := build.BuildSpec{
Expand All @@ -72,8 +72,8 @@ func (b *BuildCmd) Run(cliCtx *CliCtx) error {
Type: targetType,
},

Author: b.Author,
Labels: labels,
Author: b.Author,
Annotations: annotations,
}

out, err := yaml.Marshal(cfg)
Expand Down
4 changes: 2 additions & 2 deletions renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
"^.github/workflows/(.+).yml$"
],
"matchStrings": [
"TKO_BASE_IMAGE:\\s*(?<depName>[^:\\n]+):(?<currentValue>[^@\\n]+)(@(?<currentDigest>sha256:[a-f0-9]+))?"
"TKO_BASE_REF:\\s*(?<depName>[^:\\n]+):(?<currentValue>[^@\\n]+)(@(?<currentDigest>sha256:[a-f0-9]+))?"
],
"autoReplaceStringTemplate": "TKO_BASE_IMAGE: {{{depName}}}:{{{newValue}}}{{#if newDigest}}@{{{newDigest}}}{{/if}}",
"autoReplaceStringTemplate": "TKO_BASE_REF: {{{depName}}}:{{{newValue}}}{{#if newDigest}}@{{{newDigest}}}{{/if}}",
"datasourceTemplate": "docker"
}
]
Expand Down

0 comments on commit f7f8e69

Please sign in to comment.