-
Notifications
You must be signed in to change notification settings - Fork 1
/
metadata_store.go
158 lines (136 loc) · 4.67 KB
/
metadata_store.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
package kfp
import (
"context"
"fmt"
"github.com/hashicorp/go-bexpr"
pipelinesv1 "github.com/sky-uk/kfp-operator/apis/pipelines/v1alpha5"
"github.com/sky-uk/kfp-operator/argo/common"
"github.com/sky-uk/kfp-operator/argo/providers/kfp/ml_metadata"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"strings"
)
const (
PushedModelArtifactType = "PushedModel"
ArtifactNameCustomProperty = "name"
PushedCustomProperty = "pushed"
PipelineRunTypeName = "pipeline_run"
InvalidId = 0
)
type MetadataStore interface {
GetServingModelArtifact(ctx context.Context, workflowName string) ([]common.Artifact, error)
GetArtifacts(ctx context.Context, workflowName string, artifactDefs []pipelinesv1.OutputArtifact) ([]common.Artifact, error)
}
type GrpcMetadataStore struct {
MetadataStoreServiceClient ml_metadata.MetadataStoreServiceClient
}
func (gms *GrpcMetadataStore) GetServingModelArtifact(ctx context.Context, workflowName string) ([]common.Artifact, error) {
artifactTypeName := PushedModelArtifactType
typeResponse, err := gms.MetadataStoreServiceClient.GetArtifactType(ctx, &ml_metadata.GetArtifactTypeRequest{TypeName: &artifactTypeName})
if err != nil {
if status.Code(err) == codes.NotFound {
return nil, nil
}
return nil, err
}
artifactTypeId := typeResponse.GetArtifactType().GetId()
if artifactTypeId == InvalidId {
return nil, fmt.Errorf("invalid artifact ID")
}
pipelineRunTypeName := PipelineRunTypeName
contextResponse, err := gms.MetadataStoreServiceClient.GetContextByTypeAndName(ctx, &ml_metadata.GetContextByTypeAndNameRequest{TypeName: &pipelineRunTypeName, ContextName: &workflowName})
if err != nil {
return nil, err
}
contextId := contextResponse.GetContext().GetId()
if contextId == InvalidId {
return nil, fmt.Errorf("invalid context ID")
}
artifactsResponse, err := gms.MetadataStoreServiceClient.GetArtifactsByContext(ctx, &ml_metadata.GetArtifactsByContextRequest{
ContextId: &contextId,
})
if err != nil {
return nil, err
}
results := make([]common.Artifact, 0)
for _, artifact := range artifactsResponse.GetArtifacts() {
if artifact.GetTypeId() == artifactTypeId {
artifactUri := artifact.GetUri()
artifactName := artifact.GetCustomProperties()[ArtifactNameCustomProperty].GetStringValue()
modelHasBeenPushed := artifact.GetCustomProperties()[PushedCustomProperty].GetIntValue()
if artifactName != "" && artifactUri != "" && modelHasBeenPushed == 1 {
results = append(results, common.Artifact{
Name: artifactName,
Location: artifactUri,
})
}
}
}
return results, nil
}
func (gms *GrpcMetadataStore) GetArtifacts(ctx context.Context, workflowName string, artifactDefs []pipelinesv1.OutputArtifact) (artifacts []common.Artifact, err error) {
pipelineRunTypeName := PipelineRunTypeName
contextResponse, err := gms.MetadataStoreServiceClient.GetContextByTypeAndName(ctx, &ml_metadata.GetContextByTypeAndNameRequest{TypeName: &pipelineRunTypeName, ContextName: &workflowName})
if err != nil {
return nil, err
}
contextId := contextResponse.GetContext().GetId()
if contextId == InvalidId {
return nil, fmt.Errorf("invalid context ID")
}
artifactsResponse, err := gms.MetadataStoreServiceClient.GetArtifactsByContext(ctx, &ml_metadata.GetArtifactsByContextRequest{
ContextId: &contextId,
})
if err != nil {
return nil, err
}
for _, artifactDef := range artifactDefs {
var evaluator *bexpr.Evaluator
if artifactDef.Path.Filter != "" {
evaluator, err = bexpr.CreateEvaluator(artifactDef.Path.Filter)
if err != nil {
return nil, err
}
}
for _, artifact := range artifactsResponse.GetArtifacts() {
artifactUri := artifact.GetUri()
if artifactUri == "" {
continue
}
if !strings.HasSuffix(artifact.GetName(), artifactDef.Path.Locator.String()) {
continue
}
if evaluator != nil {
matched, err := evaluator.Evaluate(propertiesToPrimitiveMap(artifact.GetCustomProperties()))
// evaluator errors on missing properties
if err != nil {
continue
}
if !matched {
continue
}
}
artifacts = append(artifacts, common.Artifact{
Name: artifactDef.Name,
Location: artifactUri,
})
}
}
return artifacts, nil
}
func propertiesToPrimitiveMap(in map[string]*ml_metadata.Value) map[string]interface{} {
out := map[string]interface{}{}
for k, v := range in {
switch interface{}(v.GetValue()).(type) {
case *ml_metadata.Value_IntValue:
out[k] = v.GetIntValue()
case *ml_metadata.Value_StringValue:
out[k] = v.GetStringValue()
case *ml_metadata.Value_DoubleValue:
out[k] = v.GetDoubleValue()
case *ml_metadata.Value_StructValue:
out[k] = v.GetStructValue().AsMap()
}
}
return out
}