/
from_yaml.go
334 lines (279 loc) · 8.55 KB
/
from_yaml.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
Copyright 2017 The OpenEBS Authors
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 k8s
import (
"fmt"
"github.com/ghodss/yaml"
"github.com/openebs/maya/pkg/apis/openebs.io/v1alpha1"
"github.com/openebs/maya/pkg/template"
api_apps_v1 "k8s.io/api/apps/v1"
api_apps_v1beta1 "k8s.io/api/apps/v1beta1"
api_batch_v1 "k8s.io/api/batch/v1"
api_core_v1 "k8s.io/api/core/v1"
api_extn_v1beta1 "k8s.io/api/extensions/v1beta1"
)
// STSYml helps generating kubernetes StatefulSet
// object
type STSYml struct {
// YmlInBytes represents a kubernetes StatefulSet
// in yaml format
YmlInBytes []byte
}
// NewSTSYml returns a new instance of STSYml
func NewSTSYml(context, yml string, values map[string]interface{}) (*STSYml, error) {
b, err := template.AsTemplatedBytes(context, yml, values)
if err != nil {
return nil, err
}
return &STSYml{
YmlInBytes: b,
}, nil
}
// AsAppsV1STS returns a apps/v1 StatefulSet instance
func (m *STSYml) AsAppsV1STS() (*api_apps_v1.StatefulSet, error) {
if m.YmlInBytes == nil {
return nil, fmt.Errorf("missing statefulset yaml")
}
sts := &api_apps_v1.StatefulSet{}
err := yaml.Unmarshal(m.YmlInBytes, sts)
if err != nil {
return nil, err
}
return sts, nil
}
// JobYml helps generating kubernetes Job object
type JobYml struct {
YmlInBytes []byte // YmlInBytes represents a K8s Job in yaml format
}
// NewJobYml returns a new instance of JobYml
func NewJobYml(context, yml string, values map[string]interface{}) (*JobYml, error) {
b, err := template.AsTemplatedBytes(context, yml, values)
if err != nil {
return nil, err
}
return &JobYml{
YmlInBytes: b,
}, nil
}
// AsBatchV1Job returns a batch/v1 Job instance
func (m *JobYml) AsBatchV1Job() (*api_batch_v1.Job, error) {
if m.YmlInBytes == nil {
return nil, fmt.Errorf("Missing yaml")
}
job := &api_batch_v1.Job{}
err := yaml.Unmarshal(m.YmlInBytes, job)
if err != nil {
return nil, err
}
return job, nil
}
// DeploymentYml provides utility methods to generate K8s Deployment objects
type DeploymentYml struct {
// YmlInBytes represents a K8s Deployment in
// yaml format
YmlInBytes []byte
}
// NewDeploymentYml returns a DeploymentYml instance.
func NewDeploymentYml(context, yml string, values map[string]interface{}) (*DeploymentYml, error) {
b, err := template.AsTemplatedBytes(context, yml, values)
if err != nil {
return nil, err
}
return &DeploymentYml{
YmlInBytes: b,
}, nil
}
// AsExtnV1B1Deployment returns a extensions/v1beta1 Deployment instance
func (m *DeploymentYml) AsExtnV1B1Deployment() (*api_extn_v1beta1.Deployment, error) {
if m.YmlInBytes == nil {
return nil, fmt.Errorf("Missing yaml")
}
// unmarshall the byte into k8s Deployment object
deploy := &api_extn_v1beta1.Deployment{}
err := yaml.Unmarshal(m.YmlInBytes, deploy)
if err != nil {
return nil, err
}
return deploy, nil
}
// AsAppsV1B1Deployment returns a apps/v1 Deployment instance
func (m *DeploymentYml) AsAppsV1B1Deployment() (*api_apps_v1beta1.Deployment, error) {
if m.YmlInBytes == nil {
return nil, fmt.Errorf("Missing yaml")
}
// unmarshall the byte into k8s Deployment object
deploy := &api_apps_v1beta1.Deployment{}
err := yaml.Unmarshal(m.YmlInBytes, deploy)
if err != nil {
return nil, err
}
return deploy, nil
}
// AsAppsV1Deployment returns a apps/v1 Deployment instance
func (m *DeploymentYml) AsAppsV1Deployment() (*api_apps_v1.Deployment, error) {
if m.YmlInBytes == nil {
return nil, fmt.Errorf("Missing yaml")
}
// unmarshall the byte into k8s Deployment object
deploy := &api_apps_v1.Deployment{}
err := yaml.Unmarshal(m.YmlInBytes, deploy)
if err != nil {
return nil, err
}
return deploy, nil
}
// ServiceYml struct provides utility methods to generate K8s Service objects
type ServiceYml struct {
// YmlInBytes represents a K8s Service in
// yaml format
YmlInBytes []byte
}
// NewServiceYml returns a ServiceYml instance.
func NewServiceYml(context, yml string, values map[string]interface{}) (*ServiceYml, error) {
b, err := template.AsTemplatedBytes(context, yml, values)
if err != nil {
return nil, err
}
return &ServiceYml{
YmlInBytes: b,
}, nil
}
// AsCoreV1Service returns a v1 Service instance
func (m *ServiceYml) AsCoreV1Service() (*api_core_v1.Service, error) {
if m.YmlInBytes == nil {
return nil, fmt.Errorf("Missing yaml")
}
// unmarshall the byte into k8s Service object
svc := &api_core_v1.Service{}
err := yaml.Unmarshal(m.YmlInBytes, svc)
if err != nil {
return nil, err
}
return svc, nil
}
//CStorPoolYml provides utility methods to generate K8s CStorPool objects
type CStorPoolYml struct {
// YmlInBytes represents a CStorPool in
// yaml format
YmlInBytes []byte
}
//CStorVolumeYml provides utility methods to generate K8s CStorVolume objects
type CStorVolumeYml struct {
// YmlInBytes represents a CStorVolume in
// yaml format
YmlInBytes []byte
}
//StoragePoolYml provides utility methods to generate K8s StoragePool objects
type StoragePoolYml struct {
// YmlInBytes represents a StoragePool in
// yaml format
YmlInBytes []byte
}
// NewCStorPoolYml returns a CStorPoolYml instance.
func NewCStorPoolYml(context, yml string, values map[string]interface{}) (*CStorPoolYml, error) {
b, err := template.AsTemplatedBytes(context, yml, values)
if err != nil {
return nil, err
}
return &CStorPoolYml{
YmlInBytes: b,
}, nil
}
// NewStoragePoolYml returns a StoragePoolYml instance.
func NewStoragePoolYml(context, yml string, values map[string]interface{}) (*StoragePoolYml, error) {
b, err := template.AsTemplatedBytes(context, yml, values)
if err != nil {
return nil, err
}
return &StoragePoolYml{
YmlInBytes: b,
}, nil
}
// NewCStorVolumeYml returns a new CStorVolumeYml instance based on yml string and values.
func NewCStorVolumeYml(context, yml string, values map[string]interface{}) (*CStorVolumeYml, error) {
b, err := template.AsTemplatedBytes(context, yml, values)
if err != nil {
return nil, err
}
return &CStorVolumeYml{
YmlInBytes: b,
}, nil
}
// AsCStorPoolYml returns a v1 CStorPool instance
func (m *CStorPoolYml) AsCStorPoolYml() (*v1alpha1.CStorPool, error) {
if m.YmlInBytes == nil {
return nil, fmt.Errorf("Missing yaml")
}
// unmarshall the byte into CStorVolume object
cstorPool := &v1alpha1.CStorPool{}
err := yaml.Unmarshal(m.YmlInBytes, cstorPool)
if err != nil {
return nil, err
}
return cstorPool, nil
}
// AsStoragePoolYml returns a v1 StoragePool instance
func (m *StoragePoolYml) AsStoragePoolYml() (*v1alpha1.StoragePool, error) {
if m.YmlInBytes == nil {
return nil, fmt.Errorf("Missing yaml")
}
// unmarshall the byte into StoragePool object
storagePool := &v1alpha1.StoragePool{}
err := yaml.Unmarshal(m.YmlInBytes, storagePool)
if err != nil {
return nil, err
}
return storagePool, nil
}
// AsCStorVolumeYml returns a v1 CStorVolume instance
func (m *CStorVolumeYml) AsCStorVolumeYml() (*v1alpha1.CStorVolume, error) {
if m.YmlInBytes == nil {
return nil, fmt.Errorf("Missing yaml")
}
// unmarshall the byte into CStorVolume object
cstorVolume := &v1alpha1.CStorVolume{}
err := yaml.Unmarshal(m.YmlInBytes, cstorVolume)
if err != nil {
return nil, err
}
return cstorVolume, nil
}
// CStorVolumeReplicaYml provides utility methods to generate K8s CStorVolumeReplica objects
type CStorVolumeReplicaYml struct {
// YmlInBytes represents a CStorVolumeReplica in
// yaml format
YmlInBytes []byte
}
// NewCStorVolumeReplicaYml returns a CStorVolumeReplicaYml instance.
func NewCStorVolumeReplicaYml(context, yml string, values map[string]interface{}) (*CStorVolumeReplicaYml, error) {
b, err := template.AsTemplatedBytes(context, yml, values)
if err != nil {
return nil, err
}
return &CStorVolumeReplicaYml{
YmlInBytes: b,
}, nil
}
// AsCStorVolumeReplicaYml returns a v1 Service instance
func (m *CStorVolumeReplicaYml) AsCStorVolumeReplicaYml() (*v1alpha1.CStorVolumeReplica, error) {
if m.YmlInBytes == nil {
return nil, fmt.Errorf("Missing yaml")
}
// unmarshall the byte into CStorVolumeReplica object
cstorVolumeReplica := &v1alpha1.CStorVolumeReplica{}
err := yaml.Unmarshal(m.YmlInBytes, cstorVolumeReplica)
if err != nil {
return nil, err
}
return cstorVolumeReplica, nil
}