Skip to content

Commit b4b7083

Browse files
author
OpenShift Bot
authoredApr 7, 2017
Merge pull request #13671 from ncdc/fix-image-pruning-weak-strong-refs
Merged by openshift-bot
2 parents 8125090 + 32e11e6 commit b4b7083

File tree

2 files changed

+86
-7
lines changed

2 files changed

+86
-7
lines changed
 

‎pkg/image/prune/prune.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -648,19 +648,15 @@ func edgeKind(g graph.Graph, from, to gonum.Node, desiredKind string) bool {
648648
// for a tag and the image stream is at least as old as the minimum pruning
649649
// age.
650650
func imageIsPrunable(g graph.Graph, imageNode *imagegraph.ImageNode) bool {
651-
onlyWeakReferences := true
652-
653651
for _, n := range g.To(imageNode) {
654652
glog.V(4).Infof("Examining predecessor %#v", n)
655-
if !edgeKind(g, n, imageNode, WeakReferencedImageEdgeKind) {
653+
if edgeKind(g, n, imageNode, ReferencedImageEdgeKind) {
656654
glog.V(4).Infof("Strong reference detected")
657-
onlyWeakReferences = false
658-
break
655+
return false
659656
}
660657
}
661658

662-
return onlyWeakReferences
663-
659+
return true
664660
}
665661

666662
// calculatePrunableImages returns the list of prunable images and a

‎pkg/image/prune/prune_test.go

+83
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ import (
2222
"k8s.io/kubernetes/pkg/runtime"
2323
"k8s.io/kubernetes/pkg/util/sets"
2424

25+
"github.com/openshift/origin/pkg/api/graph"
2526
buildapi "github.com/openshift/origin/pkg/build/api"
2627
"github.com/openshift/origin/pkg/client/testclient"
2728
deployapi "github.com/openshift/origin/pkg/deploy/api"
2829
imageapi "github.com/openshift/origin/pkg/image/api"
30+
imagegraph "github.com/openshift/origin/pkg/image/graph/nodes"
2931
)
3032

3133
type fakeRegistryPinger struct {
@@ -1211,3 +1213,84 @@ func newBool(a bool) *bool {
12111213
*r = a
12121214
return r
12131215
}
1216+
1217+
func TestImageWithStrongAndWeakRefsIsNotPruned(t *testing.T) {
1218+
flag.Lookup("v").Value.Set(fmt.Sprint(*logLevel))
1219+
1220+
images := imageList(
1221+
agedImage("0000000000000000000000000000000000000000000000000000000000000001", "registry1.io/foo/bar@sha256:0000000000000000000000000000000000000000000000000000000000000001", 1540),
1222+
agedImage("0000000000000000000000000000000000000000000000000000000000000002", "registry1.io/foo/bar@sha256:0000000000000000000000000000000000000000000000000000000000000002", 1540),
1223+
agedImage("0000000000000000000000000000000000000000000000000000000000000003", "registry1.io/foo/bar@sha256:0000000000000000000000000000000000000000000000000000000000000003", 1540),
1224+
)
1225+
streams := streamList(
1226+
stream("registry1", "foo", "bar", tags(
1227+
tag("latest",
1228+
tagEvent("0000000000000000000000000000000000000000000000000000000000000003", "registry1.io/foo/bar@sha256:0000000000000000000000000000000000000000000000000000000000000003"),
1229+
tagEvent("0000000000000000000000000000000000000000000000000000000000000002", "registry1.io/foo/bar@sha256:0000000000000000000000000000000000000000000000000000000000000002"),
1230+
tagEvent("0000000000000000000000000000000000000000000000000000000000000001", "registry1.io/foo/bar@sha256:0000000000000000000000000000000000000000000000000000000000000001"),
1231+
),
1232+
tag("strong",
1233+
tagEvent("0000000000000000000000000000000000000000000000000000000000000001", "registry1.io/foo/bar@sha256:0000000000000000000000000000000000000000000000000000000000000001"),
1234+
),
1235+
)),
1236+
)
1237+
pods := podList()
1238+
rcs := rcList()
1239+
bcs := bcList()
1240+
builds := buildList()
1241+
dcs := dcList()
1242+
1243+
options := PrunerOptions{
1244+
Images: &images,
1245+
Streams: &streams,
1246+
Pods: &pods,
1247+
RCs: &rcs,
1248+
BCs: &bcs,
1249+
Builds: &builds,
1250+
DCs: &dcs,
1251+
}
1252+
keepYoungerThan := 24 * time.Hour
1253+
keepTagRevisions := 2
1254+
options.KeepYoungerThan = &keepYoungerThan
1255+
options.KeepTagRevisions = &keepTagRevisions
1256+
p := NewPruner(options)
1257+
p.(*pruner).registryPinger = &fakeRegistryPinger{}
1258+
1259+
imageDeleter := &fakeImageDeleter{invocations: sets.NewString()}
1260+
streamDeleter := &fakeImageStreamDeleter{invocations: sets.NewString()}
1261+
layerLinkDeleter := &fakeLayerLinkDeleter{invocations: sets.NewString()}
1262+
blobDeleter := &fakeBlobDeleter{invocations: sets.NewString()}
1263+
manifestDeleter := &fakeManifestDeleter{invocations: sets.NewString()}
1264+
1265+
if err := p.Prune(imageDeleter, streamDeleter, layerLinkDeleter, blobDeleter, manifestDeleter); err != nil {
1266+
t.Fatalf("unexpected error: %v", err)
1267+
}
1268+
1269+
if imageDeleter.invocations.Len() > 0 {
1270+
t.Fatalf("unexpected imageDeleter invocations: %v", imageDeleter.invocations)
1271+
}
1272+
if streamDeleter.invocations.Len() > 0 {
1273+
t.Fatalf("unexpected streamDeleter invocations: %v", streamDeleter.invocations)
1274+
}
1275+
if layerLinkDeleter.invocations.Len() > 0 {
1276+
t.Fatalf("unexpected layerLinkDeleter invocations: %v", layerLinkDeleter.invocations)
1277+
}
1278+
if blobDeleter.invocations.Len() > 0 {
1279+
t.Fatalf("unexpected blobDeleter invocations: %v", blobDeleter.invocations)
1280+
}
1281+
if manifestDeleter.invocations.Len() > 0 {
1282+
t.Fatalf("unexpected manifestDeleter invocations: %v", manifestDeleter.invocations)
1283+
}
1284+
}
1285+
1286+
func TestImageIsPrunable(t *testing.T) {
1287+
g := graph.New()
1288+
imageNode := imagegraph.EnsureImageNode(g, &imageapi.Image{ObjectMeta: kapi.ObjectMeta{Name: "myImage"}})
1289+
streamNode := imagegraph.EnsureImageStreamNode(g, &imageapi.ImageStream{ObjectMeta: kapi.ObjectMeta{Name: "myStream"}})
1290+
g.AddEdge(streamNode, imageNode, ReferencedImageEdgeKind)
1291+
g.AddEdge(streamNode, imageNode, WeakReferencedImageEdgeKind)
1292+
1293+
if imageIsPrunable(g, imageNode.(*imagegraph.ImageNode)) {
1294+
t.Fatalf("Image is prunable although it should not")
1295+
}
1296+
}

0 commit comments

Comments
 (0)
Please sign in to comment.