forked from kubeflow/pipelines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
73 lines (61 loc) · 2.11 KB
/
util.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
package api_server
import (
"fmt"
"time"
workflowapi "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
"github.com/ghodss/yaml"
"github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
"github.com/kubeflow/pipelines/backend/src/common/util"
"github.com/pkg/errors"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
const (
apiServerBasePath = "/api/v1/namespaces/%s/services/ml-pipeline:8888/proxy/"
apiServerDefaultTimeout = 35 * time.Second
)
// PassThroughAuth never manipulates the request
var PassThroughAuth runtime.ClientAuthInfoWriter = runtime.ClientAuthInfoWriterFunc(
func(_ runtime.ClientRequest, _ strfmt.Registry) error { return nil })
func toDateTimeTestOnly(timeInSec int64) strfmt.DateTime {
result, err := strfmt.ParseDateTime(time.Unix(timeInSec, 0).String())
if err != nil {
return strfmt.NewDateTime()
}
return result
}
func toWorkflowTestOnly(workflow string) *workflowapi.Workflow {
var result workflowapi.Workflow
err := yaml.Unmarshal([]byte(workflow), &result)
if err != nil {
return nil
}
return &result
}
func NewHTTPRuntime(clientConfig clientcmd.ClientConfig, debug bool) (
*httptransport.Runtime, error) {
// Creating k8 client
k8Client, config, namespace, err := util.GetKubernetesClientFromClientConfig(clientConfig)
if err != nil {
return nil, errors.Wrapf(err, "Error while creating K8 client")
}
// Create API client
httpClient := k8Client.RESTClient().(*rest.RESTClient).Client
masterIPAndPort := util.ExtractMasterIPAndPort(config)
runtime := httptransport.NewWithClient(masterIPAndPort, fmt.Sprintf(apiServerBasePath, namespace),
nil, httpClient)
if debug {
runtime.SetDebug(true)
}
return runtime, err
}
func CreateErrorFromAPIStatus(error string, code int32) error {
return fmt.Errorf("%v (code: %v)", error, code)
}
func CreateErrorCouldNotRecoverAPIStatus(err error) error {
return fmt.Errorf("Issue calling the service. Use the '--debug' flag to see the HTTP request/response. Raw error from the client: %v",
err.Error())
}