-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage-helper.go
122 lines (98 loc) · 3.11 KB
/
storage-helper.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
log "github.com/gookit/slog"
"github.com/ryanuber/go-glob"
)
func httpDeleteBySHA(url, username, password, name, sha string) int {
client := &http.Client{}
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/v2/%s/manifests/%v", url, name, strings.TrimSpace(sha)), nil)
if err != nil {
log.Fatal(err.Error())
}
req.SetBasicAuth(username, password)
resp, err := client.Do(req)
if err != nil {
log.Fatal(err.Error())
}
return resp.StatusCode
}
func httpGetHeader(url, username, password, tag, imageName string) string {
client := &http.Client{}
req, err := http.NewRequest("GET", fmt.Sprintf("%s/v2/%s/manifests/%v", url, imageName, tag), nil)
if err != nil {
log.Fatal(err.Error())
}
req.SetBasicAuth(username, password)
req.Header.Set("Accept", "application/vnd.docker.distribution.manifest.v2+json")
resp, err := client.Do(req)
if err != nil {
log.Fatal(err.Error())
}
return resp.Header.Get("Docker-Content-Digest")
}
func httpGetBody(url, username, password string) []byte {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal(err.Error())
}
req.SetBasicAuth(username, password)
resp, err := client.Do(req)
if err != nil {
log.Fatal(err.Error())
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err.Error())
}
return body
}
func getImagesCatalog(url, username, password string) *RegistryCatalog {
registryCatalogBody := httpGetBody(fmt.Sprintf("%s/v2/_catalog", url), username, password)
var catalog *RegistryCatalog
json.Unmarshal(registryCatalogBody, &catalog)
return catalog
}
func getAllTags(url, username, password, imageTagGlob *string) {
catalog := getImagesCatalog(*url, *username, *password)
alltags := AllTags{}
var sortedTagsArray []string
sortedTags := AllTags{}
for _, v := range catalog.Repositories {
tagsListBody := httpGetBody(fmt.Sprintf("%s/v2/%s/tags/list", *url, v), *username, *password)
var tagslist *TagsList
json.Unmarshal(tagsListBody, &tagslist)
alltags.Tags = append(alltags.Tags, *tagslist)
}
for _, taglist := range alltags.Tags {
for _, tag := range taglist.Tags {
if glob.Glob(*imageTagGlob, tag) {
sortedTagsArray = append(sortedTagsArray, tag)
}
}
sortedTags.Tags = append(sortedTags.Tags, TagsList{Name: taglist.Name, Tags: sortedTagsArray})
sortedTagsArray = nil
}
removeImageBySHA(sortedTags, url, username, password)
}
func removeImageBySHA(sortedTags AllTags, url, username, password *string) {
counter := 0
for _, taglist := range sortedTags.Tags {
for _, tag := range taglist.Tags {
sha := httpGetHeader(*url, *username, *password, tag, taglist.Name)
statusCode := httpDeleteBySHA(*url, *username, *password, taglist.Name, sha)
if statusCode != 202 {
log.Error("Error while send delete request for SHA: %s Status code: %s", sha, statusCode)
log.Warnf("Address: %s/v2/%s/manifests/%s", *url, taglist.Name, sha)
} else {
counter += 1
log.Infof("Removed tag with SHA: %v, for image %v. Removed images count: %v", sha, taglist.Name, counter)
}
}
}
}