Skip to content

Commit

Permalink
Update hook-bootkit:
Browse files Browse the repository at this point in the history
Update go.mod dependencies. Check for tink-worker
image and don't fail the image pull if it does
exist. With embedded images, the tink worker could
potentially already exist in the local Docker image
cache. And the image name could be something
unreachable via the network
(for example: 127.0.0.1/embedded/tink-worker).

Signed-off-by: Jacob Weinstock <[email protected]>
  • Loading branch information
jacobweinstock committed Aug 27, 2024
1 parent 50cdc1d commit b220c3e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
2 changes: 1 addition & 1 deletion images/hook-bootkit/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.21-alpine AS dev
FROM golang:1.22.6-alpine AS dev
COPY . /src/
WORKDIR /src
RUN go mod download
Expand Down
4 changes: 3 additions & 1 deletion images/hook-bootkit/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/tinkerbell/hook/hook-bootkit

go 1.17
go 1.22

toolchain go1.22.6

require (
github.com/cenkalti/backoff/v4 v4.3.0
Expand Down
39 changes: 25 additions & 14 deletions images/hook-bootkit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"time"

"github.com/cenkalti/backoff/v4"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/registry"
"github.com/docker/docker/client"
Expand Down Expand Up @@ -127,13 +127,22 @@ func run(ctx context.Context, log logr.Logger) error {

authStr := base64.URLEncoding.EncodeToString(encodedJSON)

pullOpts := types.ImagePullOptions{
pullOpts := image.PullOptions{
RegistryAuth: authStr,
}
var out io.ReadCloser
imagePullOperation := func() error {
// with embedded images, the tink worker could potentially already exist
// in the local Docker image cache. And the image name could be something
// unreachable via the network (for example: 127.0.0.1/embedded/tink-worker).
// Because of this we check if the image already exists and don't return an
// error if the image does not exist and the pull fails.
var imageExists bool
if _, _, err := cli.ImageInspectWithRaw(ctx, imageName); err == nil {
imageExists = true
}
out, err = cli.ImagePull(ctx, imageName, pullOpts)
if err != nil {
if err != nil && !imageExists {
log.Error(err, "image pull failure", "imageName", imageName)
return err
}
Expand All @@ -143,18 +152,20 @@ func run(ctx context.Context, log logr.Logger) error {
return err
}

buf := bufio.NewScanner(out)
for buf.Scan() {
structured := make(map[string]interface{})
if err := json.Unmarshal(buf.Bytes(), &structured); err != nil {
log.Info("image pull logs", "output", buf.Text())
} else {
log.Info("image pull logs", "logs", structured)
}
if out != nil {
buf := bufio.NewScanner(out)
for buf.Scan() {
structured := make(map[string]interface{})
if err := json.Unmarshal(buf.Bytes(), &structured); err != nil {
log.Info("image pull logs", "output", buf.Text())
} else {
log.Info("image pull logs", "logs", structured)
}

}
if err := out.Close(); err != nil {
log.Error(err, "closing image pull logs failed")
}
if err := out.Close(); err != nil {
log.Error(err, "closing image pull logs failed")
}
}

log.Info("Removing any existing tink-worker container")
Expand Down

0 comments on commit b220c3e

Please sign in to comment.