forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
imagestreams.go
211 lines (182 loc) · 5.8 KB
/
imagestreams.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
package top
import (
"fmt"
"io"
"sort"
units "github.com/docker/go-units"
gonum "github.com/gonum/graph"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
dockerv10 "github.com/openshift/api/image/docker10"
imagev1 "github.com/openshift/api/image/v1"
imagev1client "github.com/openshift/client-go/image/clientset/versioned/typed/image/v1"
imageutil "github.com/openshift/origin/pkg/image/util"
"github.com/openshift/origin/pkg/oc/lib/graph/genericgraph"
imagegraph "github.com/openshift/origin/pkg/oc/lib/graph/imagegraph/nodes"
)
const TopImageStreamsRecommendedName = "imagestreams"
var (
topImageStreamsLong = templates.LongDesc(`
Show usage statistics for ImageStreams
This command analyzes all the ImageStreams managed by the platform and presents current
usage statistics.`)
topImageStreamsExample = templates.Examples(`
# Show usage statistics for ImageStreams
%[1]s %[2]s`)
)
type TopImageStreamsOptions struct {
// internal values
Images *imagev1.ImageList
Streams *imagev1.ImageStreamList
genericclioptions.IOStreams
}
func NewTopImageStreamsOptions(streams genericclioptions.IOStreams) *TopImageStreamsOptions {
return &TopImageStreamsOptions{
IOStreams: streams,
}
}
// NewCmdTopImageStreams implements the OpenShift cli top imagestreams command.
func NewCmdTopImageStreams(f kcmdutil.Factory, parentName, name string, streams genericclioptions.IOStreams) *cobra.Command {
o := NewTopImageStreamsOptions(streams)
cmd := &cobra.Command{
Use: name,
Short: "Show usage statistics for ImageStreams",
Long: topImageStreamsLong,
Example: fmt.Sprintf(topImageStreamsExample, parentName, name),
Run: func(cmd *cobra.Command, args []string) {
kcmdutil.CheckErr(o.Complete(f, cmd, args))
kcmdutil.CheckErr(o.Validate(cmd))
kcmdutil.CheckErr(o.Run())
},
}
return cmd
}
// Complete turns a partially defined TopImageStreamsOptions into a solvent structure
// which can be validated and used for showing limits usage.
func (o *TopImageStreamsOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, args []string) error {
namespace := cmd.Flag("namespace").Value.String()
if len(namespace) == 0 {
namespace = metav1.NamespaceAll
}
clientConfig, err := f.ToRESTConfig()
if err != nil {
return err
}
imageClient, err := imagev1client.NewForConfig(clientConfig)
if err != nil {
return err
}
allImages, err := imageClient.Images().List(metav1.ListOptions{})
if err != nil {
return err
}
o.Images = allImages
allStreams, err := imageClient.ImageStreams(namespace).List(metav1.ListOptions{})
if err != nil {
return err
}
o.Streams = allStreams
return nil
}
// Validate ensures that a TopImageStreamsOptions is valid and can be used to execute command.
func (o TopImageStreamsOptions) Validate(cmd *cobra.Command) error {
return nil
}
// Run contains all the necessary functionality to show current image references.
func (o TopImageStreamsOptions) Run() error {
infos := o.imageStreamsTop()
Print(o.Out, ImageStreamColumns, infos)
return nil
}
var ImageStreamColumns = []string{"NAME", "STORAGE", "IMAGES", "LAYERS"}
// imageStreamInfo contains contains statistic information about ImageStream usage.
type imageStreamInfo struct {
ImageStream string
Storage int64
Images int
Layers int
}
var _ Info = &imageStreamInfo{}
func (i imageStreamInfo) PrintLine(out io.Writer) {
printValue(out, i.ImageStream)
printValue(out, units.BytesSize(float64(i.Storage)))
printValue(out, i.Images)
printValue(out, i.Layers)
}
// imageStreamsTop generates ImageStream information from a graph and
// returns this as a list of imageStreamInfo array.
func (o TopImageStreamsOptions) imageStreamsTop() []Info {
g := genericgraph.New()
addImagesToGraph(g, o.Images)
addImageStreamsToGraph(g, o.Streams)
infos := []Info{}
streamNodes := getImageStreamNodes(g.Nodes())
for _, sn := range streamNodes {
storage, images, layers := getImageStreamSize(g, sn)
infos = append(infos, imageStreamInfo{
ImageStream: fmt.Sprintf("%s/%s", sn.ImageStream.Namespace, sn.ImageStream.Name),
Storage: storage,
Images: images,
Layers: layers,
})
}
sort.Slice(infos, func(i, j int) bool {
a, b := infos[i].(imageStreamInfo), infos[j].(imageStreamInfo)
if a.Storage < b.Storage {
return false
}
if a.Storage > b.Storage {
return true
}
return a.Images > b.Images
})
return infos
}
func getImageStreamSize(g genericgraph.Graph, node *imagegraph.ImageStreamNode) (int64, int, int) {
imageEdges := g.OutboundEdges(node, ImageStreamImageEdgeKind)
storage := int64(0)
images := len(imageEdges)
layers := 0
blobSet := sets.NewString()
for _, e := range imageEdges {
imageNode, ok := e.To().(*imagegraph.ImageNode)
if !ok {
continue
}
image := imageNode.Image
layers += len(image.DockerImageLayers)
// we're counting only unique layers per the entire stream
for _, layer := range image.DockerImageLayers {
if blobSet.Has(layer.Name) {
continue
}
blobSet.Insert(layer.Name)
storage += layer.LayerSize
}
if err := imageutil.ImageWithMetadata(image); err != nil {
continue
}
dockerImage, ok := image.DockerImageMetadata.Object.(*dockerv10.DockerImage)
if !ok {
continue
}
if len(image.DockerImageConfig) > 0 && !blobSet.Has(dockerImage.ID) {
blobSet.Insert(dockerImage.ID)
storage += int64(len(image.DockerImageConfig))
}
}
return storage, images, layers
}
func getImageStreamNodes(nodes []gonum.Node) []*imagegraph.ImageStreamNode {
ret := []*imagegraph.ImageStreamNode{}
for i := range nodes {
if node, ok := nodes[i].(*imagegraph.ImageStreamNode); ok {
ret = append(ret, node)
}
}
return ret
}