/
manifests.go
161 lines (148 loc) · 4.31 KB
/
manifests.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
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package registryhelper
import (
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/erda-project/erda/pkg/http/httpclient"
)
type RemoveManifestsRequest struct {
RegistryURL string
Images []string
ClusterKey string
}
type RemoveManifestsResponse struct {
Succeed []string
Failed map[string]string
}
func (req RemoveManifestsRequest) removeManifests(s, clusterKey string) string {
var name, tag string
if i := strings.IndexByte(s, '/'); i != -1 {
name = s[i+1:]
if i := strings.LastIndexByte(name, ':'); i != -1 {
name, tag = name[:i], name[i+1:]
}
}
if name == "" {
return "image name is empty"
}
if tag == "" {
tag = "latest"
}
c := httpclient.New(httpclient.WithClusterDialer(clusterKey))
res, err := c.Get(req.RegistryURL).Path(fmt.Sprintf("/v2/%s/manifests/%s", name, tag)).
Header("Content-Type", "application/json").
Header("Accept", "application/vnd.docker.distribution.manifest.v2+json").
Do().DiscardBody()
if err != nil {
return "get manifests failed: " + err.Error()
}
if sc := res.StatusCode(); sc != http.StatusOK {
if sc == http.StatusNotFound { // does not exist
return ""
} else {
return "get manifests failed: status code is " + strconv.Itoa(sc)
}
}
dcd := res.ResponseHeader("Docker-Content-Digest")
if dcd == "" {
return "get manifests failed: header Docker-Content-Digest is empty"
}
res, err = c.Delete(req.RegistryURL).Path(fmt.Sprintf("/v2/%s/manifests/%s", name, dcd)).
Header("Content-Type", "application/json").
Do().DiscardBody()
if err != nil {
return "delete manifests failed: " + err.Error()
}
if sc := res.StatusCode(); sc != http.StatusOK && sc != http.StatusAccepted {
return "delete manifests failed: status code is " + strconv.Itoa(sc)
}
return ""
}
func RemoveManifests(req RemoveManifestsRequest) (*RemoveManifestsResponse, error) {
if req.RegistryURL == "" {
req.RegistryURL = os.Getenv("REGISTRY_ADDR")
}
if req.RegistryURL == "" {
return nil, errors.New("no registry url")
}
m := make(map[string]string, len(req.Images))
for _, image := range req.Images {
if _, ok := m[image]; !ok {
m[image] = req.removeManifests(image, req.ClusterKey)
}
}
var res RemoveManifestsResponse
for k, v := range m {
if v == "" {
logrus.Infof("%s: delete image succeed\n", k)
res.Succeed = append(res.Succeed, k)
} else {
if res.Failed == nil {
res.Failed = make(map[string]string)
}
logrus.Warningf("%s: %s\n", k, v)
res.Failed[k] = v
}
}
return &res, nil
}
type State struct {
Running bool `json:"running"`
StartTime time.Time `json:"startTime"`
EndTime time.Time `json:"endTime"`
LastError string `json:"lastError"`
}
type StateReply struct {
Success bool `json:"success"`
Data *State `json:"data"`
Err *struct {
Code string `json:"code"`
Msg string `json:"msg"`
Ctx interface{} `json:"ctx"`
} `json:"err"`
}
func GetGcState(registryctlURL, clusterKey string) (*State, error) {
if registryctlURL == "" {
registryctlURL = os.Getenv("REGISTRY_ADDR")
if registryctlURL != "" {
i := strings.LastIndexByte(registryctlURL, ':')
if i != -1 {
registryctlURL = registryctlURL[0:i]
}
registryctlURL = registryctlURL + ":5050"
}
}
if registryctlURL == "" {
return nil, errors.New("no registryctl url")
}
var v StateReply
c := httpclient.New(httpclient.WithClusterDialer(clusterKey))
res, err := c.Get(registryctlURL).Path("/gc").Do().JSON(&v)
if err != nil {
return nil, err
}
if sc := res.StatusCode(); sc != http.StatusOK {
return nil, errors.Errorf("get gc state failed: status code is:%d", sc)
}
if !v.Success {
return nil, errors.Errorf("get gc state failed:%s", v.Err.Msg)
}
return v.Data, nil
}