github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/cli/wait.go (about)

     1  /*
     2  Adapted from
     3  https://github.com/kubernetes/kubectl/tree/master/pkg/cmd/wait
     4  */
     5  
     6  /*
     7  Copyright 2018 The Kubernetes Authors.
     8  
     9  Licensed under the Apache License, Version 2.0 (the "License");
    10  you may not use this file except in compliance with the License.
    11  You may obtain a copy of the License at
    12  
    13      http://www.apache.org/licenses/LICENSE-2.0
    14  
    15  Unless required by applicable law or agreed to in writing, software
    16  distributed under the License is distributed on an "AS IS" BASIS,
    17  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    18  See the License for the specific language governing permissions and
    19  limitations under the License.
    20  */
    21  
    22  package cli
    23  
    24  import (
    25  	"context"
    26  	"time"
    27  
    28  	"github.com/spf13/cobra"
    29  	"k8s.io/cli-runtime/pkg/genericclioptions"
    30  	cmdutil "k8s.io/kubectl/pkg/cmd/util"
    31  	"k8s.io/kubectl/pkg/cmd/wait"
    32  	"k8s.io/kubectl/pkg/util/i18n"
    33  	"k8s.io/kubectl/pkg/util/templates"
    34  
    35  	"github.com/tilt-dev/tilt/internal/analytics"
    36  	engineanalytics "github.com/tilt-dev/tilt/internal/engine/analytics"
    37  	"github.com/tilt-dev/tilt/pkg/model"
    38  )
    39  
    40  var (
    41  	waitLong = templates.LongDesc(i18n.T(`
    42  		Experimental: Wait for a specific condition on one or many resources.
    43  
    44  		The command takes multiple resources and waits until the specified condition
    45  		is seen in the Status field of every given resource.
    46  
    47  		A successful message will be printed to stdout indicating when the specified
    48      condition has been met. You can use -o option to change to output destination.`))
    49  
    50  	waitExample = templates.Examples(i18n.T(`
    51  		# Wait for the tiltfile to load.
    52  		tilt wait --for=condition=Ready "uiresource/(Tiltfile)"
    53  
    54  		# When used with a Kubernetes resource, waits for the pod
    55      # to deploy, start running, and pass all readiness probes.
    56  		tilt wait --for=condition=Ready "uiresource/my-kubernetes-deployment"`))
    57  )
    58  
    59  type waitCmd struct {
    60  	flags *wait.WaitFlags
    61  }
    62  
    63  var _ tiltCmd = &waitCmd{}
    64  
    65  func newWaitCmd(streams genericclioptions.IOStreams) *waitCmd {
    66  	flags := wait.NewWaitFlags(nil, streams)
    67  	return &waitCmd{
    68  		flags: flags,
    69  	}
    70  }
    71  
    72  func (c *waitCmd) name() model.TiltSubcommand { return "wait" }
    73  
    74  func (c *waitCmd) register() *cobra.Command {
    75  	cmd := &cobra.Command{
    76  		Use:     "wait ([-f FILENAME] | resource.group/resource.name | resource.group [(-l label | --all)]) [--for=delete|--for condition=available]",
    77  		Short:   "Experimental: Wait for a specific condition on one or many resources",
    78  		Long:    waitLong,
    79  		Example: waitExample,
    80  
    81  		DisableFlagsInUseLine: true,
    82  	}
    83  
    84  	c.flags.AddFlags(cmd)
    85  	addConnectServerFlags(cmd)
    86  
    87  	return cmd
    88  }
    89  
    90  func (c *waitCmd) run(ctx context.Context, args []string) error {
    91  	a := analytics.Get(ctx)
    92  	cmdTags := engineanalytics.CmdTags(map[string]string{})
    93  	a.Incr("cmd.wait", cmdTags.AsMap())
    94  	defer a.Flush(time.Second)
    95  
    96  	getter, err := wireClientGetter(ctx)
    97  	if err != nil {
    98  		return err
    99  	}
   100  
   101  	c.flags.RESTClientGetter = getter
   102  
   103  	o, err := c.flags.ToOptions(args)
   104  	cmdutil.CheckErr(err)
   105  	cmdutil.CheckErr(o.RunWait())
   106  
   107  	return nil
   108  }