Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(spec): add support for health #143

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions examples/wordpress/health.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
version: '0.1-dev'

services:
- name: database
containers:
- image: mariadb:10
env:
- name: MYSQL_ROOT_PASSWORD
value: rootpasswd
- name: MYSQL_DATABASE
value: wordpress
- name: MYSQL_USER
value: wordpress
- name: MYSQL_PASSWORD
value: wordpress
ports:
- port: 3306
mounts:
- volumeRef: database
mountPath: /var/lib/mysql
health:
livenessProbe:
exec:
command:
- mysqladmin
- ping
initialDelaySeconds: 30
timeoutSeconds: 5
readinessProbe:
exec:
command:
- mysqladmin
- ping
initialDelaySeconds: 5
timeoutSeconds: 1

- name: web
containers:
- image: wordpress:4
env:
- name: WORDPRESS_DB_HOST
value: database:3306
- name: WORDPRESS_DB_PASSWORD
value: wordpress
- name: WORDPRESS_DB_USER
value: wordpress
- name: WORDPRESS_DB_NAME
value: wordpress
ports:
- port: 80
type: external
health:
livenessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 120
timeoutSeconds: 5
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 5
timeoutSeconds: 1
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@containscafeine updated as it is used in prod clusters!


volumes:
- name: database
size: 100Mi
accessMode: ReadWriteOnce
20 changes: 20 additions & 0 deletions pkg/encoding/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,23 @@ func ValidateRequiredFields(i interface{}) error {

return nil
}

// Converts the yaml read into empty interface to JSONified
// empty interface, this can be used to marshal into actual
// JSON and from there if you want it you can read it into
// Took help from https://stackoverflow.com/a/40737676/3848679
func InterfaceToJSON(i interface{}) interface{} {
switch x := i.(type) {
case map[interface{}]interface{}:
m2 := map[string]interface{}{}
for k, v := range x {
m2[k.(string)] = InterfaceToJSON(v)
}
return m2
case []interface{}:
for i, v := range x {
x[i] = InterfaceToJSON(v)
}
}
return i
}
58 changes: 58 additions & 0 deletions pkg/encoding/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"reflect"
"testing"

"gopkg.in/yaml.v2"
)

type S struct {
Expand Down Expand Up @@ -135,3 +137,59 @@ func TestValidateRequiredFields(t *testing.T) {
}
}
}

func TestInterfaceToJSON(t *testing.T) {
tests := []struct {
input string
output map[string]interface{}
}{
{
input: `foo: bar`,
output: map[string]interface{}{"foo": "bar"},
},
{
input: `
one:
- 1
- 2
- 3
two: four`,
output: map[string]interface{}{
"two": "four",
"one": []interface{}{1, 2, 3},
},
},
{
input: `
one:
two:
three: four
five: six
seven:
- eight
- nine`,
output: map[string]interface{}{
"one": map[string]interface{}{
"two": map[string]interface{}{
"three": "four",
},
},
"five": "six",
"seven": []interface{}{"eight", "nine"},
},
},
}

for _, test := range tests {
var input interface{}
err := yaml.Unmarshal([]byte(test.input), &input)
if err != nil {
t.Fatalf("failed unmarshalling: %v", err)
}
gotOutput := InterfaceToJSON(input)

if !reflect.DeepEqual(test.output, gotOutput) {
t.Errorf("Expected: %#v\nGot: %#v", test.output, gotOutput)
}
}
}
82 changes: 82 additions & 0 deletions pkg/encoding/v1/encoding.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1

import (
"encoding/json"
"errors"
"fmt"
"strconv"
Expand All @@ -10,6 +11,8 @@ import (
"github.com/redhat-developer/opencompose/pkg/goutil"
"github.com/redhat-developer/opencompose/pkg/object"
"gopkg.in/yaml.v2"

api_v1 "k8s.io/client-go/pkg/api/v1"
)

const (
Expand Down Expand Up @@ -219,11 +222,85 @@ func (m *Mount) UnmarshalYAML(unmarshal func(interface{}) error) error {
return nil
}

type Health struct {
// Data holder for ReadinessProbe while parsing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a bit more explaining.
What is a data holder? Why it's needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still missing WHY part of this.
We have to explain why we are doing it like this via interface{}.
We will forget why it was done like this next time we are reading this.

I already forgot it and you explained it to me last week :-D

// Data from the yaml file will be read into this field
ReadinessProbeData interface{} `yaml:"readinessProbe,omitempty"`
// After certain processing the data in ReadinessProbeData
// will be populated into ReadinessProbe for further use
ReadinessProbe *api_v1.Probe

LivenessProbeData interface{} `yaml:"livenessProbe,omitempty"`
LivenessProbe *api_v1.Probe
}

// If given an interface which has JSONified data of type Probe
// this function will read the interface and give concrete
// data strcuture pointer.
func interfaceToProbe(i interface{}) (*api_v1.Probe, error) {
i = util.InterfaceToJSON(i)

var b []byte
var err error
if b, err = json.Marshal(i); err != nil {
return nil, fmt.Errorf("error: marshalling interface to bytes: %v", err)
}
var p api_v1.Probe
if err = json.Unmarshal(b, &p); err != nil {
return nil, fmt.Errorf("error: unmarshalling bytes to Probe: %v", err)
}
return &p, nil
}

func (h *Health) UnmarshalYAML(unmarshal func(interface{}) error) error {
type HealthAlias Health
var st struct {
HealthAlias `yaml:",inline"`
Leftovers map[string]interface{} `yaml:",inline"` // Catches all undefined fields and must be empty after parsing.
}

err := unmarshal(&st)
if err != nil {
return err
}

if len(st.Leftovers) > 0 {
return util.NewExcessKeysErrorFromMap("Health", st.Leftovers)
}

*h = Health(st.HealthAlias)

// extract the data from interface into concrete data type 'Probe'
if h.ReadinessProbeData != nil {
h.ReadinessProbe, err = interfaceToProbe(h.ReadinessProbeData)
if err != nil {
return fmt.Errorf("readinessProbe: %v", err)
}
h.ReadinessProbeData = interface{}(nil)
}

// extract the data from interface into concrete data type 'Probe'
if h.LivenessProbeData != nil {
h.LivenessProbe, err = interfaceToProbe(h.LivenessProbeData)
if err != nil {
return fmt.Errorf("livenessProbe: %v", err)
}
h.LivenessProbeData = interface{}(nil)
}

// TODO: Right now we have no way of finding if the excess keys are given
// by the user, since we are doing the whole conversion from YAML to JSON
// and then parsing it into the internal k8s structs

return nil
}

type Container struct {
Image ImageRef `yaml:"image"`
Env []EnvVariable `yaml:"env,omitempty"`
Ports []Port `yaml:"ports,omitempty"`
Mounts []Mount `yaml:"mounts,omitempty"`
Health *Health `yaml:"health,omitempty"`
}

func (c *Container) UnmarshalYAML(unmarshal func(interface{}) error) error {
Expand Down Expand Up @@ -443,6 +520,11 @@ func (d *Decoder) Decode(data []byte) (*object.OpenCompose, error) {
oc.Mounts = append(oc.Mounts, mount)
}

if c.Health != nil {
oc.Health.LivenessProbe = c.Health.LivenessProbe
oc.Health.ReadinessProbe = c.Health.ReadinessProbe
}

// convert env
for _, e := range c.Env {
oc.Environment = append(oc.Environment, object.EnvVariable{
Expand Down
Loading