Skip to content

Commit

Permalink
feat: add support for image labels
Browse files Browse the repository at this point in the history
This will allow to link our packages back to the source repos.

Signed-off-by: Andrey Smirnov <[email protected]>
  • Loading branch information
smira authored and talos-bot committed Nov 10, 2020
1 parent f27a804 commit 533e360
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 55 deletions.
64 changes: 26 additions & 38 deletions .drone.yml
Original file line number Diff line number Diff line change
@@ -1,49 +1,35 @@
---
kind: pipeline
name: default

services:
- name: docker
image: docker:19.03.3-rc1-dind
privileged: true
entrypoint:
- dockerd
command:
- --mtu=1440
- --insecure-registry=registry.ci.svc:5000
volumes:
- name: docker-socket
path: /var/run
type: kubernetes

steps:
- name: fetch
image: docker:git
- name: setup-ci
image: autonomy/build-container:latest
commands:
- git fetch --tags
when:
event: tag
- install-ci-key
- setup-buildx-amd64-arm64
volumes:
- name: docker-socket
path: /var/run
- name: docker
path: /root/.docker/buildx

- name: build-and-test
image: autonomy/build-container:latest
pull: always
environment:
PLATFORM: linux/amd64,linux/arm64
SSH_KEY:
from_secret: ssh_key
DOCKER_USERNAME:
from_secret: docker_username
DOCKER_PASSWORD:
from_secret: docker_password
REGISTRY: registry.dev.talos-systems.io
commands:
- install-ci-key
- setup-buildx-amd64-arm64
- docker login --username "$${DOCKER_USERNAME}" --password "$${DOCKER_PASSWORD}"
- make # build everything
- make frontend PUSH=true # push only frontend for integration tests
- make PUSH=true
- make integration
volumes:
- name: docker-socket
path: /var/run
- name: docker
path: /root/.docker/buildx
when:
event:
include:
Expand All @@ -54,33 +40,35 @@ steps:
pull: always
environment:
PLATFORM: linux/amd64,linux/arm64
BUILDKIT_HOST: ${BUILDKIT_HOST=tcp://buildkitd.ci.svc:1234}
SSH_KEY:
from_secret: ssh_key
DOCKER_USERNAME:
from_secret: docker_username
DOCKER_PASSWORD:
from_secret: docker_password
GHCR_USERNAME:
from_secret: ghcr_username
GHCR_PASSWORD:
from_secret: ghcr_token
commands:
- install-ci-key
- setup-buildx-amd64-arm64
- docker login --username "$${DOCKER_USERNAME}" --password "$${DOCKER_PASSWORD}"
- docker login ghcr.io --username "$${GHCR_USERNAME}" --password "$${GHCR_PASSWORD}"
- make PUSH=true
- make integration
volumes:
- name: docker-socket
path: /var/run
- name: docker
path: /root/.docker/buildx
when:
event:
exclude:
- pull_request

volumes:
- name: docker-socket
host:
path: /var/ci-docker
- name: docker
temp: {}

---
kind: pipeline
name: notify
type: kubernetes

clone:
disable: true
Expand Down
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ COPY hack/golang/golangci-lint.yaml .
RUN --mount=type=cache,target=/root/.cache/go-build golangci-lint run --config golangci-lint.yaml

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

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

FROM bldr AS frontend
LABEL org.opencontainers.image.source https://github.com/talos-systems/bldr
ENTRYPOINT ["/bldr", "frontend"]
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
REGISTRY ?= docker.io
USERNAME ?= autonomy
REGISTRY ?= ghcr.io
USERNAME ?= talos-systems
TAG ?= $(shell git describe --tag --always --dirty)
REGISTRY_AND_USERNAME := $(REGISTRY)/$(USERNAME)
RUN_TESTS ?= TestIntegration
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,15 @@ names are ignored.
### `Pkgfile`

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

format: v1alpha2

vars:
TOOLCHAIN_IMAGE: docker.io/autonomy/toolchain:0714f82
TOOLCHAIN_IMAGE: ghcr.io/talos-systems/tools:v0.3.0-8-ge86a8f3

labels:
org.opencontainers.image.source: https://github.com/talos-systems/bldr
```
First line of the file should always be magic comment which is picked up by
Expand All @@ -145,6 +148,7 @@ Rest of the `Pkgfile` is regular YAML file with the following fields:

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

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

```yaml
- image: docker.io/autonomy/toolchain:0714f82
- image: ghcr.io/talos-systems/tools:v0.3.0-8-ge86a8f3
runtime: false
to: /
```
Expand Down
2 changes: 1 addition & 1 deletion cmd/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var frontendCmd = &cobra.Command{
To activate, put following line as the first line of Pkgfile:
# syntax = docker.io/autonomy/bldr:<version>-frontend
# syntax = ghcr.io/talos-systems/bldr:<version>-frontend
Run with:
Expand Down
7 changes: 6 additions & 1 deletion internal/pkg/pkgfile/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func Build(ctx context.Context, c client.Client, options *environment.Options) (
}

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

exportMap = b
Expand Down Expand Up @@ -139,6 +139,11 @@ func Build(ctx context.Context, c client.Client, options *environment.Options) (
Type: "layers",
},
},
Config: dockerfile2llb.ImageConfig{
ImageConfig: specs.ImageConfig{
Labels: packages.ImageLabels(),
},
},
Variant: platform.PlatformSpec.Variant,
}

Expand Down
7 changes: 5 additions & 2 deletions internal/pkg/solver/buildkit_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (bkfl *BuildkitFrontendLoader) walk(path string, process packageProcess) er
}

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

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

return pkgs, multierror.Append(multiErr, err).ErrorOrNil()
return &LoadResult{
Pkgfile: bkfl.pkgFile,
Pkgs: pkgs,
}, multierror.Append(multiErr, err).ErrorOrNil()
}
7 changes: 5 additions & 2 deletions internal/pkg/solver/filesystem_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (fspl *FilesystemPackageLoader) walkFunc() filepath.WalkFunc {
}

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

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

return fspl.pkgs, multierror.Append(fspl.multiErr, err).ErrorOrNil()
return &LoadResult{
Pkgfile: fspl.pkgFile,
Pkgs: fspl.pkgs,
}, multierror.Append(fspl.multiErr, err).ErrorOrNil()
}

func (fspl *FilesystemPackageLoader) loadPkg(path string) (*v1alpha2.Pkg, error) {
Expand Down
8 changes: 7 additions & 1 deletion internal/pkg/solver/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import (
"github.com/talos-systems/bldr/internal/pkg/types/v1alpha2"
)

// LoadResult is a result of PackageLoader.Load function.
type LoadResult struct {
Pkgfile *v1alpha2.Pkgfile
Pkgs []*v1alpha2.Pkg
}

// PackageLoader implements some way to fetch collection of Pkgs.
type PackageLoader interface {
Load() ([]*v1alpha2.Pkg, error)
Load() (*LoadResult, error)
}
13 changes: 10 additions & 3 deletions internal/pkg/solver/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@ import (
// Packages is a collect of Pkg objects with dependencies tracked.
type Packages struct {
packages map[string]*v1alpha2.Pkg
pkgfile *v1alpha2.Pkgfile
}

// NewPackages builds Packages using PackageLoader.
func NewPackages(loader PackageLoader) (*Packages, error) {
pkgs, err := loader.Load()
loadResult, err := loader.Load()
if err != nil {
return nil, err
}

result := &Packages{
packages: make(map[string]*v1alpha2.Pkg),
packages: make(map[string]*v1alpha2.Pkg, len(loadResult.Pkgs)),
pkgfile: loadResult.Pkgfile,
}

for _, pkg := range pkgs {
for _, pkg := range loadResult.Pkgs {
name := pkg.Name

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

return
}

// ImageLabels returns set of image labels to apply to the output image.
func (pkgs *Packages) ImageLabels() map[string]string {
return pkgs.pkgfile.Labels
}
5 changes: 3 additions & 2 deletions internal/pkg/types/v1alpha2/pkgfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (

// Pkgfile describes structure of 'Pkgfile'.
type Pkgfile struct {
Format string `yaml:"format"`
Vars types.Variables `yaml:"vars,omitempty"`
Format string `yaml:"format"`
Vars types.Variables `yaml:"vars,omitempty"`
Labels map[string]string `yaml:"labels,omitempty"`
}

// NewPkgfile loads Pkgfile from `[]byte` contents.
Expand Down

0 comments on commit 533e360

Please sign in to comment.