Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into 5p2O5pe25ouT/main
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <[email protected]>
  • Loading branch information
wagoodman committed Aug 24, 2023
2 parents a97d04c + d1f3d76 commit 6b26a93
Show file tree
Hide file tree
Showing 72 changed files with 748 additions and 1,671 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/oss-project-board-add.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Add to OSS board

on:
issues:
types:
- opened
- reopened
- transferred
- labeled

jobs:

run:
uses: "anchore/workflows/.github/workflows/oss-project-board-add.yaml@main"
secrets:
token: ${{ secrets.OSS_PROJECT_GH_TOKEN }}
2 changes: 1 addition & 1 deletion DEVELOPING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ After cloning the following step can help you get setup:
1. run `make bootstrap` to download go mod dependencies, create the `/.tmp` dir, and download helper utilities.
2. run `make help` to view the selection of developer commands in the Makefile

The main make tasks for common static analysis and testing are `lint`, `lint-fix`, `unit`, and `integration`.
The main make tasks for common static analysis and testing are `lint`, `format`, `lint-fix`, `unit`, and `integration`.

See `make help` for all the current make tasks.

Expand Down
27 changes: 21 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ TEMP_DIR = ./.tmp

# Command templates #################################
LINT_CMD = $(TEMP_DIR)/golangci-lint run --tests=false --config .golangci.yaml
GOIMPORTS_CMD := $(TEMP_DIR)/gosimports -local github.com/anchore

# Tool versions #################################
GOLANGCILINT_VERSION := v1.51.0
GOSIMPORTS_VERSION := v0.3.5
GOLANGCILINT_VERSION := v1.52.2
GOSIMPORTS_VERSION := v0.3.8
BOUNCER_VERSION := v0.4.0
CHRONICLE_VERSION := v0.5.1
CHRONICLE_VERSION := v0.6.0

# Formatting variables #################################
BOLD := $(shell tput -T linux bold)
Expand Down Expand Up @@ -91,16 +92,30 @@ static-analysis: check-licenses lint
.PHONY: lint
lint: ## Run gofmt + golangci lint checks
$(call title,Running linters)
# ensure there are no go fmt differences
@printf "files with gofmt issues: [$(shell gofmt -l -s .)]\n"
@test -z "$(shell gofmt -l -s .)"

# run all golangci-lint rules
$(LINT_CMD)
@[ -z "$(shell $(GOIMPORTS_CMD) -d .)" ] || (echo "goimports needs to be fixed" && false)

# go tooling does not play well with certain filename characters, ensure the common cases don't result in future "go get" failures
$(eval MALFORMED_FILENAMES := $(shell find . | grep -v tar-cache | grep -e ':'))
@bash -c "[[ '$(MALFORMED_FILENAMES)' == '' ]] || (printf '\nfound unsupported filename characters:\n$(MALFORMED_FILENAMES)\n\n' && false)"


.PHONY: format
format: ## Auto-format all source code
$(call title,Running formatters)
gofmt -w -s .
$(GOIMPORTS_CMD) -w .
go mod tidy

.PHONY: lint-fix
lint-fix: ## Auto-format all source code + run golangci lint fixers
lint-fix: format ## Auto-format all source code + run golangci lint fixers
$(call title,Running lint fixers)
gofmt -w -s .
$(LINT_CMD) --fix
go mod tidy

.PHONY: check-licenses
check-licenses:
Expand Down
89 changes: 54 additions & 35 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"fmt"
"runtime"

"github.com/anchore/go-logger"
"github.com/wagoodman/go-partybus"

"github.com/anchore/go-logger"
"github.com/anchore/stereoscope/internal/bus"
dockerClient "github.com/anchore/stereoscope/internal/docker"
"github.com/anchore/stereoscope/internal/log"
Expand All @@ -16,7 +17,6 @@ import (
"github.com/anchore/stereoscope/pkg/image/docker"
"github.com/anchore/stereoscope/pkg/image/oci"
"github.com/anchore/stereoscope/pkg/image/sif"
"github.com/wagoodman/go-partybus"
)

var rootTempDirGenerator = file.NewTempDirGenerator("stereoscope")
Expand Down Expand Up @@ -81,7 +81,10 @@ func GetImageFromSource(ctx context.Context, imgStr string, source image.Source,
}
}

provider, err := selectImageProvider(imgStr, source, cfg)
provider, cleanup, err := selectImageProvider(imgStr, source, cfg)
if cleanup != nil {
defer cleanup()
}
if err != nil {
return nil, err
}
Expand All @@ -99,74 +102,90 @@ func GetImageFromSource(ctx context.Context, imgStr string, source image.Source,
return img, nil
}

