forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
330 lines (285 loc) · 10 KB
/
util.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package images
import (
"crypto/tls"
"fmt"
"net/http"
"regexp"
"sort"
"strconv"
"strings"
g "github.com/onsi/ginkgo"
"github.com/docker/distribution"
"github.com/docker/distribution/context"
"github.com/docker/distribution/manifest/schema1"
"github.com/docker/distribution/manifest/schema2"
"github.com/docker/distribution/reference"
distclient "github.com/docker/distribution/registry/client"
"github.com/docker/distribution/registry/client/auth"
"github.com/docker/distribution/registry/client/auth/challenge"
"github.com/docker/distribution/registry/client/transport"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
restclient "k8s.io/client-go/rest"
kapiv1 "k8s.io/kubernetes/pkg/api/v1"
kcoreclient "k8s.io/kubernetes/pkg/client/clientset_generated/clientset/typed/core/v1"
dockerregistryserver "github.com/openshift/origin/pkg/dockerregistry/server"
"github.com/openshift/origin/pkg/dockerregistry/testutil"
exutil "github.com/openshift/origin/test/extended/util"
)
const (
readOnlyEnvVar = "REGISTRY_STORAGE_MAINTENANCE_READONLY"
defaultAcceptSchema2 = true
)
// GetDockerRegistryURL returns a cluster URL of internal docker registry if available.
func GetDockerRegistryURL(oc *exutil.CLI) (string, error) {
svc, err := oc.AdminKubeClient().Core().Services("default").Get("docker-registry", metav1.GetOptions{})
if err != nil {
return "", err
}
url := svc.Spec.ClusterIP
for _, p := range svc.Spec.Ports {
url = fmt.Sprintf("%s:%d", url, p.Port)
break
}
return url, nil
}
// GetRegistryStorageSize returns a number of bytes occupied by registry's data on its filesystem.
func GetRegistryStorageSize(oc *exutil.CLI) (int64, error) {
defer func(ns string) { oc.SetNamespace(ns) }(oc.Namespace())
out, err := oc.SetNamespace(metav1.NamespaceDefault).AsAdmin().Run("rsh").Args(
"dc/docker-registry", "du", "--bytes", "--summarize", "/registry/docker/registry").Output()
if err != nil {
return 0, err
}
m := regexp.MustCompile(`^\d+`).FindString(out)
if len(m) == 0 {
return 0, fmt.Errorf("failed to parse du output: %s", out)
}
size, err := strconv.ParseInt(m, 10, 64)
if err != nil {
return 0, fmt.Errorf("failed to parse du output: %s", m)
}
return size, nil
}
// DoesRegistryAcceptSchema2 returns true if the integrated registry is configured to accept manifest V2
// schema 2.
func DoesRegistryAcceptSchema2(oc *exutil.CLI) (bool, error) {
defer func(ns string) { oc.SetNamespace(ns) }(oc.Namespace())
env, err := oc.SetNamespace(metav1.NamespaceDefault).AsAdmin().Run("env").Args("dc/docker-registry", "--list").Output()
if err != nil {
return defaultAcceptSchema2, err
}
if strings.Contains(env, fmt.Sprintf("%s=", dockerregistryserver.AcceptSchema2EnvVar)) {
return strings.Contains(env, fmt.Sprintf("%s=true", dockerregistryserver.AcceptSchema2EnvVar)), nil
}
return defaultAcceptSchema2, nil
}
// RegistriConfiguration holds desired configuration options for the integrated registry. *nil* stands for
// "no change".
type RegistryConfiguration struct {
ReadOnly *bool
AcceptSchema2 *bool
}
type byAgeDesc []kapiv1.Pod
func (ba byAgeDesc) Len() int { return len(ba) }
func (ba byAgeDesc) Swap(i, j int) { ba[i], ba[j] = ba[j], ba[i] }
func (ba byAgeDesc) Less(i, j int) bool {
return ba[j].CreationTimestamp.Before(ba[i].CreationTimestamp)
}
// GetRegistryPod returns the youngest registry pod deployed.
func GetRegistryPod(podsGetter kcoreclient.PodsGetter) (*kapiv1.Pod, error) {
podList, err := podsGetter.Pods(metav1.NamespaceDefault).List(metav1.ListOptions{
LabelSelector: labels.SelectorFromSet(labels.Set{"deploymentconfig": "docker-registry"}).String(),
})
if err != nil {
return nil, err
}
if len(podList.Items) == 0 {
return nil, fmt.Errorf("failed to find any docker-registry pod")
}
sort.Sort(byAgeDesc(podList.Items))
return &podList.Items[0], nil
}
// ConfigureRegistry re-deploys the registry pod if its configuration doesn't match the desiredState. The
// function blocks until the registry is ready.
func ConfigureRegistry(oc *exutil.CLI, desiredState RegistryConfiguration) error {
defer func(ns string) { oc.SetNamespace(ns) }(oc.Namespace())
oc = oc.SetNamespace(metav1.NamespaceDefault).AsAdmin()
env, err := oc.Run("env").Args("dc/docker-registry", "--list").Output()
if err != nil {
return err
}
envOverrides := []string{}
if desiredState.AcceptSchema2 != nil {
current := !strings.Contains(env, fmt.Sprintf("%s=%t", dockerregistryserver.AcceptSchema2EnvVar, false))
if current != *desiredState.AcceptSchema2 {
new := fmt.Sprintf("%s=%t", dockerregistryserver.AcceptSchema2EnvVar, *desiredState.AcceptSchema2)
envOverrides = append(envOverrides, new)
}
}
if desiredState.ReadOnly != nil {
value := fmt.Sprintf("%s=%s", readOnlyEnvVar, makeReadonlyEnvValue(*desiredState.ReadOnly))
if !strings.Contains(env, value) {
envOverrides = append(envOverrides, value)
}
}
if len(envOverrides) == 0 {
g.By("docker-registry is already in the desired state of configuration")
return nil
}
dc, err := oc.AppsClient().Apps().DeploymentConfigs(metav1.NamespaceDefault).Get("docker-registry", metav1.GetOptions{})
if err != nil {
return err
}
waitForVersion := dc.Status.LatestVersion + 1
err = oc.Run("env").Args(append([]string{"dc/docker-registry"}, envOverrides...)...).Execute()
if err != nil {
return fmt.Errorf("failed to update registry's environment with %s: %v", &waitForVersion, err)
}
return exutil.WaitForDeploymentConfig(
oc.AdminKubeClient(),
oc.AdminAppsClient().Apps(),
metav1.NamespaceDefault,
"docker-registry",
waitForVersion,
oc)
}
// EnsureRegistryAcceptsSchema2 checks whether the registry is configured to accept manifests V2 schema 2 or
// not. If the result doesn't match given accept argument, registry's deployment config will be updated
// accordingly and the function will block until the registry have been re-deployed and ready for new
// requests.
func EnsureRegistryAcceptsSchema2(oc *exutil.CLI, accept bool) error {
return ConfigureRegistry(oc, RegistryConfiguration{AcceptSchema2: &accept})
}
func makeReadonlyEnvValue(on bool) string {
return fmt.Sprintf(`{"enabled":%t}`, on)
}
// GetRegistryClientRepository creates a repository interface to the integrated registry.
// If actions are not provided, only pull action will be requested.
func GetRegistryClientRepository(oc *exutil.CLI, repoName string, actions ...string) (distribution.Repository, error) {
endpoint, err := GetDockerRegistryURL(oc)
if err != nil {
return nil, err
}
repoName = completeRepoName(oc, repoName)
if len(actions) == 0 {
actions = []string{"pull"}
}
named, err := reference.ParseNamed(repoName)
if err != nil {
return nil, err
}
token, err := oc.Run("whoami").Args("-t").Output()
if err != nil {
return nil, err
}
creds := testutil.NewBasicCredentialStore(oc.Username(), token)
challengeManager := challenge.NewSimpleManager()
url, versions, err := ping(challengeManager, endpoint, "")
if err != nil {
return nil, fmt.Errorf("failed to ping registry endpoint %s: %v", endpoint, err)
}
fmt.Fprintf(g.GinkgoWriter, "pinged registry at %s, got api versions: %v\n", url, versions)
var rt http.RoundTripper
// TODO: use cluster certificate
rt, err = restclient.TransportFor(&restclient.Config{TLSClientConfig: restclient.TLSClientConfig{Insecure: true}})
if err != nil {
return nil, err
}
rt = transport.NewTransport(
rt,
auth.NewAuthorizer(
challengeManager,
auth.NewTokenHandler(rt, creds, repoName, actions...),
auth.NewBasicHandler(creds)))
ctx := context.Background()
repo, err := distclient.NewRepository(ctx, named, url, rt)
if err != nil {
return nil, fmt.Errorf("failed to get repository %q: %v", repoName, err)
}
return repo, nil
}
// GetManifestAndConfigByTag fetches manifest and corresponding config blob from the given repository:tag from
// the integrated registry. If the manifest is of schema 1, nil will be returned instead of config blob.
func GetManifestAndConfigByTag(oc *exutil.CLI, repoName, tag string) (
manifest distribution.Manifest,
manifestBlob []byte,
configBlob []byte,
err error,
) {
repo, err := GetRegistryClientRepository(oc, repoName)
if err != nil {
return nil, nil, nil, err
}
ctx := context.Background()
desc, err := repo.Tags(ctx).Get(ctx, tag)
if err != nil {
return nil, nil, nil, err
}
ms, err := repo.Manifests(ctx)
if err != nil {
return nil, nil, nil, err
}
manifest, err = ms.Get(ctx, desc.Digest)
if err != nil {
return nil, nil, nil, err
}
switch t := manifest.(type) {
case *schema1.SignedManifest:
manifestBlob, err = t.MarshalJSON()
if err != nil {
return nil, nil, nil, err
}
case *schema2.DeserializedManifest:
manifestBlob, err = t.MarshalJSON()
if err != nil {
return nil, nil, nil, err
}
configBlob, err = repo.Blobs(ctx).Get(ctx, t.Config.Digest)
default:
return nil, nil, nil, fmt.Errorf("got unexpected manifest type: %T", manifest)
}
if err != nil {
return nil, nil, nil, err
}
return
}
func completeRepoName(oc *exutil.CLI, name string) string {
parts := strings.SplitN(name, "/", 2)
if len(parts) > 1 {
return name
}
return strings.Join(append([]string{oc.Namespace()}, parts...), "/")
}
func ping(manager challenge.Manager, endpoint, versionHeader string) (
url string,
apiVersions []auth.APIVersion,
err error,
) {
var resp *http.Response
for _, s := range []string{"https", "http"} {
tr := &http.Transport{}
if s == "https" {
// TODO: use cluster certificate
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
client := &http.Client{Transport: tr}
resp, err = client.Get(fmt.Sprintf("%s://%s/v2/", s, endpoint))
if err == nil {
url = fmt.Sprintf("%s://%s", s, endpoint)
break
}
fmt.Fprintf(g.GinkgoWriter, "failed to ping registry at %s://%v: %v\n", s, endpoint, err)
}
if err != nil {
return "", nil, err
}
defer resp.Body.Close()
if err := manager.AddResponse(resp); err != nil {
return "", nil, err
}
if versionHeader == "" {
versionHeader = "Docker-Distribution-API-Version"
}
return url, auth.APIVersions(resp, versionHeader), nil
}