forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
images.go
279 lines (243 loc) · 7.31 KB
/
images.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
package top
import (
"fmt"
"io"
"sort"
"strings"
units "github.com/docker/go-units"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"github.com/openshift/origin/pkg/api/graph"
kubegraph "github.com/openshift/origin/pkg/api/kubegraph/nodes"
deployapi "github.com/openshift/origin/pkg/apps/apis/apps"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
imagegraph "github.com/openshift/origin/pkg/image/graph/nodes"
)
const (
TopImagesRecommendedName = "images"
maxImageIDLength = 20
)
var (
topImagesLong = templates.LongDesc(`
Show usage statistics for Images
This command analyzes all the Images managed by the platform and presents current
usage statistics.`)
topImagesExample = templates.Examples(`
# Show usage statistics for Images
%[1]s %[2]s`)
)
// NewCmdTopImages implements the OpenShift cli top images command.
func NewCmdTopImages(f *clientcmd.Factory, parentName, name string, out io.Writer) *cobra.Command {
opts := &TopImagesOptions{}
cmd := &cobra.Command{
Use: name,
Short: "Show usage statistics for Images",
Long: topImagesLong,
Example: fmt.Sprintf(topImagesExample, parentName, name),
Run: func(cmd *cobra.Command, args []string) {
kcmdutil.CheckErr(opts.Complete(f, cmd, args, out))
kcmdutil.CheckErr(opts.Validate(cmd))
kcmdutil.CheckErr(opts.Run())
},
}
return cmd
}
type TopImagesOptions struct {
// internal values
Images *imageapi.ImageList
Streams *imageapi.ImageStreamList
Pods *kapi.PodList
// helpers
out io.Writer
}
// Complete turns a partially defined TopImagesOptions into a solvent structure
// which can be validated and used for showing limits usage.
func (o *TopImagesOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, args []string, out io.Writer) error {
kClient, err := f.ClientSet()
if err != nil {
return err
}
imageClient, err := f.OpenshiftInternalImageClient()
if err != nil {
return err
}
namespace := cmd.Flag("namespace").Value.String()
if len(namespace) == 0 {
namespace = metav1.NamespaceAll
}
o.out = out
allImages, err := imageClient.Image().Images().List(metav1.ListOptions{})
if err != nil {
return err
}
o.Images = allImages
allStreams, err := imageClient.Image().ImageStreams(namespace).List(metav1.ListOptions{})
if err != nil {
return err
}
o.Streams = allStreams
allPods, err := kClient.Core().Pods(namespace).List(metav1.ListOptions{})
if err != nil {
return err
}
o.Pods = allPods
return nil
}
// Validate ensures that a TopImagesOptions is valid and can be used to execute command.
func (o TopImagesOptions) Validate(cmd *cobra.Command) error {
return nil
}
// Run contains all the necessary functionality to show current image references.
func (o TopImagesOptions) Run() error {
infos := o.imagesTop()
Print(o.out, ImageColumns, infos)
return nil
}
var ImageColumns = []string{"NAME", "IMAGESTREAMTAG", "PARENTS", "USAGE", "METADATA", "STORAGE"}
// imageInfo contains statistic information about Image usage.
type imageInfo struct {
Image string
ImageStreamTags []string
Parents []string
Usage []string
Metadata bool
Storage int64
}
var _ Info = &imageInfo{}
func (i imageInfo) PrintLine(out io.Writer) {
printValue(out, i.Image)
printArray(out, i.ImageStreamTags)
shortParents := make([]string, len(i.Parents))
for i, p := range i.Parents {
if len(p) > maxImageIDLength {
shortParents[i] = p[:maxImageIDLength-3] + "..."
} else {
shortParents[i] = p
}
}
printArray(out, shortParents)
printArray(out, i.Usage)
printBool(out, i.Metadata)
printValue(out, units.BytesSize(float64(i.Storage)))
}
// imagesTop generates Image information from a graph and returns this as a list
// of imageInfo array.
func (o TopImagesOptions) imagesTop() []Info {
g := graph.New()
addImagesToGraph(g, o.Images)
addImageStreamsToGraph(g, o.Streams)
addPodsToGraph(g, o.Pods)
markParentsInGraph(g)
infos := []Info{}
imageNodes := getImageNodes(g.Nodes())
for _, in := range imageNodes {
image := in.Image
istags := getImageStreamTags(g, in)
parents := getImageParents(g, in)
usage := getImageUsage(g, in)
metadata := len(image.DockerImageManifest) != 0 && len(image.DockerImageLayers) != 0
storage := getStorage(image)
infos = append(infos, imageInfo{
Image: image.Name,
ImageStreamTags: istags,
Parents: parents,
Usage: usage,
Metadata: metadata,
Storage: storage,
})
}
sort.Slice(infos, func(i, j int) bool {
a, b := infos[i].(imageInfo), infos[j].(imageInfo)
if len(a.ImageStreamTags) < len(b.ImageStreamTags) {
return false
}
if len(a.ImageStreamTags) > len(b.ImageStreamTags) {
return true
}
return a.Storage > b.Storage
})
return infos
}
func getStorage(image *imageapi.Image) int64 {
storage := int64(0)
blobSet := sets.NewString()
for _, layer := range image.DockerImageLayers {
if blobSet.Has(layer.Name) {
continue
}
blobSet.Insert(layer.Name)
storage += layer.LayerSize
}
if len(image.DockerImageConfig) > 0 && !blobSet.Has(image.DockerImageMetadata.ID) {
blobSet.Insert(image.DockerImageMetadata.ID)
storage += int64(len(image.DockerImageConfig))
}
return storage
}
func getImageStreamTags(g graph.Graph, node *imagegraph.ImageNode) []string {
istags := []string{}
for _, e := range g.InboundEdges(node, ImageStreamImageEdgeKind) {
streamNode, ok := e.From().(*imagegraph.ImageStreamNode)
if !ok {
continue
}
stream := streamNode.ImageStream
tags := getTags(stream, node.Image)
istags = append(istags, fmt.Sprintf("%s/%s (%s)", stream.Namespace, stream.Name, strings.Join(tags, ",")))
}
return istags
}
func getTags(stream *imageapi.ImageStream, image *imageapi.Image) []string {
tags := []string{}
for tag, history := range stream.Status.Tags {
if len(history.Items) > 0 && history.Items[0].Image == image.Name {
tags = append(tags, tag)
}
}
imageapi.PrioritizeTags(tags)
return tags
}
func getImageParents(g graph.Graph, node *imagegraph.ImageNode) []string {
parents := []string{}
for _, e := range g.InboundEdges(node, ParentImageEdgeKind) {
imageNode, ok := e.From().(*imagegraph.ImageNode)
if !ok {
continue
}
parents = append(parents, imageNode.Image.Name)
}
return parents
}
func getImageUsage(g graph.Graph, node *imagegraph.ImageNode) []string {
usage := []string{}
for _, e := range g.InboundEdges(node, PodImageEdgeKind) {
podNode, ok := e.From().(*kubegraph.PodNode)
if !ok {
continue
}
usage = append(usage, getController(podNode.Pod))
}
return usage
}
func getController(pod *kapi.Pod) string {
controller := "<unknown>"
if pod.Annotations == nil {
return controller
}
if bc, ok := pod.Annotations[buildapi.BuildAnnotation]; ok {
return fmt.Sprintf("Build: %s/%s", pod.Namespace, bc)
}
if dc, ok := pod.Annotations[deployapi.DeploymentAnnotation]; ok {
return fmt.Sprintf("Deployment: %s/%s", pod.Namespace, dc)
}
if dc, ok := pod.Annotations[deployapi.DeploymentPodAnnotation]; ok {
return fmt.Sprintf("Deployer: %s/%s", pod.Namespace, dc)
}
return controller
}