Skip to content

Commit

Permalink
wip: remove container model
Browse files Browse the repository at this point in the history
  • Loading branch information
ElaBosak233 committed Apr 21, 2024
1 parent beb0438 commit 5f6ef91
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 89 deletions.
2 changes: 1 addition & 1 deletion internal/casbin/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func initDefaultPolicy() {
{"user", "/api/teams/{id}/users/{user_id}", "DELETE"},
{"user", "/api/teams/{id}/join", "POST"},
{"user", "/api/teams/{id}/leave", "DELETE"},
{"user", "/api/challenges/", "GET"},
{"user", "/api/challenges/*", "GET"},
{"user", "/api/categories/", "GET"},
{"user", "/api/games/", "GET"},
{"user", "/api/games/{id}", "GET"},
Expand Down
15 changes: 5 additions & 10 deletions internal/container/manager/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type DockerManager struct {
PodID uint
RespID []string
Proxy proxy.IPodProxy
Container *model.Container
Nats []*model.Nat
CancelCtx context.Context
CancelFunc context.CancelFunc
}
Expand All @@ -47,7 +47,7 @@ func (c *DockerManager) Duration() (duration time.Duration) {
return c.duration
}

func (c *DockerManager) Setup() (container *model.Container, err error) {
func (c *DockerManager) Setup() (nats []*model.Nat, err error) {

c.CancelCtx, c.CancelFunc = context.WithCancel(context.Background())

Expand Down Expand Up @@ -115,7 +115,7 @@ func (c *DockerManager) Setup() (container *model.Container, err error) {
c.RespID[len(c.RespID)-1],
)

nats := make([]*model.Nat, 0)
nats = make([]*model.Nat, 0)

switch config.AppCfg().Container.Proxy.Enabled {
case true:
Expand Down Expand Up @@ -155,14 +155,9 @@ func (c *DockerManager) Setup() (container *model.Container, err error) {
}
}

container = &model.Container{
ChallengeID: c.challenge.ID,
Nats: nats,
}

c.Container = container
c.Nats = nats

return container, err
return nats, err
}

func (c *DockerManager) Status() (status string, err error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/container/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type IContainerManager interface {
Setup() (container *model.Container, err error)
Setup() (nats []*model.Nat, err error)
Status() (status string, err error)
Duration() (duration time.Duration)
Remove()
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (c *PodController) Create(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"id": pod.ID,
"container": pod.Container,
"nats": pod.Nats,
"removed_at": pod.RemovedAt,
})
}
Expand Down
1 change: 0 additions & 1 deletion internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ func syncDatabase() {
&model.Submission{},
&model.Nat{},
&model.Hint{},
&model.Container{},
&model.Pod{},
&model.Game{},
&model.GameChallenge{},
Expand Down
10 changes: 0 additions & 10 deletions internal/model/container.go

This file was deleted.

15 changes: 7 additions & 8 deletions internal/model/nat.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package model

// Nat is a model used to reveal the relationship between JeopardyImage ports and Container network port forwarding.
type Nat struct {
ID uint `json:"id"`
ContainerID uint `gorm:"not null" json:"container_id"`
Container *Container `json:"container,omitempty"`
SrcPort int `gorm:"not null" json:"src_port"` // Of Image
DstPort int `gorm:"not null" json:"dst_port"` // Of Container
Proxy string `json:"proxy"` // Of Platform
Entry string `gorm:"type:varchar(128)" json:"entry"`
ID uint `json:"id"`
PodID uint `gorm:"not null" json:"pod_id"`
Pod *Pod `json:"pod,omitempty"`
SrcPort int `gorm:"not null" json:"src_port"` // Of Image
DstPort int `gorm:"not null" json:"dst_port"` // Of Pod
Proxy string `json:"proxy"` // Of Platform
Entry string `gorm:"type:varchar(128)" json:"entry"`
}
4 changes: 1 addition & 3 deletions internal/model/pod.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package model

// Pod is the minimum unit of Container operation.
type Pod struct {
ID uint `json:"id"`
GameID uint `json:"game_id"`
Expand All @@ -11,7 +10,6 @@ type Pod struct {
Team *Team `json:"team,omitempty"`
ChallengeID uint `json:"challenge_id"`
Challenge *Challenge `json:"challenge,omitempty"`
ContainerID uint `json:"container_id"`
Container *Container `json:"container,omitempty"`
RemovedAt int64 `json:"removed_at"`
Nats []*Nat `json:"nats,omitempty"`
}
13 changes: 7 additions & 6 deletions internal/model/response/pod_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import (

type PodStatusResponse struct {
ID uint `json:"id"`
Container *model.Container `json:"container"`
Challenge *model.Challenge `json:"challenge"`
Nats []*model.Nat `json:"nats"`
RemovedAt int64 `json:"removed_at"`
Status string `json:"status"`
}

type PodResponse struct {
ID uint `json:"id"`
ChallengeID uint `json:"challenge_id"`
Container *model.Container `json:"container"`
RemovedAt int64 `json:"removed_at"`
Status string `json:"status"`
ID uint `json:"id"`
Challenge *model.Challenge `json:"challenge"`
Nats []*model.Nat `json:"nats"`
RemovedAt int64 `json:"removed_at"`
Status string `json:"status"`
}
23 changes: 0 additions & 23 deletions internal/repository/container.go

This file was deleted.

13 changes: 5 additions & 8 deletions internal/repository/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,13 @@ func (t *PodRepository) Find(req request.PodFindRequest) (pods []model.Pod, coun
}

result = db.
Preload("Container", func(db *gorm.DB) *gorm.DB {
Preload("Challenge", func(db *gorm.DB) *gorm.DB {
return db.
Preload("Challenge", func(db *gorm.DB) *gorm.DB {
return db.
Preload("Ports").
Preload("Envs").
Select([]string{"id", "title"})
}).
Preload("Nats")
Preload("Ports").
Preload("Envs").
Select([]string{"id", "title"})
}).
Preload("Nats").
Find(&pods)
return pods, count, result.Error
}
Expand Down
2 changes: 0 additions & 2 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type Repository struct {
FlagRepository IFlagRepository
PortRepository IPortRepository
NatRepository INatRepository
ContainerRepository IContainerRepository
EnvRepository IEnvRepository
FlagGenRepository IFlagGenRepository
GameTeamRepository IGameTeamRepository
Expand Down Expand Up @@ -53,7 +52,6 @@ func InitRepository() {
FlagRepository: NewFlagRepository(db),
PortRepository: NewPortRepository(db),
NatRepository: NewNatRepository(db),
ContainerRepository: NewContainerRepository(db),
EnvRepository: NewEnvRepository(db),
FlagGenRepository: NewFlagGenRepository(db),
GameTeamRepository: NewGameTeamRepository(db),
Expand Down
28 changes: 13 additions & 15 deletions internal/service/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,11 @@ type PodService struct {
podRepository repository.IPodRepository
natRepository repository.INatRepository
flagGenRepository repository.IFlagGenRepository
instanceRepository repository.IContainerRepository
}

func NewPodService(appRepository *repository.Repository) IPodService {
return &PodService{
challengeRepository: appRepository.ChallengeRepository,
instanceRepository: appRepository.ContainerRepository,
flagGenRepository: appRepository.FlagGenRepository,
podRepository: appRepository.PodRepository,
natRepository: appRepository.NatRepository,
Expand Down Expand Up @@ -150,7 +148,7 @@ func (t *PodService) Create(req request.PodCreateRequest) (res response.PodStatu
time.Duration(challenge.Duration)*time.Second,
)

container, err := ctnManager.Setup()
nats, err := ctnManager.Setup()

var gameID uint
if req.GameID != nil {
Expand All @@ -169,7 +167,7 @@ func (t *PodService) Create(req request.PodCreateRequest) (res response.PodStatu
GameID: gameID,
TeamID: teamID,
RemovedAt: removedAt,
Container: container,
Nats: nats,
})

ctnManager.SetPodID(pod.ID)
Expand All @@ -189,7 +187,7 @@ func (t *PodService) Create(req request.PodCreateRequest) (res response.PodStatu

return response.PodStatusResponse{
ID: pod.ID,
Container: pod.Container,
Nats: nats,
RemovedAt: removedAt,
}, err
}
Expand Down Expand Up @@ -248,15 +246,15 @@ func (t *PodService) Remove(req request.PodRemoveRequest) (err error) {
}

func (t *PodService) FindById(id uint) (rep response.PodResponse, err error) {
instance, _ := t.podRepository.FindById(id)
pod, _ := t.podRepository.FindById(id)
if PodManagers[id] != nil {
ctn := PodManagers[id]
status, _ := ctn.Status()
rep = response.PodResponse{
ID: id,
RemovedAt: instance.RemovedAt,
ChallengeID: instance.ChallengeID,
Status: status,
ID: id,
RemovedAt: pod.RemovedAt,
Challenge: pod.Challenge,
Status: status,
}
return rep, nil
}
Expand All @@ -278,11 +276,11 @@ func (t *PodService) Find(req request.PodFindRequest) (pods []response.PodRespon
}
}
pods = append(pods, response.PodResponse{
ID: pod.ID,
RemovedAt: pod.RemovedAt,
Container: pod.Container,
ChallengeID: pod.ChallengeID,
Status: status,
ID: pod.ID,
RemovedAt: pod.RemovedAt,
Nats: pod.Nats,
Challenge: pod.Challenge,
Status: status,
})
}
return pods, err
Expand Down

0 comments on commit 5f6ef91

Please sign in to comment.