From e1e6199743358bfca59bde02bee84b736d446c0e Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Fri, 10 Jan 2020 21:54:28 +0000 Subject: [PATCH] normalise runtime service status --- runtime/kubernetes/kubernetes.go | 13 ++++++++++++- runtime/kubernetes/service.go | 18 ++++++++++++++++++ runtime/service.go | 22 ++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/runtime/kubernetes/kubernetes.go b/runtime/kubernetes/kubernetes.go index d7dd472f..2b3b479f 100644 --- a/runtime/kubernetes/kubernetes.go +++ b/runtime/kubernetes/kubernetes.go @@ -114,7 +114,18 @@ func (k *kubernetes) getService(labels map[string]string) ([]*runtime.Service, e // parse out deployment status and inject into service metadata 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 log.Debugf("Runtime setting %s service deployment status: %v", name, status) svc.Metadata["status"] = status diff --git a/runtime/kubernetes/service.go b/runtime/kubernetes/service.go index b74eb7f2..f939b924 100644 --- a/runtime/kubernetes/service.go +++ b/runtime/kubernetes/service.go @@ -89,14 +89,18 @@ func (s *service) Start(k client.Client) error { // create deployment first; if we fail, we dont create service if err := k.Create(deploymentResource(s.kdeploy)); err != nil { log.Debugf("Runtime failed to create deployment: %v", err) + s.Status("error", err) return err } // create service now that the deployment has been created if err := k.Create(serviceResource(s.kservice)); err != nil { log.Debugf("Runtime failed to create service: %v", err) + s.Status("error", err) return err } + s.Status("started", nil) + return nil } @@ -104,20 +108,25 @@ func (s *service) Stop(k client.Client) error { // first attempt to delete service if err := k.Delete(serviceResource(s.kservice)); err != nil { log.Debugf("Runtime failed to delete service: %v", err) + s.Status("error", err) return err } // delete deployment once the service has been deleted if err := k.Delete(deploymentResource(s.kdeploy)); err != nil { log.Debugf("Runtime failed to delete deployment: %v", err) + s.Status("error", err) return err } + s.Status("stopped", nil) + return nil } func (s *service) Update(k client.Client) error { if err := k.Update(deploymentResource(s.kdeploy)); err != nil { log.Debugf("Runtime failed to update deployment: %v", err) + s.Status("error", err) return err } if err := k.Update(serviceResource(s.kservice)); err != nil { @@ -127,3 +136,12 @@ func (s *service) Update(k client.Client) error { 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() +} diff --git a/runtime/service.go b/runtime/service.go index 75e96fbf..c2b89394 100644 --- a/runtime/service.go +++ b/runtime/service.go @@ -86,10 +86,20 @@ func (s *service) Start() error { s.err = nil 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 log.Debugf("Runtime service %s forking new process", s.Service.Name) p, err := s.Process.Fork(s.Exec) if err != nil { + s.Metadata["status"] = "error" + s.Metadata["error"] = err.Error() return err } @@ -97,6 +107,8 @@ func (s *service) Start() error { s.PID = p // set to running s.running = true + // set status + s.Metadata["status"] = "running" if s.output != nil { s.streamOutput() @@ -122,10 +134,18 @@ func (s *service) Stop() error { if s.PID == nil { return nil } + + // set status + s.Metadata["status"] = "stopping" + // kill the process err := s.Process.Kill(s.PID) // wait for it to exit s.Process.Wait(s.PID) + + // set status + s.Metadata["status"] = "stopped" + // return the kill error return err } @@ -148,6 +168,8 @@ func (s *service) Wait() { // save the error if err != nil { + s.Metadata["status"] = "error" + s.Metadata["error"] = err.Error() s.err = err }