|
| 1 | +package condition |
| 2 | + |
| 3 | +// adapted from rancher/wrangler |
| 4 | + |
| 5 | +import ( |
| 6 | + "reflect" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/sirupsen/logrus" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | +) |
| 12 | + |
| 13 | +type Cond string |
| 14 | + |
| 15 | +func (c Cond) GetStatus(obj interface{}) string { |
| 16 | + return getStatus(obj, string(c)) |
| 17 | +} |
| 18 | + |
| 19 | +func (c Cond) SetError(obj interface{}, reason string, err error) { |
| 20 | + if err == nil { |
| 21 | + c.True(obj) |
| 22 | + c.Message(obj, "") |
| 23 | + c.Reason(obj, reason) |
| 24 | + return |
| 25 | + } |
| 26 | + if reason == "" { |
| 27 | + reason = "Error" |
| 28 | + } |
| 29 | + c.False(obj) |
| 30 | + c.Message(obj, err.Error()) |
| 31 | + c.Reason(obj, reason) |
| 32 | +} |
| 33 | + |
| 34 | +func (c Cond) MatchesError(obj interface{}, reason string, err error) bool { |
| 35 | + if err == nil { |
| 36 | + return c.IsTrue(obj) && |
| 37 | + c.GetMessage(obj) == "" && |
| 38 | + c.GetReason(obj) == reason |
| 39 | + } |
| 40 | + if reason == "" { |
| 41 | + reason = "Error" |
| 42 | + } |
| 43 | + return c.IsFalse(obj) && |
| 44 | + c.GetMessage(obj) == err.Error() && |
| 45 | + c.GetReason(obj) == reason |
| 46 | +} |
| 47 | + |
| 48 | +func (c Cond) SetStatus(obj interface{}, status string) { |
| 49 | + setStatus(obj, string(c), status) |
| 50 | +} |
| 51 | + |
| 52 | +func (c Cond) SetStatusBool(obj interface{}, val bool) { |
| 53 | + if val { |
| 54 | + setStatus(obj, string(c), "True") |
| 55 | + } else { |
| 56 | + setStatus(obj, string(c), "False") |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func (c Cond) True(obj interface{}) { |
| 61 | + setStatus(obj, string(c), "True") |
| 62 | +} |
| 63 | + |
| 64 | +func (c Cond) IsTrue(obj interface{}) bool { |
| 65 | + return getStatus(obj, string(c)) == "True" |
| 66 | +} |
| 67 | + |
| 68 | +func (c Cond) False(obj interface{}) { |
| 69 | + setStatus(obj, string(c), "False") |
| 70 | +} |
| 71 | + |
| 72 | +func (c Cond) IsFalse(obj interface{}) bool { |
| 73 | + return getStatus(obj, string(c)) == "False" |
| 74 | +} |
| 75 | + |
| 76 | +func (c Cond) Unknown(obj interface{}) { |
| 77 | + setStatus(obj, string(c), "Unknown") |
| 78 | +} |
| 79 | + |
| 80 | +func (c Cond) IsUnknown(obj interface{}) bool { |
| 81 | + return getStatus(obj, string(c)) == "Unknown" |
| 82 | +} |
| 83 | + |
| 84 | +func (c Cond) LastUpdated(obj interface{}, ts string) { |
| 85 | + setTS(obj, string(c), ts) |
| 86 | +} |
| 87 | + |
| 88 | +func (c Cond) GetLastUpdated(obj interface{}) string { |
| 89 | + return getTS(obj, string(c)) |
| 90 | +} |
| 91 | + |
| 92 | +func (c Cond) GetLastTransitionTime(obj interface{}) time.Time { |
| 93 | + return getLastTransitionTime(obj, string(c)) |
| 94 | +} |
| 95 | + |
| 96 | +func (c Cond) CreateUnknownIfNotExists(obj interface{}) { |
| 97 | + condSlice := getValue(obj, "Status", "Conditions") |
| 98 | + cond := findCond(obj, condSlice, string(c)) |
| 99 | + if cond == nil { |
| 100 | + c.Unknown(obj) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func (c Cond) Reason(obj interface{}, reason string) { |
| 105 | + cond := findOrCreateCond(obj, string(c)) |
| 106 | + getFieldValue(cond, "Reason").SetString(reason) |
| 107 | +} |
| 108 | + |
| 109 | +func (c Cond) GetReason(obj interface{}) string { |
| 110 | + cond := findOrNotCreateCond(obj, string(c)) |
| 111 | + if cond == nil { |
| 112 | + return "" |
| 113 | + } |
| 114 | + return getFieldValue(*cond, "Reason").String() |
| 115 | +} |
| 116 | + |
| 117 | +func (c Cond) SetMessageIfBlank(obj interface{}, message string) { |
| 118 | + if c.GetMessage(obj) == "" { |
| 119 | + c.Message(obj, message) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func (c Cond) Message(obj interface{}, message string) { |
| 124 | + cond := findOrCreateCond(obj, string(c)) |
| 125 | + setValue(cond, "Message", message) |
| 126 | +} |
| 127 | + |
| 128 | +func (c Cond) GetMessage(obj interface{}) string { |
| 129 | + cond := findOrNotCreateCond(obj, string(c)) |
| 130 | + if cond == nil { |
| 131 | + return "" |
| 132 | + } |
| 133 | + return getFieldValue(*cond, "Message").String() |
| 134 | +} |
| 135 | + |
| 136 | +func touchTS(value reflect.Value) { |
| 137 | + now := time.Now().Format(time.RFC3339) |
| 138 | + getFieldValue(value, "LastUpdateTime").SetString(now) |
| 139 | +} |
| 140 | + |
| 141 | +func getStatus(obj interface{}, condName string) string { |
| 142 | + cond := findOrNotCreateCond(obj, condName) |
| 143 | + if cond == nil { |
| 144 | + return "" |
| 145 | + } |
| 146 | + return getFieldValue(*cond, "Status").String() |
| 147 | +} |
| 148 | + |
| 149 | +func setTS(obj interface{}, condName, ts string) { |
| 150 | + cond := findOrCreateCond(obj, condName) |
| 151 | + getFieldValue(cond, "LastUpdateTime").SetString(ts) |
| 152 | +} |
| 153 | + |
| 154 | +func getTS(obj interface{}, condName string) string { |
| 155 | + cond := findOrNotCreateCond(obj, condName) |
| 156 | + if cond == nil { |
| 157 | + return "" |
| 158 | + } |
| 159 | + return getFieldValue(*cond, "LastUpdateTime").String() |
| 160 | +} |
| 161 | + |
| 162 | +func getLastTransitionTime(obj interface{}, condName string) time.Time { |
| 163 | + cond := findOrNotCreateCond(obj, condName) |
| 164 | + if cond == nil { |
| 165 | + return time.Time{} |
| 166 | + } |
| 167 | + value := getFieldValue(*cond, "LastTransitionTime").Interface() |
| 168 | + if value == nil { |
| 169 | + return time.Time{} |
| 170 | + } |
| 171 | + return value.(metav1.Time).Time |
| 172 | +} |
| 173 | + |
| 174 | +func setStatus(obj interface{}, condName, status string) { |
| 175 | + cond := findOrCreateCond(obj, condName) |
| 176 | + setValue(cond, "Status", status) |
| 177 | +} |
| 178 | + |
| 179 | +func setValue(cond reflect.Value, fieldName, newValue string) { |
| 180 | + value := getFieldValue(cond, fieldName) |
| 181 | + if value.String() != newValue { |
| 182 | + value.SetString(newValue) |
| 183 | + touchTS(cond) |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +func findOrNotCreateCond(obj interface{}, condName string) *reflect.Value { |
| 188 | + condSlice := getValue(obj, "Status", "Conditions") |
| 189 | + return findCond(obj, condSlice, condName) |
| 190 | +} |
| 191 | + |
| 192 | +func findOrCreateCond(obj interface{}, condName string) reflect.Value { |
| 193 | + condSlice := getValue(obj, "Status", "Conditions") |
| 194 | + if !condSlice.IsValid() { |
| 195 | + condSlice = getValue(obj, "Conditions") |
| 196 | + } |
| 197 | + cond := findCond(obj, condSlice, condName) |
| 198 | + if cond != nil { |
| 199 | + return *cond |
| 200 | + } |
| 201 | + |
| 202 | + newCond := reflect.New(condSlice.Type().Elem()).Elem() |
| 203 | + newCond.FieldByName("Type").SetString(condName) |
| 204 | + newCond.FieldByName("Status").SetString("Unknown") |
| 205 | + condSlice.Set(reflect.Append(condSlice, newCond)) |
| 206 | + return *findCond(obj, condSlice, condName) |
| 207 | +} |
| 208 | + |
| 209 | +func findCond(obj interface{}, val reflect.Value, name string) *reflect.Value { |
| 210 | + defer func() { |
| 211 | + if recover() != nil { |
| 212 | + logrus.Fatalf("failed to find .Status.Conditions field on %v", reflect.TypeOf(obj)) |
| 213 | + } |
| 214 | + }() |
| 215 | + |
| 216 | + for i := 0; i < val.Len(); i++ { |
| 217 | + cond := val.Index(i) |
| 218 | + typeVal := getFieldValue(cond, "Type") |
| 219 | + if typeVal.String() == name { |
| 220 | + return &cond |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + return nil |
| 225 | +} |
| 226 | + |
| 227 | +func getValue(obj interface{}, name ...string) reflect.Value { |
| 228 | + if obj == nil { |
| 229 | + return reflect.Value{} |
| 230 | + } |
| 231 | + v := reflect.ValueOf(obj) |
| 232 | + t := v.Type() |
| 233 | + if t.Kind() == reflect.Ptr { |
| 234 | + v = v.Elem() |
| 235 | + t = v.Type() |
| 236 | + } |
| 237 | + |
| 238 | + field := v.FieldByName(name[0]) |
| 239 | + if len(name) == 1 { |
| 240 | + return field |
| 241 | + } |
| 242 | + return getFieldValue(field, name[1:]...) |
| 243 | +} |
| 244 | + |
| 245 | +func getFieldValue(v reflect.Value, name ...string) reflect.Value { |
| 246 | + field := v.FieldByName(name[0]) |
| 247 | + if len(name) == 1 { |
| 248 | + return field |
| 249 | + } |
| 250 | + return getFieldValue(field, name[1:]...) |
| 251 | +} |
| 252 | + |
| 253 | +func Error(reason string, err error) error { |
| 254 | + return &conditionError{ |
| 255 | + reason: reason, |
| 256 | + message: err.Error(), |
| 257 | + } |
| 258 | +} |
| 259 | + |
| 260 | +type conditionError struct { |
| 261 | + reason string |
| 262 | + message string |
| 263 | +} |
| 264 | + |
| 265 | +func (e *conditionError) Error() string { |
| 266 | + return e.message |
| 267 | +} |
0 commit comments