forked from kubeflow/model-registry
-
Notifications
You must be signed in to change notification settings - Fork 8
/
serving_environment.go
163 lines (137 loc) · 5.61 KB
/
serving_environment.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
package core
import (
"context"
"fmt"
"github.com/golang/glog"
"github.com/kubeflow/model-registry/internal/apiutils"
"github.com/kubeflow/model-registry/internal/converter"
"github.com/kubeflow/model-registry/internal/ml_metadata/proto"
"github.com/kubeflow/model-registry/pkg/api"
"github.com/kubeflow/model-registry/pkg/openapi"
)
// SERVING ENVIRONMENT
// UpsertServingEnvironment creates a new serving environment if the provided serving environment's ID is nil,
// or updates an existing serving environment if the ID is provided.
func (serv *ModelRegistryService) UpsertServingEnvironment(servingEnvironment *openapi.ServingEnvironment) (*openapi.ServingEnvironment, error) {
var err error
var existing *openapi.ServingEnvironment
if servingEnvironment.Id == nil {
glog.Info("Creating new serving environment")
} else {
glog.Infof("Updating serving environment %s", *servingEnvironment.Id)
existing, err = serv.GetServingEnvironmentById(*servingEnvironment.Id)
if err != nil {
return nil, err
}
withNotEditable, err := serv.openapiConv.OverrideNotEditableForServingEnvironment(converter.NewOpenapiUpdateWrapper(existing, servingEnvironment))
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
servingEnvironment = &withNotEditable
}
protoCtx, err := serv.mapper.MapFromServingEnvironment(servingEnvironment)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
protoCtxResp, err := serv.mlmdClient.PutContexts(context.Background(), &proto.PutContextsRequest{
Contexts: []*proto.Context{
protoCtx,
},
})
if err != nil {
return nil, err
}
idAsString := converter.Int64ToString(&protoCtxResp.ContextIds[0])
openapiModel, err := serv.GetServingEnvironmentById(*idAsString)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
return openapiModel, nil
}
// GetServingEnvironmentById retrieves a serving environment by its unique identifier (ID).
func (serv *ModelRegistryService) GetServingEnvironmentById(id string) (*openapi.ServingEnvironment, error) {
glog.Infof("Getting serving environment %s", id)
idAsInt, err := converter.StringToInt64(&id)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
getByIdResp, err := serv.mlmdClient.GetContextsByID(context.Background(), &proto.GetContextsByIDRequest{
ContextIds: []int64{*idAsInt},
})
if err != nil {
return nil, err
}
if len(getByIdResp.Contexts) > 1 {
return nil, fmt.Errorf("multiple serving environments found for id %s: %w", id, api.ErrNotFound)
}
if len(getByIdResp.Contexts) == 0 {
return nil, fmt.Errorf("no serving environment found for id %s: %w", id, api.ErrNotFound)
}
openapiModel, err := serv.mapper.MapToServingEnvironment(getByIdResp.Contexts[0])
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
return openapiModel, nil
}
// GetServingEnvironmentByParams retrieves a serving environment based on specified parameters, such as name or external ID.
// If multiple or no serving environments are found, an error is returned accordingly.
func (serv *ModelRegistryService) GetServingEnvironmentByParams(name *string, externalId *string) (*openapi.ServingEnvironment, error) {
glog.Infof("Getting serving environment by params name=%v, externalId=%v", name, externalId)
filterQuery := ""
if name != nil {
filterQuery = fmt.Sprintf("name = \"%s\"", *name)
} else if externalId != nil {
filterQuery = fmt.Sprintf("external_id = \"%s\"", *externalId)
} else {
return nil, fmt.Errorf("invalid parameters call, supply either name or externalId: %w", api.ErrBadRequest)
}
getByParamsResp, err := serv.mlmdClient.GetContextsByType(context.Background(), &proto.GetContextsByTypeRequest{
TypeName: &serv.nameConfig.ServingEnvironmentTypeName,
Options: &proto.ListOperationOptions{
FilterQuery: &filterQuery,
},
})
if err != nil {
return nil, err
}
if len(getByParamsResp.Contexts) > 1 {
return nil, fmt.Errorf("multiple serving environments found for name=%v, externalId=%v: %w", apiutils.ZeroIfNil(name), apiutils.ZeroIfNil(externalId), api.ErrNotFound)
}
if len(getByParamsResp.Contexts) == 0 {
return nil, fmt.Errorf("no serving environments found for name=%v, externalId=%v: %w", apiutils.ZeroIfNil(name), apiutils.ZeroIfNil(externalId), api.ErrNotFound)
}
openapiModel, err := serv.mapper.MapToServingEnvironment(getByParamsResp.Contexts[0])
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
return openapiModel, nil
}
// GetServingEnvironments retrieves a list of serving environments based on the provided list options.
func (serv *ModelRegistryService) GetServingEnvironments(listOptions api.ListOptions) (*openapi.ServingEnvironmentList, error) {
listOperationOptions, err := apiutils.BuildListOperationOptions(listOptions)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
contextsResp, err := serv.mlmdClient.GetContextsByType(context.Background(), &proto.GetContextsByTypeRequest{
TypeName: &serv.nameConfig.ServingEnvironmentTypeName,
Options: listOperationOptions,
})
if err != nil {
return nil, err
}
results := []openapi.ServingEnvironment{}
for _, c := range contextsResp.Contexts {
mapped, err := serv.mapper.MapToServingEnvironment(c)
if err != nil {
return nil, fmt.Errorf("%v: %w", err, api.ErrBadRequest)
}
results = append(results, *mapped)
}
toReturn := openapi.ServingEnvironmentList{
NextPageToken: apiutils.ZeroIfNil(contextsResp.NextPageToken),
PageSize: apiutils.ZeroIfNil(listOptions.PageSize),
Size: int32(len(results)),
Items: results,
}
return &toReturn, nil
}