Skip to content

Commit 01bdf7a

Browse files
feat: harbor 镜像列表增加分页
1 parent 110b2d8 commit 01bdf7a

File tree

7 files changed

+75
-21
lines changed

7 files changed

+75
-21
lines changed

internal/api/v1/imagerepo/image_repo.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,19 @@ func (h *Handler) ListImages() iris.Handler {
168168
func (h *Handler) ListImagesByRepo() iris.Handler {
169169
return func(ctx *context.Context) {
170170
name := ctx.Params().GetString("repo")
171-
imageRepos, err := h.imageRepoService.ListImagesByRepo(name, common.DBOptions{})
171+
var req RepoConfig
172+
if err := ctx.ReadJSON(&req); err != nil {
173+
ctx.StatusCode(iris.StatusBadRequest)
174+
ctx.Values().Set("message", err.Error())
175+
return
176+
}
177+
res, err := h.imageRepoService.ListImagesByRepo(name, req.Page, req.Limit, req.Search, common.DBOptions{})
172178
if err != nil && err != storm.ErrNotFound {
173179
ctx.StatusCode(iris.StatusInternalServerError)
174180
ctx.Values().Set("message", err.Error())
175181
return
176182
}
177-
ctx.Values().Set("data", imageRepos)
183+
ctx.Values().Set("data", res)
178184
}
179185
}
180186

@@ -189,5 +195,5 @@ func Install(parent iris.Party) {
189195
sp.Put("/:name", handler.UpdateRepo())
190196
sp.Get("/cluster/:cluster", handler.ListRepoForCluster())
191197
sp.Get("/images/:cluster/:repo", handler.ListImages())
192-
sp.Get("/images/:repo", handler.ListImagesByRepo())
198+
sp.Post("/images/:repo", handler.ListImagesByRepo())
193199
}

internal/model/v1/imagerepo/image_repo.go

+5
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ type Credential struct {
1919
Username string `json:"username"`
2020
Password string `json:"password"`
2121
}
22+
23+
type RepoResponse struct {
24+
Items []string `json:"items"`
25+
ContinueToken string `json:"continueToken"`
26+
}

internal/service/v1/imagerepo/image_repo.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type Service interface {
2525
UpdateRepo(name string, repo *V1ImageRepo.ImageRepo, options common.DBOptions) (err error)
2626
ListByCluster(cluster string, options common.DBOptions) (result []V1ImageRepo.ImageRepo, err error)
2727
ListImages(repo, cluster string, options common.DBOptions) (names []string, err error)
28-
ListImagesByRepo(repo string, options common.DBOptions) (names []string, err error)
28+
ListImagesByRepo(repo string, page, limit int, search string, options common.DBOptions) (res V1ImageRepo.RepoResponse, err error)
2929
}
3030

3131
func NewService() Service {
@@ -95,7 +95,7 @@ func (s *service) ListImages(repo, cluster string, options common.DBOptions) (na
9595
return
9696
}
9797

98-
func (s *service) ListImagesByRepo(repo string, options common.DBOptions) (names []string, err error) {
98+
func (s *service) ListImagesByRepo(repo string, page, limit int, search string, options common.DBOptions) (response V1ImageRepo.RepoResponse, err error) {
9999
rp, err1 := s.GetByName(repo, options)
100100
if err1 != nil {
101101
err = err1
@@ -111,16 +111,22 @@ func (s *service) ListImagesByRepo(repo string, options common.DBOptions) (names
111111
Version: rp.Version,
112112
})
113113
request := repos.RepoRequest{
114-
Repo: rp.RepoName,
114+
Repo: rp.RepoName,
115+
Page: page,
116+
Limit: limit,
117+
Search: search,
115118
}
116119
res, err2 := client.ListImages(request)
117120
if err2 != nil {
118121
err = err2
119122
return
120123
}
124+
var names []string
121125
for _, image := range res.Items {
122126
names = append(names, rp.DownloadUrl+"/"+image)
123127
}
128+
response.Items = names
129+
response.ContinueToken = res.ContinueToken
124130
return
125131
}
126132

pkg/util/imagerepo/repos/harbor.go

+26-10
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,12 @@ func (c *harborClient) ListRepos(request ProjectRequest) (names []string, err er
5353
if request.Name != "" {
5454
projectUrl = projectUrl + "&q=name=~" + request.Name
5555
}
56-
body, _, err1 := c.HttpClient.Get(projectUrl)
57-
if err1 != nil {
58-
err = err1
56+
body, _, err := c.HttpClient.Get(projectUrl)
57+
if err != nil {
5958
return
6059
}
6160
var projects []harborBody
62-
if err1 = json.Unmarshal(body, &projects); err1 != nil {
63-
err = err1
61+
if err = json.Unmarshal(body, &projects); err != nil {
6462
return
6563
}
6664
for _, r := range projects {
@@ -71,7 +69,6 @@ func (c *harborClient) ListRepos(request ProjectRequest) (names []string, err er
7169

7270
func (c *harborClient) ListImages(request RepoRequest) (response RepoResponse, err error) {
7371
project := request.Repo
74-
//计数器
7572
startCount := (request.Page - 1) * request.Limit
7673

7774
if c.Version == "v2" {
@@ -80,6 +77,7 @@ func (c *harborClient) ListImages(request RepoRequest) (response RepoResponse, e
8077
limit := 100
8178
artifactCount := 0
8279
repoArCount := 0
80+
artifactStart := 0
8381
var repos []harborBody
8482
for start {
8583
repoUrl := fmt.Sprintf("%s/%s/%s?page=%d&&page_size=%d", getProjectUrl(c.Version), project, repositoryUrl, p, limit)
@@ -93,26 +91,31 @@ func (c *harborClient) ListImages(request RepoRequest) (response RepoResponse, e
9391
err = err1
9492
return
9593
}
94+
if len(items) < limit {
95+
start = false
96+
}
9697
for _, v := range items {
9798
if v.ArtifactCount == 0 {
9899
continue
99100
}
101+
artifactCount = artifactCount + v.ArtifactCount
100102
if artifactCount >= startCount {
101103
repoArCount = repoArCount + v.ArtifactCount
102-
if repoArCount >= limit {
104+
if repoArCount >= request.Limit {
103105
start = false
104106
break
105107
}
106108
repos = append(repos, v)
107109
continue
108110
}
109-
if artifactCount+v.ArtifactCount <= startCount {
110-
artifactCount = artifactCount + v.ArtifactCount
111+
if artifactCount >= startCount {
112+
artifactStart = artifactCount - startCount
111113
}
112114
}
113115
p++
114116
}
115117
repoUrl := fmt.Sprintf("%s/%s/%s", getProjectUrl(c.Version), project, repositoryUrl)
118+
var items []string
116119
for _, r := range repos {
117120
repoName := strings.Replace(r.Name, project+"/", "", -1)
118121
body, res, err2 := c.HttpClient.Get(fmt.Sprintf("%s/%s/%s?page=%d&&page_size=%d", repoUrl, repoName, artifactUrl, 1, r.ArtifactCount))
@@ -132,10 +135,23 @@ func (c *harborClient) ListImages(request RepoRequest) (response RepoResponse, e
132135
}
133136
for _, art := range artifacts {
134137
for _, tag := range art.Tags {
135-
response.Items = append(response.Items, r.Name+":"+tag.Name)
138+
items = append(items, r.Name+":"+tag.Name)
136139
}
137140
}
138141
}
142+
artifactEnd := artifactStart + limit
143+
if artifactStart+limit > len(items) {
144+
artifactEnd = len(items) - 1
145+
}
146+
if artifactStart > artifactEnd {
147+
response.Items = []string{}
148+
return
149+
}
150+
response.Items = items[artifactStart : artifactEnd+1]
151+
if len(response.Items) == request.Limit {
152+
response.ContinueToken = "continue"
153+
}
154+
139155
} else {
140156
result, err1 := c.HttpClient.GetNameResult(getProjectUrl(c.Version))
141157
if err1 != nil {

pkg/util/imagerepo/repos/repo_config.go

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type RepoRequest struct {
1717
Repo string `json:"repo"`
1818
Page int `json:"page"`
1919
Limit int `json:"limit"`
20+
Search string `json:"search"`
2021
ContinueToken string `json:"continueToken"`
2122
}
2223

web/kubepi/src/api/imagerepos.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ export function listImages(cluster,repo) {
3535
return get(`${baseUrl}/images/${cluster}/${repo}`)
3636
}
3737

38-
export function listImagesByRepo(repo){
39-
return get(`${baseUrl}/images/${repo}`)
38+
export function listImagesByRepo(repo,searchRequest){
39+
return post(`${baseUrl}/images/${repo}`,searchRequest)
4040
}

web/kubepi/src/business/imagerepo-management/detail/index.vue

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<template>
22
<layout-content :header="$t('business.image_repos.images')" :back-to="{ name: 'ImageRepos' }">
3-
43
<el-alert
54
:title="tip"
65
type="info">
@@ -12,6 +11,11 @@
1211
</template>
1312
</el-table-column>
1413
</complex-table>
14+
<div style="float: right">
15+
<el-button icon="el-icon-arrow-left" @click="prePage" :disabled="searchRequest.page<=1"></el-button>
16+
<span>{{searchRequest.page}}</span>
17+
<el-button icon="el-icon-arrow-right" @click="nextPage" :disabled="searchRequest.continueToken===''"></el-button>
18+
</div>
1519
</layout-content>
1620
</template>
1721

@@ -32,13 +36,29 @@ export default {
3236
loading: false,
3337
repoObj: {},
3438
tip: "",
39+
searchRequest: {
40+
page:1,
41+
limit:10,
42+
search: "",
43+
continueToken: ""
44+
}
3545
}
3646
},
3747
methods: {
48+
nextPage() {
49+
this.searchRequest.page ++
50+
this.search()
51+
},
52+
prePage() {
53+
this.searchRequest.page --
54+
this.search()
55+
},
56+
3857
search () {
3958
this.loading = true
40-
listImagesByRepo(this.repo).then(res => {
41-
this.images = res.data
59+
listImagesByRepo(this.repo,this.searchRequest).then(res => {
60+
this.images = res.data.items
61+
this.searchRequest.continueToken = res.data.continueToken
4262
}).finally(() => {
4363
this.loading = false
4464
})

0 commit comments

Comments
 (0)