2017-11-15 23:59:47 +00:00
|
|
|
package status
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"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 conditionMapping struct {
|
|
|
|
Name string
|
|
|
|
State string
|
|
|
|
Transition bool
|
|
|
|
Error bool
|
|
|
|
FalseIsGood bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type condition struct {
|
|
|
|
Type string
|
|
|
|
Status string
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
|
|
|
var conditionMappings = []conditionMapping{
|
|
|
|
{
|
|
|
|
Name: "Initialized",
|
|
|
|
Transition: true,
|
|
|
|
State: "initializing",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Ready",
|
|
|
|
Transition: true,
|
|
|
|
State: "activating",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Available",
|
|
|
|
Transition: true,
|
|
|
|
State: "activating",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Progressing",
|
|
|
|
Transition: true,
|
|
|
|
State: "updating",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "ConfigOK",
|
|
|
|
Transition: true,
|
|
|
|
State: "configuring",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "PodScheduled",
|
|
|
|
Transition: true,
|
|
|
|
State: "scheduling",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Completed",
|
|
|
|
State: "completed",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Failed",
|
|
|
|
Error: true,
|
|
|
|
State: "error",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "OutOfDisk",
|
|
|
|
Error: true,
|
|
|
|
FalseIsGood: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "MemoryPressure",
|
|
|
|
Error: true,
|
|
|
|
FalseIsGood: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "DiskPressure",
|
|
|
|
Error: true,
|
|
|
|
FalseIsGood: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "NetworkUnavailable",
|
|
|
|
Error: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Unschedulable",
|
|
|
|
Error: true,
|
|
|
|
FalseIsGood: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "ReplicaFailure",
|
|
|
|
Error: true,
|
|
|
|
FalseIsGood: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func Set(data map[string]interface{}) {
|
2017-11-29 21:38:39 +00:00
|
|
|
val, ok := values.GetValue(data, "status", "conditions")
|
2017-11-15 23:59:47 +00:00
|
|
|
if !ok || val == nil {
|
2017-11-29 21:38:39 +00:00
|
|
|
// TODO: remove
|
|
|
|
data["state"] = "active"
|
2017-11-15 23:59:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var conditions []condition
|
|
|
|
if err := convert.ToObj(val, &conditions); err != nil {
|
|
|
|
// ignore error
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
conditionMap := map[string]condition{}
|
|
|
|
for _, c := range conditions {
|
|
|
|
conditionMap[c.Type] = condition{
|
|
|
|
Status: c.Status,
|
|
|
|
Message: c.Message,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state := ""
|
|
|
|
error := false
|
|
|
|
transitioning := false
|
|
|
|
message := ""
|
|
|
|
|
|
|
|
for _, conditionMapping := range conditionMappings {
|
|
|
|
good := true
|
|
|
|
condition, ok := conditionMap[conditionMapping.Name]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if conditionMapping.FalseIsGood && condition.Status == "True" {
|
|
|
|
good = false
|
|
|
|
} else if !conditionMapping.FalseIsGood && condition.Status == "False" {
|
|
|
|
good = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !good && conditionMapping.Transition {
|
|
|
|
transitioning = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !good && state == "" && conditionMapping.State != "" {
|
|
|
|
state = conditionMapping.State
|
|
|
|
}
|
|
|
|
|
|
|
|
if !good && conditionMapping.Error {
|
|
|
|
error = true
|
|
|
|
if len(message) > 0 {
|
|
|
|
message = strings.Join([]string{message, condition.Message}, ",")
|
|
|
|
} else {
|
|
|
|
message = condition.Message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if state == "" {
|
2017-11-29 21:38:39 +00:00
|
|
|
val, _ := values.GetValueN(data, "status", "phase").(string)
|
2017-11-15 23:59:47 +00:00
|
|
|
if val != "" {
|
|
|
|
state = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if state == "" {
|
|
|
|
state = "active"
|
|
|
|
}
|
|
|
|
|
|
|
|
data["state"] = strings.ToLower(state)
|
|
|
|
if error {
|
|
|
|
data["transitioning"] = "error"
|
|
|
|
} else if transitioning {
|
|
|
|
data["transitioning"] = "yes"
|
|
|
|
} else {
|
|
|
|
data["transitioning"] = "no"
|
|
|
|
}
|
|
|
|
|
|
|
|
data["transitioningMessage"] = message
|
|
|
|
}
|