Skip to content

Commit 092d71a

Browse files
author
malc0lm
committed
Support set CCR least tag number
1 parent 8db9ca1 commit 092d71a

File tree

3 files changed

+41
-23
lines changed

3 files changed

+41
-23
lines changed

pkg/apis/ccrapis/ccrapi.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ func (ai *CCRAPIClient) GetAllCcrRepo(secret map[string]configs.Secret, ccrRegio
126126
offset := int64(0)
127127
limit := int64(100)
128128
maxGorutineNum := 5
129-
page := toltalCount/limit
130-
if toltalCount%limit >0 {
129+
page := toltalCount / limit
130+
if toltalCount%limit > 0 {
131131
page++
132132
}
133133
wg := sync.WaitGroup{}
@@ -141,12 +141,12 @@ func (ai *CCRAPIClient) GetAllCcrRepo(secret map[string]configs.Secret, ccrRegio
141141
}
142142
}()
143143

144-
for j :=int64(0);j< page;j++ {
144+
for j := int64(0); j < page; j++ {
145145
wg.Add(1)
146146
<-leakCh
147147
go func(n int64) {
148148
defer wg.Done()
149-
offset = n*limit
149+
offset = n * limit
150150
resp, err := ai.DescribeRepositoryOwnerPersonal(secretID, secretKey, ccrRegion, offset, limit)
151151
if err != nil {
152152
log.Errorf("get ccr repo offset %d limit %d error, %s", offset, limit, err)
@@ -159,14 +159,18 @@ func (ai *CCRAPIClient) GetAllCcrRepo(secret map[string]configs.Secret, ccrRegio
159159
}
160160
}
161161
leakCh <- struct{}{}
162-
}(j)
162+
}(j)
163163
}
164164
wg.Wait()
165165
return nil
166166
}
167167

168168
// GetRepoTags get ccr repo tags
169-
func (ai *CCRAPIClient) GetRepoTags(secretID, secretKey, ccrRegion, repoName string) ([]string, error) {
169+
func (ai *CCRAPIClient) GetRepoTags(secretID, secretKey, ccrRegion, repoName string, ccrTagNum int64) ([]string, error) {
170+
171+
if ccrTagNum < 0 {
172+
return nil, errors.Errorf("Invalid value ccrTagNum %v", ccrTagNum)
173+
}
170174

171175
offset := int64(0)
172176
count := int64(0)
@@ -189,21 +193,32 @@ func (ai *CCRAPIClient) GetRepoTags(secretID, secretKey, ccrRegion, repoName str
189193
if tagCount == 0 {
190194
return nil, nil
191195
}
196+
if ccrTagNum >= tagCount {
197+
ccrTagNum = tagCount
198+
}
192199

193200
count += int64(len(resp.Response.Data.TagInfo))
194201
for _, tagInfo := range resp.Response.Data.TagInfo {
195202
result = append(result, *tagInfo.TagName)
196203
}
197204

205+
if count >= ccrTagNum {
206+
break
207+
}
208+
198209
if count >= tagCount {
199210
break
200211
} else {
201212
offset += limit
202213
}
203214

204215
}
216+
// in case index out of range
217+
if ccrTagNum > int64(len(result)) {
218+
ccrTagNum = int64(len(result))
219+
}
205220

206-
return result, nil
221+
return result[:ccrTagNum], nil
207222
}
208223

209224
func (ai *CCRAPIClient) DescribeImagePersonal(secretID, secretKey,

pkg/image-transfer/options/config.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,19 @@ import (
2424

2525
// ConfigOptions 基础配置信息
2626
type ConfigOptions struct {
27-
SecurityFile string
28-
RuleFile string
29-
RoutineNums int
30-
RetryNums int
31-
QPS int
32-
DefaultRegistry string
27+
SecurityFile string
28+
RuleFile string
29+
RoutineNums int
30+
RetryNums int
31+
QPS int
32+
DefaultRegistry string
3333
DefaultNamespace string
34-
CCRToTCR bool
35-
CCRRegion string
36-
TCRRegion string
37-
TCRName string
38-
SecretFile string
34+
CCRToTCR bool
35+
CCRRegion string
36+
CCRTagNums int
37+
TCRRegion string
38+
TCRName string
39+
SecretFile string
3940
// if target tag is exist override it
4041
TagExistOverridden bool
4142
}
@@ -62,15 +63,17 @@ func (o *ConfigOptions) AddFlags(fs *pflag.FlagSet) {
6263
fs.StringVar(&o.RuleFile, "ruleFile", o.RuleFile,
6364
"Get images rules config from config file path")
6465
fs.StringVar(&o.DefaultRegistry, "registry", o.DefaultRegistry,
65-
"default destinate registry url when destinate registry is not " +
66-
"given in the config file, can also be set with DEFAULT_REGISTRY environment value")
66+
"default destinate registry url when destinate registry is not "+
67+
"given in the config file, can also be set with DEFAULT_REGISTRY environment value")
6768
fs.StringVar(&o.DefaultNamespace, "ns", o.DefaultNamespace,
68-
"default destinate namespace when destinate namespace is not" +
69-
" given in the config file, can also be set with DEFAULT_NAMESPACE environment value")
69+
"default destinate namespace when destinate namespace is not"+
70+
" given in the config file, can also be set with DEFAULT_NAMESPACE environment value")
7071
fs.IntVar(&o.RoutineNums, "routines", 5,
7172
"number of goroutines, default value is 5, max routines is 50")
7273
fs.IntVar(&o.RetryNums, "retry", 2,
7374
"number of retries, default value is 2")
75+
fs.IntVar(&o.CCRTagNums, "ccrTagNums", 100,
76+
"number of ccr recent tags for every repo, default value is 100, set 0 to sync all tag")
7477
fs.IntVar(&o.QPS, "qps", 100,
7578
"QPS of request, default value is 100, max is 30000")
7679
fs.BoolVar(&o.CCRToTCR, "ccrToTcr", false,

pkg/image-transfer/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ func (c *Client) GenCcrtoTcrTagURLPair(source string, target string, wg *sync.Wa
10231023
return err
10241024
}
10251025

1026-
sourceTags, err := ccrClient.GetRepoTags(ccrSecretID, ccrSecretKey, c.config.FlagConf.Config.CCRRegion, sourceURL.GetRepoWithNamespace())
1026+
sourceTags, err := ccrClient.GetRepoTags(ccrSecretID, ccrSecretKey, c.config.FlagConf.Config.CCRRegion, sourceURL.GetRepoWithNamespace(), int64(c.config.FlagConf.Config.CCRTagNums))
10271027
log.Debugf("ccr target %s tags is %s", sourceURL.GetOriginURL(), sourceTags)
10281028
if err != nil {
10291029
log.Errorf("Failed get ccr repo %s tags, error: %s", sourceURL.GetRepoWithNamespace(), err)

0 commit comments

Comments
 (0)