cleanup: move init funcs to the top of the source

We make sure they're not hiding at the bottom or in the middle
which makes debugging an utter nightmare!

Signed-off-by: Milos Gajdos <milosthegajdos@gmail.com>
This commit is contained in:
Milos Gajdos
2023-11-28 06:50:48 +00:00
parent d9abc517e8
commit d8ff41a344
14 changed files with 203 additions and 201 deletions

View File

@@ -9,6 +9,16 @@ import (
"github.com/opencontainers/go-digest"
)
var routeDescriptorsMap map[string]RouteDescriptor
func init() {
routeDescriptorsMap = make(map[string]RouteDescriptor, len(routeDescriptors))
for _, descriptor := range routeDescriptors {
routeDescriptorsMap[descriptor.Name] = descriptor
}
}
var (
nameParameterDescriptor = ParameterDescriptor{
Name: "name",
@@ -1600,13 +1610,3 @@ var routeDescriptors = []RouteDescriptor{
},
},
}
var routeDescriptorsMap map[string]RouteDescriptor
func init() {
routeDescriptorsMap = make(map[string]RouteDescriptor, len(routeDescriptors))
for _, descriptor := range routeDescriptors {
routeDescriptorsMap[descriptor.Name] = descriptor
}
}

View File

@@ -45,6 +45,16 @@ var (
ErrAuthenticationFailure = errors.New("authentication failure")
)
// InitFunc is the type of an AccessController factory function and is used
// to register the constructor for different AccesController backends.
type InitFunc func(options map[string]interface{}) (AccessController, error)
var accessControllers map[string]InitFunc
func init() {
accessControllers = make(map[string]InitFunc)
}
// UserInfo carries information about
// an autenticated/authorized client.
type UserInfo struct {
@@ -104,16 +114,6 @@ type CredentialAuthenticator interface {
AuthenticateUser(username, password string) error
}
// InitFunc is the type of an AccessController factory function and is used
// to register the constructor for different AccesController backends.
type InitFunc func(options map[string]interface{}) (AccessController, error)
var accessControllers map[string]InitFunc
func init() {
accessControllers = make(map[string]InitFunc)
}
// Register is used to register an InitFunc for
// an AccessController backend with the given name.
func Register(name string, initFunc InitFunc) error {

View File

@@ -23,6 +23,12 @@ import (
"github.com/sirupsen/logrus"
)
func init() {
if err := auth.Register("htpasswd", auth.InitFunc(newAccessController)); err != nil {
logrus.Errorf("failed to register htpasswd auth: %v", err)
}
}
type accessController struct {
realm string
path string
@@ -150,9 +156,3 @@ func createHtpasswdFile(path string) error {
}).Warnf("htpasswd is missing, provisioning with default user")
return nil
}
func init() {
if err := auth.Register("htpasswd", auth.InitFunc(newAccessController)); err != nil {
logrus.Errorf("failed to register htpasswd auth: %v", err)
}
}

View File

@@ -16,6 +16,13 @@ import (
"github.com/sirupsen/logrus"
)
// init registers the silly auth backend.
func init() {
if err := auth.Register("silly", auth.InitFunc(newAccessController)); err != nil {
logrus.Errorf("failed to register silly auth: %v", err)
}
}
// accessController provides a simple implementation of auth.AccessController
// that simply checks for a non-empty Authorization header. It is useful for
// demonstration and testing.
@@ -85,10 +92,3 @@ func (ch challenge) SetHeaders(r *http.Request, w http.ResponseWriter) {
func (ch challenge) Error() string {
return fmt.Sprintf("silly authentication challenge: %#v", ch)
}
// init registers the silly auth backend.
func init() {
if err := auth.Register("silly", auth.InitFunc(newAccessController)); err != nil {
logrus.Errorf("failed to register silly auth: %v", err)
}
}

View File

@@ -17,6 +17,13 @@ import (
"github.com/sirupsen/logrus"
)
// init handles registering the token auth backend.
func init() {
if err := auth.Register("token", auth.InitFunc(newAccessController)); err != nil {
logrus.Errorf("tailed to register token auth: %v", err)
}
}
// accessSet maps a typed, named resource to
// a set of actions requested or authorized.
type accessSet map[auth.Resource]actionSet
@@ -337,10 +344,3 @@ func (ac *accessController) Authorized(req *http.Request, accessItems ...auth.Ac
Resources: claims.resources(),
}, nil
}
// init handles registering the token auth backend.
func init() {
if err := auth.Register("token", auth.InitFunc(newAccessController)); err != nil {
logrus.Errorf("tailed to register token auth: %v", err)
}
}

View File

@@ -20,14 +20,20 @@ import (
"github.com/opencontainers/go-digest"
)
var sbsMu sync.Mutex
var randSource rand.Rand
var (
sbsMu sync.Mutex
randSource rand.Rand
)
type statsBlobStore struct {
stats map[string]int
blobs distribution.BlobStore
}
func init() {
randSource = *rand.New(rand.NewSource(42))
}
func (sbs statsBlobStore) Put(ctx context.Context, mediaType string, p []byte) (distribution.Descriptor, error) {
sbsMu.Lock()
sbs.stats["put"]++
@@ -195,10 +201,6 @@ func makeBlob(size int) []byte {
return blob
}
func init() {
randSource = *rand.New(rand.NewSource(42))
}
func populate(t *testing.T, te *testEnv, blobCount, size, numUnique int) {
var inRemote []distribution.Descriptor

View File

@@ -36,6 +36,34 @@ type proxyMetricsCollector struct {
manifestMetrics Metrics
}
// proxyMetrics tracks metrics about the proxy cache. This is
// kept globally and made available via expvar.
var proxyMetrics = &proxyMetricsCollector{}
func init() {
registry := expvar.Get("registry")
if registry == nil {
registry = expvar.NewMap("registry")
}
pm := registry.(*expvar.Map).Get("proxy")
if pm == nil {
pm = &expvar.Map{}
pm.(*expvar.Map).Init()
registry.(*expvar.Map).Set("proxy", pm)
}
pm.(*expvar.Map).Set("blobs", expvar.Func(func() interface{} {
return proxyMetrics.blobMetrics
}))
pm.(*expvar.Map).Set("manifests", expvar.Func(func() interface{} {
return proxyMetrics.manifestMetrics
}))
metrics.Register(prometheus.ProxyNamespace)
}
// BlobPull tracks metrics about blobs pulled into the cache
func (pmc *proxyMetricsCollector) BlobPull(bytesPulled uint64) {
atomic.AddUint64(&pmc.blobMetrics.Misses, 1)
@@ -83,31 +111,3 @@ func (pmc *proxyMetricsCollector) ManifestPush(bytesPushed uint64, isHit bool) {
hits.WithValues("manifest").Inc(1)
}
}
// proxyMetrics tracks metrics about the proxy cache. This is
// kept globally and made available via expvar.
var proxyMetrics = &proxyMetricsCollector{}
func init() {
registry := expvar.Get("registry")
if registry == nil {
registry = expvar.NewMap("registry")
}
pm := registry.(*expvar.Map).Get("proxy")
if pm == nil {
pm = &expvar.Map{}
pm.(*expvar.Map).Init()
registry.(*expvar.Map).Set("proxy", pm)
}
pm.(*expvar.Map).Set("blobs", expvar.Func(func() interface{} {
return proxyMetrics.blobMetrics
}))
pm.(*expvar.Map).Set("manifests", expvar.Func(func() interface{} {
return proxyMetrics.manifestMetrics
}))
metrics.Register(prometheus.ProxyNamespace)
}

View File

@@ -20,6 +20,13 @@ import (
"github.com/sirupsen/logrus"
)
// init registers the cloudfront layerHandler backend.
func init() {
if err := storagemiddleware.Register("cloudfront", newCloudFrontStorageMiddleware); err != nil {
logrus.Errorf("failed to register cloudfront middleware: %v", err)
}
}
// cloudFrontStorageMiddleware provides a simple implementation of layerHandler that
// constructs temporary signed CloudFront URLs from the storagedriver layer URL,
// then issues HTTP Temporary Redirects to this CloudFront content URL.
@@ -224,10 +231,3 @@ func (lh *cloudFrontStorageMiddleware) RedirectURL(r *http.Request, path string)
}
return cfURL, nil
}
// init registers the cloudfront layerHandler backend.
func init() {
if err := storagemiddleware.Register("cloudfront", newCloudFrontStorageMiddleware); err != nil {
logrus.Errorf("failed to register cloudfront middleware: %v", err)
}
}

View File

@@ -12,6 +12,12 @@ import (
"github.com/sirupsen/logrus"
)
func init() {
if err := storagemiddleware.Register("redirect", newRedirectStorageMiddleware); err != nil {
logrus.Errorf("tailed to register redirect storage middleware: %v", err)
}
}
type redirectStorageMiddleware struct {
storagedriver.StorageDriver
scheme string
@@ -51,9 +57,3 @@ func (r *redirectStorageMiddleware) RedirectURL(_ *http.Request, urlPath string)
u := &url.URL{Scheme: r.scheme, Host: r.host, Path: urlPath}
return u.String(), nil
}
func init() {
if err := storagemiddleware.Register("redirect", newRedirectStorageMiddleware); err != nil {
logrus.Errorf("tailed to register redirect storage middleware: %v", err)
}
}

View File

@@ -24,6 +24,14 @@ import (
// Test hooks up gocheck into the "go test" runner.
func Test(t *testing.T) { check.TestingT(t) }
// randomBytes pre-allocates all of the memory sizes needed for the test. If
// anything panics while accessing randomBytes, just make this number bigger.
var randomBytes = make([]byte, 128<<20)
func init() {
_, _ = crand.Read(randomBytes) // always returns len(randomBytes) and nil error
}
// RegisterSuite registers an in-process storage driver test suite with
// the go test runner.
func RegisterSuite(driverConstructor DriverConstructor, skipCheck SkipCheck) {
@@ -1343,14 +1351,6 @@ func randomFilename(length int64) string {
return string(b)
}
// randomBytes pre-allocates all of the memory sizes needed for the test. If
// anything panics while accessing randomBytes, just make this number bigger.
var randomBytes = make([]byte, 128<<20)
func init() {
_, _ = crand.Read(randomBytes) // always returns len(randomBytes) and nil error
}
func randomContents(length int64) []byte {
return randomBytes[:length]
}