Bumping k8s dependencies to 1.13
This commit is contained in:
97
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/wait/wait.go
generated
vendored
97
vendor/k8s.io/kubernetes/pkg/kubectl/cmd/wait/wait.go
generated
vendored
@@ -17,25 +17,56 @@ limitations under the License.
|
||||
package wait
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
"k8s.io/apimachinery/pkg/watch"
|
||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||
"k8s.io/cli-runtime/pkg/genericclioptions/printers"
|
||||
"k8s.io/cli-runtime/pkg/genericclioptions/resource"
|
||||
"k8s.io/client-go/dynamic"
|
||||
watchtools "k8s.io/client-go/tools/watch"
|
||||
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
|
||||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers"
|
||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
|
||||
)
|
||||
|
||||
var (
|
||||
wait_long = templates.LongDesc(`
|
||||
Experimental: Wait for a specific condition on one or many resources.
|
||||
|
||||
The command takes multiple resources and waits until the specified condition
|
||||
is seen in the Status field of every given resource.
|
||||
|
||||
Alternatively, the command can wait for the given set of resources to be deleted
|
||||
by providing the "delete" keyword as the value to the --for flag.
|
||||
|
||||
A successful message will be printed to stdout indicating when the specified
|
||||
condition has been met. One can use -o option to change to output destination.`)
|
||||
|
||||
wait_example = templates.Examples(`
|
||||
# Wait for the pod "busybox1" to contain the status condition of type "Ready".
|
||||
kubectl wait --for=condition=Ready pod/busybox1
|
||||
|
||||
# Wait for the pod "busybox1" to be deleted, with a timeout of 60s, after having issued the "delete" command.
|
||||
kubectl delete pod/busybox1
|
||||
kubectl wait --for=delete pod/busybox1 --timeout=60s`)
|
||||
)
|
||||
|
||||
// errNoMatchingResources is returned when there is no resources matching a query.
|
||||
var errNoMatchingResources = errors.New("no matching resources found")
|
||||
|
||||
// WaitFlags directly reflect the information that CLI is gathering via flags. They will be converted to Options, which
|
||||
// reflect the runtime requirements for the command. This structure reduces the transformation to wiring and makes
|
||||
// the logic itself easy to unit test
|
||||
@@ -73,7 +104,9 @@ func NewCmdWait(restClientGetter genericclioptions.RESTClientGetter, streams gen
|
||||
cmd := &cobra.Command{
|
||||
Use: "wait resource.group/name [--for=delete|--for condition=available]",
|
||||
DisableFlagsInUseLine: true,
|
||||
Short: "Experimental: Wait for one condition on one or many resources",
|
||||
Short: "Experimental: Wait for a specific condition on one or many resources.",
|
||||
Long: wait_long,
|
||||
Example: wait_example,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
o, err := flags.ToOptions(args)
|
||||
cmdutil.CheckErr(err)
|
||||
@@ -151,12 +184,23 @@ func conditionFuncFor(condition string) (ConditionFunc, error) {
|
||||
return nil, fmt.Errorf("unrecognized condition: %q", condition)
|
||||
}
|
||||
|
||||
type ResourceLocation struct {
|
||||
GroupResource schema.GroupResource
|
||||
Namespace string
|
||||
Name string
|
||||
}
|
||||
|
||||
type UIDMap map[ResourceLocation]types.UID
|
||||
|
||||
// WaitOptions is a set of options that allows you to wait. This is the object reflects the runtime needs of a wait
|
||||
// command, making the logic itself easy to unit test with our existing mocks.
|
||||
type WaitOptions struct {
|
||||
ResourceFinder genericclioptions.ResourceFinder
|
||||
DynamicClient dynamic.Interface
|
||||
Timeout time.Duration
|
||||
// UIDMap maps a resource location to a UID. It is optional, but ConditionFuncs may choose to use it to make the result
|
||||
// more reliable. For instance, delete can look for UID consistency during delegated calls.
|
||||
UIDMap UIDMap
|
||||
DynamicClient dynamic.Interface
|
||||
Timeout time.Duration
|
||||
|
||||
Printer printers.ResourcePrinter
|
||||
ConditionFn ConditionFunc
|
||||
@@ -168,11 +212,13 @@ type ConditionFunc func(info *resource.Info, o *WaitOptions) (finalObject runtim
|
||||
|
||||
// RunWait runs the waiting logic
|
||||
func (o *WaitOptions) RunWait() error {
|
||||
return o.ResourceFinder.Do().Visit(func(info *resource.Info, err error) error {
|
||||
visitCount := 0
|
||||
err := o.ResourceFinder.Do().Visit(func(info *resource.Info, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
visitCount++
|
||||
finalObject, success, err := o.ConditionFn(info, o)
|
||||
if success {
|
||||
o.Printer.PrintObj(finalObject, o.Out)
|
||||
@@ -183,6 +229,13 @@ func (o *WaitOptions) RunWait() error {
|
||||
}
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if visitCount == 0 {
|
||||
return errNoMatchingResources
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// IsDeleted is a condition func for waiting for something to be deleted
|
||||
@@ -190,13 +243,23 @@ func IsDeleted(info *resource.Info, o *WaitOptions) (runtime.Object, bool, error
|
||||
endTime := time.Now().Add(o.Timeout)
|
||||
for {
|
||||
gottenObj, err := o.DynamicClient.Resource(info.Mapping.Resource).Namespace(info.Namespace).Get(info.Name, metav1.GetOptions{})
|
||||
if errors.IsNotFound(err) {
|
||||
if apierrors.IsNotFound(err) {
|
||||
return info.Object, true, nil
|
||||
}
|
||||
if err != nil {
|
||||
// TODO this could do something slightly fancier if we wish
|
||||
return info.Object, false, err
|
||||
}
|
||||
resourceLocation := ResourceLocation{
|
||||
GroupResource: info.Mapping.Resource.GroupResource(),
|
||||
Namespace: gottenObj.GetNamespace(),
|
||||
Name: gottenObj.GetName(),
|
||||
}
|
||||
if uid, ok := o.UIDMap[resourceLocation]; ok {
|
||||
if gottenObj.GetUID() != uid {
|
||||
return gottenObj, true, nil
|
||||
}
|
||||
}
|
||||
|
||||
watchOptions := metav1.ListOptions{}
|
||||
watchOptions.FieldSelector = "metadata.name=" + info.Name
|
||||
@@ -211,11 +274,14 @@ func IsDeleted(info *resource.Info, o *WaitOptions) (runtime.Object, bool, error
|
||||
// we're out of time
|
||||
return gottenObj, false, wait.ErrWaitTimeout
|
||||
}
|
||||
watchEvent, err := watch.Until(o.Timeout, objWatch, isDeleted)
|
||||
|
||||
ctx, cancel := watchtools.ContextWithOptionalTimeout(context.Background(), o.Timeout)
|
||||
watchEvent, err := watchtools.UntilWithoutRetry(ctx, objWatch, isDeleted)
|
||||
cancel()
|
||||
switch {
|
||||
case err == nil:
|
||||
return watchEvent.Object, true, nil
|
||||
case err == watch.ErrWatchClosed:
|
||||
case err == watchtools.ErrWatchClosed:
|
||||
continue
|
||||
case err == wait.ErrWaitTimeout:
|
||||
if watchEvent != nil {
|
||||
@@ -245,7 +311,7 @@ func (w ConditionalWait) IsConditionMet(info *resource.Info, o *WaitOptions) (ru
|
||||
resourceVersion := ""
|
||||
gottenObj, err := o.DynamicClient.Resource(info.Mapping.Resource).Namespace(info.Namespace).Get(info.Name, metav1.GetOptions{})
|
||||
switch {
|
||||
case errors.IsNotFound(err):
|
||||
case apierrors.IsNotFound(err):
|
||||
resourceVersion = "0"
|
||||
case err != nil:
|
||||
return info.Object, false, err
|
||||
@@ -273,11 +339,14 @@ func (w ConditionalWait) IsConditionMet(info *resource.Info, o *WaitOptions) (ru
|
||||
// we're out of time
|
||||
return gottenObj, false, wait.ErrWaitTimeout
|
||||
}
|
||||
watchEvent, err := watch.Until(o.Timeout, objWatch, w.isConditionMet)
|
||||
|
||||
ctx, cancel := watchtools.ContextWithOptionalTimeout(context.Background(), o.Timeout)
|
||||
watchEvent, err := watchtools.UntilWithoutRetry(ctx, objWatch, w.isConditionMet)
|
||||
cancel()
|
||||
switch {
|
||||
case err == nil:
|
||||
return watchEvent.Object, true, nil
|
||||
case err == watch.ErrWatchClosed:
|
||||
case err == watchtools.ErrWatchClosed:
|
||||
continue
|
||||
case err == wait.ErrWaitTimeout:
|
||||
if watchEvent != nil {
|
||||
|
Reference in New Issue
Block a user