[ Cmii ] [ Operator ] - Harbor Operator
This commit is contained in:
165
agent-operator/image/HarborOperator.go
Normal file
165
agent-operator/image/HarborOperator.go
Normal file
@@ -0,0 +1,165 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/mittwald/goharbor-client/v5/apiv2"
|
||||
"github.com/mittwald/goharbor-client/v5/apiv2/model"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type HarborOperator struct {
|
||||
HarborHost string
|
||||
HarborPort string
|
||||
HarborUser string
|
||||
HarborPass string
|
||||
HarborClient *apiv2.RESTClient
|
||||
}
|
||||
|
||||
var LocalHarborOperator = newDefaultInstance
|
||||
|
||||
func newDefaultInstance() {
|
||||
|
||||
}
|
||||
|
||||
const (
|
||||
DefaultHarborPort = "8033"
|
||||
DefaultHarborUser = "admin"
|
||||
DefaultHarborPass = "V2ryStr@ngPss"
|
||||
)
|
||||
|
||||
func (op *HarborOperator) BuildOperator() (bool, error) {
|
||||
if op.HarborPort != "" {
|
||||
op.HarborHost = op.HarborHost + ":" + op.HarborPort
|
||||
}
|
||||
if !strings.HasSuffix(op.HarborHost, "/api/") {
|
||||
op.HarborHost = op.HarborHost + "/api/"
|
||||
}
|
||||
log.InfoF("[Harbor Client Create] - start to create harbor client %s", op.HarborHost)
|
||||
|
||||
if op.HarborUser == "" {
|
||||
op.HarborUser = DefaultHarborUser
|
||||
}
|
||||
if op.HarborPass == "" {
|
||||
op.HarborPass = DefaultHarborPass
|
||||
}
|
||||
|
||||
// check connection
|
||||
|
||||
client, err := apiv2.NewRESTClientForHost(op.HarborHost, op.HarborUser, op.HarborPass, nil)
|
||||
if err != nil {
|
||||
errorLog := fmt.Sprintf("Error creating REST client: %s\n", err.Error())
|
||||
log.Error(errorLog)
|
||||
return false, err
|
||||
}
|
||||
|
||||
op.HarborClient = client
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (op *HarborOperator) RepoListAll(projectName string) []*model.Repository {
|
||||
|
||||
listRepositories, err := op.HarborClient.ListRepositories(context.TODO(), projectName)
|
||||
if err != nil {
|
||||
log.ErrorF("RepoListAll error is %s", err.Error())
|
||||
return nil
|
||||
}
|
||||
return listRepositories
|
||||
}
|
||||
|
||||
// RepoAllCmiiImage 返回没有前缀的内容 cmii-uav-user
|
||||
func (op *HarborOperator) RepoAllCmiiImage() (repoNameList []string) {
|
||||
|
||||
listAll := op.RepoListAll("cmii")
|
||||
for _, repository := range listAll {
|
||||
prefix := strings.TrimPrefix(repository.Name, "cmii/")
|
||||
if strings.HasPrefix(prefix, "cmii") {
|
||||
repoNameList = append(repoNameList, prefix)
|
||||
}
|
||||
}
|
||||
|
||||
// 返回没有
|
||||
return repoNameList
|
||||
}
|
||||
|
||||
func (op *HarborOperator) ArtifactListAll(projectName, repoName string) []*model.Artifact {
|
||||
listArtifacts, err := op.HarborClient.ListArtifacts(context.TODO(), projectName, repoName)
|
||||
if err != nil {
|
||||
log.ErrorF("ArtifactListAll error is %s", err.Error())
|
||||
return nil
|
||||
}
|
||||
return listArtifacts
|
||||
}
|
||||
|
||||
func (op *HarborOperator) ArtifactListOne(projectName, repoName, artifactReference string) *model.Artifact {
|
||||
artifact, err := op.HarborClient.GetArtifact(context.TODO(), projectName, repoName, artifactReference)
|
||||
if err != nil {
|
||||
log.ErrorF("ArtifactListOne error is %s", err.Error())
|
||||
return nil
|
||||
}
|
||||
return artifact
|
||||
}
|
||||
|
||||
func (op *HarborOperator) ArtifactDeleteOne(projectName, repoName, artifactReference string) bool {
|
||||
err := op.HarborClient.DeleteArtifact(context.TODO(), projectName, repoName, artifactReference)
|
||||
if err != nil {
|
||||
log.ErrorF("ArtifactDeleteOne error is %s", err.Error())
|
||||
return false
|
||||
}
|
||||
log.InfoF("ArtifactDeleteOne of %s %s %s is success", projectName, repoName, artifactReference)
|
||||
return true
|
||||
}
|
||||
|
||||
func (op *HarborOperator) RepoListOne(projectName, repoName string) *model.Repository {
|
||||
|
||||
repository, err := op.HarborClient.GetRepository(context.TODO(), projectName, repoName)
|
||||
if err != nil {
|
||||
log.ErrorF("RepoListOne error is %s", err.Error())
|
||||
return nil
|
||||
}
|
||||
|
||||
return repository
|
||||
}
|
||||
|
||||
// CmiiTagFilter 根据tag前缀 找到所有的cmii tag
|
||||
func (op *HarborOperator) CmiiTagFilter(tagPrefix string) (allCmiiImageList []string) {
|
||||
|
||||
repoNameList := op.RepoAllCmiiImage() // all cmii repo name
|
||||
|
||||
for _, cmiiRepo := range repoNameList {
|
||||
// cmii-uav-user
|
||||
artifactList := op.ArtifactListAll("cmii", cmiiRepo)
|
||||
for _, artifact := range artifactList {
|
||||
// list all artifact of cmii-uav-user
|
||||
for _, tag := range artifact.Tags {
|
||||
if strings.HasPrefix(tag.Name, tagPrefix) {
|
||||
// tag has prefix 5.4.0-123 5.4.0-123-123
|
||||
allCmiiImageList = append(allCmiiImageList, cmiiRepo+":"+tag.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return allCmiiImageList
|
||||
}
|
||||
|
||||
func (op *HarborOperator) ArtifactDeleteFromNameTagList(projectName string, nameTagList []string) (errorDeleteList []string) {
|
||||
for _, nameTag := range nameTagList {
|
||||
split := strings.Split(nameTag, ":")
|
||||
if len(split) != 2 {
|
||||
log.ErrorF("ArtifactDeleteFromNameTagList error is %s", nameTag)
|
||||
errorDeleteList = append(errorDeleteList, nameTag)
|
||||
continue
|
||||
}
|
||||
|
||||
repoName := split[0]
|
||||
tagName := split[1]
|
||||
if !op.ArtifactDeleteOne(projectName, repoName, tagName) {
|
||||
log.ErrorF("ArtifactDeleteFromNameTagList error is %s %s %s", projectName, repoName, tagName)
|
||||
errorDeleteList = append(errorDeleteList, nameTag)
|
||||
}
|
||||
}
|
||||
|
||||
return errorDeleteList
|
||||
}
|
||||
96
agent-operator/image/HarborOperator_test.go
Normal file
96
agent-operator/image/HarborOperator_test.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package image
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"wdd.io/agent-common/utils"
|
||||
)
|
||||
|
||||
var DefaultHarborOperator *HarborOperator
|
||||
|
||||
func TestHarborOperator_BuildOperator(t *testing.T) {
|
||||
harborOperator := &HarborOperator{
|
||||
HarborHost: "http://harbor.wdd.io",
|
||||
HarborPort: "8033",
|
||||
HarborUser: "",
|
||||
HarborPass: "",
|
||||
HarborClient: nil,
|
||||
}
|
||||
|
||||
_, err := harborOperator.BuildOperator()
|
||||
if err != nil {
|
||||
t.Logf("error is %s", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
DefaultHarborOperator = harborOperator
|
||||
|
||||
}
|
||||
|
||||
func TestHarborOperator_RepoListAll(t *testing.T) {
|
||||
|
||||
TestHarborOperator_BuildOperator(t)
|
||||
|
||||
repoListAll := DefaultHarborOperator.RepoListAll("cmii")
|
||||
|
||||
utils.BeautifulPrint(repoListAll)
|
||||
|
||||
}
|
||||
|
||||
func TestHarborOperator_RepoAllCmiiImage(t *testing.T) {
|
||||
|
||||
TestHarborOperator_BuildOperator(t)
|
||||
|
||||
repoListAll := DefaultHarborOperator.RepoAllCmiiImage()
|
||||
|
||||
utils.BeautifulPrint(repoListAll)
|
||||
}
|
||||
|
||||
func TestHarborOperator_ArtifactListAll(t *testing.T) {
|
||||
|
||||
TestHarborOperator_BuildOperator(t)
|
||||
|
||||
artifactListAll := DefaultHarborOperator.ArtifactListAll("cmii", "cmii-uav-user")
|
||||
|
||||
for _, artifact := range artifactListAll {
|
||||
for _, tag := range artifact.Tags {
|
||||
fmt.Println(tag.Name)
|
||||
}
|
||||
}
|
||||
utils.BeautifulPrint(artifactListAll)
|
||||
|
||||
}
|
||||
|
||||
func TestHarborOperator_ArtifactListOne(t *testing.T) {
|
||||
TestHarborOperator_BuildOperator(t)
|
||||
|
||||
//reference := "sha256:0048162a053eef4d4ce3fe7518615bef084403614f8bca43b40ae2e762e11e06" // not ok icon
|
||||
reference := "sha256:27bd0055156abc20c29863750f13bbcc14019126da36d3941cfd82eb104ec31a" // ok digest
|
||||
// reference := "5.2.0" // ok tag
|
||||
artifactListOne := DefaultHarborOperator.ArtifactListOne("cmii", "cmii-uav-user", reference)
|
||||
|
||||
utils.BeautifulPrint(artifactListOne)
|
||||
}
|
||||
|
||||
func TestHarborOperator_ArtifactDeleteOne(t *testing.T) {
|
||||
TestHarborOperator_BuildOperator(t)
|
||||
reference := "sha256:27bd0055156abc20c29863750f13bbcc14019126da36d3941cfd82eb104ec31a"
|
||||
ccc := DefaultHarborOperator.ArtifactDeleteOne("cmii", "cmii-uav-user", reference)
|
||||
if !ccc {
|
||||
log.Error("delete failed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHarborOperator_CmiiTagFilter(t *testing.T) {
|
||||
TestHarborOperator_BuildOperator(t)
|
||||
imageMap := DefaultHarborOperator.CmiiTagFilter("4")
|
||||
utils.BeautifulPrint(imageMap)
|
||||
}
|
||||
|
||||
func TestHarborOperator_ArtifactDeleteFromNameTagList(t *testing.T) {
|
||||
|
||||
TestHarborOperator_BuildOperator(t)
|
||||
allCmiiImageList := DefaultHarborOperator.CmiiTagFilter("5")
|
||||
errorDeleteList := DefaultHarborOperator.ArtifactDeleteFromNameTagList("cmii", allCmiiImageList)
|
||||
utils.BeautifulPrint(errorDeleteList)
|
||||
}
|
||||
Reference in New Issue
Block a user