This commit is contained in:
zeaslity
2023-11-14 16:04:41 +08:00
3 changed files with 160 additions and 12 deletions

View File

@@ -64,7 +64,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)
ok, createClient := hOp.CheckAndBuildHarborClient(funcArgs[0])
if !ok {
return false, []string{
"[Harbor Create Project] - Error !",
@@ -111,9 +111,8 @@ func (hOp *HarborOperator) CreateProjectExec(funcArgs []string) (bool, []string)
return true, []string{successLog}
}
func (hOp *HarborOperator) CheckAndBuildHarborClient(funcArgs []string) (bool, *apiv2.RESTClient) {
func (hOp *HarborOperator) CheckAndBuildHarborClient(targetHarborHost string) (bool, *apiv2.RESTClient) {
targetHarborHost := funcArgs[0]
log.InfoF("[Harbor Client Create] - start to create harbor client %s", targetHarborHost)
parseIP := net.ParseIP(targetHarborHost)
@@ -141,7 +140,7 @@ func (hOp *HarborOperator) CheckAndBuildHarborClient(funcArgs []string) (bool, *
func (hOp *HarborOperator) ListProjectExec(funcArgs []string) (bool, []string) {
if hOp.TargetHarborClient == nil {
ok, createClient := hOp.CheckAndBuildHarborClient(funcArgs)
ok, createClient := hOp.CheckAndBuildHarborClient(funcArgs[0])
if !ok {
return false, []string{
"[Harbor Create Project ] - Error !",
@@ -173,9 +172,92 @@ 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])
if !ok {
return false, []string{
"[Harbor Sync Project ] - Error !",
}
}
hOp.TargetHarborClient = createClient
}
targetClient := hOp.TargetHarborClient
if hOp.SourceHarborClient == nil {
ok, createClient := hOp.CheckAndBuildHarborClient(funcArgs[1])
if !ok {
return false, []string{
"[Harbor Sync Project ] - Error !",
}
}
hOp.SourceHarborClient = createClient
}
sourceClient := hOp.SourceHarborClient
ctx := context.Background()
// check both source and target harbor project exists
needToCreateProjectNameList := []string{"cmii", "rancher"}
for _, projectName := range needToCreateProjectNameList {
syncNotExistHarborProjectError := []string{
"[Harbor Sync Project ] - project not exists !",
}
exists, _ := targetClient.ProjectExists(ctx, projectName)
if !exists {
return false, append(syncNotExistHarborProjectError, "targetClient")
}
projectExists, _ := sourceClient.ProjectExists(ctx, projectName)
if !projectExists {
return false, append(syncNotExistHarborProjectError, "sourceClient")
}
}
// 创建复制策略
newPolicy := &model.ReplicationPolicy{
Name: "sync-repositories-to-target",
Enabled: true,
Deletion: false,
Override: true,
SrcRegistry: &model.Registry{
ID: 0, // 源 Harbor 实例的注册表 ID通常为 0
},
DestRegistry: &model.Registry{
ID: 1, // 目标 Harbor 实例的注册表 ID需要根据实际情况设置
},
DestNamespace: "", // 可以指定目标 Harbor 中的特定项目,如果为空,则使用源项目名称
Trigger: &model.ReplicationTrigger{
Type: "manual", // 可以是 "manual", "scheduled", 或 "event_based"
// 如果是 "scheduled",还需要设置 Cron 表达式
// TriggerSettings: &model.TriggerSettings{Cron: "0 * * * *"},
},
Filters: []*model.ReplicationFilter{
{
Type: "name",
Value: "cmii/**", // 根据需要同步的仓库进行调整
},
{
Type: "name",
Value: "rancher/**", // 根据需要同步的仓库进行调整
},
},
}
// 在源 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)
if err != nil {
fmt.Printf("Error creating replication policy: %v\n", err)
return false, []string{
"[Harbor Sync Project ] - Sync Project Failed !",
}
}
return true, nil
}
func (hOp *HarborOperator) Command(baseFuncName string, funcArgs ...string) []string {
return nil
}