[ Server ] [ Harbor ] - sync status - 1

This commit is contained in:
zeaslity
2023-11-16 14:33:26 +08:00
parent 96a871d371
commit d9572ea8ce
4 changed files with 134 additions and 10 deletions

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"net"
"os"
"strconv"
"strings"
"github.com/mittwald/goharbor-client/v5/apiv2"
@@ -39,6 +40,7 @@ func NewHarborOperator() *HarborOperator {
// HarborOperatorCache 饿汉式单例
var HarborOperatorCache = NewHarborOperator()
var OctopusReplicationPolicyName = "octopus-sync-replication"
func (hOp *HarborOperator) Exec(baseFuncName string, funcArgs ...string) (bool, []string) {
// 参见 HarborFunctionEnum
@@ -56,7 +58,9 @@ func (hOp *HarborOperator) Exec(baseFuncName string, funcArgs ...string) (bool,
case "SYNC_PROJECT_BETWEEN_HARBOR":
resultOk, resultLog = hOp.SyncProjectExec(funcArgs)
break
case "SYNC_STATUS_HARBOR":
resultOk, resultLog = hOp.SyncStatusExec(funcArgs)
break
}
return resultOk, resultLog
@@ -221,7 +225,7 @@ func (hOp *HarborOperator) SyncProjectExec(funcArgs []string) (bool, []string) {
return false, append(syncNotExistHarborProjectError, "targetClient")
}
}
octopusReplicationPolicyName := "octopus-sync-replication"
OctopusSourceHarborName := "octopus-source"
// add source registry to destination harbor
@@ -231,7 +235,7 @@ func (hOp *HarborOperator) SyncProjectExec(funcArgs []string) (bool, []string) {
policies, _ := targetClient.ListReplicationPolicies(ctx)
if policies != nil {
for _, policy := range policies {
if policy.Name == octopusReplicationPolicyName {
if policy.Name == OctopusReplicationPolicyName {
err := targetClient.DeleteReplicationPolicyByID(ctx, policy.ID)
if err != nil {
log.ErrorF("[Harbor Sync Project ] - delete exists replication policy failed ! => %v ", policy)
@@ -296,9 +300,13 @@ func (hOp *HarborOperator) SyncProjectExec(funcArgs []string) (bool, []string) {
Type: "name",
Value: "cmii/**", // 根据需要同步的仓库进行调整
},
{
Type: "name",
Value: "rancher/**", // 根据需要同步的仓库进行调整
},
},
ID: 0,
Name: octopusReplicationPolicyName,
Name: OctopusReplicationPolicyName,
Override: true,
ReplicateDeletion: false,
Speed: nil,
@@ -322,7 +330,7 @@ func (hOp *HarborOperator) SyncProjectExec(funcArgs []string) (bool, []string) {
}
}
realOctopusReplicationPolicy, err := targetClient.GetReplicationPolicyByName(ctx, octopusReplicationPolicyName)
realOctopusReplicationPolicy, err := targetClient.GetReplicationPolicyByName(ctx, OctopusReplicationPolicyName)
if err != nil {
return false, []string{
"[Harbor Sync Project ] - failed to get the realOctopusReplicationPolicy!",
@@ -335,13 +343,65 @@ func (hOp *HarborOperator) SyncProjectExec(funcArgs []string) (bool, []string) {
})
if err != nil {
return false, []string{
"[Harbor Sync Project ] - failed to start the harbor sync execution !",
"[ Harbor Sync Project ] - failed to start the harbor sync execution !",
err.Error(),
}
}
return true, nil
return true, []string{
"[ Harbor Sync Project ] - sync started !",
}
}
func (hOp *HarborOperator) SyncStatusExec(funcArgs []string) (bool, []string) {
if hOp.TargetHarborClient == nil {
ok, createClient := hOp.CheckAndBuildHarborClient(funcArgs[0], true)
if !ok {
return false, []string{
"[ Sync Status ] - Error !",
}
}
hOp.TargetHarborClient = createClient
}
targetClient := hOp.TargetHarborClient
ctx := context.Background()
// check replication policy exists
replicationPolicy, err := targetClient.GetReplicationPolicyByName(ctx, OctopusReplicationPolicyName)
if err != nil {
return false, []string{
"[ Sync Status ] - get replication error !",
err.Error(),
}
}
// list execution status
replicationExecutions, err := targetClient.ListReplicationExecutions(ctx, &replicationPolicy.ID, nil, nil)
if err != nil {
return false, []string{
"[ Sync Status ] - replication has no sync work !",
err.Error(),
}
}
// find the newest one only have one here
for _, execution := range replicationExecutions {
if !strings.HasPrefix(execution.Status, "Succeed") {
log.InfoF("[sync status]- status are => %v", execution)
// report status
return false, []string{
fmt.Sprintf("[sync status] - not complete ! progress is => %s %", strconv.FormatInt(execution.Succeed/execution.Total, 10)),
}
}
}
return true, []string{
"[sync status]- sync completed !",
}
}
func (hOp *HarborOperator) Command(baseFuncName string, funcArgs ...string) []string {
return nil
}