cleaned up registry and improved logging (#378)

* cleaned up registry and improved logging
* last bit of logging improvements/import clean up

---------

Signed-off-by: Zack Brady <zackbrady123@gmail.com>
This commit is contained in:
Zack Brady
2025-01-09 15:21:03 -05:00
committed by GitHub
parent cc17b030a9
commit e97adcdfed
16 changed files with 66 additions and 61 deletions

View File

@@ -5,6 +5,7 @@ import (
"os" "os"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"hauler.dev/go/hauler/internal/flags" "hauler.dev/go/hauler/internal/flags"
) )

View File

@@ -138,7 +138,7 @@ func addStoreServeRegistry(rso *flags.StoreRootOpts, ro *flags.CliRootOpts) *cob
return err return err
} }
return store.ServeRegistryCmd(ctx, o, s, ro) return store.ServeRegistryCmd(ctx, o, s, rso, ro)
}, },
} }

View File

@@ -5,13 +5,13 @@ import (
"os" "os"
"github.com/google/go-containerregistry/pkg/name" "github.com/google/go-containerregistry/pkg/name"
"hauler.dev/go/hauler/pkg/artifacts/file/getter"
"hauler.dev/go/hauler/pkg/consts"
"helm.sh/helm/v3/pkg/action" "helm.sh/helm/v3/pkg/action"
"hauler.dev/go/hauler/internal/flags" "hauler.dev/go/hauler/internal/flags"
"hauler.dev/go/hauler/pkg/apis/hauler.cattle.io/v1alpha1" "hauler.dev/go/hauler/pkg/apis/hauler.cattle.io/v1alpha1"
"hauler.dev/go/hauler/pkg/artifacts/file" "hauler.dev/go/hauler/pkg/artifacts/file"
"hauler.dev/go/hauler/pkg/artifacts/file/getter"
"hauler.dev/go/hauler/pkg/consts"
"hauler.dev/go/hauler/pkg/content/chart" "hauler.dev/go/hauler/pkg/content/chart"
"hauler.dev/go/hauler/pkg/cosign" "hauler.dev/go/hauler/pkg/cosign"
"hauler.dev/go/hauler/pkg/log" "hauler.dev/go/hauler/pkg/log"

View File

@@ -19,7 +19,7 @@ func CopyCmd(ctx context.Context, o *flags.CopyOpts, s *store.Layout, targetRef
components := strings.SplitN(targetRef, "://", 2) components := strings.SplitN(targetRef, "://", 2)
switch components[0] { switch components[0] {
case "dir": case "dir":
l.Debugf("identified directory target reference") l.Debugf("identified directory target reference of [%s]", components[1])
fs := content.NewFile(components[1]) fs := content.NewFile(components[1])
defer fs.Close() defer fs.Close()
@@ -29,7 +29,7 @@ func CopyCmd(ctx context.Context, o *flags.CopyOpts, s *store.Layout, targetRef
} }
case "registry": case "registry":
l.Debugf("identified registry target reference") l.Debugf("identified registry target reference of [%s]", components[1])
ropts := content.RegistryOptions{ ropts := content.RegistryOptions{
Username: o.Username, Username: o.Username,
Password: o.Password, Password: o.Password,

View File

@@ -53,7 +53,7 @@ func unarchiveLayoutTo(ctx context.Context, archivePath string, dest string, tem
} }
defer os.RemoveAll(tempDir) defer os.RemoveAll(tempDir)
l.Debugf("using temporary directory at %s", tempDir) l.Debugf("using temporary directory at [%s]", tempDir)
if err := archiver.Unarchive(archivePath, tempDir); err != nil { if err := archiver.Unarchive(archivePath, tempDir); err != nil {
return err return err

View File

@@ -106,7 +106,7 @@ func writeExportsManifest(ctx context.Context, dir string, platformStr string) e
for _, desc := range imx.Manifests { for _, desc := range imx.Manifests {
l.Debugf("descriptor [%s] >>> %s", desc.Digest.String(), desc.MediaType) l.Debugf("descriptor [%s] >>> %s", desc.Digest.String(), desc.MediaType)
if artifactType := types.MediaType(desc.ArtifactType); artifactType != "" && !artifactType.IsImage() && !artifactType.IsIndex() { if artifactType := types.MediaType(desc.ArtifactType); artifactType != "" && !artifactType.IsImage() && !artifactType.IsIndex() {
l.Debugf("descriptor [%s] <<< SKIPPING ARTIFACT (%q)", desc.Digest.String(), desc.ArtifactType) l.Debugf("descriptor [%s] <<< SKIPPING ARTIFACT [%q]", desc.Digest.String(), desc.ArtifactType)
continue continue
} }
if desc.Annotations != nil { if desc.Annotations != nil {
@@ -157,7 +157,7 @@ func writeExportsManifest(ctx context.Context, dir string, platformStr string) e
} }
} }
default: default:
l.Debugf("descriptor [%s] <<< SKIPPING KIND (%q)", desc.Digest.String(), kind) l.Debugf("descriptor [%s] <<< SKIPPING KIND [%q]", desc.Digest.String(), kind)
} }
} }
} }

