-
Notifications
You must be signed in to change notification settings - Fork 674
/
tf_job_client.go
147 lines (124 loc) · 4.31 KB
/
tf_job_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
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
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package k8sutil
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/tensorflow/k8s/pkg/spec"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/pkg/api"
"k8s.io/client-go/rest"
"github.com/tensorflow/k8s/pkg/util"
log "github.com/golang/glog"
)
// TFJobClient defines an interface for working with TfJob CRDs.
type TfJobClient interface {
// Get returns a TfJob
Get(ns string, name string) (*spec.TfJob, error)
// Create a TfJob
Create(ns string, j *spec.TfJob) (*spec.TfJob, error)
// Delete a TfJob
Delete(ns string, name string) error
// List returns a list of TfJobs
List(ns string) (*spec.TfJobList, error)
// Update a TfJob.
Update(ns string, c *spec.TfJob) (*spec.TfJob, error)
// Watch TfJobs.
Watch(host, ns string, httpClient *http.Client, resourceVersion string) (*http.Response, error)
}
// TfJobRestClient uses the Kubernetes rest interface to talk to the CRD.
type TfJobRestClient struct {
restcli *rest.RESTClient
}
func NewTfJobClient() (*TfJobRestClient, error) {
config, err := GetClusterConfig()
if err != nil {
return nil, err
}
config.GroupVersion = &spec.SchemeGroupVersion
config.APIPath = "/apis"
config.ContentType = runtime.ContentTypeJSON
config.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: api.Codecs}
restcli, err := rest.RESTClientFor(config)
if err != nil {
return nil, err
}
cli := &TfJobRestClient{
restcli: restcli,
}
return cli, nil
}
// HttpClient returns the http client used.
func (c *TfJobRestClient) Client() *http.Client {
return c.restcli.Client
}
func (c *TfJobRestClient) Watch(host, ns string, httpClient *http.Client, resourceVersion string) (*http.Response, error) {
host = strings.TrimRight(host, "/")
return c.restcli.Client.Get(fmt.Sprintf("%s/apis/%s/%s/%s?watch=true&resourceVersion=%s",
host, spec.CRDGroup, spec.CRDVersion, spec.CRDKindPlural, resourceVersion))
}
func (c *TfJobRestClient) List(ns string) (*spec.TfJobList, error) {
b, err := c.restcli.Get().RequestURI(listTfJobsURI(ns)).DoRaw()
if err != nil {
return nil, err
}
jobs := &spec.TfJobList{}
if err := json.Unmarshal(b, jobs); err != nil {
return nil, err
}
return jobs, nil
}
func listTfJobsURI(ns string) string {
return fmt.Sprintf("/apis/%s/%s/namespaces/%s/%s", spec.CRDGroup, spec.CRDVersion, ns, spec.CRDKindPlural)
}
func (c *TfJobRestClient) Create(ns string, j *spec.TfJob) (*spec.TfJob, error) {
// Set the TypeMeta or we will get a BadRequest
j.TypeMeta.APIVersion = fmt.Sprintf("%v/%v", spec.CRDGroup, spec.CRDVersion)
j.TypeMeta.Kind = spec.CRDKind
b, err := c.restcli.Post().Resource(spec.CRDKindPlural).Namespace(ns).Body(j).DoRaw()
if err != nil {
log.Errorf("Creating the TfJob:\n%v\nError:\n%v", util.Pformat(j), util.Pformat(err))
return nil, err
}
return readOutTfJob(b)
}
func (c *TfJobRestClient) Get(ns, name string) (*spec.TfJob, error) {
b, err := c.restcli.Get().Resource(spec.CRDKindPlural).Namespace(ns).Name(name).DoRaw()
if err != nil {
return nil, err
}
return readOutTfJob(b)
}
func (c *TfJobRestClient) Update(ns string, j *spec.TfJob) (*spec.TfJob, error) {
// Set the TypeMeta or we will get a BadRequest
j.TypeMeta.APIVersion = fmt.Sprintf("%v/%v", spec.CRDGroup, spec.CRDVersion)
j.TypeMeta.Kind = spec.CRDKind
b, err := c.restcli.Put().Resource(spec.CRDKindPlural).Namespace(ns).Name(j.Metadata.Name).Body(j).DoRaw()
if err != nil {
return nil, err
}
return readOutTfJob(b)
}
func (c *TfJobRestClient) Delete(ns, name string) error {
_, err := c.restcli.Delete().Resource(spec.CRDKindPlural).Namespace(ns).Name(name).DoRaw()
return err
}
func readOutTfJob(b []byte) (*spec.TfJob, error) {
cluster := &spec.TfJob{}
if err := json.Unmarshal(b, cluster); err != nil {
return nil, err
}
return cluster, nil
}