-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Add App struct for application-scoped data
Signed-off-by: Oleg Bulatov <[email protected]>
- Loading branch information
Oleg Bulatov
committed
Aug 30, 2017
1 parent
7999046
commit 1fdaada
Showing
16 changed files
with
293 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/docker/distribution" | ||
"github.com/docker/distribution/configuration" | ||
"github.com/docker/distribution/context" | ||
"github.com/docker/distribution/registry/auth" | ||
"github.com/docker/distribution/registry/handlers" | ||
storagedriver "github.com/docker/distribution/registry/storage/driver" | ||
|
||
"github.com/openshift/origin/pkg/dockerregistry/server/api" | ||
"github.com/openshift/origin/pkg/dockerregistry/server/client" | ||
registryconfig "github.com/openshift/origin/pkg/dockerregistry/server/configuration" | ||
) | ||
|
||
var ( | ||
// quotaEnforcing contains shared caches of quota objects keyed by project | ||
// name. Will be initialized only if the quota is enforced. | ||
// See EnforceQuotaEnvVar. | ||
quotaEnforcing *quotaEnforcingConfig | ||
) | ||
|
||
type App struct { | ||
registryClient client.RegistryClient | ||
extraConfig *registryconfig.Configuration | ||
|
||
// driver gives access to the blob store. | ||
// This variable holds the object created by docker/distribution. We | ||
// import it into our namespace because there are no other ways to access | ||
// it. In other cases it is hidden from us. | ||
driver storagedriver.StorageDriver | ||
|
||
// registry represents a collection of repositories, addressable by name. | ||
// This variable holds the object created by docker/distribution. We | ||
// import it into our namespace because there are no other ways to access | ||
// it. In other cases it is hidden from us. | ||
registry distribution.Namespace | ||
} | ||
|
||
func NewApp(ctx context.Context, registryClient client.RegistryClient, dockerConfig *configuration.Configuration, extraConfig *registryconfig.Configuration) (*handlers.App, *App) { | ||
app := &App{ | ||
registryClient: registryClient, | ||
extraConfig: extraConfig, | ||
} | ||
ctx = withApp(ctx, app) | ||
|
||
patchConfig(dockerConfig, app) | ||
|
||
dockerApp := handlers.NewApp(ctx, dockerConfig) | ||
|
||
// Add a token handling endpoint | ||
if dockerConfig.Auth.Type() == authOpenShift { | ||
tokenRealm, err := TokenRealm(dockerConfig.Auth[authOpenShift]) | ||
if err != nil { | ||
context.GetLogger(dockerApp).Fatalf("error setting up token auth: %s", err) | ||
} | ||
err = dockerApp.NewRoute().Methods("GET").PathPrefix(tokenRealm.Path).Handler(NewTokenHandler(ctx, registryClient)).GetError() | ||
if err != nil { | ||
context.GetLogger(dockerApp).Fatalf("error setting up token endpoint at %q: %v", tokenRealm.Path, err) | ||
} | ||
context.GetLogger(dockerApp).Debugf("configured token endpoint at %q", tokenRealm.String()) | ||
} | ||
|
||
// TODO add https scheme | ||
adminRouter := dockerApp.NewRoute().PathPrefix(api.AdminPrefix).Subrouter() | ||
pruneAccessRecords := func(*http.Request) []auth.Access { | ||
return []auth.Access{ | ||
{ | ||
Resource: auth.Resource{ | ||
Type: "admin", | ||
}, | ||
Action: "prune", | ||
}, | ||
} | ||
} | ||
|
||
dockerApp.RegisterRoute( | ||
// DELETE /admin/blobs/<digest> | ||
adminRouter.Path(api.AdminPath).Methods("DELETE"), | ||
// handler | ||
BlobDispatcher, | ||
// repo name not required in url | ||
handlers.NameNotRequired, | ||
// custom access records | ||
pruneAccessRecords, | ||
) | ||
|
||
// Registry extensions endpoint provides extra functionality to handle the image | ||
// signatures. | ||
RegisterSignatureHandler(dockerApp) | ||
|
||
// Registry extensions endpoint provides prometheus metrics. | ||
if extraConfig.Metrics.Enabled { | ||
if len(extraConfig.Metrics.Secret) == 0 { | ||
context.GetLogger(dockerApp).Fatalf("openshift.metrics.secret field cannot be empty when metrics are enabled") | ||
} | ||
RegisterMetricHandler(dockerApp) | ||
} | ||
|
||
// Advertise features supported by OpenShift | ||
if dockerApp.Config.HTTP.Headers == nil { | ||
dockerApp.Config.HTTP.Headers = http.Header{} | ||
} | ||
dockerApp.Config.HTTP.Headers.Set("X-Registry-Supports-Signatures", "1") | ||
|
||
return dockerApp, app | ||
} | ||
|
||
func (app *App) Repository(ctx context.Context, repo distribution.Repository, options map[string]interface{}) (distribution.Repository, error) { | ||
if app.driver == nil { | ||
return nil, fmt.Errorf("configuration error: the OpenShift storage driver middleware is not activated") | ||
} | ||
|
||
if app.registry == nil { | ||
return nil, fmt.Errorf("configuration error: the OpenShift registry middleware is not activated") | ||
} | ||
|
||
registryOSClient, err := app.registryClient.Client() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if quotaEnforcing == nil { | ||
quotaEnforcing = newQuotaEnforcingConfig(ctx, os.Getenv(EnforceQuotaEnvVar), os.Getenv(ProjectCacheTTLEnvVar), options) | ||
} | ||
|
||
return newRepositoryWithClient(app, ctx, registryOSClient, repo, options) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.