1
1
package v1
2
2
3
3
import (
4
+ "encoding/json"
4
5
"errors"
5
6
"fmt"
6
7
"strconv"
@@ -10,6 +11,8 @@ import (
10
11
"github.com/redhat-developer/opencompose/pkg/goutil"
11
12
"github.com/redhat-developer/opencompose/pkg/object"
12
13
"gopkg.in/yaml.v2"
14
+
15
+ api_v1 "k8s.io/client-go/pkg/api/v1"
13
16
)
14
17
15
18
const (
@@ -219,11 +222,85 @@ func (m *Mount) UnmarshalYAML(unmarshal func(interface{}) error) error {
219
222
return nil
220
223
}
221
224
225
+ type Health struct {
226
+ // Data holder for ReadinessProbe while parsing
227
+ // Data from the yaml file will be read into this field
228
+ ReadinessProbeData interface {} `yaml:"readinessProbe,omitempty"`
229
+ // After certain processing the data in ReadinessProbeData
230
+ // will be populated into ReadinessProbe for further use
231
+ ReadinessProbe * api_v1.Probe
232
+
233
+ LivenessProbeData interface {} `yaml:"livenessProbe,omitempty"`
234
+ LivenessProbe * api_v1.Probe
235
+ }
236
+
237
+ // If given an interface which has JSONified data of type Probe
238
+ // this function will read the interface and give concrete
239
+ // data strcuture pointer.
240
+ func interfaceToProbe (i interface {}) (* api_v1.Probe , error ) {
241
+ i = util .InterfaceToJSON (i )
242
+
243
+ var b []byte
244
+ var err error
245
+ if b , err = json .Marshal (i ); err != nil {
246
+ return nil , fmt .Errorf ("error: marshalling interface to bytes: %v" , err )
247
+ }
248
+ var p api_v1.Probe
249
+ if err = json .Unmarshal (b , & p ); err != nil {
250
+ return nil , fmt .Errorf ("error: unmarshalling bytes to Probe: %v" , err )
251
+ }
252
+ return & p , nil
253
+ }
254
+
255
+ func (h * Health ) UnmarshalYAML (unmarshal func (interface {}) error ) error {
256
+ type HealthAlias Health
257
+ var st struct {
258
+ HealthAlias `yaml:",inline"`
259
+ Leftovers map [string ]interface {} `yaml:",inline"` // Catches all undefined fields and must be empty after parsing.
260
+ }
261
+
262
+ err := unmarshal (& st )
263
+ if err != nil {
264
+ return err
265
+ }
266
+
267
+ if len (st .Leftovers ) > 0 {
268
+ return util .NewExcessKeysErrorFromMap ("Health" , st .Leftovers )
269
+ }
270
+
271
+ * h = Health (st .HealthAlias )
272
+
273
+ // extract the data from interface into concrete data type 'Probe'
274
+ if h .ReadinessProbeData != nil {
275
+ h .ReadinessProbe , err = interfaceToProbe (h .ReadinessProbeData )
276
+ if err != nil {
277
+ return fmt .Errorf ("readinessProbe: %v" , err )
278
+ }
279
+ h .ReadinessProbeData = interface {}(nil )
280
+ }
281
+
282
+ // extract the data from interface into concrete data type 'Probe'
283
+ if h .LivenessProbeData != nil {
284
+ h .LivenessProbe , err = interfaceToProbe (h .LivenessProbeData )
285
+ if err != nil {
286
+ return fmt .Errorf ("livenessProbe: %v" , err )
287
+ }
288
+ h .LivenessProbeData = interface {}(nil )
289
+ }
290
+
291
+ // TODO: Right now we have no way of finding if the excess keys are given
292
+ // by the user, since we are doing the whole conversion from YAML to JSON
293
+ // and then parsing it into the internal k8s structs
294
+
295
+ return nil
296
+ }
297
+
222
298
type Container struct {
223
299
Image ImageRef `yaml:"image"`
224
300
Env []EnvVariable `yaml:"env,omitempty"`
225
301
Ports []Port `yaml:"ports,omitempty"`
226
302
Mounts []Mount `yaml:"mounts,omitempty"`
303
+ Health * Health `yaml:"health,omitempty"`
227
304
}
228
305
229
306
func (c * Container ) UnmarshalYAML (unmarshal func (interface {}) error ) error {
@@ -443,6 +520,11 @@ func (d *Decoder) Decode(data []byte) (*object.OpenCompose, error) {
443
520
oc .Mounts = append (oc .Mounts , mount )
444
521
}
445
522
523
+ if c .Health != nil {
524
+ oc .Health .LivenessProbe = c .Health .LivenessProbe
525
+ oc .Health .ReadinessProbe = c .Health .ReadinessProbe
526
+ }
527
+
446
528
// convert env
447
529
for _ , e := range c .Env {
448
530
oc .Environment = append (oc .Environment , object.EnvVariable {
0 commit comments