Merge pull request #137470 from brianpursley/fix-wait-panic

Check condition cast to avoid potential panic in kubectl wait
This commit is contained in:
Kubernetes Prow Robot
2026-03-13 06:15:48 +05:30
committed by GitHub
2 changed files with 48 additions and 1 deletions

View File

@@ -59,7 +59,10 @@ func (w ConditionalWait) checkCondition(obj *unstructured.Unstructured) (bool, e
return false, nil
}
for _, conditionUncast := range conditions {
condition := conditionUncast.(map[string]interface{})
condition, ok := conditionUncast.(map[string]interface{})
if !ok {
continue
}
name, found, err := unstructured.NestedString(condition, "type")
if !found || err != nil || !strings.EqualFold(name, w.conditionName) {
continue

View File

@@ -1924,3 +1924,47 @@ func TestWaitForMultipleConditions(t *testing.T) {
})
}
}
func TestConditionalWaitCheckConditionHandlesMalformedConditions(t *testing.T) {
tests := []struct {
name string
conditions []interface{}
expectMet bool
}{
{
name: "single malformed condition item",
conditions: []interface{}{
"not-a-map",
},
expectMet: false,
},
{
name: "malformed condition followed by matching condition",
conditions: []interface{}{
"not-a-map",
map[string]interface{}{
"type": "Ready",
"status": "True",
},
},
expectMet: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
obj := newUnstructured("group/version", "TheKind", "ns-foo", "name-foo")
require.NoError(t, unstructured.SetNestedSlice(obj.Object, tc.conditions, "status", "conditions"))
w := ConditionalWait{
conditionName: "Ready",
conditionStatus: "True",
errOut: io.Discard,
}
met, err := w.checkCondition(obj)
require.NoError(t, err)
require.Equal(t, tc.expectMet, met)
})
}
}