Skip to content

Commit 3d52b8b

Browse files
authored
Merge pull request kubernetes#90969 from zhouya0/kubectl_wait_add_ignore_not_found
Ignore not found in `kubectl wait --for=delete`
2 parents 9621ac6 + 3d2d95e commit 3d52b8b

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/wait/wait.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ func (flags *WaitFlags) ToOptions(args []string) (*WaitOptions, error) {
165165
ResourceFinder: builder,
166166
DynamicClient: dynamicClient,
167167
Timeout: effectiveTimeout,
168+
ForCondition: flags.ForCondition,
168169

169170
Printer: printer,
170171
ConditionFn: conditionFn,
@@ -215,6 +216,7 @@ type WaitOptions struct {
215216
UIDMap UIDMap
216217
DynamicClient dynamic.Interface
217218
Timeout time.Duration
219+
ForCondition string
218220

219221
Printer printers.ResourcePrinter
220222
ConditionFn ConditionFunc
@@ -227,7 +229,7 @@ type ConditionFunc func(info *resource.Info, o *WaitOptions) (finalObject runtim
227229
// RunWait runs the waiting logic
228230
func (o *WaitOptions) RunWait() error {
229231
visitCount := 0
230-
err := o.ResourceFinder.Do().Visit(func(info *resource.Info, err error) error {
232+
visitFunc := func(info *resource.Info, err error) error {
231233
if err != nil {
232234
return err
233235
}
@@ -242,7 +244,13 @@ func (o *WaitOptions) RunWait() error {
242244
return fmt.Errorf("%v unsatisified for unknown reason", finalObject)
243245
}
244246
return err
245-
})
247+
}
248+
visitor := o.ResourceFinder.Do()
249+
if visitor, ok := visitor.(*resource.Result); ok && strings.ToLower(o.ForCondition) == "delete" {
250+
visitor.IgnoreErrors(apierrors.IsNotFound)
251+
}
252+
253+
err := visitor.Visit(visitFunc)
246254
if err != nil {
247255
return err
248256
}

staging/src/k8s.io/kubectl/pkg/cmd/wait/wait_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,3 +787,30 @@ func TestWaitForCondition(t *testing.T) {
787787
})
788788
}
789789
}
790+
791+
func TestWaitForDeletionIgnoreNotFound(t *testing.T) {
792+
scheme := runtime.NewScheme()
793+
infos := []*resource.Info{
794+
{
795+
Mapping: &meta.RESTMapping{
796+
Resource: schema.GroupVersionResource{Group: "group", Version: "version", Resource: "theresource"},
797+
},
798+
Name: "name-foo",
799+
Namespace: "ns-foo",
800+
},
801+
}
802+
fakeClient := dynamicfakeclient.NewSimpleDynamicClient(scheme)
803+
804+
o := &WaitOptions{
805+
ResourceFinder: genericclioptions.NewSimpleFakeResourceFinder(infos...),
806+
DynamicClient: fakeClient,
807+
Printer: printers.NewDiscardingPrinter(),
808+
ConditionFn: IsDeleted,
809+
IOStreams: genericclioptions.NewTestIOStreamsDiscard(),
810+
ForCondition: "delete",
811+
}
812+
err := o.RunWait()
813+
if err != nil {
814+
t.Fatalf("unexpected error: %v", err)
815+
}
816+
}

0 commit comments

Comments
 (0)