func (ra *RepositoryAPI) initRepositoryClient(repoName string) (r *registry.Repository, err error) { endpoint := config.InternalRegistryURL() username, password, ok := ra.Ctx.Request.BasicAuth() if ok { return newRepositoryClient(endpoint, api.GetIsInsecure(), username, password, repoName, "repository", repoName, "pull", "push", "*") } username, err = ra.getUsername() if err != nil { return nil, err } return cache.NewRepositoryClient(endpoint, api.GetIsInsecure(), username, repoName, "repository", repoName, "pull", "push", "*") }
func initRegistryClient() (r *registry.Registry, err error) { endpoint := config.InternalRegistryURL() addr := endpoint if strings.Contains(endpoint, "/") { addr = endpoint[strings.LastIndex(endpoint, "/")+1:] } ch := make(chan int, 1) go func() { var err error var c net.Conn for { c, err = net.DialTimeout("tcp", addr, 20*time.Second) if err == nil { c.Close() ch <- 1 } else { log.Errorf("failed to connect to registry client, retry after 2 seconds :%v", err) time.Sleep(2 * time.Second) } } }() select { case <-ch: case <-time.After(60 * time.Second): panic("Failed to connect to registry client after 60 seconds") } registryClient, err := cache.NewRegistryClient(endpoint, true, "admin", "registry", "catalog", "*") if err != nil { return nil, err } return registryClient, nil }
func diffRepos(reposInRegistry []string, reposInDB []string) ([]string, []string, error) { var needsAdd []string var needsDel []string sort.Strings(reposInRegistry) sort.Strings(reposInDB) i, j := 0, 0 repoInR, repoInD := "", "" for i < len(reposInRegistry) && j < len(reposInDB) { repoInR = reposInRegistry[i] repoInD = reposInDB[j] d := strings.Compare(repoInR, repoInD) if d < 0 { i++ exist, err := projectExists(repoInR) if err != nil { log.Errorf("failed to check the existence of project %s: %v", repoInR, err) continue } if !exist { continue } // TODO remove the workaround when the bug of registry is fixed endpoint := config.InternalRegistryURL() client, err := cache.NewRepositoryClient(endpoint, true, "admin", repoInR, "repository", repoInR) if err != nil { return needsAdd, needsDel, err } exist, err = repositoryExist(repoInR, client) if err != nil { return needsAdd, needsDel, err } if !exist { continue } needsAdd = append(needsAdd, repoInR) } else if d > 0 { needsDel = append(needsDel, repoInD) j++ } else { // TODO remove the workaround when the bug of registry is fixed endpoint := config.InternalRegistryURL() client, err := cache.NewRepositoryClient(endpoint, true, "admin", repoInR, "repository", repoInR) if err != nil { return needsAdd, needsDel, err } exist, err := repositoryExist(repoInR, client) if err != nil { return needsAdd, needsDel, err } if !exist { needsDel = append(needsDel, repoInD) } i++ j++ } } for i < len(reposInRegistry) { repoInR = reposInRegistry[i] i++ exist, err := projectExists(repoInR) if err != nil { log.Errorf("failed to check whether project of %s exists: %v", repoInR, err) continue } if !exist { continue } needsAdd = append(needsAdd, repoInR) } for j < len(reposInDB) { needsDel = append(needsDel, reposInDB[j]) j++ } return needsAdd, needsDel, nil }