-
Notifications
You must be signed in to change notification settings - Fork 12
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
||
volumes: | ||
- name: database | ||
size: 100Mi | ||
accessMode: ReadWriteOnce |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package v1 | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
|
@@ -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 ( | ||
|
@@ -219,11 +222,85 @@ func (m *Mount) UnmarshalYAML(unmarshal func(interface{}) error) error { | |
return nil | ||
} | ||
|
||
type Health struct { | ||
// Data holder for ReadinessProbe while parsing | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs a bit more explaining. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm still missing WHY part of 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 { | ||
|
@@ -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{ | ||
|
There was a problem hiding this comment.
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!