Skip to content

Commit 533e360

Browse files
smiratalos-bot
authored andcommitted
feat: add support for image labels
This will allow to link our packages back to the source repos. Signed-off-by: Andrey Smirnov <[email protected]>
1 parent f27a804 commit 533e360

File tree

11 files changed

+74
-55
lines changed

11 files changed

+74
-55
lines changed

.drone.yml

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,35 @@
11
---
22
kind: pipeline
33
name: default
4-
5-
services:
6-
- name: docker
7-
image: docker:19.03.3-rc1-dind
8-
privileged: true
9-
entrypoint:
10-
- dockerd
11-
command:
12-
- --mtu=1440
13-
- --insecure-registry=registry.ci.svc:5000
14-
volumes:
15-
- name: docker-socket
16-
path: /var/run
4+
type: kubernetes
175

186
steps:
19-
- name: fetch
20-
image: docker:git
7+
- name: setup-ci
8+
image: autonomy/build-container:latest
219
commands:
2210
- git fetch --tags
23-
when:
24-
event: tag
11+
- install-ci-key
12+
- setup-buildx-amd64-arm64
13+
volumes:
14+
- name: docker-socket
15+
path: /var/run
16+
- name: docker
17+
path: /root/.docker/buildx
2518

2619
- name: build-and-test
2720
image: autonomy/build-container:latest
2821
pull: always
2922
environment:
3023
PLATFORM: linux/amd64,linux/arm64
31-
SSH_KEY:
32-
from_secret: ssh_key
33-
DOCKER_USERNAME:
34-
from_secret: docker_username
35-
DOCKER_PASSWORD:
36-
from_secret: docker_password
24+
REGISTRY: registry.dev.talos-systems.io
3725
commands:
38-
- install-ci-key
39-
- setup-buildx-amd64-arm64
40-
- docker login --username "$${DOCKER_USERNAME}" --password "$${DOCKER_PASSWORD}"
41-
- make # build everything
42-
- make frontend PUSH=true # push only frontend for integration tests
26+
- make PUSH=true
4327
- make integration
4428
volumes:
4529
- name: docker-socket
4630
path: /var/run
31+
- name: docker
32+
path: /root/.docker/buildx
4733
when:
4834
event:
4935
include:
@@ -54,33 +40,35 @@ steps:
5440
pull: always
5541
environment:
5642
PLATFORM: linux/amd64,linux/arm64
57-
BUILDKIT_HOST: ${BUILDKIT_HOST=tcp://buildkitd.ci.svc:1234}
58-
SSH_KEY:
59-
from_secret: ssh_key
60-
DOCKER_USERNAME:
61-
from_secret: docker_username
62-
DOCKER_PASSWORD:
63-
from_secret: docker_password
43+
GHCR_USERNAME:
44+
from_secret: ghcr_username
45+
GHCR_PASSWORD:
46+
from_secret: ghcr_token
6447
commands:
65-
- install-ci-key
66-
- setup-buildx-amd64-arm64
67-
- docker login --username "$${DOCKER_USERNAME}" --password "$${DOCKER_PASSWORD}"
48+
- docker login ghcr.io --username "$${GHCR_USERNAME}" --password "$${GHCR_PASSWORD}"
6849
- make PUSH=true
6950
- make integration
7051
volumes:
7152
- name: docker-socket
7253
path: /var/run
54+
- name: docker
55+
path: /root/.docker/buildx
7356
when:
7457
event:
7558
exclude:
7659
- pull_request
7760

7861
volumes:
7962
- name: docker-socket
63+
host:
64+
path: /var/ci-docker
65+
- name: docker
8066
temp: {}
67+
8168
---
8269
kind: pipeline
8370
name: notify
71+
type: kubernetes
8472

8573
clone:
8674
disable: true

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ COPY hack/golang/golangci-lint.yaml .
3434
RUN --mount=type=cache,target=/root/.cache/go-build golangci-lint run --config golangci-lint.yaml
3535

3636
FROM scratch AS bldr
37+
LABEL org.opencontainers.image.source https://github.com/talos-systems/bldr
3738
COPY --from=build /bldr /bldr
3839

3940
FROM scratch AS integration.test
4041
COPY --from=build /src/integration.test /integration.test
4142

4243
FROM bldr AS frontend
44+
LABEL org.opencontainers.image.source https://github.com/talos-systems/bldr
4345
ENTRYPOINT ["/bldr", "frontend"]

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
REGISTRY ?= docker.io
2-
USERNAME ?= autonomy
1+
REGISTRY ?= ghcr.io
2+
USERNAME ?= talos-systems
33
TAG ?= $(shell git describe --tag --always --dirty)
44
REGISTRY_AND_USERNAME := $(REGISTRY)/$(USERNAME)
55
RUN_TESTS ?= TestIntegration

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,15 @@ names are ignored.
129129
### `Pkgfile`
130130

131131
```yaml
132-
# syntax = docker.io/autonomy/bldr:v0.1.0-alpha.0-frontend
132+
# syntax = ghcr.io/talos-systems/bldr:v0.2.0-alpha.3-frontend
133133

134134
format: v1alpha2
135135

136136
vars:
137-
TOOLCHAIN_IMAGE: docker.io/autonomy/toolchain:0714f82
137+
TOOLCHAIN_IMAGE: ghcr.io/talos-systems/tools:v0.3.0-8-ge86a8f3
138+
139+
labels:
140+
org.opencontainers.image.source: https://github.com/talos-systems/bldr
138141
```
139142
140143
First line of the file should always be magic comment which is picked up by
@@ -145,6 +148,7 @@ Rest of the `Pkgfile` is regular YAML file with the following fields:
145148

146149
- `format` (*string*, *required*): format of the `pkg.yaml` files, the only allowed value today is `v1alpha2`.
147150
- `vars` (*map[str]str*, *optional*): set of variables which are used to process `pkg.yaml` as a template.
151+
- `labels` (*map[str]str*, *optional*): labels to apply to the output images (only in frontend mode).
148152

149153
`bldr` parses `Pkgfile` as the first thing during the build, it should always
150154
reside at the root of the build tree.
@@ -233,7 +237,7 @@ Internal dependency:
233237
External dependency:
234238

235239
```yaml
236-
- image: docker.io/autonomy/toolchain:0714f82
240+
- image: ghcr.io/talos-systems/tools:v0.3.0-8-ge86a8f3
237241
runtime: false
238242
to: /
239243
```

cmd/frontend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var frontendCmd = &cobra.Command{
2323
2424
To activate, put following line as the first line of Pkgfile:
2525
26-
# syntax = docker.io/autonomy/bldr:<version>-frontend
26+
# syntax = ghcr.io/talos-systems/bldr:<version>-frontend
2727
2828
Run with:
2929

internal/pkg/pkgfile/build.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func Build(ctx context.Context, c client.Client, options *environment.Options) (
6666
}
6767

6868
if !b && exportMap {
69-
return nil, fmt.Errorf("returning multiple target plaforms is not allowed")
69+
return nil, fmt.Errorf("returning multiple target platforms is not allowed")
7070
}
7171

7272
exportMap = b
@@ -139,6 +139,11 @@ func Build(ctx context.Context, c client.Client, options *environment.Options) (
139139
Type: "layers",
140140
},
141141
},
142+
Config: dockerfile2llb.ImageConfig{
143+
ImageConfig: specs.ImageConfig{
144+
Labels: packages.ImageLabels(),
145+
},
146+
},
142147
Variant: platform.PlatformSpec.Variant,
143148
}
144149

