harbor api v2.0

v2.0

“harbor api v2.0”与v1区别较大,此处harbor也做了https。另外,通过接口拿到的数据也是只能默认1页10个,所以脚本根据实际情况一页页的循环抓取数据
脚本主要用于统计repo(仓库)、image,以及所有镜像的tag数,函数change_images_txt用于调整格式,函数alert将tag数大于30的告警出来,最后登录harbor,将告警的镜像中较老的tag手动清理掉(当然也可以通过如下接口拿到所有镜像的tag,根据时间来排序,将较老的部分通过接口自动删除,但是“reference”这个参数获取比较复杂,暂未采用这种方式)
在这里插入图片描述

#!/bin/bash
HARBOR_URL=harbor.example.com
HARBOR_USER=admin
HARBOR_PASSWD=Harbor12345
#OLD_VERSION_NUM=30
script_path=$(dirname $0)
cd $script_path


function get_repos_list(){
  mkdir -p $PWD/reposList
  >$PWD/reposList/reposList.txt
  for i in `seq 1 8`;do
    repos_list=$(curl -s -k -u ${HARBOR_USER}:${HARBOR_PASSWD} https://${HARBOR_URL}/api/v2.0/projects?page=$i)
    echo "${repos_list}" | jq '.[]' | jq -r '.name' >> $PWD/reposList/reposList.txt
  done
}
function get_images_list(){
  mkdir -p $PWD/imagesList
  rm -f $PWD/imagesList/*.txt
  for repo in $(cat $PWD/reposList/reposList.txt);do
	  for j in `seq 1 20`;do
		  images_list=$(curl -s -k -u ${HARBOR_USER}:${HARBOR_PASSWD} https://${HARBOR_URL}/api/v2.0/projects/$repo/repositories?page=$j)
		  echo "${images_list}" | jq '.[]' | jq -r '.name' >> $PWD/imagesList/${repo}.txt

	  done
  done
}
function conut_tags(){
  >images.txt
  for k in `seq 1 100`;do
    htmlinfo=$(curl -s -k -u ${HARBOR_USER}:${HARBOR_PASSWD} https://${HARBOR_URL}/api/v2.0/repositories?page=$k)
    echo $htmlinfo |jq '.[]|select(.artifact_count > 30)'|jq '.name, .artifact_count' >> images.txt
  done
  sed -i 's/"//g' images.txt
}

function change_images_txt(){
  >images1.txt
  num=0
  for i in `cat $1`;do
	let num++    #;echo $num
	if [ $((num%2)) -eq 1 ];then
		IMG=$i
	else
		CNT=$i
		echo "$IMG $CNT" >>images1.txt
	fi
  done
  cat images1.txt |sort |awk '{print $2,$1}' > images2.txt
}
function alert(){
    METRIC=$1
    LINE=`cat $METRIC|wc -l`
    if [ $LINE -gt 0 ];then
	MSG=""
	while read line;do
	    MSG="$MSG\n$line"
	done < $METRIC
	#echo -e $MSG
	#MSG=234567890
	CMD="curl -H \"Content-Type:application/json\" -X POST --data '{\"mobile\":\"13123456789\",\"message\":\"新镜像仓库镜像tag数>30$MSG\",\"priority\":\"2\",\"moserial\":\"1234567\"}' http://xxxxxxxxxxxxxxxx"
	#echo "$CMD"
	eval "$CMD"
    fi
}

conut_tags
change_images_txt images.txt
alert images2.txt
GitHub 加速计划 / ha / harbor
23.24 K
4.67 K
下载
Harbor 是一个开源的容器镜像仓库,用于存储和管理 Docker 镜像和其他容器镜像。 * 容器镜像仓库、存储和管理 Docker 镜像和其他容器镜像 * 有什么特点:支持多种镜像格式、易于使用、安全性和访问控制
最近提交(Master分支:1 个月前 )
c5d26723 chore(deps): bump github.com/go-openapi/runtime in /src Bumps [github.com/go-openapi/runtime](https://github.com/go-openapi/runtime) from 0.26.2 to 0.28.0. - [Release notes](https://github.com/go-openapi/runtime/releases) - [Commits](https://github.com/go-openapi/runtime/compare/v0.26.2...v0.28.0) --- updated-dependencies: - dependency-name: github.com/go-openapi/runtime dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: miner <yminer@vmware.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Shengwen YU <yshengwen@vmware.com> Co-authored-by: miner <yminer@vmware.com> 14 天前
76624373 chore(deps): bump github.com/coreos/go-oidc/v3 in /src Bumps [github.com/coreos/go-oidc/v3](https://github.com/coreos/go-oidc) from 3.10.0 to 3.11.0. - [Release notes](https://github.com/coreos/go-oidc/releases) - [Commits](https://github.com/coreos/go-oidc/compare/v3.10.0...v3.11.0) --- updated-dependencies: - dependency-name: github.com/coreos/go-oidc/v3 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Shengwen YU <yshengwen@vmware.com> Co-authored-by: miner <yminer@vmware.com> 14 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