fix bugs
This commit is contained in:
@@ -64,7 +64,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)
|
ok, createClient := hOp.CheckAndBuildHarborClient(funcArgs[0])
|
||||||
if !ok {
|
if !ok {
|
||||||
return false, []string{
|
return false, []string{
|
||||||
"[Harbor Create Project] - Error !",
|
"[Harbor Create Project] - Error !",
|
||||||
@@ -111,9 +111,8 @@ func (hOp *HarborOperator) CreateProjectExec(funcArgs []string) (bool, []string)
|
|||||||
return true, []string{successLog}
|
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)
|
log.InfoF("[Harbor Client Create] - start to create harbor client %s", targetHarborHost)
|
||||||
|
|
||||||
parseIP := net.ParseIP(targetHarborHost)
|
parseIP := net.ParseIP(targetHarborHost)
|
||||||
@@ -141,7 +140,7 @@ func (hOp *HarborOperator) CheckAndBuildHarborClient(funcArgs []string) (bool, *
|
|||||||
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)
|
ok, createClient := hOp.CheckAndBuildHarborClient(funcArgs[0])
|
||||||
if !ok {
|
if !ok {
|
||||||
return false, []string{
|
return false, []string{
|
||||||
"[Harbor Create Project ] - Error !",
|
"[Harbor Create Project ] - Error !",
|
||||||
@@ -173,9 +172,92 @@ 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 {
|
||||||
|
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
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hOp *HarborOperator) Command(baseFuncName string, funcArgs ...string) []string {
|
func (hOp *HarborOperator) Command(baseFuncName string, funcArgs ...string) []string {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,7 +78,9 @@ public class FuncServiceImpl implements FuncService {
|
|||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
return JudgeSyncBaseCommandResult(syncResultLog);
|
boolean harborResult = JudgeSyncBaseCommandResult(syncResultLog);
|
||||||
|
|
||||||
|
return harborResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean JudgeSyncBaseCommandResult(List<String> syncResultLog) {
|
private boolean JudgeSyncBaseCommandResult(List<String> syncResultLog) {
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -60,13 +62,12 @@ public class HarborFuncScheduler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 1 - sync harbor
|
// 1 - sync harbor
|
||||||
|
if (!SyncBetweenHarbor(projectDeployContext)) {
|
||||||
|
log.error("sync harbor project failed !");
|
||||||
|
}
|
||||||
|
|
||||||
// 1 - load image from tar.gz
|
// 1 - load image from tar.gz
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
log.info("Harbor Image Synchronized Succeed !");
|
log.info("Harbor Image Synchronized Succeed !");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,13 +116,13 @@ public class HarborFuncScheduler {
|
|||||||
listProjectArgList.add(masterNode.getServerIpInV4());
|
listProjectArgList.add(masterNode.getServerIpInV4());
|
||||||
|
|
||||||
// send harbor create message
|
// send harbor create message
|
||||||
boolean createProjectOK = funcService.callHarborFuncAndJudge(
|
boolean listProjectOK = funcService.callHarborFuncAndJudge(
|
||||||
masterNode.getTopicName(),
|
masterNode.getTopicName(),
|
||||||
HarborFunctionEnum.LIST_PROJECT,
|
HarborFunctionEnum.LIST_PROJECT,
|
||||||
listProjectArgList
|
listProjectArgList
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!createProjectOK) {
|
if (!listProjectOK) {
|
||||||
log.error(
|
log.error(
|
||||||
"[ListHarborProject] - List Harbor Project Failed !=> {}",
|
"[ListHarborProject] - List Harbor Project Failed !=> {}",
|
||||||
listProjectArgList
|
listProjectArgList
|
||||||
@@ -132,6 +133,69 @@ public class HarborFuncScheduler {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean SyncBetweenHarbor(ProjectDeployContext projectDeployContext) {
|
||||||
|
|
||||||
|
// use master node as harbor server
|
||||||
|
ServerInfoPO masterNode = projectDeployContext.getMasterNode();
|
||||||
|
|
||||||
|
ArrayList<String> syncHarborArgList = new ArrayList<>();
|
||||||
|
syncHarborArgList.add(HarborFunctionEnum.LIST_PROJECT.getOpName());
|
||||||
|
syncHarborArgList.add(masterNode.getServerIpInV4());
|
||||||
|
|
||||||
|
String sourceHarborHost = getIPv4Address();
|
||||||
|
syncHarborArgList.add(sourceHarborHost);
|
||||||
|
|
||||||
|
boolean syncHarborProjectOk = funcService.callHarborFuncAndJudge(
|
||||||
|
masterNode.getTopicName(),
|
||||||
|
HarborFunctionEnum.SYNC_PROJECT_BETWEEN_HARBOR,
|
||||||
|
syncHarborArgList
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!syncHarborProjectOk) {
|
||||||
|
log.error(
|
||||||
|
"[SYNC_PROJECT_BETWEEN_HARBOR] - Sync Harbor Project Failed !=> {}",
|
||||||
|
syncHarborArgList
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getIPv4Address() {
|
||||||
|
|
||||||
|
String os = System
|
||||||
|
.getProperty("os.name")
|
||||||
|
.toLowerCase();
|
||||||
|
String devHarborSourceHost = "10.250.0.100";
|
||||||
|
if (os.contains("linux")) {
|
||||||
|
try {
|
||||||
|
InetAddress localhost = InetAddress.getLocalHost();
|
||||||
|
|
||||||
|
if (localhost.getHostAddress() != null && !localhost
|
||||||
|
.getHostAddress()
|
||||||
|
.isEmpty()) {
|
||||||
|
return localhost.getHostAddress();
|
||||||
|
} else {
|
||||||
|
|
||||||
|
return devHarborSourceHost;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
log.info(
|
||||||
|
"getIPv4Address => dev env set ipv4 addr to => {}",
|
||||||
|
devHarborSourceHost
|
||||||
|
);
|
||||||
|
return devHarborSourceHost;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private void beforeRunProcedure(ProjectDeployContext projectDeployContext) {
|
private void beforeRunProcedure(ProjectDeployContext projectDeployContext) {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user