-
Notifications
You must be signed in to change notification settings - Fork 460
/
EphemeralConatinersService.go
117 lines (100 loc) · 4.33 KB
/
EphemeralConatinersService.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package cluster
import (
"errors"
"github.com/devtron-labs/devtron/pkg/cluster/repository"
"go.uber.org/zap"
"time"
)
type EphemeralContainerRequest struct {
BasicData *EphemeralContainerBasicData `json:"basicData"`
AdvancedData *EphemeralContainerAdvancedData `json:"advancedData"`
Namespace string `json:"namespace" validate:"required"`
ClusterId int `json:"clusterId" validate:"gt=0"`
PodName string `json:"podName" validate:"required"`
UserId int32 `json:"-"`
}
type EphemeralContainerAdvancedData struct {
Manifest string `json:"manifest"`
}
type EphemeralContainerBasicData struct {
ContainerName string `json:"containerName"`
TargetContainerName string `json:"targetContainerName"`
Image string `json:"image"`
}
func (request EphemeralContainerRequest) getContainerBean() repository.EphemeralContainerBean {
return repository.EphemeralContainerBean{
Name: request.BasicData.ContainerName,
ClusterId: request.ClusterId,
Namespace: request.Namespace,
PodName: request.PodName,
TargetContainer: request.BasicData.TargetContainerName,
Config: request.AdvancedData.Manifest,
IsExternallyCreated: false,
}
}
type EphemeralContainerService interface {
AuditEphemeralContainerAction(model EphemeralContainerRequest, actionType repository.ContainerAction) error
}
type EphemeralContainerServiceImpl struct {
repository repository.EphemeralContainersRepository
logger *zap.SugaredLogger
}
func NewEphemeralContainerServiceImpl(repository repository.EphemeralContainersRepository, logger *zap.SugaredLogger) *EphemeralContainerServiceImpl {
return &EphemeralContainerServiceImpl{
repository: repository,
logger: logger,
}
}
func (impl *EphemeralContainerServiceImpl) AuditEphemeralContainerAction(model EphemeralContainerRequest, actionType repository.ContainerAction) error {
container, err := impl.repository.FindContainerByName(model.ClusterId, model.Namespace, model.PodName, model.BasicData.ContainerName)
if err != nil {
impl.logger.Errorw("error in finding ephemeral container in the database", "err", err, "ClusterId", model.ClusterId, "Namespace", model.Namespace, "PodName", model.PodName, "ContainerName", model.BasicData.ContainerName)
return err
}
if container != nil && actionType == repository.ActionCreate {
impl.logger.Errorw("Container already present in the provided pod", "ClusterId", model.ClusterId, "Namespace", model.Namespace, "PodName", model.PodName, "ContainerName", model.BasicData.ContainerName)
return errors.New("container already present in the provided pod")
}
tx, err := impl.repository.StartTx()
defer func() {
err = impl.repository.RollbackTx(tx)
if err != nil {
impl.logger.Infow("error in rolling back transaction", "err", err, "ClusterId", model.ClusterId, "Namespace", model.Namespace, "PodName", model.PodName, "ContainerName", model.BasicData.ContainerName)
}
}()
if err != nil {
impl.logger.Errorw("error in creating transaction", "err", err)
return err
}
var auditLogBean repository.EphemeralContainerAction
if container == nil {
bean := model.getContainerBean()
if actionType != repository.ActionCreate {
// if a container is not present in database and the user is trying to access/terminate it means it is externally created
bean.IsExternallyCreated = true
}
err = impl.repository.SaveEphemeralContainerData(tx, &bean)
if err != nil {
impl.logger.Errorw("Failed to save ephemeral container", "error", err)
return err
}
auditLogBean.EphemeralContainerId = bean.Id
} else {
auditLogBean.EphemeralContainerId = container.Id
}
auditLogBean.ActionType = actionType
auditLogBean.PerformedAt = time.Now()
auditLogBean.PerformedBy = model.UserId
err = impl.repository.SaveEphemeralContainerActionAudit(tx, &auditLogBean)
if err != nil {
impl.logger.Errorw("Failed to save ephemeral container", "error", err)
return err
}
err = impl.repository.CommitTx(tx)
if err != nil {
impl.logger.Errorw("error in committing transaction", "err", err, "req", model)
return err
}
impl.logger.Infow("transaction committed successfully", "EphemeralContainerId", auditLogBean.EphemeralContainerId, "ephemeralContainerActionsId", auditLogBean.Id)
return nil
}