func selectImageProvider(imgStr string, source image.Source, cfg config) (image.Provider, error) {
// nolint:funlen
func selectImageProvider(imgStr string, source image.Source, cfg config) (image.Provider, func(), error) {
var provider image.Provider
tempDirGenerator := rootTempDirGenerator.NewGenerator()
platformSelectionUnsupported := fmt.Errorf("specified platform=%q however image source=%q does not support selecting platform", cfg.Platform.String(), source.String())

if err := setPlatform(source, &cfg, runtime.GOARCH); err != nil {
return nil, err
}
cleanup := func() {}

switch source {
case image.DockerTarballSource:
if cfg.Platform != nil {
return nil, cleanup, platformSelectionUnsupported
}
// note: the imgStr is the path on disk to the tar file
provider = docker.NewProviderFromTarball(imgStr, tempDirGenerator)
case image.DockerDaemonSource:
c, err := dockerClient.GetClient()
if err != nil {
return nil, err
return nil, cleanup, err
}

cleanup = func() {
if err := c.Close(); err != nil {
log.Errorf("unable to close docker client: %+v", err)
}
}

provider, err = docker.NewProviderFromDaemon(imgStr, tempDirGenerator, c, cfg.Platform)
if err != nil {
return nil, err
return nil, cleanup, err
}
case image.PodmanDaemonSource:
c, err := podman.GetClient()
if err != nil {
return nil, err
return nil, cleanup, err
}

cleanup = func() {
if err := c.Close(); err != nil {
log.Errorf("unable to close docker client: %+v", err)
}
}

provider, err = docker.NewProviderFromDaemon(imgStr, tempDirGenerator, c, cfg.Platform)
if err != nil {
return nil, err
return nil, cleanup, err
}
case image.OciDirectorySource:
if cfg.Platform != nil {
return nil, cleanup, platformSelectionUnsupported
}
provider = oci.NewProviderFromPath(imgStr, tempDirGenerator)
case image.OciTarballSource:
if cfg.Platform != nil {
return nil, cleanup, platformSelectionUnsupported
}
provider = oci.NewProviderFromTarball(imgStr, tempDirGenerator)
case image.OciRegistrySource:
defaultPlatformIfNil(&cfg)
provider = oci.NewProviderFromRegistry(imgStr, tempDirGenerator, cfg.Registry, cfg.Platform)
case image.SingularitySource:
if cfg.Platform != nil {
return nil, cleanup, platformSelectionUnsupported
}
provider = sif.NewProviderFromPath(imgStr, tempDirGenerator)
default:
return nil, fmt.Errorf("unable to determine image source")
return nil, cleanup, fmt.Errorf("unable to determine image source")
}
return provider, nil
return provider, cleanup, nil
}

func setPlatform(source image.Source, cfg *config, defaultArch string) error {
// we should override the platform based on the host architecture if the user did not specify a platform
// see https://github.com/anchore/stereoscope/issues/149 for more details
defaultPlatform, err := image.NewPlatform(defaultArch)
if err != nil {
log.WithFields("error", err).Warnf("unable to set default platform to %q", runtime.GOARCH)
defaultPlatform = nil
}

switch source {
case image.DockerTarballSource, image.OciDirectorySource, image.OciTarballSource, image.SingularitySource:
if cfg.Platform != nil {
return fmt.Errorf("specified platform=%q however image source=%q does not support selecting platform", cfg.Platform.String(), source.String())
// defaultPlatformIfNil sets the platform to use the host's architecture
// if no platform was specified. The OCI registry provider uses "linux/amd64"
// as a hard-coded default platform, which has surprised customers
// running stereoscope on non-amd64 hosts. If platform is already
// set on the config, or the code can't generate a matching platform,
// do nothing.
func defaultPlatformIfNil(cfg *config) {
if cfg.Platform == nil {
p, err := image.NewPlatform(fmt.Sprintf("linux/%s", runtime.GOARCH))
if err == nil {
cfg.Platform = p
}

case image.DockerDaemonSource, image.PodmanDaemonSource, image.OciRegistrySource:
if cfg.Platform == nil {
cfg.Platform = defaultPlatform
}

default:
return fmt.Errorf("unable to determine image source to select platform")
}
return nil
}

// GetImage parses the user provided image string and provides an image object;
Expand Down
Loading

0 comments on commit 6b26a93

Please sign in to comment.