Skip to content

Commit

Permalink
Add func for pruning an image from a stream
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Goldstein committed May 21, 2015
1 parent 2cbf0c9 commit 7bbd24d
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 222 deletions.
2 changes: 1 addition & 1 deletion pkg/api/graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type Node struct {
graph.Node
concrete.Node
UniqueName
}

Expand Down
10 changes: 9 additions & 1 deletion pkg/api/graph/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,16 +504,24 @@ func (*ImageStreamNode) Kind() int {
return ImageStreamGraphKind
}

func imageStreamName(stream *image.ImageStream) UniqueName {
return UniqueName(fmt.Sprintf("%d|%s", ImageStreamGraphKind, stream.Status.DockerImageRepository))
}

// ImageStream adds a graph node for the Image Stream if it does not already exist.
func ImageStream(g MutableUniqueGraph, stream *image.ImageStream) graph.Node {
return EnsureUnique(g,
UniqueName(fmt.Sprintf("%d|%s", ImageStreamGraphKind, stream.Status.DockerImageRepository)),
imageStreamName(stream),
func(node Node) graph.Node {
return &ImageStreamNode{node, stream}
},
)
}

func FindImageStream(g MutableUniqueGraph, stream *image.ImageStream) graph.Node {
return g.Find(imageStreamName(stream))
}

type ReplicationControllerNode struct {
Node
*kapi.ReplicationController
Expand Down
1 change: 0 additions & 1 deletion pkg/client/fake_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func (c *FakeImages) Create(image *imageapi.Image) (*imageapi.Image, error) {
}

func (c *FakeImages) Delete(name string) error {
c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "delete-image", Value: name})
_, err := c.Fake.Invokes(FakeAction{Action: "delete-image", Value: name}, nil)
return err
}
49 changes: 34 additions & 15 deletions pkg/cmd/admin/prune/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,42 +88,60 @@ func NewCmdPruneImages(f *clientcmd.Factory, parentName, name string, out io.Wri
w := tabwriter.NewWriter(out, 10, 4, 3, ' ', 0)
defer w.Flush()

var streams util.StringSet
printImageHeader := true
describingImagePruneFunc := func(image *imageapi.Image, streams []*imageapi.ImageStream) []error {
describingImagePruneFunc := func(image *imageapi.Image) error {
if printImageHeader {
printImageHeader = false
fmt.Fprintf(w, "IMAGE\tSTREAMS\n")
fmt.Fprintf(w, "IMAGE\tSTREAMS")
}
streamNames := util.NewStringSet()
for _, stream := range streams {
streamNames.Insert(fmt.Sprintf("%s/%s", stream.Namespace, stream.Name))

if streams.Len() > 0 {
fmt.Fprintf(w, strings.Join(streams.List(), ", "))
}
fmt.Fprintf(w, "%s\t%s\n", image.Name, strings.Join(streamNames.List(), ", "))

fmt.Fprintf(w, "\n%s\t", image.Name)
streams = util.NewStringSet()

return nil
}

describingImageStreamPruneFunc := func(stream *imageapi.ImageStream, image *imageapi.Image) (*imageapi.ImageStream, error) {
streams.Insert(stream.Status.DockerImageRepository)
return stream, nil
}

printLayerHeader := true
describingLayerPruneFunc := func(registryURL, repo, layer string) error {
if printLayerHeader {
printLayerHeader = false
fmt.Fprintf(w, "\nREGISTRY\tSTREAM\tLAYER\n")
// need to print the remaining streams for the last image
if streams.Len() > 0 {
fmt.Fprintf(w, strings.Join(streams.List(), ", "))
}
fmt.Fprintf(w, "\n\nREGISTRY\tSTREAM\tLAYER\n")
}
fmt.Fprintf(w, "%s\t%s\t%s\n", registryURL, repo, layer)
return nil
}

var (
imagePruneFunc prune.ImagePruneFunc
layerPruneFunc prune.LayerPruneFunc
blobPruneFunc prune.BlobPruneFunc
manifestPruneFunc prune.ManifestPruneFunc
imagePruneFunc prune.ImagePruneFunc
imageStreamPruneFunc prune.ImageStreamPruneFunc
layerPruneFunc prune.LayerPruneFunc
blobPruneFunc prune.BlobPruneFunc
manifestPruneFunc prune.ManifestPruneFunc
)

switch cfg.DryRun {
case false:
imagePruneFunc = func(image *imageapi.Image, referencedStreams []*imageapi.ImageStream) []error {
describingImagePruneFunc(image, referencedStreams)
return prune.DeletingImagePruneFunc(osClient.Images(), osClient)(image, referencedStreams)
imagePruneFunc = func(image *imageapi.Image) error {
describingImagePruneFunc(image)
return prune.DeletingImagePruneFunc(osClient.Images())(image)
}
imageStreamPruneFunc = func(stream *imageapi.ImageStream, image *imageapi.Image) (*imageapi.ImageStream, error) {
describingImageStreamPruneFunc(stream, image)
return prune.DeletingImageStreamPruneFunc(osClient)(stream, image)
}
layerPruneFunc = func(registryURL, repo, layer string) error {
describingLayerPruneFunc(registryURL, repo, layer)
Expand All @@ -134,6 +152,7 @@ func NewCmdPruneImages(f *clientcmd.Factory, parentName, name string, out io.Wri
default:
fmt.Fprintln(os.Stderr, "Dry run enabled - no modifications will be made.")
imagePruneFunc = describingImagePruneFunc
imageStreamPruneFunc = describingImageStreamPruneFunc
layerPruneFunc = describingLayerPruneFunc
blobPruneFunc = func(registryURL, blob string) error {
return nil
Expand All @@ -143,7 +162,7 @@ func NewCmdPruneImages(f *clientcmd.Factory, parentName, name string, out io.Wri
}
}

pruner.Run(imagePruneFunc, layerPruneFunc, blobPruneFunc, manifestPruneFunc)
pruner.Run(imagePruneFunc, imageStreamPruneFunc, layerPruneFunc, blobPruneFunc, manifestPruneFunc)
},
}

Expand Down
Loading

0 comments on commit 7bbd24d

Please sign in to comment.