[ Server ] [ Harbor ] - sync projects - 4
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/mittwald/goharbor-client/v5/apiv2"
|
||||
"github.com/mittwald/goharbor-client/v5/apiv2/model"
|
||||
@@ -64,7 +65,7 @@ func (hOp *HarborOperator) Exec(baseFuncName string, funcArgs ...string) (bool,
|
||||
func (hOp *HarborOperator) CreateProjectExec(funcArgs []string) (bool, []string) {
|
||||
|
||||
if hOp.TargetHarborClient == nil {
|
||||
ok, createClient := hOp.CheckAndBuildHarborClient(funcArgs[0])
|
||||
ok, createClient := hOp.CheckAndBuildHarborClient(funcArgs[0], true)
|
||||
if !ok {
|
||||
return false, []string{
|
||||
"[Harbor Create Project] - Error !",
|
||||
@@ -111,7 +112,7 @@ func (hOp *HarborOperator) CreateProjectExec(funcArgs []string) (bool, []string)
|
||||
return true, []string{successLog}
|
||||
}
|
||||
|
||||
func (hOp *HarborOperator) CheckAndBuildHarborClient(targetHarborHost string) (bool, *apiv2.RESTClient) {
|
||||
func (hOp *HarborOperator) CheckAndBuildHarborClient(targetHarborHost string, isTarget bool) (bool, *apiv2.RESTClient) {
|
||||
|
||||
log.InfoF("[Harbor Client Create] - start to create harbor client %s", targetHarborHost)
|
||||
|
||||
@@ -123,11 +124,22 @@ func (hOp *HarborOperator) CheckAndBuildHarborClient(targetHarborHost string) (b
|
||||
return false, nil
|
||||
}
|
||||
|
||||
hOp.TargetHarborHost = "http://" + targetHarborHost + ":" + hOp.HarborPort + "/api/"
|
||||
log.DebugF("[Harbor Client Create] - harbor host is => %s", hOp.TargetHarborHost)
|
||||
if isTarget {
|
||||
hOp.TargetHarborHost = "http://" + targetHarborHost + ":" + hOp.HarborPort + "/api/"
|
||||
log.DebugF("[Harbor Client Create] - harbor host is => %s", hOp.TargetHarborHost)
|
||||
} else {
|
||||
hOp.SourceHarborHost = "http://" + targetHarborHost + ":" + hOp.HarborPort + "/api/"
|
||||
log.DebugF("[Harbor Client Create] - harbor host is => %s", hOp.SourceHarborHost)
|
||||
}
|
||||
|
||||
// check connection
|
||||
client, err := apiv2.NewRESTClientForHost(hOp.TargetHarborHost, hOp.HarborAdminUser, hOp.HarborAdminPass, nil)
|
||||
var client *apiv2.RESTClient
|
||||
var err error
|
||||
if isTarget {
|
||||
client, err = apiv2.NewRESTClientForHost(hOp.TargetHarborHost, hOp.HarborAdminUser, hOp.HarborAdminPass, nil)
|
||||
} else {
|
||||
client, err = apiv2.NewRESTClientForHost(hOp.SourceHarborHost, hOp.HarborAdminUser, hOp.HarborAdminPass, nil)
|
||||
}
|
||||
if err != nil {
|
||||
errorLog := fmt.Sprintf("Error creating REST client: %s\n", err.Error())
|
||||
log.Error(errorLog)
|
||||
@@ -140,7 +152,7 @@ func (hOp *HarborOperator) CheckAndBuildHarborClient(targetHarborHost string) (b
|
||||
func (hOp *HarborOperator) ListProjectExec(funcArgs []string) (bool, []string) {
|
||||
|
||||
if hOp.TargetHarborClient == nil {
|
||||
ok, createClient := hOp.CheckAndBuildHarborClient(funcArgs[0])
|
||||
ok, createClient := hOp.CheckAndBuildHarborClient(funcArgs[0], true)
|
||||
if !ok {
|
||||
return false, []string{
|
||||
"[Harbor Create Project ] - Error !",
|
||||
@@ -174,7 +186,7 @@ func (hOp *HarborOperator) ListProjectExec(funcArgs []string) (bool, []string) {
|
||||
func (hOp *HarborOperator) SyncProjectExec(funcArgs []string) (bool, []string) {
|
||||
|
||||
if hOp.TargetHarborClient == nil {
|
||||
ok, createClient := hOp.CheckAndBuildHarborClient(funcArgs[0])
|
||||
ok, createClient := hOp.CheckAndBuildHarborClient(funcArgs[0], true)
|
||||
if !ok {
|
||||
return false, []string{
|
||||
"[Harbor Sync Project ] - Error !",
|
||||
@@ -185,7 +197,7 @@ func (hOp *HarborOperator) SyncProjectExec(funcArgs []string) (bool, []string) {
|
||||
targetClient := hOp.TargetHarborClient
|
||||
|
||||
if hOp.SourceHarborClient == nil {
|
||||
ok, createClient := hOp.CheckAndBuildHarborClient(funcArgs[1])
|
||||
ok, createClient := hOp.CheckAndBuildHarborClient(funcArgs[1], false)
|
||||
if !ok {
|
||||
return false, []string{
|
||||
"[Harbor Sync Project ] - Error !",
|
||||
@@ -193,7 +205,6 @@ func (hOp *HarborOperator) SyncProjectExec(funcArgs []string) (bool, []string) {
|
||||
}
|
||||
hOp.SourceHarborClient = createClient
|
||||
}
|
||||
sourceClient := hOp.SourceHarborClient
|
||||
|
||||
log.DebugF("[Harbor Sync Project ] - start to check projects all exists!")
|
||||
ctx := context.Background()
|
||||
@@ -209,13 +220,40 @@ func (hOp *HarborOperator) SyncProjectExec(funcArgs []string) (bool, []string) {
|
||||
if !exists {
|
||||
return false, append(syncNotExistHarborProjectError, "targetClient")
|
||||
}
|
||||
}
|
||||
|
||||
projectExists, _ := sourceClient.ProjectExists(ctx, projectName)
|
||||
if !projectExists {
|
||||
return false, append(syncNotExistHarborProjectError, "sourceClient")
|
||||
// add source registry to destination harbor
|
||||
log.InfoF("[Harbor Sync Project ] - start to create source harbor endpoints => %s", hOp.SourceHarborHost)
|
||||
OctopusSourceHarborName := "octopus-source"
|
||||
exitRegistry, _ := targetClient.GetRegistryByName(ctx, OctopusSourceHarborName)
|
||||
if exitRegistry != nil {
|
||||
log.Debug("[Harbor Sync Project ] - source endpoints already exists ! delete it !")
|
||||
err := targetClient.DeleteRegistryByID(ctx, exitRegistry.ID)
|
||||
if err != nil {
|
||||
return false, []string{
|
||||
"[Harbor Sync Project ] - source endpoints delete failed !",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
octopusSourceRegistry := &model.Registry{
|
||||
Credential: &model.RegistryCredential{
|
||||
AccessKey: "admin",
|
||||
AccessSecret: "V2ryStr@ngPss",
|
||||
Type: "basic",
|
||||
},
|
||||
Insecure: true,
|
||||
ID: 7,
|
||||
Name: OctopusSourceHarborName, // 源 Harbor 实例的注册表 ID,通常为 0
|
||||
Type: "harbor",
|
||||
URL: strings.Split(hOp.SourceHarborHost, "/api")[0],
|
||||
}
|
||||
err := targetClient.NewRegistry(ctx, octopusSourceRegistry)
|
||||
if err != nil {
|
||||
return false, []string{
|
||||
"[Harbor Sync Project ] - source endpoints create failed !",
|
||||
}
|
||||
}
|
||||
// 创建复制策略
|
||||
newPolicy := &model.ReplicationPolicy{
|
||||
CopyByChunk: nil,
|
||||
@@ -223,44 +261,23 @@ func (hOp *HarborOperator) SyncProjectExec(funcArgs []string) (bool, []string) {
|
||||
Description: "",
|
||||
DestNamespace: "", // 可以指定目标 Harbor 中的特定项目,如果为空,则使用源项目名称
|
||||
DestNamespaceReplaceCount: nil,
|
||||
DestRegistry: &model.Registry{
|
||||
Credential: &model.RegistryCredential{
|
||||
AccessKey: "admin",
|
||||
AccessSecret: "V2ryStr@ngPss",
|
||||
Type: "basic",
|
||||
},
|
||||
Description: "",
|
||||
ID: 4,
|
||||
Insecure: true,
|
||||
Name: "cmii", // 源 Harbor 实例的注册表 ID,通常为 0
|
||||
Type: "harbor",
|
||||
URL: "http://10.250.0.126:8033",
|
||||
},
|
||||
Enabled: true,
|
||||
SrcRegistry: octopusSourceRegistry,
|
||||
Enabled: true,
|
||||
Filters: []*model.ReplicationFilter{
|
||||
{
|
||||
Type: "name",
|
||||
Value: "cmii/**", // 根据需要同步的仓库进行调整
|
||||
},
|
||||
{
|
||||
Type: "name",
|
||||
Value: "rancher/**", // 根据需要同步的仓库进行调整
|
||||
},
|
||||
},
|
||||
ID: 0,
|
||||
Name: "sync-repositories-to-target",
|
||||
Name: "octopus-sync-replication",
|
||||
Override: true,
|
||||
ReplicateDeletion: false,
|
||||
Speed: nil,
|
||||
SrcRegistry: &model.Registry{
|
||||
Credential: &model.RegistryCredential{
|
||||
AccessKey: "admin",
|
||||
AccessSecret: "V2ryStr@ngPss",
|
||||
Type: "basic",
|
||||
},
|
||||
Description: "",
|
||||
ID: 4,
|
||||
Insecure: true,
|
||||
Name: "cmii", // 源 Harbor 实例的注册表 ID,通常为 0
|
||||
Type: "harbor",
|
||||
URL: "http://10.250.0.100:8033",
|
||||
},
|
||||
Trigger: &model.ReplicationTrigger{
|
||||
Type: "manual", // 可以是 "manual", "scheduled", 或 "event_based"
|
||||
// 如果是 "scheduled",还需要设置 Cron 表达式
|
||||
@@ -270,8 +287,7 @@ func (hOp *HarborOperator) SyncProjectExec(funcArgs []string) (bool, []string) {
|
||||
|
||||
// 在源 Harbor 中创建复制策略
|
||||
log.Info("[Harbor Sync Project ] - Start To Sync Project !")
|
||||
err := sourceClient.NewReplicationPolicy(ctx, newPolicy.DestRegistry, newPolicy.SrcRegistry, newPolicy.Deletion, newPolicy.Override, newPolicy.Enabled, newPolicy.Filters, newPolicy.Trigger, newPolicy.DestNamespace, newPolicy.Name, newPolicy.Name)
|
||||
|
||||
err = targetClient.NewReplicationPolicy(ctx, newPolicy.DestRegistry, newPolicy.SrcRegistry, newPolicy.Deletion, newPolicy.Override, newPolicy.Enabled, newPolicy.Filters, newPolicy.Trigger, newPolicy.DestNamespace, newPolicy.Name, newPolicy.Name)
|
||||
if err != nil {
|
||||
syncErrorMessage := fmt.Sprintf("[Harbor Sync Project ] - Sync Project Failed !: %s\n", err.Error())
|
||||
log.Error(syncErrorMessage)
|
||||
|
||||
Reference in New Issue
Block a user