Skip to content

Commit

Permalink
Merge pull request #960 from traPtitech/fix/delete_app
Browse files Browse the repository at this point in the history
fix: delete runtime images before deleting application
  • Loading branch information
pirosiki197 authored Sep 2, 2024
2 parents 3567ac9 + 6374788 commit fac5dbc
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 33 deletions.
3 changes: 2 additions & 1 deletion cmd/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/domain/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ type ArtifactRepository interface {

type RuntimeImageRepository interface {
CreateRuntimeImage(ctx context.Context, image *RuntimeImage) error
DeleteRuntimeImagesByAppID(ctx context.Context, appId string) error
}

type GetBuildCondition struct {
Expand Down
23 changes: 23 additions & 0 deletions pkg/infrastructure/repository/runtime_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -28,3 +31,23 @@ func (r *runtimeImageRepository) CreateRuntimeImage(ctx context.Context, image *
}
return nil
}

func (r *runtimeImageRepository) DeleteRuntimeImagesByAppID(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
}
8 changes: 7 additions & 1 deletion pkg/usecase/apiserver/app_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.DeleteRuntimeImagesByAppID(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 {
Expand Down
66 changes: 35 additions & 31 deletions pkg/usecase/apiserver/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -25,28 +26,30 @@ 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
}

func NewService(
artifactRepo domain.ArtifactRepository,
runtimeImageRepo domain.RuntimeImageRepository,
appRepo domain.ApplicationRepository,
buildRepo domain.BuildRepository,
envRepo domain.EnvironmentRepository,
Expand All @@ -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(),
Expand Down

0 comments on commit fac5dbc

Please sign in to comment.