mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-07-17 20:00:07 +00:00
Merge pull request #137470 from brianpursley/fix-wait-panic
Check condition cast to avoid potential panic in kubectl wait
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user