forked from openshift/origin
/
images.go
275 lines (230 loc) · 8.94 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
package prune
import (
"crypto/x509"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"text/tabwriter"
"time"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kclient "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/golang/glog"
"github.com/openshift/origin/pkg/client"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
imageapi "github.com/openshift/origin/pkg/image/api"
"github.com/openshift/origin/pkg/image/prune"
"github.com/spf13/cobra"
)
const imagesLongDesc = `%s %s - prunes images`
const PruneImagesRecommendedName = "images"
type pruneImagesConfig struct {
Confirm bool
KeepYoungerThan time.Duration
TagRevisionsToKeep int
CABundle string
RegistryUrlOverride string
}
func NewCmdPruneImages(f *clientcmd.Factory, parentName, name string, out io.Writer) *cobra.Command {
cfg := &pruneImagesConfig{
Confirm: false,
KeepYoungerThan: 60 * time.Minute,
TagRevisionsToKeep: 3,
}
cmd := &cobra.Command{
Use: name,
Short: "Remove unreferenced images",
Long: fmt.Sprintf(imagesLongDesc, parentName, name),
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 0 {
glog.Fatal("No arguments are allowed to this command")
}
osClient, kClient, registryClient, err := getClients(f, cfg)
cmdutil.CheckErr(err)
allImages, err := osClient.Images().List(labels.Everything(), fields.Everything())
cmdutil.CheckErr(err)
allStreams, err := osClient.ImageStreams(kapi.NamespaceAll).List(labels.Everything(), fields.Everything())
cmdutil.CheckErr(err)
allPods, err := kClient.Pods(kapi.NamespaceAll).List(labels.Everything(), fields.Everything())
cmdutil.CheckErr(err)
allRCs, err := kClient.ReplicationControllers(kapi.NamespaceAll).List(labels.Everything())
cmdutil.CheckErr(err)
allBCs, err := osClient.BuildConfigs(kapi.NamespaceAll).List(labels.Everything(), fields.Everything())
cmdutil.CheckErr(err)
allBuilds, err := osClient.Builds(kapi.NamespaceAll).List(labels.Everything(), fields.Everything())
cmdutil.CheckErr(err)
allDCs, err := osClient.DeploymentConfigs(kapi.NamespaceAll).List(labels.Everything(), fields.Everything())
cmdutil.CheckErr(err)
pruner := prune.NewImagePruner(
cfg.KeepYoungerThan,
cfg.TagRevisionsToKeep,
allImages,
allStreams,
allPods,
allRCs,
allBCs,
allBuilds,
allDCs,
)
w := tabwriter.NewWriter(out, 10, 4, 3, ' ', 0)
defer w.Flush()
var streams util.StringSet
printImageHeader := true
describingImagePruneFunc := func(image *imageapi.Image) error {
if printImageHeader {
printImageHeader = false
fmt.Fprintf(w, "IMAGE\tSTREAMS")
}
if streams.Len() > 0 {
fmt.Fprintf(w, strings.Join(streams.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
// 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
imageStreamPruneFunc prune.ImageStreamPruneFunc
layerPruneFunc prune.LayerPruneFunc
blobPruneFunc prune.BlobPruneFunc
manifestPruneFunc prune.ManifestPruneFunc
)
switch cfg.Confirm {
case true:
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)
return prune.DeletingLayerPruneFunc(registryClient)(registryURL, repo, layer)
}
blobPruneFunc = prune.DeletingBlobPruneFunc(registryClient)
manifestPruneFunc = prune.DeletingManifestPruneFunc(registryClient)
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
}
manifestPruneFunc = func(registryURL, repo, manifest string) error {
return nil
}
}
if len(cfg.RegistryUrlOverride) > 0 {
originalLayerPruneFunc := layerPruneFunc
layerPruneFunc = func(registryURL, repo, layer string) error {
return originalLayerPruneFunc(cfg.RegistryUrlOverride, repo, layer)
}
originalBlobPruneFunc := blobPruneFunc
blobPruneFunc = func(registryURL, blob string) error {
return originalBlobPruneFunc(cfg.RegistryUrlOverride, blob)
}
originalManifestPruneFunc := manifestPruneFunc
manifestPruneFunc = func(registryURL, repo, manifest string) error {
return originalManifestPruneFunc(cfg.RegistryUrlOverride, repo, manifest)
}
}
pruner.Run(imagePruneFunc, imageStreamPruneFunc, layerPruneFunc, blobPruneFunc, manifestPruneFunc)
},
}
cmd.Flags().BoolVar(&cfg.Confirm, "confirm", cfg.Confirm, "Specify that image pruning should proceed. Defaults to false, displaying what would be deleted but not actually deleting anything.")
cmd.Flags().DurationVar(&cfg.KeepYoungerThan, "keep-younger-than", cfg.KeepYoungerThan, "Specify the minimum age of a build for it to be considered a candidate for pruning.")
cmd.Flags().IntVar(&cfg.TagRevisionsToKeep, "keep-tag-revisions", cfg.TagRevisionsToKeep, "Specify the number of image revisions for a tag in an image stream that will be preserved.")
cmd.Flags().StringVar(&cfg.CABundle, "certificate-authority", cfg.CABundle, "The path to a certificate authority bundle to use when communicating with the OpenShift-managed registries. Defaults to the certificate authority data from the current user's config file.")
cmd.Flags().StringVar(&cfg.RegistryUrlOverride, "registry-url", cfg.RegistryUrlOverride, "The address to use when contacting the registry, instead of using the default value. This is useful if you can't resolve or reach the registry (e.g.; the default is a cluster-internal URL) but you do have an alternative route that works.")
return cmd
}
func getClients(f *clientcmd.Factory, cfg *pruneImagesConfig) (*client.Client, *kclient.Client, *http.Client, error) {
clientConfig, err := f.OpenShiftClientConfig.ClientConfig()
if err != nil {
return nil, nil, nil, err
}
var (
token string
osClient *client.Client
kClient *kclient.Client
registryClient *http.Client
)
switch {
case len(clientConfig.BearerToken) > 0:
osClient, kClient, err = f.Clients()
if err != nil {
return nil, nil, nil, err
}
token = clientConfig.BearerToken
default:
err = errors.New("You must use a client config with a token")
return nil, nil, nil, err
}
// copy the config
registryClientConfig := *clientConfig
// zero out everything we don't want to use
registryClientConfig.BearerToken = ""
registryClientConfig.CertFile = ""
registryClientConfig.CertData = []byte{}
registryClientConfig.KeyFile = ""
registryClientConfig.KeyData = []byte{}
// we have to set a username to something for the Docker login
// but it's not actually used
registryClientConfig.Username = "unused"
// set the "password" to be the token
registryClientConfig.Password = token
tlsConfig, err := kclient.TLSConfigFor(®istryClientConfig)
if err != nil {
return nil, nil, nil, err
}
// if the user specified a CA on the command line, add it to the
// client config's CA roots
if len(cfg.CABundle) > 0 {
data, err := ioutil.ReadFile(cfg.CABundle)
if err != nil {
return nil, nil, nil, err
}
if tlsConfig.RootCAs == nil {
tlsConfig.RootCAs = x509.NewCertPool()
}
tlsConfig.RootCAs.AppendCertsFromPEM(data)
}
transport := http.Transport{
TLSClientConfig: tlsConfig,
}
wrappedTransport, err := kclient.HTTPWrappersForConfig(®istryClientConfig, &transport)
if err != nil {
return nil, nil, nil, err
}
registryClient = &http.Client{
Transport: wrappedTransport,
}
return osClient, kClient, registryClient, nil
}