From bb6d4635a10c7988cc9f3b09e1c5744c7d5557c3 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Fri, 25 Oct 2024 22:11:55 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E5=AE=B9=E5=99=A8?= =?UTF-8?q?=E9=95=9C=E5=83=8F=E4=BB=93=E5=BA=93=E7=BC=96=E8=BE=91=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98=20(#6845)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs #6839 --- backend/app/service/image_repo.go | 64 ++++++++++++++++++------------- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/backend/app/service/image_repo.go b/backend/app/service/image_repo.go index c2540db99..b4247c2f9 100644 --- a/backend/app/service/image_repo.go +++ b/backend/app/service/image_repo.go @@ -84,8 +84,11 @@ func (u *ImageRepoService) Create(req dto.ImageRepoCreate) error { if imageRepo.ID != 0 { return constant.ErrRecordExist } + if req.Protocol == "http" { - _ = u.handleRegistries(req.DownloadUrl, "", "create") + if err := u.handleRegistries(req.DownloadUrl, "", "create"); err != nil { + return fmt.Errorf("create registry %s failed, err: %v", req.DownloadUrl, err) + } if err := validateDockerConfig(); err != nil { return err } @@ -114,23 +117,18 @@ func (u *ImageRepoService) Create(req dto.ImageRepoCreate) error { return err } } - - if err := copier.Copy(&imageRepo, &req); err != nil { - return errors.WithMessage(constant.ErrStructTransform, err.Error()) - } - - imageRepo.Status = constant.StatusSuccess if req.Auth { if err := u.CheckConn(req.DownloadUrl, req.Username, req.Password); err != nil { - imageRepo.Status = constant.StatusFailed - imageRepo.Message = err.Error() + return err } } - if err := imageRepoRepo.Create(&imageRepo); err != nil { - return err + + if err := copier.Copy(&imageRepo, &req); err != nil { + return errors.WithMessage(constant.ErrStructTransform, err.Error()) } - return nil + imageRepo.Status = constant.StatusSuccess + return imageRepoRepo.Create(&imageRepo) } func (u *ImageRepoService) BatchDelete(req dto.ImageRepoDelete) error { @@ -156,33 +154,47 @@ func (u *ImageRepoService) Update(req dto.ImageRepoUpdate) error { if err != nil { return err } - if repo.DownloadUrl != req.DownloadUrl || (!repo.Auth && req.Auth) { - _ = u.handleRegistries(req.DownloadUrl, repo.DownloadUrl, "update") + if repo.Protocol == "http" && req.Protocol == "https" { + if err := u.handleRegistries("", repo.DownloadUrl, "delete"); err != nil { + return fmt.Errorf("delete registry %s failed, err: %v", repo.DownloadUrl, err) + } + } + if repo.Protocol == "http" && req.Protocol == "http" { + if err := u.handleRegistries(req.DownloadUrl, repo.DownloadUrl, "update"); err != nil { + return fmt.Errorf("update registry %s => %s failed, err: %v", repo.DownloadUrl, req.DownloadUrl, err) + } + } + if repo.Protocol == "https" && req.Protocol == "http" { + if err := u.handleRegistries(req.DownloadUrl, "", "create"); err != nil { + return fmt.Errorf("create registry %s failed, err: %v", req.DownloadUrl, err) + } + } + if repo.Auth != req.Auth || repo.DownloadUrl != req.DownloadUrl { if repo.Auth { _, _ = cmd.ExecWithCheck("docker", "logout", repo.DownloadUrl) } - if err := validateDockerConfig(); err != nil { - return err - } - if err := restartDocker(); err != nil { - return err + if req.Auth { + if err := u.CheckConn(req.DownloadUrl, req.Username, req.Password); err != nil { + return err + } } } + + if err := validateDockerConfig(); err != nil { + return err + } + if err := restartDocker(); err != nil { + return err + } + upMap := make(map[string]interface{}) upMap["download_url"] = req.DownloadUrl upMap["protocol"] = req.Protocol upMap["username"] = req.Username upMap["password"] = req.Password upMap["auth"] = req.Auth - upMap["status"] = constant.StatusSuccess upMap["message"] = "" - if req.Auth { - if err := u.CheckConn(req.DownloadUrl, req.Username, req.Password); err != nil { - upMap["status"] = constant.StatusFailed - upMap["message"] = err.Error() - } - } return imageRepoRepo.Update(req.ID, upMap) }