Skip to content

Commit

Permalink
Support set CCR least tag number
Browse files Browse the repository at this point in the history
  • Loading branch information
malc0lm committed Jun 1, 2021
1 parent 8db9ca1 commit 092d71a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
29 changes: 22 additions & 7 deletions pkg/apis/ccrapis/ccrapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ func (ai *CCRAPIClient) GetAllCcrRepo(secret map[string]configs.Secret, ccrRegio
offset := int64(0)
limit := int64(100)
maxGorutineNum := 5
page := toltalCount/limit
if toltalCount%limit >0 {
page := toltalCount / limit
if toltalCount%limit > 0 {
page++
}
wg := sync.WaitGroup{}
Expand All @@ -141,12 +141,12 @@ func (ai *CCRAPIClient) GetAllCcrRepo(secret map[string]configs.Secret, ccrRegio
}
}()

for j :=int64(0);j< page;j++ {
for j := int64(0); j < page; j++ {
wg.Add(1)
<-leakCh
go func(n int64) {
defer wg.Done()
offset = n*limit
offset = n * limit
resp, err := ai.DescribeRepositoryOwnerPersonal(secretID, secretKey, ccrRegion, offset, limit)
if err != nil {
log.Errorf("get ccr repo offset %d limit %d error, %s", offset, limit, err)
Expand All @@ -159,14 +159,18 @@ func (ai *CCRAPIClient) GetAllCcrRepo(secret map[string]configs.Secret, ccrRegio
}
}
leakCh <- struct{}{}
}(j)
}(j)
}
wg.Wait()
return nil
}

// GetRepoTags get ccr repo tags
func (ai *CCRAPIClient) GetRepoTags(secretID, secretKey, ccrRegion, repoName string) ([]string, error) {
func (ai *CCRAPIClient) GetRepoTags(secretID, secretKey, ccrRegion, repoName string, ccrTagNum int64) ([]string, error) {

if ccrTagNum < 0 {
return nil, errors.Errorf("Invalid value ccrTagNum %v", ccrTagNum)
}

offset := int64(0)
count := int64(0)
Expand All @@ -189,21 +193,32 @@ func (ai *CCRAPIClient) GetRepoTags(secretID, secretKey, ccrRegion, repoName str
if tagCount == 0 {
return nil, nil
}
if ccrTagNum >= tagCount {
ccrTagNum = tagCount
}

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

if count >= ccrTagNum {
break
}

if count >= tagCount {
break
} else {
offset += limit
}

}
// in case index out of range
if ccrTagNum > int64(len(result)) {
ccrTagNum = int64(len(result))
}

return result, nil
return result[:ccrTagNum], nil
}

func (ai *CCRAPIClient) DescribeImagePersonal(secretID, secretKey,
Expand Down
33 changes: 18 additions & 15 deletions pkg/image-transfer/options/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,19 @@ import (

// ConfigOptions 基础配置信息
type ConfigOptions struct {
SecurityFile string
RuleFile string
RoutineNums int
RetryNums int
QPS int
DefaultRegistry string
SecurityFile string
RuleFile string
RoutineNums int
RetryNums int
QPS int
DefaultRegistry string
DefaultNamespace string
CCRToTCR bool
CCRRegion string
TCRRegion string
TCRName string
SecretFile string
CCRToTCR bool
CCRRegion string
CCRTagNums int
TCRRegion string
TCRName string
SecretFile string
// if target tag is exist override it
TagExistOverridden bool
}
Expand All @@ -62,15 +63,17 @@ func (o *ConfigOptions) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.RuleFile, "ruleFile", o.RuleFile,
"Get images rules config from config file path")
fs.StringVar(&o.DefaultRegistry, "registry", o.DefaultRegistry,
"default destinate registry url when destinate registry is not " +
"given in the config file, can also be set with DEFAULT_REGISTRY environment value")
"default destinate registry url when destinate registry is not "+
"given in the config file, can also be set with DEFAULT_REGISTRY environment value")
fs.StringVar(&o.DefaultNamespace, "ns", o.DefaultNamespace,
"default destinate namespace when destinate namespace is not" +
" given in the config file, can also be set with DEFAULT_NAMESPACE environment value")
"default destinate namespace when destinate namespace is not"+
" given in the config file, can also be set with DEFAULT_NAMESPACE environment value")
fs.IntVar(&o.RoutineNums, "routines", 5,
"number of goroutines, default value is 5, max routines is 50")
fs.IntVar(&o.RetryNums, "retry", 2,
"number of retries, default value is 2")
fs.IntVar(&o.CCRTagNums, "ccrTagNums", 100,
"number of ccr recent tags for every repo, default value is 100, set 0 to sync all tag")
fs.IntVar(&o.QPS, "qps", 100,
"QPS of request, default value is 100, max is 30000")
fs.BoolVar(&o.CCRToTCR, "ccrToTcr", false,
Expand Down
2 changes: 1 addition & 1 deletion pkg/image-transfer/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ func (c *Client) GenCcrtoTcrTagURLPair(source string, target string, wg *sync.Wa
return err
}

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

0 comments on commit 092d71a

Please sign in to comment.