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.go
183 lines (170 loc) · 7.25 KB
/
resource.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
package transformers
import (
"context"
repoInterfaces "github.com/flyteorg/flyteadmin/pkg/repositories/interfaces"
"github.com/flyteorg/flytestdlib/logger"
"github.com/golang/protobuf/proto"
"github.com/flyteorg/flyteadmin/pkg/errors"
"github.com/flyteorg/flyteadmin/pkg/repositories/models"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
"google.golang.org/grpc/codes"
)
func WorkflowAttributesToResourceModel(attributes admin.WorkflowAttributes, resource admin.MatchableResource) (models.Resource, error) {
attributeBytes, err := proto.Marshal(attributes.MatchingAttributes)
if err != nil {
return models.Resource{}, err
}
return models.Resource{
Project: attributes.Project,
Domain: attributes.Domain,
Workflow: attributes.Workflow,
ResourceType: resource.String(),
Priority: models.ResourcePriorityWorkflowLevel,
Attributes: attributeBytes,
}, nil
}
func mergeUpdatePluginOverrides(existingAttributes admin.MatchingAttributes,
newMatchingAttributes *admin.MatchingAttributes) *admin.MatchingAttributes {
taskPluginOverrides := make(map[string]*admin.PluginOverride)
if existingAttributes.GetPluginOverrides() != nil && len(existingAttributes.GetPluginOverrides().Overrides) > 0 {
for _, pluginOverride := range existingAttributes.GetPluginOverrides().Overrides {
taskPluginOverrides[pluginOverride.TaskType] = pluginOverride
}
}
if newMatchingAttributes.GetPluginOverrides() != nil &&
len(newMatchingAttributes.GetPluginOverrides().Overrides) > 0 {
for _, pluginOverride := range newMatchingAttributes.GetPluginOverrides().Overrides {
taskPluginOverrides[pluginOverride.TaskType] = pluginOverride
}
}
updatedPluginOverrides := make([]*admin.PluginOverride, 0, len(taskPluginOverrides))
for _, pluginOverride := range taskPluginOverrides {
updatedPluginOverrides = append(updatedPluginOverrides, pluginOverride)
}
return &admin.MatchingAttributes{
Target: &admin.MatchingAttributes_PluginOverrides{
PluginOverrides: &admin.PluginOverrides{
Overrides: updatedPluginOverrides,
},
},
}
}
func MergeUpdateWorkflowAttributes(ctx context.Context, model models.Resource, resource admin.MatchableResource,
resourceID *repoInterfaces.ResourceID, workflowAttributes *admin.WorkflowAttributes) (models.Resource, error) {
switch resource {
case admin.MatchableResource_PLUGIN_OVERRIDE:
var existingAttributes admin.MatchingAttributes
err := proto.Unmarshal(model.Attributes, &existingAttributes)
if err != nil {
return models.Resource{}, errors.NewFlyteAdminErrorf(codes.Internal,
"Unable to unmarshal existing resource attributes for [%+v] with err: %v", resourceID, err)
}
updatedAttributes := mergeUpdatePluginOverrides(existingAttributes, workflowAttributes.GetMatchingAttributes())
marshaledAttributes, err := proto.Marshal(updatedAttributes)
if err != nil {
return models.Resource{}, errors.NewFlyteAdminErrorf(codes.Internal,
"Failed to marshal merge-updated attributes for [%+v] with err: %v", resourceID, err)
}
model.Attributes = marshaledAttributes
return model, nil
default:
logger.Warningf(ctx, "Tried to merge-update an unsupported resource type [%s] for [%+v]",
resource.String(), resourceID)
return models.Resource{}, errors.NewFlyteAdminErrorf(codes.Internal,
"Tried to merge-update an unsupported resource type [%s] for [%+v]",
resource.String(), resourceID)
}
}
func FromResourceModelToWorkflowAttributes(model models.Resource) (admin.WorkflowAttributes, error) {
var attributes admin.MatchingAttributes
err := proto.Unmarshal(model.Attributes, &attributes)
if err != nil {
return admin.WorkflowAttributes{}, errors.NewFlyteAdminErrorf(
codes.Internal, "Failed to decode project domain resource projectDomainAttributes with err: %v", err)
}
return admin.WorkflowAttributes{
Project: model.Project,
Domain: model.Domain,
Workflow: model.Workflow,
MatchingAttributes: &attributes,
}, nil
}
func ProjectDomainAttributesToResourceModel(attributes admin.ProjectDomainAttributes, resource admin.MatchableResource) (models.Resource, error) {
attributeBytes, err := proto.Marshal(attributes.MatchingAttributes)
if err != nil {
return models.Resource{}, err
}
return models.Resource{
Project: attributes.Project,
Domain: attributes.Domain,
ResourceType: resource.String(),
Priority: models.ResourcePriorityProjectDomainLevel,
Attributes: attributeBytes,
}, nil
}
func MergeUpdateProjectDomainAttributes(ctx context.Context, model models.Resource, resource admin.MatchableResource,
resourceID *repoInterfaces.ResourceID, attributes *admin.ProjectDomainAttributes) (models.Resource, error) {
switch resource {
case admin.MatchableResource_PLUGIN_OVERRIDE:
var existingAttributes admin.MatchingAttributes
err := proto.Unmarshal(model.Attributes, &existingAttributes)
if err != nil {
return models.Resource{}, errors.NewFlyteAdminErrorf(codes.Internal,
"Unable to unmarshal existing resource attributes for [%+v] with err: %v", resourceID, err)
}
updatedAttributes := mergeUpdatePluginOverrides(existingAttributes, attributes.GetMatchingAttributes())
marshaledAttributes, err := proto.Marshal(updatedAttributes)
if err != nil {
return models.Resource{}, errors.NewFlyteAdminErrorf(codes.Internal,
"Failed to marshal merge-updated attributes for [%+v] with err: %v", resourceID, err)
}
model.Attributes = marshaledAttributes
return model, nil
default:
logger.Warningf(ctx, "Tried to merge-update an unsupported resource type [%s] for [%+v]",
resource.String(), resourceID)
return models.Resource{}, errors.NewFlyteAdminErrorf(codes.Internal,
"Tried to merge-update an unsupported resource type [%s] for [%+v]",
resource.String(), resourceID)
}
}
func FromResourceModelToProjectDomainAttributes(model models.Resource) (admin.ProjectDomainAttributes, error) {
var attributes admin.MatchingAttributes
err := proto.Unmarshal(model.Attributes, &attributes)
if err != nil {
return admin.ProjectDomainAttributes{}, errors.NewFlyteAdminErrorf(
codes.Internal, "Failed to decode project domain resource projectDomainAttributes with err: %v", err)
}
return admin.ProjectDomainAttributes{
Project: model.Project,
Domain: model.Domain,
MatchingAttributes: &attributes,
}, nil
}
func FromResourceModelToMatchableAttributes(model models.Resource) (admin.MatchableAttributesConfiguration, error) {
var attributes admin.MatchingAttributes
err := proto.Unmarshal(model.Attributes, &attributes)
if err != nil {
return admin.MatchableAttributesConfiguration{}, errors.NewFlyteAdminErrorf(
codes.Internal, "Failed to decode MatchableAttributesConfiguration with err: %v", err)
}
return admin.MatchableAttributesConfiguration{
Attributes: &attributes,
Project: model.Project,
Domain: model.Domain,
Workflow: model.Workflow,
LaunchPlan: model.LaunchPlan,
}, nil
}
func FromResourceModelsToMatchableAttributes(models []models.Resource) (
[]*admin.MatchableAttributesConfiguration, error) {
configs := make([]*admin.MatchableAttributesConfiguration, len(models))
for idx, model := range models {
attributesConfig, err := FromResourceModelToMatchableAttributes(model)
if err != nil {
return nil, err
}
configs[idx] = &attributesConfig
}
return configs, nil
}