[ Server ] [ Harbor ] - sync projects - 4
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/mittwald/goharbor-client/v5/apiv2"
|
"github.com/mittwald/goharbor-client/v5/apiv2"
|
||||||
"github.com/mittwald/goharbor-client/v5/apiv2/model"
|
"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) {
|
func (hOp *HarborOperator) CreateProjectExec(funcArgs []string) (bool, []string) {
|
||||||
|
|
||||||
if hOp.TargetHarborClient == nil {
|
if hOp.TargetHarborClient == nil {
|
||||||
ok, createClient := hOp.CheckAndBuildHarborClient(funcArgs[0])
|
ok, createClient := hOp.CheckAndBuildHarborClient(funcArgs[0], true)
|
||||||
if !ok {
|
if !ok {
|
||||||
return false, []string{
|
return false, []string{
|
||||||
"[Harbor Create Project] - Error !",
|
"[Harbor Create Project] - Error !",
|
||||||
@@ -111,7 +112,7 @@ func (hOp *HarborOperator) CreateProjectExec(funcArgs []string) (bool, []string)
|
|||||||
return true, []string{successLog}
|
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)
|
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
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
hOp.TargetHarborHost = "http://" + targetHarborHost + ":" + hOp.HarborPort + "/api/"
|
if isTarget {
|
||||||
log.DebugF("[Harbor Client Create] - harbor host is => %s", hOp.TargetHarborHost)
|
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
|
// 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 {
|
if err != nil {
|
||||||
errorLog := fmt.Sprintf("Error creating REST client: %s\n", err.Error())
|
errorLog := fmt.Sprintf("Error creating REST client: %s\n", err.Error())
|
||||||
log.Error(errorLog)
|
log.Error(errorLog)
|
||||||
@@ -140,7 +152,7 @@ func (hOp *HarborOperator) CheckAndBuildHarborClient(targetHarborHost string) (b
|
|||||||
func (hOp *HarborOperator) ListProjectExec(funcArgs []string) (bool, []string) {
|
func (hOp *HarborOperator) ListProjectExec(funcArgs []string) (bool, []string) {
|
||||||
|
|
||||||
if hOp.TargetHarborClient == nil {
|
if hOp.TargetHarborClient == nil {
|
||||||
ok, createClient := hOp.CheckAndBuildHarborClient(funcArgs[0])
|
ok, createClient := hOp.CheckAndBuildHarborClient(funcArgs[0], true)
|
||||||
if !ok {
|
if !ok {
|
||||||
return false, []string{
|
return false, []string{
|
||||||
"[Harbor Create Project ] - Error !",
|
"[Harbor Create Project ] - Error !",
|
||||||
@@ -174,7 +186,7 @@ func (hOp *HarborOperator) ListProjectExec(funcArgs []string) (bool, []string) {
|
|||||||
func (hOp *HarborOperator) SyncProjectExec(funcArgs []string) (bool, []string) {
|
func (hOp *HarborOperator) SyncProjectExec(funcArgs []string) (bool, []string) {
|
||||||
|
|
||||||
if hOp.TargetHarborClient == nil {
|
if hOp.TargetHarborClient == nil {
|
||||||
ok, createClient := hOp.CheckAndBuildHarborClient(funcArgs[0])
|
ok, createClient := hOp.CheckAndBuildHarborClient(funcArgs[0], true)
|
||||||
if !ok {
|
if !ok {
|
||||||
return false, []string{
|
return false, []string{
|
||||||
"[Harbor Sync Project ] - Error !",
|
"[Harbor Sync Project ] - Error !",
|
||||||
@@ -185,7 +197,7 @@ func (hOp *HarborOperator) SyncProjectExec(funcArgs []string) (bool, []string) {
|
|||||||
targetClient := hOp.TargetHarborClient
|
targetClient := hOp.TargetHarborClient
|
||||||
|
|
||||||
if hOp.SourceHarborClient == nil {
|
if hOp.SourceHarborClient == nil {
|
||||||
ok, createClient := hOp.CheckAndBuildHarborClient(funcArgs[1])
|
ok, createClient := hOp.CheckAndBuildHarborClient(funcArgs[1], false)
|
||||||
if !ok {
|
if !ok {
|
||||||
return false, []string{
|
return false, []string{
|
||||||
"[Harbor Sync Project ] - Error !",
|
"[Harbor Sync Project ] - Error !",
|
||||||
@@ -193,7 +205,6 @@ func (hOp *HarborOperator) SyncProjectExec(funcArgs []string) (bool, []string) {
|
|||||||
}
|
}
|
||||||
hOp.SourceHarborClient = createClient
|
hOp.SourceHarborClient = createClient
|
||||||
}
|
}
|
||||||
sourceClient := hOp.SourceHarborClient
|
|
||||||
|
|
||||||
log.DebugF("[Harbor Sync Project ] - start to check projects all exists!")
|
log.DebugF("[Harbor Sync Project ] - start to check projects all exists!")
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
@@ -209,13 +220,40 @@ func (hOp *HarborOperator) SyncProjectExec(funcArgs []string) (bool, []string) {
|
|||||||
if !exists {
|
if !exists {
|
||||||
return false, append(syncNotExistHarborProjectError, "targetClient")
|
return false, append(syncNotExistHarborProjectError, "targetClient")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
projectExists, _ := sourceClient.ProjectExists(ctx, projectName)
|
// add source registry to destination harbor
|
||||||
if !projectExists {
|
log.InfoF("[Harbor Sync Project ] - start to create source harbor endpoints => %s", hOp.SourceHarborHost)
|
||||||
return false, append(syncNotExistHarborProjectError, "sourceClient")
|
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{
|
newPolicy := &model.ReplicationPolicy{
|
||||||
CopyByChunk: nil,
|
CopyByChunk: nil,
|
||||||
@@ -223,44 +261,23 @@ func (hOp *HarborOperator) SyncProjectExec(funcArgs []string) (bool, []string) {
|
|||||||
Description: "",
|
Description: "",
|
||||||
DestNamespace: "", // 可以指定目标 Harbor 中的特定项目,如果为空,则使用源项目名称
|
DestNamespace: "", // 可以指定目标 Harbor 中的特定项目,如果为空,则使用源项目名称
|
||||||
DestNamespaceReplaceCount: nil,
|
DestNamespaceReplaceCount: nil,
|
||||||
DestRegistry: &model.Registry{
|
SrcRegistry: octopusSourceRegistry,
|
||||||
Credential: &model.RegistryCredential{
|
Enabled: true,
|
||||||
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,
|
|
||||||
Filters: []*model.ReplicationFilter{
|
Filters: []*model.ReplicationFilter{
|
||||||
{
|
{
|
||||||
Type: "name",
|
Type: "name",
|
||||||
Value: "cmii/**", // 根据需要同步的仓库进行调整
|
Value: "cmii/**", // 根据需要同步的仓库进行调整
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Type: "name",
|
||||||
|
Value: "rancher/**", // 根据需要同步的仓库进行调整
|
||||||
|
},
|
||||||
},
|
},
|
||||||
ID: 0,
|
ID: 0,
|
||||||
Name: "sync-repositories-to-target",
|
Name: "octopus-sync-replication",
|
||||||
Override: true,
|
Override: true,
|
||||||
ReplicateDeletion: false,
|
ReplicateDeletion: false,
|
||||||
Speed: nil,
|
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{
|
Trigger: &model.ReplicationTrigger{
|
||||||
Type: "manual", // 可以是 "manual", "scheduled", 或 "event_based"
|
Type: "manual", // 可以是 "manual", "scheduled", 或 "event_based"
|
||||||
// 如果是 "scheduled",还需要设置 Cron 表达式
|
// 如果是 "scheduled",还需要设置 Cron 表达式
|
||||||
@@ -270,8 +287,7 @@ func (hOp *HarborOperator) SyncProjectExec(funcArgs []string) (bool, []string) {
|
|||||||
|
|
||||||
// 在源 Harbor 中创建复制策略
|
// 在源 Harbor 中创建复制策略
|
||||||
log.Info("[Harbor Sync Project ] - Start To Sync Project !")
|
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 {
|
if err != nil {
|
||||||
syncErrorMessage := fmt.Sprintf("[Harbor Sync Project ] - Sync Project Failed !: %s\n", err.Error())
|
syncErrorMessage := fmt.Sprintf("[Harbor Sync Project ] - Sync Project Failed !: %s\n", err.Error())
|
||||||
log.Error(syncErrorMessage)
|
log.Error(syncErrorMessage)
|
||||||
|
|||||||
@@ -34,10 +34,10 @@ public class TestBaseFuncScheduler {
|
|||||||
|
|
||||||
ServerQueryEntity serverQueryEntity = new ServerQueryEntity();
|
ServerQueryEntity serverQueryEntity = new ServerQueryEntity();
|
||||||
// exsi server
|
// exsi server
|
||||||
serverQueryEntity.setServerName("Chengdu-amd64-99");
|
// serverQueryEntity.setServerName("Chengdu-amd64-99");
|
||||||
|
|
||||||
// lappro
|
// lappro
|
||||||
// serverQueryEntity.setServerName("Chengdu-amd64-65");
|
serverQueryEntity.setServerName("Chengdu-amd64-65");
|
||||||
|
|
||||||
ServerInfoPO serverInfoPO = serverService
|
ServerInfoPO serverInfoPO = serverService
|
||||||
.serverGetByPage(serverQueryEntity)
|
.serverGetByPage(serverQueryEntity)
|
||||||
|
|||||||
Reference in New Issue
Block a user