From cd9f79d1413a22504ffeee876dd155aec3b02ab7 Mon Sep 17 00:00:00 2001 From: pirosiki197 Date: Mon, 2 Sep 2024 19:23:26 +0900 Subject: [PATCH 1/2] fix: delete runtime images before deleting application --- cmd/wire_gen.go | 3 +- pkg/domain/repository.go | 1 + .../repository/runtime_image.go | 23 +++++++ pkg/usecase/apiserver/app_service.go | 8 ++- pkg/usecase/apiserver/service.go | 66 ++++++++++--------- 5 files changed, 68 insertions(+), 33 deletions(-) diff --git a/cmd/wire_gen.go b/cmd/wire_gen.go index 9f8098cd..3a3ef9ed 100644 --- a/cmd/wire_gen.go +++ b/cmd/wire_gen.go @@ -283,6 +283,7 @@ func NewGateway(c Config) (component, error) { return nil, err } artifactRepository := repository.NewArtifactRepository(db) + runtimeImageRepository := repository.NewRuntimeImageRepository(db) applicationRepository := repository.NewApplicationRepository(db) buildRepository := repository.NewBuildRepository(db) environmentRepository := repository.NewEnvironmentRepository(db) @@ -325,7 +326,7 @@ func NewGateway(c Config) (component, error) { if err != nil { return nil, err } - service, err := apiserver.NewService(artifactRepository, applicationRepository, buildRepository, environmentRepository, gitRepositoryRepository, repositoryCommitRepository, userRepository, storage, mariaDBManager, mongoDBManager, metricsService, containerLogger, controllerServiceClient, imageConfig, publicKeys) + service, err := apiserver.NewService(artifactRepository, runtimeImageRepository, applicationRepository, buildRepository, environmentRepository, gitRepositoryRepository, repositoryCommitRepository, userRepository, storage, mariaDBManager, mongoDBManager, metricsService, containerLogger, controllerServiceClient, imageConfig, publicKeys) if err != nil { return nil, err } diff --git a/pkg/domain/repository.go b/pkg/domain/repository.go index 814538a2..ac33094c 100644 --- a/pkg/domain/repository.go +++ b/pkg/domain/repository.go @@ -102,6 +102,7 @@ type ArtifactRepository interface { type RuntimeImageRepository interface { CreateRuntimeImage(ctx context.Context, image *RuntimeImage) error + HardDeleteRuntimeImages(ctx context.Context, appId string) error } type GetBuildCondition struct { diff --git a/pkg/infrastructure/repository/runtime_image.go b/pkg/infrastructure/repository/runtime_image.go index 2b3f8b34..433d8b60 100644 --- a/pkg/infrastructure/repository/runtime_image.go +++ b/pkg/infrastructure/repository/runtime_image.go @@ -3,11 +3,14 @@ package repository import ( "context" "database/sql" + "fmt" "github.com/friendsofgo/errors" "github.com/traPtitech/neoshowcase/pkg/domain" + "github.com/traPtitech/neoshowcase/pkg/infrastructure/repository/models" "github.com/traPtitech/neoshowcase/pkg/infrastructure/repository/repoconvert" "github.com/volatiletech/sqlboiler/v4/boil" + "github.com/volatiletech/sqlboiler/v4/queries/qm" ) type runtimeImageRepository struct { @@ -28,3 +31,23 @@ func (r *runtimeImageRepository) CreateRuntimeImage(ctx context.Context, image * } return nil } + +func (r *runtimeImageRepository) HardDeleteRuntimeImages(ctx context.Context, appID string) error { + images, err := models.RuntimeImages( + qm.InnerJoin(fmt.Sprintf( + "%s ON %s = %s", + models.TableNames.Builds, + models.BuildTableColumns.ID, + models.RuntimeImageTableColumns.BuildID, + )), + models.BuildWhere.ApplicationID.EQ(appID), + ).All(ctx, r.db) + if err != nil { + return errors.Wrap(err, "failed to get runtime images") + } + _, err = images.DeleteAll(ctx, r.db) + if err != nil { + return errors.Wrap(err, "failed to delete runtime images") + } + return nil +} diff --git a/pkg/usecase/apiserver/app_service.go b/pkg/usecase/apiserver/app_service.go index 066452bd..95a93b1a 100644 --- a/pkg/usecase/apiserver/app_service.go +++ b/pkg/usecase/apiserver/app_service.go @@ -3,9 +3,10 @@ package apiserver import ( "context" "fmt" - "github.com/regclient/regclient/types/ref" "strconv" + "github.com/regclient/regclient/types/ref" + "github.com/traPtitech/neoshowcase/pkg/util/regutil" "github.com/friendsofgo/errors" @@ -383,6 +384,11 @@ func (s *Service) DeleteApplication(ctx context.Context, id string) error { if err != nil { return err } + // delete runtime images + err = s.runtimeImageRepo.HardDeleteRuntimeImages(ctx, app.ID) + if err != nil { + return err + } // delete builds builds, err := s.buildRepo.GetBuilds(ctx, domain.GetBuildCondition{ApplicationID: optional.From(app.ID)}) if err != nil { diff --git a/pkg/usecase/apiserver/service.go b/pkg/usecase/apiserver/service.go index afa4305c..cbfbf0f5 100644 --- a/pkg/usecase/apiserver/service.go +++ b/pkg/usecase/apiserver/service.go @@ -2,9 +2,10 @@ package apiserver import ( "context" - "github.com/motoki317/sc" "time" + "github.com/motoki317/sc" + "github.com/friendsofgo/errors" "github.com/go-git/go-git/v5/plumbing/transport/ssh" @@ -25,21 +26,22 @@ func handleRepoError[T any](entity T, err error) (T, error) { } type Service struct { - artifactRepo domain.ArtifactRepository - appRepo domain.ApplicationRepository - buildRepo domain.BuildRepository - envRepo domain.EnvironmentRepository - gitRepo domain.GitRepositoryRepository - commitRepo domain.RepositoryCommitRepository - userRepo domain.UserRepository - storage domain.Storage - mariaDBManager domain.MariaDBManager - mongoDBManager domain.MongoDBManager - metricsService domain.MetricsService - containerLogger domain.ContainerLogger - controller domain.ControllerServiceClient - fallbackKey *ssh.PublicKeys - image builder.ImageConfig + artifactRepo domain.ArtifactRepository + runtimeImageRepo domain.RuntimeImageRepository + appRepo domain.ApplicationRepository + buildRepo domain.BuildRepository + envRepo domain.EnvironmentRepository + gitRepo domain.GitRepositoryRepository + commitRepo domain.RepositoryCommitRepository + userRepo domain.UserRepository + storage domain.Storage + mariaDBManager domain.MariaDBManager + mongoDBManager domain.MongoDBManager + metricsService domain.MetricsService + containerLogger domain.ContainerLogger + controller domain.ControllerServiceClient + fallbackKey *ssh.PublicKeys + image builder.ImageConfig systemInfo *sc.Cache[struct{}, *domain.SystemInfo] tmpKeys *tmpKeyPairService @@ -47,6 +49,7 @@ type Service struct { func NewService( artifactRepo domain.ArtifactRepository, + runtimeImageRepo domain.RuntimeImageRepository, appRepo domain.ApplicationRepository, buildRepo domain.BuildRepository, envRepo domain.EnvironmentRepository, @@ -63,21 +66,22 @@ func NewService( fallbackKey *ssh.PublicKeys, ) (*Service, error) { return &Service{ - artifactRepo: artifactRepo, - appRepo: appRepo, - buildRepo: buildRepo, - envRepo: envRepo, - gitRepo: gitRepo, - commitRepo: commitRepo, - userRepo: userRepo, - storage: storage, - mariaDBManager: mariaDBManager, - mongoDBManager: mongoDBManager, - metricsService: metricsService, - containerLogger: containerLogger, - controller: controller, - fallbackKey: fallbackKey, - image: image, + artifactRepo: artifactRepo, + runtimeImageRepo: runtimeImageRepo, + appRepo: appRepo, + buildRepo: buildRepo, + envRepo: envRepo, + gitRepo: gitRepo, + commitRepo: commitRepo, + userRepo: userRepo, + storage: storage, + mariaDBManager: mariaDBManager, + mongoDBManager: mongoDBManager, + metricsService: metricsService, + containerLogger: containerLogger, + controller: controller, + fallbackKey: fallbackKey, + image: image, systemInfo: sc.NewMust(scutil.WrapFunc(controller.GetSystemInfo), 5*time.Minute, 10*time.Minute), tmpKeys: newTmpKeyPairService(), From 6374788913f4b79b11c4050cb1982fa9c75f0dd2 Mon Sep 17 00:00:00 2001 From: pirosiki197 Date: Mon, 2 Sep 2024 20:47:21 +0900 Subject: [PATCH 2/2] change function name --- pkg/domain/repository.go | 2 +- pkg/infrastructure/repository/runtime_image.go | 2 +- pkg/usecase/apiserver/app_service.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/domain/repository.go b/pkg/domain/repository.go index ac33094c..e0a0af75 100644 --- a/pkg/domain/repository.go +++ b/pkg/domain/repository.go @@ -102,7 +102,7 @@ type ArtifactRepository interface { type RuntimeImageRepository interface { CreateRuntimeImage(ctx context.Context, image *RuntimeImage) error - HardDeleteRuntimeImages(ctx context.Context, appId string) error + DeleteRuntimeImagesByAppID(ctx context.Context, appId string) error } type GetBuildCondition struct { diff --git a/pkg/infrastructure/repository/runtime_image.go b/pkg/infrastructure/repository/runtime_image.go index 433d8b60..19cad416 100644 --- a/pkg/infrastructure/repository/runtime_image.go +++ b/pkg/infrastructure/repository/runtime_image.go @@ -32,7 +32,7 @@ func (r *runtimeImageRepository) CreateRuntimeImage(ctx context.Context, image * return nil } -func (r *runtimeImageRepository) HardDeleteRuntimeImages(ctx context.Context, appID string) error { +func (r *runtimeImageRepository) DeleteRuntimeImagesByAppID(ctx context.Context, appID string) error { images, err := models.RuntimeImages( qm.InnerJoin(fmt.Sprintf( "%s ON %s = %s", diff --git a/pkg/usecase/apiserver/app_service.go b/pkg/usecase/apiserver/app_service.go index 95a93b1a..2e937163 100644 --- a/pkg/usecase/apiserver/app_service.go +++ b/pkg/usecase/apiserver/app_service.go @@ -385,7 +385,7 @@ func (s *Service) DeleteApplication(ctx context.Context, id string) error { return err } // delete runtime images - err = s.runtimeImageRepo.HardDeleteRuntimeImages(ctx, app.ID) + err = s.runtimeImageRepo.DeleteRuntimeImagesByAppID(ctx, app.ID) if err != nil { return err }