forked from kubeflow/kfp-tekton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline_upload_client.go
121 lines (99 loc) · 3.79 KB
/
pipeline_upload_client.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
package api_server
import (
"context"
"fmt"
"os"
"github.com/go-openapi/runtime"
"github.com/go-openapi/strfmt"
apiclient "github.com/kubeflow/pipelines/backend/api/v1/go_http_client/pipeline_upload_client"
params "github.com/kubeflow/pipelines/backend/api/v1/go_http_client/pipeline_upload_client/pipeline_upload_service"
model "github.com/kubeflow/pipelines/backend/api/v1/go_http_client/pipeline_upload_model"
"github.com/kubeflow/pipelines/backend/src/common/util"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/tools/clientcmd"
)
const (
pipelineUploadFieldName = "uploadfile"
pipelineUploadPath = "pipelines/upload"
pipelineUploadServerBasePath = "/api/v1/namespaces/%s/services/ml-pipeline:8888/proxy/apis/v1beta1/%s"
pipelineUploadContentTypeKey = "Content-Type"
pipelineVersionUploadPath = "pipelines/upload_version"
)
type PipelineUploadInterface interface {
UploadFile(filePath string, parameters *params.UploadPipelineParams) (*model.V1Pipeline, error)
}
type PipelineUploadClient struct {
apiClient *apiclient.PipelineUpload
}
func NewPipelineUploadClient(clientConfig clientcmd.ClientConfig, debug bool) (
*PipelineUploadClient, error) {
runtime, err := NewHTTPRuntime(clientConfig, debug)
if err != nil {
return nil, err
}
apiClient := apiclient.New(runtime, strfmt.Default)
// Creating upload client
return &PipelineUploadClient{
apiClient: apiClient,
}, nil
}
func (c *PipelineUploadClient) UploadFile(filePath string, parameters *params.UploadPipelineParams) (
*model.V1Pipeline, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, util.NewUserErrorWithSingleMessage(err,
fmt.Sprintf("Failed to open file '%s'", filePath))
}
defer file.Close()
parameters.Uploadfile = runtime.NamedReader(filePath, file)
return c.Upload(parameters)
}
func (c *PipelineUploadClient) Upload(parameters *params.UploadPipelineParams) (*model.V1Pipeline,
error) {
// Create context with timeout
ctx, cancel := context.WithTimeout(context.Background(), apiServerDefaultTimeout)
defer cancel()
// Make service call
parameters.Context = ctx
response, err := c.apiClient.PipelineUploadService.UploadPipeline(parameters, PassThroughAuth)
if err != nil {
if defaultError, ok := err.(*params.UploadPipelineDefault); ok {
err = CreateErrorFromAPIStatus(defaultError.Payload.Error, defaultError.Payload.Code)
} else {
err = CreateErrorCouldNotRecoverAPIStatus(err)
}
return nil, util.NewUserError(err,
fmt.Sprintf("Failed to upload pipeline. Params: '%v'", parameters),
fmt.Sprintf("Failed to upload pipeline"))
}
return response.Payload, nil
}
// UploadPipelineVersion uploads pipeline version from local file.
func (c *PipelineUploadClient) UploadPipelineVersion(filePath string, parameters *params.UploadPipelineVersionParams) (*model.V1PipelineVersion,
error) {
// Get file
file, err := os.Open(filePath)
if err != nil {
return nil, util.NewUserErrorWithSingleMessage(err,
fmt.Sprintf("Failed to open file '%s'", filePath))
}
defer file.Close()
parameters.Uploadfile = runtime.NamedReader(filePath, file)
// Create context with timeout
ctx, cancel := context.WithTimeout(context.Background(), apiServerDefaultTimeout)
defer cancel()
// Make service call
parameters.Context = ctx
response, err := c.apiClient.PipelineUploadService.UploadPipelineVersion(parameters, PassThroughAuth)
if err != nil {
if defaultError, ok := err.(*params.UploadPipelineVersionDefault); ok {
err = CreateErrorFromAPIStatus(defaultError.Payload.Error, defaultError.Payload.Code)
} else {
err = CreateErrorCouldNotRecoverAPIStatus(err)
}
return nil, util.NewUserError(err,
fmt.Sprintf("Failed to upload pipeline version. Params: '%v'", parameters),
fmt.Sprintf("Failed to upload pipeline version"))
}
return response.Payload, nil
}