Skip to content

Commit b5e94a5

Browse files
authored
Merge pull request #84 from flanksource/fix-pod-handling
Fix pod ready handling
2 parents 21c1a49 + bdb85ea commit b5e94a5

File tree

3 files changed

+63
-11
lines changed

3 files changed

+63
-11
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
.idea/
1+
.idea/
2+
.vscode

utils.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,10 @@ func IsNode(obj *unstructured.Unstructured) bool {
420420
return obj.GetKind() == "Node"
421421
}
422422

423+
func IsPod(obj *unstructured.Unstructured) bool {
424+
return obj.GetKind() == "Pod"
425+
}
426+
423427
func NewDeployment(ns, name, image string, labels map[string]string, port int32, args ...string) *apps.Deployment {
424428
if labels == nil {
425429
labels = make(map[string]string)

wait.go

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ func (c *Client) IsReady(item *unstructured.Unstructured) (bool, string) {
224224
c.Debugf("[%s] checking readiness", GetName(item))
225225

226226
switch {
227+
case IsPod(item):
228+
return IsPodReadyAndRunning(item)
227229
case IsSecret(item) || IsConfigMap(item):
228230
return IsDataContainerReady(item)
229231
case IsService(item):
@@ -253,7 +255,35 @@ func (c *Client) IsReady(item *unstructured.Unstructured) (bool, string) {
253255
case IsNode(item):
254256
return IsNodeReady(item)
255257
}
258+
if item.Object["status"] == nil {
259+
return false, "⏳ waiting to become ready"
260+
}
261+
262+
status := item.Object["status"].(map[string]interface{})
263+
264+
if _, found := status["conditions"]; !found {
265+
return false, "⏳ waiting to become ready"
266+
}
267+
268+
conditions := status["conditions"].([]interface{})
269+
if len(conditions) == 0 {
270+
return false, "⏳ waiting to become ready"
271+
}
256272

273+
failureTypesSuffix := []string{"Failed", "NotReady", "Pressure"}
274+
for _, raw := range conditions {
275+
condition := raw.(map[string]interface{})
276+
if condition["type"] == "Ready" && condition["status"] == "True" {
277+
return true, ""
278+
}
279+
if anySuffixesInItem(failureTypesSuffix, condition["type"].(string)) && condition["status"] != "False" {
280+
return false, fmt.Sprintf("⏳ waiting for %s/%s: %s", condition["type"], condition["status"], condition["reason"])
281+
}
282+
}
283+
return true, ""
284+
}
285+
286+
func IsPodReadyAndRunning(item *unstructured.Unstructured) (bool, string) {
257287
if item.Object["status"] == nil {
258288
return false, "⏳ waiting to become ready"
259289
}
@@ -268,21 +298,19 @@ func (c *Client) IsReady(item *unstructured.Unstructured) (bool, string) {
268298
if len(conditions) == 0 {
269299
return false, "⏳ waiting to become ready"
270300
}
301+
var successTypes = []string{"Ready", "Initialized", "ContainersReady", "PodScheduled"}
271302
for _, raw := range conditions {
272303
condition := raw.(map[string]interface{})
273-
trueIsGood := false
304+
successStatus := false
274305

275-
if condition["type"] == "Ready" {
276-
trueIsGood = true
277-
}
278-
if strings.HasSuffix("initialized", strings.ToLower(condition["type"].(string))) {
279-
trueIsGood = true
306+
if sliceContains(successTypes, condition["type"].(string)) {
307+
successStatus = true
280308
}
281-
if !trueIsGood && condition["status"] != "False" {
282-
return false, fmt.Sprintf("⏳ waiting for %s/%s: %s", condition["type"], condition["status"], condition["message"])
309+
if successStatus && condition["status"] != "True" {
310+
return false, fmt.Sprintf("⏳ waiting for %s/%s: %s", condition["type"], condition["status"], condition["reason"])
283311
}
284-
if trueIsGood && condition["status"] != "True" {
285-
return false, fmt.Sprintf("⏳ waiting for %s/%s: %s", condition["type"], condition["status"], condition["message"])
312+
if !successStatus && condition["status"] != "False" {
313+
return false, fmt.Sprintf("⏳ waiting for %s/%s: %s", condition["type"], condition["status"], condition["reason"])
286314
}
287315
}
288316
return true, ""
@@ -861,3 +889,22 @@ func (c *Client) WaitForPodCommand(ns, name string, container string, timeout ti
861889
time.Sleep(5 * time.Second)
862890
}
863891
}
892+
893+
func itemInList(items []string, ptr string) bool {
894+
for _, item := range items {
895+
if item == ptr {
896+
return true
897+
}
898+
}
899+
return false
900+
}
901+
902+
// checks if any suffix on the given list of suffixes is present on the item
903+
func anySuffixesInItem(suffixes []string, item string) bool {
904+
for _, suffix := range suffixes {
905+
if strings.HasSuffix(item, suffix) {
906+
return true
907+
}
908+
}
909+
return false
910+
}

0 commit comments

Comments
 (0)