k8s runtime - get status from pods (#1283)
This commit is contained in:
		| @@ -48,12 +48,20 @@ func (k *kubernetes) getService(labels map[string]string) ([]*runtime.Service, e | |||||||
| 		Kind:  "deployment", | 		Kind:  "deployment", | ||||||
| 		Value: depList, | 		Value: depList, | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// get the deployment from k8s |  | ||||||
| 	if err := k.client.Get(d, labels); err != nil { | 	if err := k.client.Get(d, labels); err != nil { | ||||||
| 		return nil, err | 		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 | 	// service map | ||||||
| 	svcMap := make(map[string]*runtime.Service) | 	svcMap := make(map[string]*runtime.Service) | ||||||
|  |  | ||||||
| @@ -107,37 +115,22 @@ func (k *kubernetes) getService(labels map[string]string) ([]*runtime.Service, e | |||||||
| 				svc.Metadata[k] = v | 				svc.Metadata[k] = v | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			// parse out deployment status and inject into service metadata | 			// get the status from the pods | ||||||
| 			if len(kdep.Status.Conditions) > 0 { | 			status := "unknown" | ||||||
| 				var status string | 			if len(podList.Items) > 0 { | ||||||
| 				switch kdep.Status.Conditions[0].Type { | 				switch podList.Items[0].Status.Conditions[0].Type { | ||||||
| 				case "Available": | 				case "PodScheduled": | ||||||
| 					status = "running" |  | ||||||
| 					delete(svc.Metadata, "error") |  | ||||||
| 				case "Progressing": |  | ||||||
| 					status = "starting" | 					status = "starting" | ||||||
| 					delete(svc.Metadata, "error") | 				case "Initialized": | ||||||
| 				default: | 					status = "starting" | ||||||
| 					status = "error" | 				case "Ready": | ||||||
| 					svc.Metadata["error"] = kdep.Status.Conditions[0].Message | 					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 |  | ||||||
| 			} | 			} | ||||||
|  | 			log.Debugf("Runtime setting %s service deployment status: %v", name, status) | ||||||
| 			// parse out deployment build | 			svc.Metadata["status"] = status | ||||||
| 			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()) |  | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
| @@ -103,8 +103,16 @@ type Pod struct { | |||||||
|  |  | ||||||
| // PodStatus | // PodStatus | ||||||
| type PodStatus struct { | type PodStatus struct { | ||||||
| 	PodIP string `json:"podIP"` | 	PodIP      string         `json:"podIP"` | ||||||
| 	Phase string `json:"phase"` | 	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 | // Resource is API resource | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user