View File

@@ -2,7 +2,10 @@ package store
import ( import (
"context" "context"
"fmt"
"net/http"
"os" "os"
"strings"
"github.com/distribution/distribution/v3/configuration" "github.com/distribution/distribution/v3/configuration"
dcontext "github.com/distribution/distribution/v3/context" dcontext "github.com/distribution/distribution/v3/context"
@@ -10,6 +13,7 @@ import (
_ "github.com/distribution/distribution/v3/registry/storage/driver/filesystem" _ "github.com/distribution/distribution/v3/registry/storage/driver/filesystem"
_ "github.com/distribution/distribution/v3/registry/storage/driver/inmemory" _ "github.com/distribution/distribution/v3/registry/storage/driver/inmemory"
"github.com/distribution/distribution/v3/version" "github.com/distribution/distribution/v3/version"
"gopkg.in/yaml.v3"
"hauler.dev/go/hauler/internal/flags" "hauler.dev/go/hauler/internal/flags"
"hauler.dev/go/hauler/internal/server" "hauler.dev/go/hauler/internal/server"
@@ -17,7 +21,35 @@ import (
"hauler.dev/go/hauler/pkg/store" "hauler.dev/go/hauler/pkg/store"
) )
func ServeRegistryCmd(ctx context.Context, o *flags.ServeRegistryOpts, s *store.Layout, ro *flags.CliRootOpts) error { func DefaultRegistryConfig(o *flags.ServeRegistryOpts, rso *flags.StoreRootOpts, ro *flags.CliRootOpts) *configuration.Configuration {
cfg := &configuration.Configuration{
Version: "0.1",
Storage: configuration.Storage{
"cache": configuration.Parameters{"blobdescriptor": "inmemory"},
"filesystem": configuration.Parameters{"rootdirectory": o.RootDir},
"maintenance": configuration.Parameters{
"readonly": map[any]any{"enabled": o.ReadOnly},
},
},
}
if o.TLSCert != "" && o.TLSKey != "" {
cfg.HTTP.TLS.Certificate = o.TLSCert
cfg.HTTP.TLS.Key = o.TLSKey
}
cfg.HTTP.Addr = fmt.Sprintf(":%d", o.Port)
cfg.HTTP.Headers = http.Header{
"X-Content-Type-Options": []string{"nosniff"},
}
cfg.Log.Level = configuration.Loglevel(ro.LogLevel)
cfg.Validation.Manifests.URLs.Allow = []string{".+"}
return cfg
}
func ServeRegistryCmd(ctx context.Context, o *flags.ServeRegistryOpts, s *store.Layout, rso *flags.StoreRootOpts, ro *flags.CliRootOpts) error {
l := log.FromContext(ctx) l := log.FromContext(ctx)
ctx = dcontext.WithVersion(ctx, version.Version) ctx = dcontext.WithVersion(ctx, version.Version)
@@ -33,7 +65,7 @@ func ServeRegistryCmd(ctx context.Context, o *flags.ServeRegistryOpts, s *store.
tr.Close() tr.Close()
cfg := o.DefaultRegistryConfig() cfg := DefaultRegistryConfig(o, rso, ro)
if o.ConfigFile != "" { if o.ConfigFile != "" {
ucfg, err := loadConfig(o.ConfigFile) ucfg, err := loadConfig(o.ConfigFile)
if err != nil { if err != nil {
@@ -43,6 +75,16 @@ func ServeRegistryCmd(ctx context.Context, o *flags.ServeRegistryOpts, s *store.
} }
l.Infof("starting registry on port [%d]", o.Port) l.Infof("starting registry on port [%d]", o.Port)
yamlConfig, err := yaml.Marshal(cfg)
if err != nil {
l.Errorf("failed to validate/output registry configuration: %v", err)
} else {
l.Infof("using registry configuration... \n%s", strings.TrimSpace(string(yamlConfig)))
}
l.Debugf("detailed registry configuration: %+v", cfg)
r, err := server.NewRegistry(ctx, cfg) r, err := server.NewRegistry(ctx, cfg)
if err != nil { if err != nil {
return err return err

View File

@@ -29,7 +29,7 @@ func SyncCmd(ctx context.Context, o *flags.SyncOpts, s *store.Layout, rso *flags
// if passed products, check for a remote manifest to retrieve and use. // if passed products, check for a remote manifest to retrieve and use.
for _, product := range o.Products { for _, product := range o.Products {
l.Infof("processing content file for product: '%s'", product) l.Infof("processing content file for product [%s]", product)
parts := strings.Split(product, "=") parts := strings.Split(product, "=")
tag := strings.ReplaceAll(parts[1], "+", "-") tag := strings.ReplaceAll(parts[1], "+", "-")
@@ -40,7 +40,7 @@ func SyncCmd(ctx context.Context, o *flags.SyncOpts, s *store.Layout, rso *flags
} }
manifestLoc := fmt.Sprintf("%s/hauler/%s-manifest.yaml:%s", ProductRegistry, parts[0], tag) manifestLoc := fmt.Sprintf("%s/hauler/%s-manifest.yaml:%s", ProductRegistry, parts[0], tag)
l.Infof("retrieving product manifest from: '%s'", manifestLoc) l.Infof("retrieving product manifest from [%s]", manifestLoc)
img := v1alpha1.Image{ img := v1alpha1.Image{
Name: manifestLoc, Name: manifestLoc,
} }
@@ -66,7 +66,7 @@ func SyncCmd(ctx context.Context, o *flags.SyncOpts, s *store.Layout, rso *flags
// if passed a local manifest, process it // if passed a local manifest, process it
for _, filename := range o.ContentFiles { for _, filename := range o.ContentFiles {
l.Debugf("processing content file: '%s'", filename) l.Debugf("processing content file: [%s]", filename)
fi, err := os.Open(filename) fi, err := os.Open(filename)
if err != nil { if err != nil {
return err return err

4
go.mod
View File

@@ -8,7 +8,6 @@ require (
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be
github.com/containerd/containerd v1.7.23 github.com/containerd/containerd v1.7.23
github.com/distribution/distribution/v3 v3.0.0-20221208165359-362910506bc2 github.com/distribution/distribution/v3 v3.0.0-20221208165359-362910506bc2
github.com/docker/go-metrics v0.0.1
github.com/google/go-containerregistry v0.16.1 github.com/google/go-containerregistry v0.16.1
github.com/gorilla/handlers v1.5.1 github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0 github.com/gorilla/mux v1.8.0
@@ -23,6 +22,7 @@ require (
github.com/spf13/afero v1.10.0 github.com/spf13/afero v1.10.0
github.com/spf13/cobra v1.8.1 github.com/spf13/cobra v1.8.1
golang.org/x/sync v0.10.0 golang.org/x/sync v0.10.0
gopkg.in/yaml.v3 v3.0.1
helm.sh/helm/v3 v3.16.3 helm.sh/helm/v3 v3.16.3
k8s.io/apimachinery v0.31.3 k8s.io/apimachinery v0.31.3
k8s.io/client-go v0.31.3 k8s.io/client-go v0.31.3
@@ -63,6 +63,7 @@ require (
github.com/docker/docker-credential-helpers v0.7.0 // indirect github.com/docker/docker-credential-helpers v0.7.0 // indirect
github.com/docker/go-connections v0.5.0 // indirect github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c // indirect github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c // indirect
github.com/docker/go-metrics v0.0.1 // indirect
github.com/docker/libtrust v0.0.0-20150114040149-fa567046d9b1 // indirect github.com/docker/libtrust v0.0.0-20150114040149-fa567046d9b1 // indirect
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect github.com/emicklei/go-restful/v3 v3.11.0 // indirect
@@ -162,7 +163,6 @@ require (
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.31.3 // indirect k8s.io/api v0.31.3 // indirect
k8s.io/apiextensions-apiserver v0.31.3 // indirect k8s.io/apiextensions-apiserver v0.31.3 // indirect
k8s.io/apiserver v0.31.3 // indirect k8s.io/apiserver v0.31.3 // indirect

View File

@@ -1,10 +1,6 @@
package flags package flags
import ( import (
"fmt"
"net/http"
"github.com/distribution/distribution/v3/configuration"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"hauler.dev/go/hauler/pkg/consts" "hauler.dev/go/hauler/pkg/consts"
) )
@@ -35,34 +31,6 @@ func (o *ServeRegistryOpts) AddFlags(cmd *cobra.Command) {
cmd.MarkFlagsRequiredTogether("tls-cert", "tls-key") cmd.MarkFlagsRequiredTogether("tls-cert", "tls-key")
} }
func (o *ServeRegistryOpts) DefaultRegistryConfig() *configuration.Configuration {
cfg := &configuration.Configuration{
Version: "0.1",
Storage: configuration.Storage{
"cache": configuration.Parameters{"blobdescriptor": "inmemory"},
"filesystem": configuration.Parameters{"rootdirectory": o.RootDir},
"maintenance": configuration.Parameters{
"readonly": map[any]any{"enabled": o.ReadOnly},
},
},
}
if o.TLSCert != "" && o.TLSKey != "" {
cfg.HTTP.TLS.Certificate = o.TLSCert
cfg.HTTP.TLS.Key = o.TLSKey
}
cfg.HTTP.Addr = fmt.Sprintf(":%d", o.Port)
cfg.HTTP.Headers = http.Header{
"X-Content-Type-Options": []string{"nosniff"},
}
cfg.Log.Level = "info"
cfg.Validation.Manifests.URLs.Allow = []string{".+"}
return cfg
}
type ServeFilesOpts struct { type ServeFilesOpts struct {
*StoreRootOpts *StoreRootOpts

View File

@@ -41,7 +41,7 @@ func (o *StoreRootOpts) Store(ctx context.Context) (*store.Layout, error) {
return nil, err return nil, err
} }
l.Debugf("using store at %s", abs) l.Debugf("using store at [%s]", abs)
if _, err := os.Stat(abs); errors.Is(err, os.ErrNotExist) { if _, err := os.Stat(abs); errors.Is(err, os.ErrNotExist) {
if err := os.MkdirAll(abs, os.ModePerm); err != nil { if err := os.MkdirAll(abs, os.ModePerm); err != nil {

View File

@@ -11,7 +11,6 @@ import (
"github.com/distribution/distribution/v3/configuration" "github.com/distribution/distribution/v3/configuration"
"github.com/distribution/distribution/v3/registry" "github.com/distribution/distribution/v3/registry"
"github.com/distribution/distribution/v3/registry/handlers" "github.com/distribution/distribution/v3/registry/handlers"
"github.com/docker/go-metrics"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@@ -22,14 +21,6 @@ func NewRegistry(ctx context.Context, cfg *configuration.Configuration) (*regist
return nil, err return nil, err
} }
if cfg.HTTP.Debug.Prometheus.Enabled {
path := cfg.HTTP.Debug.Prometheus.Path
if path == "" {
path = "/metrics"
}
http.Handle(path, metrics.Handler())
}
return r, nil return r, nil
} }
@@ -45,7 +36,7 @@ func NewTempRegistry(ctx context.Context, root string) *tmpRegistryServer {
"filesystem": configuration.Parameters{"rootdirectory": root}, "filesystem": configuration.Parameters{"rootdirectory": root},
}, },
} }
// Add validation configuration
cfg.Validation.Manifests.URLs.Allow = []string{".+"} cfg.Validation.Manifests.URLs.Allow = []string{".+"}
cfg.Log.Level = "error" cfg.Log.Level = "error"

View File

@@ -1,8 +1,9 @@
package v1alpha1 package v1alpha1
import ( import (
"hauler.dev/go/hauler/pkg/consts"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"hauler.dev/go/hauler/pkg/consts"
) )
var ( var (

View File

@@ -64,7 +64,7 @@ func SaveImage(ctx context.Context, s *store.Layout, ref string, platform string
if err != nil { if err != nil {
return err return err
} }
l.Debugf("multi-arch image: %v", isMultiArch) l.Debugf("multi-arch image [%v]", isMultiArch)
cmd := exec.Command(cosignBinaryPath, "save", ref, "--dir", s.Root) cmd := exec.Command(cosignBinaryPath, "save", ref, "--dir", s.Root)
// Conditionally add platform. // Conditionally add platform.
@@ -248,7 +248,7 @@ func EnsureBinaryExists(ctx context.Context, bin embed.FS, ro *flags.CliRootOpts
return fmt.Errorf("error: %v", err) return fmt.Errorf("error: %v", err)
} }
l.Debugf("using hauler directory at %s", filepath.Dir(binaryPath)) l.Debugf("using hauler directory at [%s]", filepath.Dir(binaryPath))
// Determine the architecture so that we pull the correct embedded binary // Determine the architecture so that we pull the correct embedded binary
arch := runtime.GOARCH arch := runtime.GOARCH

View File

@@ -7,6 +7,7 @@ import (
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"hauler.dev/go/hauler/pkg/consts" "hauler.dev/go/hauler/pkg/consts"
) )

View File

@@ -8,6 +8,7 @@ import (
"strings" "strings"
gname "github.com/google/go-containerregistry/pkg/name" gname "github.com/google/go-containerregistry/pkg/name"
"hauler.dev/go/hauler/pkg/consts" "hauler.dev/go/hauler/pkg/consts"
) )