Skip to content

Commit

Permalink
offsetgen: support Docker username and password (open-telemetry#1239)
Browse files Browse the repository at this point in the history
* offsetgen: support Docker username and password

* Pass environment variables to make docker-offsets
  • Loading branch information
damemi authored Nov 5, 2024
1 parent 1ea899a commit 5da8ee5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ offsets: | $(OFFSETGEN)

.PHONY: docker-offsets
docker-offsets:
docker run --rm -v /tmp:/tmp -v /var/run/docker.sock:/var/run/docker.sock -v $(shell pwd):/app golang:1.22 /bin/sh -c "cd ../app && make offsets"
docker run -e DOCKER_USERNAME=$(DOCKER_USERNAME) -e DOCKER_PASSWORD=$(DOCKER_PASSWORD) --rm -v /tmp:/tmp -v /var/run/docker.sock:/var/run/docker.sock -v $(shell pwd):/app golang:1.22 /bin/sh -c "cd ../app && make offsets"

.PHONY: update-licenses
update-licenses: generate $(GOLICENSES)
Expand Down
22 changes: 21 additions & 1 deletion internal/tools/inspect/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ package inspect
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"log/slog"
"os"
"path/filepath"

"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"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"
"github.com/docker/docker/pkg/stdcopy"
"github.com/hashicorp/go-version"
Expand Down Expand Up @@ -112,7 +116,23 @@ func (b *builder) pullImage(ctx context.Context) error {
return nil
}

rc, err := b.cli.ImagePull(ctx, b.GoImage, image.PullOptions{})
pullOpts := image.PullOptions{}
username := os.Getenv("DOCKER_USERNAME")
password := os.Getenv("DOCKER_PASSWORD")
if len(username) > 0 && len(password) > 0 {
authConfig := registry.AuthConfig{
Username: username,
Password: password,
}
encodedJSON, err := json.Marshal(authConfig)
if err != nil {
panic(err)
}
authStr := base64.URLEncoding.EncodeToString(encodedJSON)
pullOpts.RegistryAuth = authStr
}

rc, err := b.cli.ImagePull(ctx, b.GoImage, pullOpts)
if err != nil {
return err
}
Expand Down

0 comments on commit 5da8ee5

Please sign in to comment.