Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions api/apis/k8sApi.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ import (
type IK8sApi interface {
DeployGame(game *models.Game) error
ReadGameUrl(gameId uuid.UUID) (string, error)
DeleteGame(game *models.Game) error
}

func (g k8sApi) DeleteGame(game *models.Game) error {
resource, err := createAndVerifyGameResource(game)
if err != nil {
return err
}

ctx := context.Background()
return g.k8sClient.Delete(ctx, resource)
}

func (g k8sApi) DeployGame(game *models.Game) error {
Expand Down
1 change: 1 addition & 0 deletions api/controllers/gameController.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func (g gameController) DeleteGameById(c *gin.Context) {
return
}

//Delete game from db, azure storage and k8s/aks
err = g.service.Delete(_uuid)
if err != nil { //TODO handle different errors
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
Expand Down
33 changes: 31 additions & 2 deletions api/services/gameService.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"log"
"mime/multipart"
"os"
"strings"
)

type IGameService interface {
Expand Down Expand Up @@ -86,12 +87,35 @@ func (g gameService) Save(fileHeader *multipart.FileHeader, title string, owner
}

func (g gameService) Delete(id uuid.UUID) error {

err := g.azure.DeleteGame(os.Getenv("AZURE_CONTAINER_NAME"), id.String())
//Get the game, we need the details to delete it from k8s
game, err := g.repository.FindByID(id)
if err != nil {
return err
}

//Delete from azure storage
err = g.azure.DeleteGame(os.Getenv("AZURE_CONTAINER_NAME"), id.String())
if err != nil {
if isNotFound(err) {
log.Println(fmt.Sprintf("Game %s is already deleted from azure storage", id.String()))
} else {
return err
}
}

//Delete from k8s/aks, if the game has an url
if game.Url != "" {
err = g.k8s.DeleteGame(game)
if err != nil {
if isNotFound(err) {
log.Println(fmt.Sprintf("Game %s is already deleted from aks", id.String()))
} else {
return err
}
}
}

//Delete from db and return
return g.repository.Delete(id)
}

Expand All @@ -118,3 +142,8 @@ func GameService(repository repositories.IGameRepository, k8s apis.IK8sApi, azur
azure: azure,
}
}

func isNotFound(err error) bool {
e := strings.ToLower(err.Error())
return strings.Contains(e, "not found") || strings.Contains(e, "notfound")
}