forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhardprune.go
533 lines (463 loc) · 19.5 KB
/
hardprune.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
package images
import (
"fmt"
"regexp"
"sort"
"strconv"
"strings"
g "github.com/onsi/ginkgo"
o "github.com/onsi/gomega"
"github.com/docker/distribution/manifest/schema2"
kapiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
kcoreclient "k8s.io/client-go/kubernetes/typed/core/v1"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
exutil "github.com/openshift/origin/test/extended/util"
testutil "github.com/openshift/origin/test/util"
)
var _ = g.Describe("[Feature:ImagePrune][registry][Serial][Suite:openshift/registry/serial][local] Image hard prune", func() {
defer g.GinkgoRecover()
var oc = exutil.NewCLI("prune-images", exutil.KubeConfigPath())
var originalAcceptSchema2 *bool
g.JustBeforeEach(func() {
if originalAcceptSchema2 == nil {
accepts, err := DoesRegistryAcceptSchema2(oc)
o.Expect(err).NotTo(o.HaveOccurred())
originalAcceptSchema2 = &accepts
}
readOnly := false
acceptSchema2 := true
_, err := ConfigureRegistry(oc,
RegistryConfiguration{
ReadOnly: &readOnly,
AcceptSchema2: &acceptSchema2,
})
o.Expect(err).NotTo(o.HaveOccurred())
g.By("waiting for default service account")
err = exutil.WaitForServiceAccount(oc.KubeClient().Core().ServiceAccounts(oc.Namespace()), "default")
o.Expect(err).NotTo(o.HaveOccurred())
g.By("waiting for builder service account")
err = exutil.WaitForServiceAccount(oc.KubeClient().Core().ServiceAccounts(oc.Namespace()), "builder")
o.Expect(err).NotTo(o.HaveOccurred())
defer func(ns string) { oc.SetNamespace(ns) }(oc.Namespace())
g.By(fmt.Sprintf("give a user %s a right to prune images with %s role", oc.Username(), "system:image-pruner"))
err = oc.AsAdmin().WithoutNamespace().Run("adm").Args("policy", "add-cluster-role-to-user", "system:image-pruner", oc.Username()).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
err = oc.AsAdmin().SetNamespace(metav1.NamespaceDefault).Run("adm").
Args("policy", "add-cluster-role-to-user", "system:image-pruner",
fmt.Sprintf("system:serviceaccount:%s:registry", metav1.NamespaceDefault)).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
// make sure to remove all unreferenced blobs from the storage
_, err = RunHardPrune(oc, false)
o.Expect(err).NotTo(o.HaveOccurred())
})
g.AfterEach(func() {
readOnly := false
_, err := ConfigureRegistry(oc,
RegistryConfiguration{
ReadOnly: &readOnly,
AcceptSchema2: originalAcceptSchema2,
})
o.Expect(err).NotTo(o.HaveOccurred())
})
mergeOrSetExpectedDeletions := func(expected, new *RegistryStorageFiles, merge bool) *RegistryStorageFiles {
if !merge {
return new
}
for _, repo := range new.Repos {
expected.Repos = append(expected.Repos, repo)
}
for name, links := range new.ManifestLinks {
expected.ManifestLinks.Add(name, links...)
}
for name, links := range new.LayerLinks {
expected.LayerLinks.Add(name, links...)
}
for _, blob := range new.Blobs {
expected.Blobs = append(expected.Blobs, blob)
}
return expected
}
testHardPrune := func(dryRun bool) {
outSink := g.GinkgoWriter
registryURL, err := GetDockerRegistryURL(oc)
o.Expect(err).NotTo(o.HaveOccurred())
cleanUp := NewCleanUpContainer(oc)
defer cleanUp.Run()
dClient, err := testutil.NewDockerClient()
o.Expect(err).NotTo(o.HaveOccurred())
baseImg1, imageId, err := BuildAndPushImageOfSizeWithDocker(oc, dClient, "a", "latest", testImageSize, 2, outSink, true, false)
o.Expect(err).NotTo(o.HaveOccurred())
cleanUp.AddImage(baseImg1, imageId, "")
baseImg1Spec := fmt.Sprintf("%s/%s/a:latest", registryURL, oc.Namespace())
baseImg2, imageId, err := BuildAndPushImageOfSizeWithDocker(oc, dClient, "b", "latest", testImageSize, 2, outSink, true, false)
o.Expect(err).NotTo(o.HaveOccurred())
cleanUp.AddImage(baseImg2, imageId, "")
baseImg2Spec := fmt.Sprintf("%s/%s/b:latest", registryURL, oc.Namespace())
baseImg3, imageId, err := BuildAndPushImageOfSizeWithDocker(oc, dClient, "c", "latest", testImageSize, 2, outSink, true, false)
o.Expect(err).NotTo(o.HaveOccurred())
cleanUp.AddImage(baseImg3, imageId, "")
baseImg3Spec := fmt.Sprintf("%s/%s/c:latest", registryURL, oc.Namespace())
baseImg4, imageId, err := BuildAndPushImageOfSizeWithDocker(oc, dClient, "a", "img4", testImageSize, 2, outSink, true, false)
o.Expect(err).NotTo(o.HaveOccurred())
cleanUp.AddImage(baseImg4, imageId, "")
childImg1, imageId, err := BuildAndPushChildImage(oc, dClient, baseImg1Spec, "c", "latest", 1, outSink, true)
o.Expect(err).NotTo(o.HaveOccurred())
cleanUp.AddImage(childImg1, "", "")
childImg2, imageId, err := BuildAndPushChildImage(oc, dClient, baseImg2Spec, "b", "latest", 1, outSink, true)
o.Expect(err).NotTo(o.HaveOccurred())
cleanUp.AddImage(childImg2, "", "")
childImg3, imageId, err := BuildAndPushChildImage(oc, dClient, baseImg3Spec, "c", "latest", 1, outSink, true)
o.Expect(err).NotTo(o.HaveOccurred())
cleanUp.AddImage(childImg3, "", "")
err = oc.Run("tag").Args("--source=istag", "a:latest", "a-tagged:latest").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
imgs := map[string]*imageapi.Image{}
for _, imgName := range []string{baseImg1, baseImg2, baseImg3, baseImg4, childImg1, childImg2, childImg3} {
img, err := oc.AsAdmin().ImageClient().Image().Images().Get(imgName, metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
imgs[imgName] = img
o.Expect(img.DockerImageManifestMediaType).To(o.Equal(schema2.MediaTypeManifest))
}
// this shouldn't delete anything
deleted, err := RunHardPrune(oc, dryRun)
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(deleted.Len()).To(o.Equal(0))
/* TODO: use a persistent storage for the registry to preserve data across re-deployments
readOnly := true
restarted, err = registryutil.ConfigureRegistry(oc, registryutil.RegistryConfiguration{ReadOnly: &readOnly})
o.Expect(err).NotTo(o.HaveOccurred())
*/
/* imageName | parent | layers | imagestreams
* ---------- | -------- | ------ | ------------
* baseImg1 | | 1 2 | a a-tagged
* baseImg2 | | 4 5 | b
* baseImg3 | | 7 8 | c
* baseImg4 | | 11 12 | a
* childImg1 | baseImg1 | 1 2 3 | c
* childImg2 | baseImg2 | 4 5 6 | b
* childImg3 | baseImg3 | 7 8 9 | c
*/
err = oc.AsAdmin().ImageClient().Image().ImageStreamTags(oc.Namespace()).Delete("a:latest", nil)
o.Expect(err).NotTo(o.HaveOccurred())
deleted, err = RunHardPrune(oc, dryRun)
o.Expect(err).NotTo(o.HaveOccurred())
expectedDeletions := &RegistryStorageFiles{
/* TODO: reenable once we delete layer links as well
LayerLinks: RepoLinks{oc.Namespace()+"/a": []string{
imgs[baseImg1].DockerImageMetadata.ID,
imgs[baseImg1].DockerImageLayers[0].Name,
imgs[baseImg1].DockerImageLayers[1].Name,
}},
*/
ManifestLinks: RepoLinks{oc.Namespace() + "/a": []string{baseImg1}},
}
err = AssertDeletedStorageFiles(deleted, expectedDeletions)
o.Expect(err).NotTo(o.HaveOccurred())
err = oc.AsAdmin().ImageClient().Image().Images().Delete(childImg1, nil)
o.Expect(err).NotTo(o.HaveOccurred())
// The repository a-tagged will not be removed even though it has no tags anymore.
// For the repository to be removed, the image stream itself needs to be deleted.
err = oc.AsAdmin().ImageClient().Image().ImageStreamTags(oc.Namespace()).Delete("a-tagged:latest", nil)
o.Expect(err).NotTo(o.HaveOccurred())
deleted, err = RunHardPrune(oc, dryRun)
o.Expect(err).NotTo(o.HaveOccurred())
expectedDeletions = mergeOrSetExpectedDeletions(expectedDeletions,
&RegistryStorageFiles{
/* TODO: reenable once we delete layer links as well
LayerLinks: RepoLinks{oc.Namespace()+"/c": []string{
imgs[childImg1].DockerImageMetadata.ID,
imgs[childImg1].DockerImageLayers[0].Name,
}},
*/
ManifestLinks: RepoLinks{oc.Namespace() + "/c": []string{childImg1}},
Blobs: []string{
childImg1, // manifest blob
imgs[childImg1].DockerImageMetadata.ID, // manifest config
imgs[childImg1].DockerImageLayers[0].Name,
},
},
dryRun)
err = AssertDeletedStorageFiles(deleted, expectedDeletions)
o.Expect(err).NotTo(o.HaveOccurred())
err = oc.AsAdmin().ImageClient().Image().Images().Delete(baseImg1, nil)
o.Expect(err).NotTo(o.HaveOccurred())
deleted, err = RunHardPrune(oc, dryRun)
o.Expect(err).NotTo(o.HaveOccurred())
expectedDeletions = mergeOrSetExpectedDeletions(expectedDeletions,
&RegistryStorageFiles{
Blobs: []string{
baseImg1, // manifest blob
imgs[baseImg1].DockerImageMetadata.ID, // manifest config
imgs[baseImg1].DockerImageLayers[0].Name,
imgs[baseImg1].DockerImageLayers[1].Name,
},
},
dryRun)
err = AssertDeletedStorageFiles(deleted, expectedDeletions)
o.Expect(err).NotTo(o.HaveOccurred())
err = oc.AsAdmin().ImageClient().Image().Images().Delete(childImg2, nil)
o.Expect(err).NotTo(o.HaveOccurred())
deleted, err = RunHardPrune(oc, dryRun)
o.Expect(err).NotTo(o.HaveOccurred())
expectedDeletions = mergeOrSetExpectedDeletions(expectedDeletions,
&RegistryStorageFiles{
/* TODO: reenable once we delete layer links as well
LayerLinks: RepoLinks{oc.Namespace()+"/b": []string{
imgs[childImg2].DockerImageMetadata.ID,
imgs[childImg2].DockerImageLayers[0].Name,
}},
*/
ManifestLinks: RepoLinks{oc.Namespace() + "/b": []string{childImg2}},
Blobs: []string{
childImg2, // manifest blob
imgs[childImg2].DockerImageMetadata.ID, // manifest config
imgs[childImg2].DockerImageLayers[0].Name,
},
},
dryRun)
err = AssertDeletedStorageFiles(deleted, expectedDeletions)
o.Expect(err).NotTo(o.HaveOccurred())
// untag both baseImg2 and childImg2
err = oc.AsAdmin().ImageClient().Image().ImageStreams(oc.Namespace()).Delete("b", nil)
o.Expect(err).NotTo(o.HaveOccurred())
delete(expectedDeletions.ManifestLinks, oc.Namespace()+"/b")
err = oc.AsAdmin().ImageClient().Image().Images().Delete(baseImg2, nil)
o.Expect(err).NotTo(o.HaveOccurred())
deleted, err = RunHardPrune(oc, dryRun)
o.Expect(err).NotTo(o.HaveOccurred())
expectedDeletions = mergeOrSetExpectedDeletions(expectedDeletions,
&RegistryStorageFiles{
/* TODO: reenable once we delete layer links as well
LayerLinks: RepoLinks{oc.Namespace()+"/b": []string{
imgs[baseImg2].DockerImageMetadata.ID,
imgs[baseImg2].DockerImageLayers[0].Name,
imgs[baseImg2].DockerImageLayers[1].Name,
}},
*/
Repos: []string{oc.Namespace() + "/b"},
Blobs: []string{
baseImg2, // manifest blob
imgs[baseImg2].DockerImageMetadata.ID, // manifest config
imgs[baseImg2].DockerImageLayers[0].Name,
imgs[baseImg2].DockerImageLayers[1].Name,
},
},
dryRun)
err = AssertDeletedStorageFiles(deleted, expectedDeletions)
o.Expect(err).NotTo(o.HaveOccurred())
/* updated is/image table
* imageName | parent | layers | imagestreams
* ---------- | -------- | ------ | ------------
* baseImg3 | | 7 8 | c
* baseImg4 | | 11 12 | a
* childImg3 | baseImg3 | 7 8 9 | c
*/
// delete baseImg3 using soft prune
output, err := oc.WithoutNamespace().Run("adm").Args(
"prune", "images", "--keep-tag-revisions=1", "--keep-younger-than=0").Output()
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(output).To(o.ContainSubstring(baseImg3))
o.Expect(output).To(o.ContainSubstring(imgs[baseImg3].DockerImageMetadata.ID))
for _, layer := range imgs[baseImg3].DockerImageLayers {
o.Expect(output).To(o.ContainSubstring(layer.Name))
}
o.Expect(output).NotTo(o.ContainSubstring(baseImg4))
o.Expect(output).NotTo(o.ContainSubstring(childImg3))
// there should be nothing left for hard pruner to delete
deleted, err = RunHardPrune(oc, dryRun)
o.Expect(err).NotTo(o.HaveOccurred())
if !dryRun {
expectedDeletions = &RegistryStorageFiles{}
}
err = AssertDeletedStorageFiles(deleted, expectedDeletions)
o.Expect(err).NotTo(o.HaveOccurred())
err = oc.AsAdmin().ImageClient().Image().Images().Delete(childImg3, nil)
o.Expect(err).NotTo(o.HaveOccurred())
deleted, err = RunHardPrune(oc, dryRun)
o.Expect(err).NotTo(o.HaveOccurred())
expectedDeletions = mergeOrSetExpectedDeletions(expectedDeletions,
&RegistryStorageFiles{
/* TODO: reenable once we delete layer links as well
LayerLinks: RepoLinks{oc.Namespace()+"/b": []string{
imgs[baseImg2].DockerImageMetadata.ID,
imgs[baseImg2].DockerImageLayers[0].Name,
imgs[baseImg2].DockerImageLayers[1].Name,
}},
*/
ManifestLinks: RepoLinks{oc.Namespace() + "/c": []string{childImg3}},
Blobs: []string{
childImg3,
imgs[childImg3].DockerImageMetadata.ID, // manifest config
imgs[childImg3].DockerImageLayers[0].Name,
},
},
dryRun)
err = AssertDeletedStorageFiles(deleted, expectedDeletions)
o.Expect(err).NotTo(o.HaveOccurred())
/* updated is/image table
* imageName | parent | layers | imagestreams
* ---------- | -------- | ------ | ------------
* baseImg3 | | 7 8 | c
* baseImg4 | | 11 12 | a
*/
assertImageBlobsPresent := func(present bool, img *imageapi.Image) {
for _, layer := range img.DockerImageLayers {
o.Expect(pathExistsInRegistry(oc, strings.Split(blobToPath("", layer.Name), "/")...)).
To(o.Equal(present))
}
o.Expect(pathExistsInRegistry(oc, strings.Split(blobToPath("", img.DockerImageMetadata.ID), "/")...)).
To(o.Equal(present))
o.Expect(pathExistsInRegistry(oc, strings.Split(blobToPath("", img.Name), "/")...)).
To(o.Equal(present))
}
for _, img := range []string{baseImg1, childImg1, baseImg2, childImg2} {
assertImageBlobsPresent(dryRun, imgs[img])
}
for _, img := range []string{baseImg3, baseImg4} {
assertImageBlobsPresent(true, imgs[img])
}
}
g.It("should show orphaned blob deletions in dry-run mode", func() {
testHardPrune(true)
})
g.It("should delete orphaned blobs", func() {
testHardPrune(false)
})
})
const (
AcceptSchema2EnvVar = "REGISTRY_MIDDLEWARE_REPOSITORY_OPENSHIFT_ACCEPTSCHEMA2"
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) {
return oc.Run("registry").Args("info").Output()
}
// 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
}
// LogRegistryPod attempts to write registry log to a file in artifacts directory.
func LogRegistryPod(oc *exutil.CLI) error {
pod, err := GetRegistryPod(oc.KubeClient().Core())
if err != nil {
return fmt.Errorf("failed to get registry pod: %v", err)
}
ocLocal := *oc
path, err := ocLocal.Run("logs").Args("dc/docker-registry").OutputToFile("pod-" + pod.Name + ".log")
if err == nil {
fmt.Fprintf(g.GinkgoWriter, "written registry pod log to %s\n", path)
}
return err
}
// 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) (bool, error) {
defer func(ns string) { oc.SetNamespace(ns) }(oc.Namespace())
oc = oc.SetNamespace(metav1.NamespaceDefault).AsAdmin()
env, err := oc.Run("set", "env").Args("dc/docker-registry", "--list").Output()
if err != nil {
return false, err
}
envOverrides := []string{}
if desiredState.AcceptSchema2 != nil {
current := !strings.Contains(env, fmt.Sprintf("%s=%t", AcceptSchema2EnvVar, false))
if current != *desiredState.AcceptSchema2 {
new := fmt.Sprintf("%s=%t", 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 false, nil
}
if err := oc.Run("set", "env").Args(append([]string{"dc/docker-registry"}, envOverrides...)...).Execute(); err != nil {
return false, fmt.Errorf("failed to update registry's environment: %v", err)
}
if err := oc.Run("rollout").Args("status", "dc/docker-registry").Execute(); err != nil {
return false, fmt.Errorf("unable to get registry rollout status: %v", err)
}
return true, nil
}
func RedeployRegistry(oc *exutil.CLI) (bool, error) {
defer func(ns string) { oc.SetNamespace(ns) }(oc.Namespace())
oc = oc.SetNamespace(metav1.NamespaceDefault).AsAdmin()
if err := oc.Run("rollout").Args("latest", "dc/docker-registry").Execute(); err != nil {
return false, fmt.Errorf("failed to rollout registry: %v", err)
}
if err := oc.Run("rollout").Args("status", "dc/docker-registry").Execute(); err != nil {
return false, fmt.Errorf("unable to get registry rollout status: %v", err)
}
return true, nil
}
// 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) (bool, error) {
return ConfigureRegistry(oc, RegistryConfiguration{AcceptSchema2: &accept})
}
func makeReadonlyEnvValue(on bool) string {
return fmt.Sprintf(`{"enabled":%t}`, on)
}
// 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("set", "env").Args("dc/docker-registry", "--list").Output()
if err != nil {
return defaultAcceptSchema2, err
}
if strings.Contains(env, fmt.Sprintf("%s=", AcceptSchema2EnvVar)) {
return strings.Contains(env, fmt.Sprintf("%s=true", AcceptSchema2EnvVar)), nil
}
return defaultAcceptSchema2, nil
}