Unverified Commit 08bee2cc authored by Kubernetes Prow Robot's avatar Kubernetes Prow Robot Committed by GitHub

Merge pull request #72825 from liggitt/wait-resource-version

Find current resourceVersion for waiting for deletion/conditions
parents dd99c2ba c12d8a56
...@@ -11,6 +11,7 @@ go_library( ...@@ -11,6 +11,7 @@ go_library(
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/fields:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
......
...@@ -29,6 +29,7 @@ import ( ...@@ -29,6 +29,7 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors" apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/types"
...@@ -255,7 +256,11 @@ func IsDeleted(info *resource.Info, o *WaitOptions) (runtime.Object, bool, error ...@@ -255,7 +256,11 @@ func IsDeleted(info *resource.Info, o *WaitOptions) (runtime.Object, bool, error
if len(info.Name) == 0 { if len(info.Name) == 0 {
return info.Object, false, fmt.Errorf("resource name must be provided") return info.Object, false, fmt.Errorf("resource name must be provided")
} }
gottenObj, err := o.DynamicClient.Resource(info.Mapping.Resource).Namespace(info.Namespace).Get(info.Name, metav1.GetOptions{})
nameSelector := fields.OneTermEqualSelector("metadata.name", info.Name).String()
// List with a name field selector to get the current resourceVersion to watch from (not the object's resourceVersion)
gottenObjList, err := o.DynamicClient.Resource(info.Mapping.Resource).Namespace(info.Namespace).List(metav1.ListOptions{FieldSelector: nameSelector})
if apierrors.IsNotFound(err) { if apierrors.IsNotFound(err) {
return info.Object, true, nil return info.Object, true, nil
} }
...@@ -263,6 +268,10 @@ func IsDeleted(info *resource.Info, o *WaitOptions) (runtime.Object, bool, error ...@@ -263,6 +268,10 @@ func IsDeleted(info *resource.Info, o *WaitOptions) (runtime.Object, bool, error
// TODO this could do something slightly fancier if we wish // TODO this could do something slightly fancier if we wish
return info.Object, false, err return info.Object, false, err
} }
if len(gottenObjList.Items) != 1 {
return info.Object, true, nil
}
gottenObj := &gottenObjList.Items[0]
resourceLocation := ResourceLocation{ resourceLocation := ResourceLocation{
GroupResource: info.Mapping.Resource.GroupResource(), GroupResource: info.Mapping.Resource.GroupResource(),
Namespace: gottenObj.GetNamespace(), Namespace: gottenObj.GetNamespace(),
...@@ -275,8 +284,8 @@ func IsDeleted(info *resource.Info, o *WaitOptions) (runtime.Object, bool, error ...@@ -275,8 +284,8 @@ func IsDeleted(info *resource.Info, o *WaitOptions) (runtime.Object, bool, error
} }
watchOptions := metav1.ListOptions{} watchOptions := metav1.ListOptions{}
watchOptions.FieldSelector = "metadata.name=" + info.Name watchOptions.FieldSelector = nameSelector
watchOptions.ResourceVersion = gottenObj.GetResourceVersion() watchOptions.ResourceVersion = gottenObjList.GetResourceVersion()
objWatch, err := o.DynamicClient.Resource(info.Mapping.Resource).Namespace(info.Namespace).Watch(watchOptions) objWatch, err := o.DynamicClient.Resource(info.Mapping.Resource).Namespace(info.Namespace).Watch(watchOptions)
if err != nil { if err != nil {
return gottenObj, false, err return gottenObj, false, err
...@@ -343,14 +352,21 @@ func (w ConditionalWait) IsConditionMet(info *resource.Info, o *WaitOptions) (ru ...@@ -343,14 +352,21 @@ func (w ConditionalWait) IsConditionMet(info *resource.Info, o *WaitOptions) (ru
if len(info.Name) == 0 { if len(info.Name) == 0 {
return info.Object, false, fmt.Errorf("resource name must be provided") return info.Object, false, fmt.Errorf("resource name must be provided")
} }
nameSelector := fields.OneTermEqualSelector("metadata.name", info.Name).String()
var gottenObj *unstructured.Unstructured
// List with a name field selector to get the current resourceVersion to watch from (not the object's resourceVersion)
gottenObjList, err := o.DynamicClient.Resource(info.Mapping.Resource).Namespace(info.Namespace).List(metav1.ListOptions{FieldSelector: nameSelector})
resourceVersion := "" resourceVersion := ""
gottenObj, err := o.DynamicClient.Resource(info.Mapping.Resource).Namespace(info.Namespace).Get(info.Name, metav1.GetOptions{})
switch { switch {
case apierrors.IsNotFound(err):
resourceVersion = "0"
case err != nil: case err != nil:
return info.Object, false, err return info.Object, false, err
case len(gottenObjList.Items) != 1:
resourceVersion = gottenObjList.GetResourceVersion()
default: default:
gottenObj = &gottenObjList.Items[0]
conditionMet, err := w.checkCondition(gottenObj) conditionMet, err := w.checkCondition(gottenObj)
if conditionMet { if conditionMet {
return gottenObj, true, nil return gottenObj, true, nil
...@@ -358,11 +374,11 @@ func (w ConditionalWait) IsConditionMet(info *resource.Info, o *WaitOptions) (ru ...@@ -358,11 +374,11 @@ func (w ConditionalWait) IsConditionMet(info *resource.Info, o *WaitOptions) (ru
if err != nil { if err != nil {
return gottenObj, false, err return gottenObj, false, err
} }
resourceVersion = gottenObj.GetResourceVersion() resourceVersion = gottenObjList.GetResourceVersion()
} }
watchOptions := metav1.ListOptions{} watchOptions := metav1.ListOptions{}
watchOptions.FieldSelector = "metadata.name=" + info.Name watchOptions.FieldSelector = nameSelector
watchOptions.ResourceVersion = resourceVersion watchOptions.ResourceVersion = resourceVersion
objWatch, err := o.DynamicClient.Resource(info.Mapping.Resource).Namespace(info.Namespace).Watch(watchOptions) objWatch, err := o.DynamicClient.Resource(info.Mapping.Resource).Namespace(info.Namespace).Watch(watchOptions)
if err != nil { if err != nil {
......
...@@ -303,6 +303,7 @@ func (c *dynamicResourceClient) List(opts metav1.ListOptions) (*unstructured.Uns ...@@ -303,6 +303,7 @@ func (c *dynamicResourceClient) List(opts metav1.ListOptions) (*unstructured.Uns
} }
list := &unstructured.UnstructuredList{} list := &unstructured.UnstructuredList{}
list.SetResourceVersion(entireList.GetResourceVersion())
for i := range entireList.Items { for i := range entireList.Items {
item := &entireList.Items[i] item := &entireList.Items[i]
metadata, err := meta.Accessor(item) metadata, err := meta.Accessor(item)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment