Skip to content

Commit

Permalink
Use JSON logger if not in a TTY
Browse files Browse the repository at this point in the history
  • Loading branch information
hlubek committed May 22, 2024
1 parent 90c28fb commit d41c740
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

A tool to rebase container (Docker) images instead of rebuilding them.

It is based around the idea of OCI annotations for base images to just change the layers of the base image if a newer version is available.
It is based around the idea of adding OCI annotations for base images to just replace the layers of the base image if a newer version is available.
Have a look at [crane rebase](https://github.com/google/go-containerregistry/blob/main/cmd/crane/rebase.md) for more information.

This enables updates of app images without needing a complete build and is useful e.g. when running many different apps based on the same base image(s).

## Usage

### Requirements

- You need to build your app images with OCI base image annoations (e.g. with [crane rebase](https://github.com/google/go-containerregistry/blob/main/cmd/crane/rebase.md))
- You need to build your app images with OCI base image annotations (e.g. with [crane append --set-base-image-annotations](https://github.com/google/go-containerregistry/blob/main/cmd/crane/doc/crane_append.md))
- The `FROM` image in your app Dockerfile should point to a base image tag that is updated as an alias (e.g. `:1` or `:latest`)
- You need to include `{"$rebase": "[identifier]:name"}` and `{"$rebase": "[identifier]:tag"}` annotations as comments in a YAML file, where `identifier` needs to be unique per file

Expand Down Expand Up @@ -72,6 +75,27 @@ The next step is to push the changes back to your Git repository, so a GitOps op

Ideally this is done in a scheduled CI pipeline to keep the images up-to-date.

### CLI

```
NAME:
stacker - Automatic rebasing of images using OCI base image annotations
USAGE:
cmd [global options] command [command options] [directory]
DESCRIPTION:
Recurses through the given directory to find YAML files with a rebase annotation and rebases the image onto the newest base image.
COMMANDS:
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--help, -h show help (default: false)
--super-verbose, --vv Enable super verbose logging (default: false)
--verbose, -v Enable verbose logging (default: false)
```

## Acknowledgements

This tool is based on the code from https://github.com/google/go-containerregistry/tree/main/cmd/crane for the image manipulation.
Expand Down
15 changes: 11 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/hashicorp/go-multierror"
"github.com/mattn/go-isatty"
"github.com/networkteam/slogutils"
specsv1 "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -45,11 +46,17 @@ func main() {
level = slogutils.LevelTrace
}

slog.SetDefault(slog.New(
slogutils.NewCLIHandler(os.Stderr, &slogutils.CLIHandlerOptions{
var handler slog.Handler
if isatty.IsTerminal(os.Stderr.Fd()) {
handler = slogutils.NewCLIHandler(os.Stderr, &slogutils.CLIHandlerOptions{
Level: level,
}),
))
})
} else {
handler = slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
Level: level,
})
}
slog.SetDefault(slog.New(handler))

return nil
}
Expand Down

0 comments on commit d41c740

Please sign in to comment.