k8s runtime - get status from pods (#1283)

This commit is contained in:
ben-toogood 2020-03-02 15:49:10 +00:00 committed by GitHub
parent b555269b1b
commit 1f2e067f71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 32 deletions

View File

@ -48,12 +48,20 @@ func (k *kubernetes) getService(labels map[string]string) ([]*runtime.Service, e
Kind: "deployment",
Value: depList,
}
// get the deployment from k8s
if err := k.client.Get(d, labels); err != nil {
return nil, err
}
// get the pods from k8s
podList := new(client.PodList)
p := &client.Resource{
Kind: "pod",
Value: podList,
}
if err := k.client.Get(p, labels); err != nil {
return nil, err
}
// service map
svcMap := make(map[string]*runtime.Service)
@ -107,38 +115,23 @@ func (k *kubernetes) getService(labels map[string]string) ([]*runtime.Service, e
svc.Metadata[k] = v
}
// parse out deployment status and inject into service metadata
if len(kdep.Status.Conditions) > 0 {
var status string
switch kdep.Status.Conditions[0].Type {
case "Available":
status = "running"
delete(svc.Metadata, "error")
case "Progressing":
// get the status from the pods
status := "unknown"
if len(podList.Items) > 0 {
switch podList.Items[0].Status.Conditions[0].Type {
case "PodScheduled":
status = "starting"
delete(svc.Metadata, "error")
default:
status = "error"
svc.Metadata["error"] = kdep.Status.Conditions[0].Message
case "Initialized":
status = "starting"
case "Ready":
status = "ready"
case "ContainersReady":
status = "ready"
}
}
// 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
}
// parse out deployment build
if build, ok := kdep.Spec.Template.Metadata.Annotations["build"]; ok {
buildTime, err := time.Parse(time.RFC3339, build)
if err != nil {
log.Debugf("Runtime failed parsing build time for %s: %v", name, err)
continue
}
svc.Metadata["build"] = fmt.Sprintf("%d", buildTime.Unix())
continue
}
// if no build annotation is found, set it to current time
svc.Metadata["build"] = fmt.Sprintf("%d", time.Now().Unix())
}
}
// collect all the services and return

View File

@ -105,6 +105,14 @@ type Pod struct {
type PodStatus struct {
PodIP string `json:"podIP"`
Phase string `json:"phase"`
Conditions []PodCondition `json:"conditions,omitempty"`
}
// PodCondition describes the state of pod
type PodCondition struct {
Type string `json:"type"`
Reason string `json:"reason,omitempty"`
Message string `json:"message,omitempty"`
}
// Resource is API resource