This repository has been archived by the owner on Jan 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
141 lines (126 loc) · 4.11 KB
/
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
package crd
import (
"github.com/liyinan926/spark-operator/pkg/apis/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/rest"
)
// ClientInterface is the interface for client implementations for talking to the API server for
// CRUD operations on the SparkApplication custom resource objects.
type ClientInterface interface {
RESTClient() rest.Interface
Create(app *v1alpha1.SparkApplication) (*v1alpha1.SparkApplication, error)
Delete(name string, namespace string) error
Get(name string, namespace string) (*v1alpha1.SparkApplication, error)
Update(app *v1alpha1.SparkApplication) (*v1alpha1.SparkApplication, error)
UpdateStatus(app *v1alpha1.SparkApplication) (*v1alpha1.SparkApplication, error)
}
// clientImpl is a real implementation of ClientInterface for talking to the API server for CRUD
// operations on the SparkApplication custom resource objects.
type clientImpl struct {
restClient rest.Interface
}
// SchemeGroupVersion is the group version used to register the SparkApplication custom resource objects.
var schemeGroupVersion = schema.GroupVersion{Group: Group, Version: Version}
// NewClient creates a new real client for the SparkApplication CRD.
func NewClient(cfg *rest.Config) (ClientInterface, error) {
scheme := runtime.NewScheme()
schemaBuilder := runtime.NewSchemeBuilder(addKnownTypes)
if err := schemaBuilder.AddToScheme(scheme); err != nil {
return nil, err
}
config := *cfg
config.GroupVersion = &schemeGroupVersion
config.APIPath = "/apis"
config.ContentType = runtime.ContentTypeJSON
config.NegotiatedSerializer = serializer.DirectCodecFactory{
CodecFactory: serializer.NewCodecFactory(scheme)}
client, err := rest.RESTClientFor(&config)
if err != nil {
return nil, err
}
return &clientImpl{restClient: client}, nil
}
// addKnownTypes adds the set of types defined in this package to the supplied scheme.
func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(schemeGroupVersion,
&v1alpha1.SparkApplication{},
&v1alpha1.SparkApplicationList{},
)
metav1.AddToGroupVersion(scheme, schemeGroupVersion)
return nil
}
func (c *clientImpl) RESTClient() rest.Interface {
return c.restClient
}
// Create creates a new SparkApplication through an HTTP POST request.
func (c *clientImpl) Create(app *v1alpha1.SparkApplication) (*v1alpha1.SparkApplication, error) {
var result v1alpha1.SparkApplication
err := c.restClient.Post().
Namespace(app.Namespace).
Name(app.Name).
Resource(Plural).
Body(app).
Do().
Into(&result)
if err != nil {
return nil, err
}
return &result, nil
}
// Delete deletes an existing SparkApplication through an HTTP DELETE request.
func (c *clientImpl) Delete(name string, namespace string) error {
return c.restClient.Delete().
Namespace(namespace).
Name(name).
Resource(Plural).
Do().
Error()
}
// Update updates an existing SparkApplication through an HTTP PUT request.
func (c *clientImpl) Update(app *v1alpha1.SparkApplication) (*v1alpha1.SparkApplication, error) {
var result v1alpha1.SparkApplication
err := c.restClient.Put().
Namespace(app.Namespace).
Name(app.Name).
Resource(Plural).
Body(app).
Do().
Into(&result)
if err != nil {
return nil, err
}
return &result, nil
}
// UpdateStatus updates the status of an existing SparkApplication through an HTTP PUT request.
func (c *clientImpl) UpdateStatus(app *v1alpha1.SparkApplication) (*v1alpha1.SparkApplication, error) {
var result v1alpha1.SparkApplication
err := c.restClient.Put().
Namespace(app.Namespace).
Name(app.Name).
Resource(Plural).
SubResource("status").
Body(app).
Do().
Into(&result)
if err != nil {
return nil, err
}
return &result, nil
}
// Get gets a SparkApplication through an HTTP GET request.
func (c *clientImpl) Get(name string, namespace string) (*v1alpha1.SparkApplication, error) {
var result v1alpha1.SparkApplication
err := c.restClient.Get().
Namespace(namespace).
Name(name).
Resource(Plural).
Do().
Into(&result)
if err != nil {
return nil, err
}
return &result, nil
}