Skip to content

Commit

Permalink
rm debug slog, unused struct, unused interface
Browse files Browse the repository at this point in the history
Signed-off-by: Brooks Newberry <[email protected]>
  • Loading branch information
brooksn committed Feb 19, 2025
1 parent d76d5ad commit 9668a41
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 114 deletions.
18 changes: 1 addition & 17 deletions cmd/release/cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"os"
"text/tabwriter"

Expand Down Expand Up @@ -86,21 +85,6 @@ var inspectCmd = &cobra.Command{
Short: "Inspect release artifacts",
Long: `Inspect release artifacts for a given version.
Currently supports inspecting the image list for published rke2 releases.`,
PreRun: func(cmd *cobra.Command, args []string) {
// Set up slog configuration based on debug flag
var logLevel slog.Level
if debug {
logLevel = slog.LevelDebug
} else {
logLevel = slog.LevelInfo
}

opts := &slog.HandlerOptions{
Level: logLevel,
}
handler := slog.NewTextHandler(os.Stderr, opts)
slog.SetDefault(slog.New(handler))
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("expected at least one argument: [version]")
Expand All @@ -115,7 +99,7 @@ Currently supports inspecting the image list for published rke2 releases.`,

ossClient := reg.NewClient(ossRegistry, debug)

var primeClient reg.Client
var primeClient *reg.Client
if rootConfig.PrimeRegistry != "" {
primeClient = reg.NewClient(rootConfig.PrimeRegistry, debug)
}
Expand Down
67 changes: 11 additions & 56 deletions registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"net/http"

"github.com/google/go-containerregistry/pkg/name"
Expand All @@ -22,58 +21,34 @@ func (p Platform) String() string {
return fmt.Sprintf("%s/%s", p.OS, p.Architecture)
}

// Image contains information about an image in a specific registry
type Image struct {
Exists bool
Platforms map[Platform]bool
}

// ImageInfo contains information about an image across registries
type ImageInfo struct {
Reference name.Reference
OSSImage Image
PrimeImage Image
}

// Client provides methods for interacting with a container registry
type Client interface {
GetImageInfo(ctx context.Context, ref name.Reference) (Image, error)
}

type defaultClient struct {
type Client struct {
registry string
debug bool
}

// NewClient returns a new registry client for the specified registry
func NewClient(registry string, debug bool) Client {
return &defaultClient{
registry: registry,
debug: debug,
}
func NewClient(registry string, debug bool) *Client {
return &Client{registry}
}

func (c *defaultClient) GetImageInfo(ctx context.Context, ref name.Reference) (Image, error) {
func (c *Client) Image(ctx context.Context, ref name.Reference) (Image, error) {
info := Image{
Platforms: make(map[Platform]bool),
}

newRef, err := name.ParseReference(fmt.Sprintf("%s/%s:%s", c.registry, ref.Context().RepositoryStr(), ref.Identifier()))
if err != nil {
return info, fmt.Errorf("creating registry reference: %w", err)
}

if c.debug {
slog.Debug("getting image descriptor", "registry", c.registry, "reference", newRef.String())
return info, err
}

desc, err := remote.Get(newRef)
if err != nil {
var transportErr *transport.Error
if errors.As(err, &transportErr) && transportErr.StatusCode == http.StatusNotFound {
if c.debug {
slog.Debug("image not found", "registry", c.registry, "reference", newRef.String())
}
return info, nil
}
return info, fmt.Errorf("getting descriptor: %w", err)
Expand All @@ -82,41 +57,27 @@ func (c *defaultClient) GetImageInfo(ctx context.Context, ref name.Reference) (I
info.Exists = true

if desc.MediaType.IsIndex() {
if c.debug {
slog.Debug("handling multi-arch image", "registry", c.registry, "reference", newRef.String())
}
if err := c.handleMultiArchImage(desc, &info); err != nil {
return info, err
}
} else {
if c.debug {
slog.Debug("handling single-arch image", "registry", c.registry, "reference", newRef.String())
}
if err := c.handleSingleArchImage(desc, &info); err != nil {
return info, err
}
}

if c.debug {
slog.Debug("image info retrieved",
"registry", c.registry,
"reference", newRef.String(),
"exists", info.Exists,
"platform_count", len(info.Platforms))
}

return info, nil
}

func (c *defaultClient) handleMultiArchImage(desc *remote.Descriptor, info *Image) error {
func (c *Client) handleMultiArchImage(desc *remote.Descriptor, info *Image) error {
idx, err := desc.ImageIndex()
if err != nil {
return fmt.Errorf("getting index: %w", err)
return err
}

manifest, err := idx.IndexManifest()
if err != nil {
return fmt.Errorf("getting manifest: %w", err)
return err
}

for _, m := range manifest.Manifests {
Expand All @@ -125,33 +86,27 @@ func (c *defaultClient) handleMultiArchImage(desc *remote.Descriptor, info *Imag
Architecture: m.Platform.Architecture,
}
info.Platforms[platform] = true
if c.debug {
slog.Debug("found platform", "os", platform.OS, "arch", platform.Architecture)
}
}

return nil
}

func (c *defaultClient) handleSingleArchImage(desc *remote.Descriptor, info *Image) error {
func (c *Client) handleSingleArchImage(desc *remote.Descriptor, info *Image) error {
img, err := desc.Image()
if err != nil {
return fmt.Errorf("getting image: %w", err)
return err
}

cfg, err := img.ConfigFile()
if err != nil {
return fmt.Errorf("getting config: %w", err)
return err
}

platform := Platform{
OS: cfg.OS,
Architecture: cfg.Architecture,
}
info.Platforms[platform] = true
if c.debug {
slog.Debug("found platform", "os", platform.OS, "arch", platform.Architecture)
}

return nil
}
Expand Down
47 changes: 6 additions & 41 deletions release/rke2/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io"
"io/fs"
"log/slog"
"strings"

"github.com/google/go-containerregistry/pkg/name"
Expand Down Expand Up @@ -36,12 +35,12 @@ type ImageStatus struct {

type ReleaseInspector struct {
fs fs.FS
oss reg.Client
prime reg.Client
oss *reg.Client
prime *reg.Client
debug bool
}

func NewReleaseInspector(fs fs.FS, oss, prime reg.Client, debug bool) *ReleaseInspector {
func NewReleaseInspector(fs fs.FS, oss, prime *reg.Client, debug bool) *ReleaseInspector {
return &ReleaseInspector{
fs: fs,
oss: oss,
Expand All @@ -59,17 +58,11 @@ func (r *ReleaseInspector) InspectRelease(ctx context.Context, version string) (
if err != nil {
return nil, err
}
if r.debug {
slog.Debug("found architecture lists", "lists", mapKeys(archLists))
}

imageExpectations, err := r.processImageLists(archLists)
if err != nil {
return nil, err
}
if r.debug {
slog.Debug("processed images", "count", len(imageExpectations))
}

return r.checkImages(ctx, imageExpectations)
}
Expand All @@ -92,9 +85,6 @@ func (r *ReleaseInspector) getArchitectureLists() (map[Architecture]string, erro

for _, entry := range entries {
name := entry.Name()
if r.debug {
slog.Debug("found asset", "name", name)
}
switch name {
case "rke2-images-all.linux-amd64.txt":
lists[ArchLinuxAmd64] = name
Expand All @@ -115,16 +105,10 @@ func (r *ReleaseInspector) processImageLists(archLists map[Architecture]string)
continue
}

if r.debug {
slog.Debug("reading image list", "arch", arch, "filename", filename)
}
images, err := r.readImageList(filename)
if err != nil {
return nil, err
}
if r.debug {
slog.Debug("found images", "arch", arch, "count", len(images))
}

for _, image := range images {
if image == "" {
Expand All @@ -133,9 +117,6 @@ func (r *ReleaseInspector) processImageLists(archLists map[Architecture]string)

ref, err := reg.ParseReference(image)
if err != nil {
if r.debug {
slog.Debug("failed to parse image reference", "image", image, "error", err)
}
continue
}

Expand Down Expand Up @@ -176,20 +157,10 @@ func (r *ReleaseInspector) readImageList(filename string) ([]string, error) {

func (r *ReleaseInspector) checkImages(ctx context.Context, expectations map[string]imageExpectations) ([]ImageStatus, error) {
var results []ImageStatus
if r.debug {
slog.Debug("checking images in registries", "count", len(expectations))
}

for key, expect := range expectations {
if r.debug {
slog.Debug("checking image", "image", key)
}

ossImage, err := r.oss.GetImageInfo(ctx, expect.Reference)
for _, expect := range expectations {
ossImage, err := r.oss.Image(ctx, expect.Reference)
if err != nil {
if r.debug {
slog.Debug("failed to get OSS info", "image", key, "error", err)
}
ossImage = reg.Image{
Exists: false,
Platforms: make(map[reg.Platform]bool),
Expand All @@ -198,10 +169,7 @@ func (r *ReleaseInspector) checkImages(ctx context.Context, expectations map[str

var primeImage reg.Image
if r.prime != nil {
primeImage, err = r.prime.GetImageInfo(ctx, expect.Reference)
if err != nil && r.debug {
slog.Debug("failed to get Prime info", "image", key, "error", err)
}
primeImage, err = r.prime.Image(ctx, expect.Reference)
}

status := ImageStatus{
Expand All @@ -213,8 +181,5 @@ func (r *ReleaseInspector) checkImages(ctx context.Context, expectations map[str
results = append(results, status)
}

if r.debug {
slog.Debug("found images", "count", len(results))
}
return results, nil
}

0 comments on commit 9668a41

Please sign in to comment.