This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
resource_repo.go
221 lines (193 loc) · 7.65 KB
/
resource_repo.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package gormimpl
import (
"context"
"errors"
"fmt"
flyteAdminDbErrors "github.com/flyteorg/flyteadmin/pkg/repositories/errors"
"github.com/flyteorg/flyteadmin/pkg/repositories/interfaces"
"github.com/flyteorg/flyteadmin/pkg/repositories/models"
"github.com/flyteorg/flytestdlib/promutils"
"google.golang.org/grpc/codes"
"gorm.io/gorm"
flyteAdminErrors "github.com/flyteorg/flyteadmin/pkg/errors"
)
type ResourceRepo struct {
db *gorm.DB
errorTransformer flyteAdminDbErrors.ErrorTransformer
metrics gormMetrics
}
const priorityDescending = "priority desc"
/*
The data in the Resource repo maps to the following rules:
* Domain and ResourceType can never be empty.
* Empty string can be interpreted as all. Example: "" for Project field can be interpreted as all Projects for a domain.
* One cannot provide specific value for Project, unless a specific value for Domain is provided.
** Project is always scoped within a domain.
** Example: Domain="" Project="Lyft" is invalid.
* One cannot provide specific value for Workflow, unless a specific value for Domain and Project is provided.
** Workflow is always scoped within a domain and project.
** Example: Domain="staging" Project="" Workflow="W1" is invalid.
* One cannot provide specific value for Launch plan, unless a specific value for Domain, Project and Workflow is provided.
** Launch plan is always scoped within a domain, project and workflow.
** Example: Domain="staging" Project="Lyft" Workflow="" LaunchPlan= "l1" is invalid.
*/
func validateCreateOrUpdateResourceInput(project, domain, workflow, launchPlan, resourceType string) bool {
if resourceType == "" {
return false
}
if domain == "" && project == "" {
return false
}
if project == "" && (workflow != "" || launchPlan != "") {
return false
}
if workflow == "" && launchPlan != "" {
return false
}
return true
}
func (r *ResourceRepo) CreateOrUpdate(ctx context.Context, input models.Resource) error {
if !validateCreateOrUpdateResourceInput(input.Project, input.Domain, input.Workflow, input.LaunchPlan, input.ResourceType) {
return flyteAdminDbErrors.GetInvalidInputError(fmt.Sprintf("%v", input))
}
if input.Priority == 0 {
return flyteAdminDbErrors.GetInvalidInputError(fmt.Sprintf("invalid priority %v", input))
}
timer := r.metrics.GetDuration.Start()
var record models.Resource
tx := r.db.FirstOrCreate(&record, models.Resource{
Project: input.Project,
Domain: input.Domain,
Workflow: input.Workflow,
LaunchPlan: input.LaunchPlan,
ResourceType: input.ResourceType,
Priority: input.Priority,
})
timer.Stop()
if tx.Error != nil {
return r.errorTransformer.ToFlyteAdminError(tx.Error)
}
timer = r.metrics.UpdateDuration.Start()
record.Attributes = input.Attributes
tx = r.db.Save(&record)
timer.Stop()
if tx.Error != nil {
return r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return nil
}
// Get returns the most-specific attribute setting for the given ResourceType.
func (r *ResourceRepo) Get(ctx context.Context, ID interfaces.ResourceID) (models.Resource, error) {
if !validateCreateOrUpdateResourceInput(ID.Project, ID.Domain, ID.Workflow, ID.LaunchPlan, ID.ResourceType) {
return models.Resource{}, r.errorTransformer.ToFlyteAdminError(flyteAdminDbErrors.GetInvalidInputError(fmt.Sprintf("%v", ID)))
}
var resources []models.Resource
timer := r.metrics.GetDuration.Start()
txWhereClause := "resource_type = ? AND domain IN (?) AND project IN (?) AND workflow IN (?) AND launch_plan IN (?)"
project := []string{""}
if ID.Project != "" {
project = append(project, ID.Project)
}
domain := []string{""}
if ID.Domain != "" {
domain = append(domain, ID.Domain)
}
workflow := []string{""}
if ID.Workflow != "" {
workflow = append(workflow, ID.Workflow)
}
launchPlan := []string{""}
if ID.LaunchPlan != "" {
launchPlan = append(launchPlan, ID.LaunchPlan)
}
tx := r.db.Where(txWhereClause, ID.ResourceType, domain, project, workflow, launchPlan)
tx.Order(priorityDescending).First(&resources)
timer.Stop()
if (tx.Error != nil && errors.Is(tx.Error, gorm.ErrRecordNotFound)) || len(resources) == 0 {
return models.Resource{}, flyteAdminErrors.NewFlyteAdminErrorf(codes.NotFound,
"Resource [%+v] not found", ID)
} else if tx.Error != nil {
return models.Resource{}, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return resources[0], nil
}
// GetProjectLevel differs from Get in that it returns only the project-level attribute setting for the
// given ResourceType if it exists. The reason this exists is because we want to return project level
// attributes to Flyte Console, regardless of whether a more specific setting exists.
func (r *ResourceRepo) GetProjectLevel(ctx context.Context, ID interfaces.ResourceID) (models.Resource, error) {
if ID.Project == "" {
return models.Resource{}, r.errorTransformer.ToFlyteAdminError(flyteAdminDbErrors.GetInvalidInputError(fmt.Sprintf("%v", ID)))
}
var resources []models.Resource
timer := r.metrics.GetDuration.Start()
txWhereClause := "resource_type = ? AND domain = '' AND project = ? AND workflow = '' AND launch_plan = ''"
tx := r.db.Where(txWhereClause, ID.ResourceType, ID.Project)
tx.Order(priorityDescending).First(&resources)
timer.Stop()
if (tx.Error != nil && errors.Is(tx.Error, gorm.ErrRecordNotFound)) || len(resources) == 0 {
return models.Resource{}, flyteAdminErrors.NewFlyteAdminErrorf(codes.NotFound,
"Resource [%+v] not found", ID)
} else if tx.Error != nil {
return models.Resource{}, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return resources[0], nil
}
func (r *ResourceRepo) GetRaw(ctx context.Context, ID interfaces.ResourceID) (models.Resource, error) {
if ID.Domain == "" || ID.ResourceType == "" {
return models.Resource{}, r.errorTransformer.ToFlyteAdminError(flyteAdminDbErrors.GetInvalidInputError(fmt.Sprintf("%v", ID)))
}
var model models.Resource
timer := r.metrics.GetDuration.Start()
tx := r.db.Where(&models.Resource{
Project: ID.Project,
Domain: ID.Domain,
Workflow: ID.Workflow,
LaunchPlan: ID.LaunchPlan,
ResourceType: ID.ResourceType,
}).First(&model)
timer.Stop()
if tx.Error != nil && errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return models.Resource{}, flyteAdminErrors.NewFlyteAdminErrorf(codes.NotFound,
"%v", ID)
} else if tx.Error != nil {
return models.Resource{}, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return model, nil
}
func (r *ResourceRepo) ListAll(ctx context.Context, resourceType string) ([]models.Resource, error) {
var resources []models.Resource
timer := r.metrics.ListDuration.Start()
tx := r.db.Where(&models.Resource{ResourceType: resourceType}).Order(priorityDescending).Find(&resources)
timer.Stop()
if tx.Error != nil {
return nil, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return resources, nil
}
func (r *ResourceRepo) Delete(ctx context.Context, ID interfaces.ResourceID) error {
var tx *gorm.DB
r.metrics.DeleteDuration.Time(func() {
tx = r.db.Where(&models.Resource{
Project: ID.Project,
Domain: ID.Domain,
Workflow: ID.Workflow,
LaunchPlan: ID.LaunchPlan,
ResourceType: ID.ResourceType,
}).Unscoped().Delete(models.Resource{})
})
if tx.Error != nil && errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return flyteAdminErrors.NewFlyteAdminErrorf(codes.NotFound, "%v", ID)
} else if tx.Error != nil {
return r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return nil
}
func NewResourceRepo(db *gorm.DB, errorTransformer flyteAdminDbErrors.ErrorTransformer,
scope promutils.Scope) interfaces.ResourceRepoInterface {
metrics := newMetrics(scope)
return &ResourceRepo{
db: db,
errorTransformer: errorTransformer,
metrics: metrics,
}
}