Example #1
0
    def update_from_import_version(self, chart, version, force=False):
        """
        - read from index.yaml
        -> entries -> chart -> versions -> version
        """
        self.chart = chart
        self.version = version.get("version")
        self.name = version.get("name")
        self.home = version.get("home")
        self.description = version.get("description")
        self.engine = version.get("engine", "default")
        self.created = normalize_time(version.get("created"))
        maintainers = version.get("maintainers")
        if maintainers:
            self.maintainers = version.get("maintainers")
        sources = version.get("sources")
        if sources:
            self.sources = sources
        urls = version.get("urls")

        # fix url which don't contains repo url
        repo_url = self.chart.repository.url
        for idx, url in enumerate(urls):
            urls[idx] = fix_chart_url(url, repo_url)
        self.urls = urls

        keywords = version.get("keywords")
        if keywords:
            self.keywords = ','.join(keywords)

        old_digest = self.digest
        current_digest = version.get("digest")

        # 标识是否有变动
        # 当digest变动时,肯定chart有变动
        # 当repo server允许版本覆盖时,内容有变动但是digest相同,这时也认为有变动
        chart_version_changed = old_digest != current_digest
        if force or chart_version_changed:
            self.digest = version.get("digest")
            # donwload the tar.gz and update files and questions
            url = self.urls[0] if self.urls else None
            if not url:
                self.save()
                return chart_version_changed

            ok, files, questions = download_template_data(
                chart.name, url, auths=self.chart.repository.plain_auths)
            if not ok:
                self.save()
                return chart_version_changed

            if self.files != files:
                self.files = files
                chart_version_changed = True
            if self.questions != questions:
                self.questions = questions
                chart_version_changed = True

        self.save()
        return chart_version_changed
Example #2
0
def sync_helm_repo(repo_id, force=False):
    # if in processing, then do nothing
    sign = InProcessSign(repo_id)
    # TODO: FIXME: uncomment
    if sign.exists():
        logger.error("the helm repo %s if in processing, this task will not be started", repo_id)
        return

    repo = Repository.objects.get(id=repo_id)
    repo_name = repo.name
    repo_url = repo.url
    plain_auths = repo.plain_auths

    try:
        # NOTE: 针对白名单中的项目先开启增量同步
        if enable_increment(force, repo.project_id):
            if not plain_auths:
                username, password = None, None
            else:
                credentials = plain_auths[0]["credentials"]
                username, password = credentials["username"], credentials["password"]
            start_time = normalize_time(repo.refreshed_at)
            charts_info, charts_info_hash = get_incremental_charts_and_hash_value(
                repo_name, username, password, start_time
            )
        else:
            charts_info, charts_info_hash = prepareRepoCharts(repo_url, repo_name, plain_auths)
    except Exception as e:
        logger.exception("prepareRepoCharts fail: repo_url=%s, repo_name=%s, error: %s", repo_url, repo_name, e)
        return

    logger.debug("prepareRepoCharts repo_url=%s, charts_info=%s", repo_url, charts_info)

    # 如果不存在或者为空,认为同步失败
    if not charts_info:
        logger.error("load chart info from repo fail![name=%s, url=%s]", repo_name, repo_url)
        sign.delete()
        return

    # if the index_hash is the same as the commit in db
    # 现阶段兼容先前逻辑,仍然比对MD5,判断是否需要更新
    if not force and charts_info_hash == repo.commit:
        logger.info(
            "the chart index commit [%s] of repo %s not been update since last refresh: %s",
            repo.commit,
            repo_id,
            repo.refreshed_at,
        )
        return

    try:
        # 增量获取的数据,直接添加到本地记录
        if enable_increment(force, repo.project_id):
            _add_charts(repo, sign, charts_info, charts_info_hash, force)
        else:
            _do_helm_repo_charts_update(repo, sign, charts_info, charts_info_hash, force)
    except Exception as e:
        logger.exception("_do_helm_repo_charts_update fail, error: %s", e)
        sign.delete()
Example #3
0
 def normalize_condition(condition: Condition):
     """格式时间 lambda 函数
     """
     condition["lastTransitionTime"] = basic.normalize_time(
         condition["lastTransitionTime"])
     return condition