normalise runtime service status

This commit is contained in:
Asim Aslam 2020-01-10 21:54:28 +00:00
parent 61dd2b8489
commit e1e6199743
3 changed files with 52 additions and 1 deletions

View File

@ -114,7 +114,18 @@ func (k *kubernetes) getService(labels map[string]string) ([]*runtime.Service, e
// parse out deployment status and inject into service metadata // parse out deployment status and inject into service metadata
if len(kdep.Status.Conditions) > 0 { if len(kdep.Status.Conditions) > 0 {
status := kdep.Status.Conditions[0].Type var status string
switch kdep.Status.Conditions[0].Type {
case "Available":
status = "running"
delete(svc.Metadata, "error")
case "Progressing":
status = "starting"
delete(svc.Metadata, "error")
default:
status = "error"
svc.Metadata["error"] = kdep.Status.Conditions[0].Message
}
// pick the last known condition type and mark the service status with it // pick the last known condition type and mark the service status with it
log.Debugf("Runtime setting %s service deployment status: %v", name, status) log.Debugf("Runtime setting %s service deployment status: %v", name, status)
svc.Metadata["status"] = status svc.Metadata["status"] = status

View File

@ -89,14 +89,18 @@ func (s *service) Start(k client.Client) error {
// create deployment first; if we fail, we dont create service // create deployment first; if we fail, we dont create service
if err := k.Create(deploymentResource(s.kdeploy)); err != nil { if err := k.Create(deploymentResource(s.kdeploy)); err != nil {
log.Debugf("Runtime failed to create deployment: %v", err) log.Debugf("Runtime failed to create deployment: %v", err)
s.Status("error", err)
return err return err
} }
// create service now that the deployment has been created // create service now that the deployment has been created
if err := k.Create(serviceResource(s.kservice)); err != nil { if err := k.Create(serviceResource(s.kservice)); err != nil {
log.Debugf("Runtime failed to create service: %v", err) log.Debugf("Runtime failed to create service: %v", err)
s.Status("error", err)
return err return err
} }
s.Status("started", nil)
return nil return nil
} }
@ -104,20 +108,25 @@ func (s *service) Stop(k client.Client) error {
// first attempt to delete service // first attempt to delete service
if err := k.Delete(serviceResource(s.kservice)); err != nil { if err := k.Delete(serviceResource(s.kservice)); err != nil {
log.Debugf("Runtime failed to delete service: %v", err) log.Debugf("Runtime failed to delete service: %v", err)
s.Status("error", err)
return err return err
} }
// delete deployment once the service has been deleted // delete deployment once the service has been deleted
if err := k.Delete(deploymentResource(s.kdeploy)); err != nil { if err := k.Delete(deploymentResource(s.kdeploy)); err != nil {
log.Debugf("Runtime failed to delete deployment: %v", err) log.Debugf("Runtime failed to delete deployment: %v", err)
s.Status("error", err)
return err return err
} }
s.Status("stopped", nil)
return nil return nil
} }
func (s *service) Update(k client.Client) error { func (s *service) Update(k client.Client) error {
if err := k.Update(deploymentResource(s.kdeploy)); err != nil { if err := k.Update(deploymentResource(s.kdeploy)); err != nil {
log.Debugf("Runtime failed to update deployment: %v", err) log.Debugf("Runtime failed to update deployment: %v", err)
s.Status("error", err)
return err return err
} }
if err := k.Update(serviceResource(s.kservice)); err != nil { if err := k.Update(serviceResource(s.kservice)); err != nil {
@ -127,3 +136,12 @@ func (s *service) Update(k client.Client) error {
return nil return nil
} }
func (s *service) Status(status string, err error) {
if err == nil {
s.Metadata["status"] = status
return
}
s.Metadata["status"] = "error"
s.Metadata["error"] = err.Error()
}

View File

@ -86,10 +86,20 @@ func (s *service) Start() error {
s.err = nil s.err = nil
s.closed = make(chan bool) s.closed = make(chan bool)
if s.Metadata == nil {
s.Metadata = make(map[string]string)
}
s.Metadata["status"] = "starting"
// delete any existing error
delete(s.Metadata, "error")
// TODO: pull source & build binary // TODO: pull source & build binary
log.Debugf("Runtime service %s forking new process", s.Service.Name) log.Debugf("Runtime service %s forking new process", s.Service.Name)
p, err := s.Process.Fork(s.Exec) p, err := s.Process.Fork(s.Exec)
if err != nil { if err != nil {
s.Metadata["status"] = "error"
s.Metadata["error"] = err.Error()
return err return err
} }
@ -97,6 +107,8 @@ func (s *service) Start() error {
s.PID = p s.PID = p
// set to running // set to running
s.running = true s.running = true
// set status
s.Metadata["status"] = "running"
if s.output != nil { if s.output != nil {
s.streamOutput() s.streamOutput()
@ -122,10 +134,18 @@ func (s *service) Stop() error {
if s.PID == nil { if s.PID == nil {
return nil return nil
} }
// set status
s.Metadata["status"] = "stopping"
// kill the process // kill the process
err := s.Process.Kill(s.PID) err := s.Process.Kill(s.PID)
// wait for it to exit // wait for it to exit
s.Process.Wait(s.PID) s.Process.Wait(s.PID)
// set status
s.Metadata["status"] = "stopped"
// return the kill error // return the kill error
return err return err
} }
@ -148,6 +168,8 @@ func (s *service) Wait() {
// save the error // save the error
if err != nil { if err != nil {
s.Metadata["status"] = "error"
s.Metadata["error"] = err.Error()
s.err = err s.err = err
} }