forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.go
134 lines (107 loc) · 5.06 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package server
import (
"github.com/docker/distribution/context"
"github.com/openshift/origin/pkg/client"
"github.com/openshift/origin/pkg/dockerregistry/server/configuration"
"github.com/openshift/origin/pkg/dockerregistry/server/maxconnections"
)
type contextKey string
const (
// repositoryKey serves to store/retrieve repository object to/from context.
repositoryKey contextKey = "repository"
// remoteBlobAccessCheckEnabledKey is the key for the flag in Contexts
// to allow blobDescriptorService to stat remote blobs.
remoteBlobAccessCheckEnabledKey contextKey = "remoteBlobAccessCheckEnabled"
// registryClientKey is the key for RegistryClient values in Contexts.
registryClientKey contextKey = "registryClient"
// writeLimiterKey is the key for write limiters in Contexts.
writeLimiterKey contextKey = "writeLimiter"
// userClientKey is the key for a origin's client with the current user's
// credentials in Contexts.
userClientKey contextKey = "userClient"
// authPerformedKey is the key to indicate that authentication was
// performed in Contexts.
authPerformedKey contextKey = "authPerformed"
// deferredErrorsKey is the key for deferred errors in Contexts.
deferredErrorsKey contextKey = "deferredErrors"
// configurationKey is the key for Configuration in Context.
configurationKey contextKey = "configuration"
)
// withRepository returns a new Context that carries value repo.
func withRepository(parent context.Context, repo *repository) context.Context {
return context.WithValue(parent, repositoryKey, repo)
}
// repositoryFrom returns the repository value stored in ctx, if any.
func repositoryFrom(ctx context.Context) (repo *repository, found bool) {
repo, found = ctx.Value(repositoryKey).(*repository)
return
}
// withRemoteBlobAccessCheckEnabled returns a new Context that allows
// blobDescriptorService to stat remote blobs. It is useful only in case
// of manifest verification.
func withRemoteBlobAccessCheckEnabled(parent context.Context, enable bool) context.Context {
return context.WithValue(parent, remoteBlobAccessCheckEnabledKey, enable)
}
// remoteBlobAccessCheckEnabledFrom reports whether ctx allows
// blobDescriptorService to stat remote blobs.
func remoteBlobAccessCheckEnabledFrom(ctx context.Context) bool {
enabled, _ := ctx.Value(remoteBlobAccessCheckEnabledKey).(bool)
return enabled
}
// WithRegistryClient returns a new Context with provided registry client.
func WithRegistryClient(ctx context.Context, client RegistryClient) context.Context {
return context.WithValue(ctx, registryClientKey, client)
}
// RegistryClientFrom returns the registry client stored in ctx if present.
// It will panic otherwise.
func RegistryClientFrom(ctx context.Context) RegistryClient {
return ctx.Value(registryClientKey).(RegistryClient)
}
// WithWriteLimiter returns a new Context with a write limiter.
func WithWriteLimiter(ctx context.Context, writeLimiter maxconnections.Limiter) context.Context {
return context.WithValue(ctx, writeLimiterKey, writeLimiter)
}
// WriteLimiterFrom returns the write limiter if one is stored in ctx, or nil otherwise.
func WriteLimiterFrom(ctx context.Context) maxconnections.Limiter {
writeLimiter, _ := ctx.Value(writeLimiterKey).(maxconnections.Limiter)
return writeLimiter
}
// withUserClient returns a new Context with the origin's client.
// This client should have the current user's credentials
func withUserClient(parent context.Context, userClient client.Interface) context.Context {
return context.WithValue(parent, userClientKey, userClient)
}
// userClientFrom returns the origin's client stored in ctx, if any.
func userClientFrom(ctx context.Context) (client.Interface, bool) {
userClient, ok := ctx.Value(userClientKey).(client.Interface)
return userClient, ok
}
// withAuthPerformed returns a new Context with indication that authentication
// was performed.
func withAuthPerformed(parent context.Context) context.Context {
return context.WithValue(parent, authPerformedKey, true)
}
// authPerformed reports whether ctx has indication that authentication was
// performed.
func authPerformed(ctx context.Context) bool {
authPerformed, ok := ctx.Value(authPerformedKey).(bool)
return ok && authPerformed
}
// withDeferredErrors returns a new Context that carries deferred errors.
func withDeferredErrors(parent context.Context, errs deferredErrors) context.Context {
return context.WithValue(parent, deferredErrorsKey, errs)
}
// deferredErrorsFrom returns the deferred errors stored in ctx, if any.
func deferredErrorsFrom(ctx context.Context) (deferredErrors, bool) {
errs, ok := ctx.Value(deferredErrorsKey).(deferredErrors)
return errs, ok
}
// WithConfiguration returns a new Context with provided configuration.
func WithConfiguration(ctx context.Context, config *configuration.Configuration) context.Context {
return context.WithValue(ctx, configurationKey, config)
}
// ConfigurationFrom returns the configuration stored in ctx if present.
// It will panic otherwise.
func ConfigurationFrom(ctx context.Context) *configuration.Configuration {
return ctx.Value(configurationKey).(*configuration.Configuration)
}