projects

api/v2.0/projects

列出已有harbor已有项目及信息

package main

import (
  "fmt"
  "net/http"
  "io/ioutil"
)

func main() {

  url := "http://harbor.cfuture.shop/api/v2.0/projects"
  method := "GET"

  client := &http.Client {
  }
  req, err := http.NewRequest(method, url, nil)

  if err != nil {
    fmt.Println(err)
    return
  }
  req.Header.Add("Cookie", "_gorilla_csrf=MTY1NTgwOTY2OXxJalpFUkhjd05WQXZUVGRaY1doWlREaERiMEp3V2pGemRUZHhkbE55WWswM1YxQXJZVFI2VURnMmJsVTlJZ289fP6uA3oEHIs4r6JzUDTdQqwbGaQ1PghYcky8rtGXIrV7; sid=c5499826f0b1d51ae150cc68460ff5d3") //请求的header头需要添加cookie返回的headers中才会有X-Harbor-Csrf-Token
  //req.Header.Add("Authorization", "Basic *********") 请求中添加Authorization才会获取到私有项目,否则只会返回公开项目
  res, err := client.Do(req)
  if err != nil {
    fmt.Println(err)
    return
  }
  defer res.Body.Close()

  body, err := ioutil.ReadAll(res.Body)
  if err != nil {
    fmt.Println(err)
    return
  }
  fmt.Println(string(body))
}

请求的header头需要添加cookie返回的headers中才会有X-Harbor-Csrf-Token(这个在后面会用到)

api/v2.0/projects

创建harbor项目,下面为创建一个名称为test3的无存储限制的公开项目。

package main

import (
  "fmt"
  "strings"
  "net/http"
  "io/ioutil"
)

func main() {

  url := "http://harbor.cfuture.shop/api/v2.0/projects"
  method := "POST"

  payload := strings.NewReader(`{
    "project_name":"test3",
    "metadata":{"public":"true"},
    "storage_limit":-1,
    "registry_id":null
}`)

  client := &http.Client {
  }
  req, err := http.NewRequest(method, url, payload)

  if err != nil {
    fmt.Println(err)
    return
  }
  req.Header.Add("X-Harbor-CSRF-Token", "v8C2Z6TSkxgsgiM2i6R64JCvcDElkGwW/T43Pul6e0YvYn1be9SF0PSWaLfX8tv2WG4s3Pv/dhAeNUKmNfEBHA==")
  req.Header.Add("Authorization", "Basic *********")
  req.Header.Add("Content-Type", "application/json")
  req.Header.Add("Cookie", "_gorilla_csrf=MTY1NTgwOTY2OXxJalpFUkhjd05WQXZUVGRaY1doWlREaERiMEp3V2pGemRUZHhkbE55WWswM1YxQXJZVFI2VURnMmJsVTlJZ289fP6uA3oEHIs4r6JzUDTdQqwbGaQ1PghYcky8rtGXIrV7; sid=c5499826f0b1d51ae150cc68460ff5d3")

  res, err := client.Do(req)
  if err != nil {
    fmt.Println(err)
    return
  }
  defer res.Body.Close()

  body, err := ioutil.ReadAll(res.Body)
  if err != nil {
    fmt.Println(err)
    return
  }
  fmt.Println(string(body))
}

retentions

GET api/v2.0/retentions/1

获取项目已有策略

package main

import (
  "fmt"
  "net/http"
  "io/ioutil"
)

func main() {

  url := "http://harbor.cfuture.shop/api/v2.0/retentions/1"
  method := "GET"

  client := &http.Client {
  }
  req, err := http.NewRequest(method, url, nil)

  if err != nil {
    fmt.Println(err)
    return
  }
  req.Header.Add("Authorization", "Basic *******")
  req.Header.Add("Cookie", "_gorilla_csrf=MTY1NTgwOTY2OXxJalpFUkhjd05WQXZUVGRaY1doWlREaERiMEp3V2pGemRUZHhkbE55WWswM1YxQXJZVFI2VURnMmJsVTlJZ289fP6uA3oEHIs4r6JzUDTdQqwbGaQ1PghYcky8rtGXIrV7; sid=c5499826f0b1d51ae150cc68460ff5d3")

  res, err := client.Do(req)
  if err != nil {
    fmt.Println(err)
    return
  }
  defer res.Body.Close()

  body, err := ioutil.ReadAll(res.Body)
  if err != nil {
    fmt.Println(err)
    return
  }
  fmt.Println(string(body))
}

PUT api/v2.0/retentions/1

新建项目的策略,下面为新建一个定时清理策略,保留所有仓库最近推送的5个镜像,清理时间为每周。
在这里插入图片描述

package main

import (
  "fmt"
  "strings"
  "net/http"
  "io/ioutil"
)

func main() {

  url := "http://harbor.cfuture.shop/api/v2.0/retentions/1"
  method := "PUT"

  payload := strings.NewReader(`{"algorithm":"or","rules":[{"action":"retain","params":{"latestPushedK":5},"scope_selectors":{"repository":[{"decoration":"repoMatches","kind":"doublestar","pattern":"**"}]},"tag_selectors":[{"decoration":"matches","extras":"{\"untagged\":true}","kind":"doublestar","pattern":"**"}],"template":"latestPushedK"}],"scope":{"level":"project","ref":2},"trigger":{"kind":"Schedule","settings":{"cron":"0 0 0 * * 0"}}}`)

  client := &http.Client {
  }
  req, err := http.NewRequest(method, url, payload)

  if err != nil {
    fmt.Println(err)
    return
  }
  req.Header.Add("X-Harbor-CSRF-Token", "Tni7u0teESVyIAbx0iEBKarfCLgATsC+ZajVk33xrWYtrjNcHkSEPEtuvG9obxIWz/LDAYvO5jlhqe4abYA/bA==")
  req.Header.Add("Authorization", "Basic *******")
  req.Header.Add("Content-Type", "application/json")
  req.Header.Add("Cookie", "_gorilla_csrf=MTY1NTgwOTY2OXxJalpFUkhjd05WQXZUVGRaY1doWlREaERiMEp3V2pGemRUZHhkbE55WWswM1YxQXJZVFI2VURnMmJsVTlJZ289fP6uA3oEHIs4r6JzUDTdQqwbGaQ1PghYcky8rtGXIrV7; sid=c5499826f0b1d51ae150cc68460ff5d3")

  res, err := client.Do(req)
  if err != nil {
    fmt.Println(err)
    return
  }
  defer res.Body.Close()

  body, err := ioutil.ReadAll(res.Body)
  if err != nil {
    fmt.Println(err)
    return
  }
  fmt.Println(string(body))
}
GitHub 加速计划 / ha / harbor
23.24 K
4.68 K
下载
Harbor 是一个开源的容器镜像仓库,用于存储和管理 Docker 镜像和其他容器镜像。 * 容器镜像仓库、存储和管理 Docker 镜像和其他容器镜像 * 有什么特点:支持多种镜像格式、易于使用、安全性和访问控制
最近提交(Master分支:2 个月前 )
9e55afbb pull image from registry.goharbor.io instead of dockerhub Update testcase to support Docker Image Can Be Pulled With Credential Change gitlab project name when user changed. Update permissions count and permission count total Change webhook_endpoint_ui Signed-off-by: stonezdj <stone.zhang@broadcom.com> Co-authored-by: Wang Yan <wangyan@vmware.com> 9 天前
3dbfd422 Signed-off-by: wang yan <wangyan@vmware.com> 10 天前
Logo

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

更多推荐