1
0
mirror of https://github.com/rancher/types.git synced 2025-06-20 02:41:53 +00:00
types/status/status.go

210 lines
4.4 KiB
Go
Raw Normal View History

2017-11-15 23:59:47 +00:00
package status
import (
"strings"
2017-12-04 23:42:18 +00:00
"time"
2017-11-15 23:59:47 +00:00
"github.com/rancher/norman/types/convert"
2017-11-29 21:38:39 +00:00
"github.com/rancher/norman/types/values"
2017-11-15 23:59:47 +00:00
)
type condition struct {
Type string
Status string
Message string
}
2018-01-12 10:01:35 +00:00
// True ==
// False == error
// Unknown == transitioning
var transitioningMap = map[string]string{
2018-01-16 19:55:19 +00:00
"Active": "activating",
2018-01-12 10:01:35 +00:00
"AgentInstalled": "installing",
"Available": "activating",
"BackingNamespaceCreated": "configuring",
"ConfigOK": "configuring",
"CreatorMadeOwner": "configuring",
"DefaultNamespaceAssigned": "configuring",
"DefaultProjectCreated": "configuring",
2018-01-16 19:55:19 +00:00
"Downloaded": "downloading",
2018-01-17 01:06:45 +00:00
"Inactive": "deactivating",
2018-01-12 10:01:35 +00:00
"Initialized": "initializing",
"MachinesCreated": "provisioning",
"PodScheduled": "scheduling",
"Progressing": "updating",
"Provisioned": "provisioning",
2018-01-22 23:45:34 +00:00
"Registered": "waiting",
2018-01-12 10:01:35 +00:00
"Removed": "removing",
"Saved": "saving",
"Updated": "updating",
"Updating": "updating",
}
// True == error
// False ==
// Unknown ==
var reverseErrorMap = map[string]bool{
"OutOfDisk": true,
"MemoryPressure": true,
"DiskPressure": true,
"NetworkUnavailable": true,
"KernelHasNoDeadlock": true,
"Unschedulable": true,
"ReplicaFailure": true,
}
// True ==
// False == error
// Unknown ==
var errorMapping = map[string]bool{
"Failed": true,
}
// True ==
// False == transitioning
// Unknown ==
var doneMap = map[string]string{
"Completed": "activating",
"Ready": "unavailable",
}
func concat(str, next string) string {
if str == "" {
return next
}
if next == "" {
return str
}
return str + ", " + next
2017-11-15 23:59:47 +00:00
}
func Set(data map[string]interface{}) {
if data == nil {
return
}
2017-12-04 23:42:18 +00:00
val, ok := values.GetValue(data, "metadata", "removed")
if ok && val != "" && val != nil {
data["state"] = "removing"
data["transitioning"] = "yes"
finalizers, ok := values.GetStringSlice(data, "metadata", "finalizers")
if ok && len(finalizers) > 0 {
data["transitioningMessage"] = "Waiting on " + finalizers[0]
if i, err := convert.ToTimestamp(val); err == nil {
if time.Unix(i/1000, 0).Add(5 * time.Minute).Before(time.Now()) {
data["transitioning"] = "error"
}
}
}
return
}
val, ok = values.GetValue(data, "status", "conditions")
2017-11-15 23:59:47 +00:00
var conditions []condition
if err := convert.ToObj(val, &conditions); err != nil {
// ignore error
return
}
state := ""
error := false
transitioning := false
message := ""
2018-01-12 10:01:35 +00:00
for _, c := range conditions {
if errorMapping[c.Type] && c.Status == "False" {
error = true
message = c.Message
break
2017-11-15 23:59:47 +00:00
}
2018-01-12 10:01:35 +00:00
}
2017-11-15 23:59:47 +00:00
2018-01-12 10:01:35 +00:00
if !error {
for _, c := range conditions {
if reverseErrorMap[c.Type] && c.Status == "True" {
error = true
message = concat(message, c.Message)
2017-12-27 16:48:28 +00:00
}
2017-11-15 23:59:47 +00:00
}
2018-01-12 10:01:35 +00:00
}
2017-11-15 23:59:47 +00:00
2018-01-12 10:01:35 +00:00
for _, c := range conditions {
newState, ok := transitioningMap[c.Type]
if !ok {
continue
2017-11-15 23:59:47 +00:00
}
2018-01-12 10:01:35 +00:00
if c.Status == "False" {
2017-11-15 23:59:47 +00:00
error = true
2018-01-12 10:01:35 +00:00
state = newState
message = concat(message, c.Message)
} else if c.Status == "Unknown" && state == "" {
transitioning = true
state = newState
message = concat(message, c.Message)
}
}
for _, c := range conditions {
if state != "" {
break
}
newState, ok := doneMap[c.Type]
if !ok {
continue
}
if c.Status == "False" {
transitioning = true
state = newState
message = concat(message, c.Message)
2017-11-15 23:59:47 +00:00
}
}
if state == "" {
val, ok := values.GetValue(data, "spec", "active")
if ok {
if convert.ToBool(val) {
state = "active"
} else {
state = "inactive"
}
}
}
if state == "" {
2018-01-12 10:01:35 +00:00
val, ok := values.GetValueN(data, "status", "phase").(string)
if val != "" && ok {
state = val
}
}
if state == "" && len(conditions) == 0 {
if val, ok := values.GetValue(data, "metadata", "created"); ok {
if i, err := convert.ToTimestamp(val); err == nil {
2018-01-12 10:01:35 +00:00
if time.Unix(i/1000, 0).Add(5 * time.Second).After(time.Now()) {
state = "initializing"
transitioning = true
}
2017-12-14 20:49:44 +00:00
}
}
}
2017-12-14 20:49:44 +00:00
if state == "" {
state = "active"
2017-11-15 23:59:47 +00:00
}
if error {
data["transitioning"] = "error"
} else if transitioning {
data["transitioning"] = "yes"
} else {
data["transitioning"] = "no"
}
2018-01-17 01:06:45 +00:00
data["state"] = strings.ToLower(state)
2017-11-15 23:59:47 +00:00
data["transitioningMessage"] = message
}