internal/pkg/solver/buildkit_loader.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (bkfl *BuildkitFrontendLoader) walk(path string, process packageProcess) er
6363
}
6464

6565
// Load implements PackageLoader.
66-
func (bkfl *BuildkitFrontendLoader) Load() ([]*v1alpha2.Pkg, error) {
66+
func (bkfl *BuildkitFrontendLoader) Load() (*LoadResult, error) {
6767
if bkfl.Logger == nil {
6868
bkfl.Logger = log.New(log.Writer(), "[loader] ", log.Flags())
6969
}
@@ -106,5 +106,8 @@ func (bkfl *BuildkitFrontendLoader) Load() ([]*v1alpha2.Pkg, error) {
106106

107107
err = bkfl.walk("/", process)
108108

109-
return pkgs, multierror.Append(multiErr, err).ErrorOrNil()
109+
return &LoadResult{
110+
Pkgfile: bkfl.pkgFile,
111+
Pkgs: pkgs,
112+
}, multierror.Append(multiErr, err).ErrorOrNil()
110113
}

internal/pkg/solver/filesystem_loader.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (fspl *FilesystemPackageLoader) walkFunc() filepath.WalkFunc {
6363
}
6464

6565
// Load implements PackageLoader.
66-
func (fspl *FilesystemPackageLoader) Load() ([]*v1alpha2.Pkg, error) {
66+
func (fspl *FilesystemPackageLoader) Load() (*LoadResult, error) {
6767
if fspl.Logger == nil {
6868
fspl.Logger = log.New(log.Writer(), "[loader] ", log.Flags())
6969
}
@@ -87,7 +87,10 @@ func (fspl *FilesystemPackageLoader) Load() ([]*v1alpha2.Pkg, error) {
8787

8888
err = filepath.Walk(fspl.Root, fspl.walkFunc())
8989

90-
return fspl.pkgs, multierror.Append(fspl.multiErr, err).ErrorOrNil()
90+
return &LoadResult{
91+
Pkgfile: fspl.pkgFile,
92+
Pkgs: fspl.pkgs,
93+
}, multierror.Append(fspl.multiErr, err).ErrorOrNil()
9194
}
9295

9396
func (fspl *FilesystemPackageLoader) loadPkg(path string) (*v1alpha2.Pkg, error) {

internal/pkg/solver/loader.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ import (
88
"github.com/talos-systems/bldr/internal/pkg/types/v1alpha2"
99
)
1010

11+
// LoadResult is a result of PackageLoader.Load function.
12+
type LoadResult struct {
13+
Pkgfile *v1alpha2.Pkgfile
14+
Pkgs []*v1alpha2.Pkg
15+
}
16+
1117
// PackageLoader implements some way to fetch collection of Pkgs.
1218
type PackageLoader interface {
13-
Load() ([]*v1alpha2.Pkg, error)
19+
Load() (*LoadResult, error)
1420
}

internal/pkg/solver/packages.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,22 @@ import (
1313
// Packages is a collect of Pkg objects with dependencies tracked.
1414
type Packages struct {
1515
packages map[string]*v1alpha2.Pkg
16+
pkgfile *v1alpha2.Pkgfile
1617
}
1718

1819
// NewPackages builds Packages using PackageLoader.
1920
func NewPackages(loader PackageLoader) (*Packages, error) {
20-
pkgs, err := loader.Load()
21+
loadResult, err := loader.Load()
2122
if err != nil {
2223
return nil, err
2324
}
2425

2526
result := &Packages{
26-
packages: make(map[string]*v1alpha2.Pkg),
27+
packages: make(map[string]*v1alpha2.Pkg, len(loadResult.Pkgs)),
28+
pkgfile: loadResult.Pkgfile,
2729
}
2830

29-
for _, pkg := range pkgs {
31+
for _, pkg := range loadResult.Pkgs {
3032
name := pkg.Name
3133

3234
if dup, exists := result.packages[name]; exists {
@@ -111,3 +113,8 @@ func (pkgs *Packages) ToSet() (set PackageSet) {
111113

112114
return
113115
}
116+
117+
// ImageLabels returns set of image labels to apply to the output image.
118+
func (pkgs *Packages) ImageLabels() map[string]string {
119+
return pkgs.pkgfile.Labels
120+
}

0 commit comments

Comments
 (0)