...

Source file src/github.com/emissary-ingress/emissary/v3/cmd/entrypoint/envoy.go

Documentation: github.com/emissary-ingress/emissary/v3/cmd/entrypoint

     1  package entrypoint
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"os"
     8  	"time"
     9  
    10  	"github.com/datawire/dlib/dcontext"
    11  	"github.com/datawire/dlib/dexec"
    12  	"github.com/datawire/dlib/dgroup"
    13  	"github.com/datawire/dlib/dlog"
    14  	"github.com/emissary-ingress/emissary/v3/pkg/envoytest"
    15  )
    16  
    17  func runEnvoy(ctx context.Context, envoyHUP chan os.Signal) error {
    18  	// Wait until we get a SIGHUP to start envoy.
    19  	//var bootstrap string
    20  	select {
    21  	case <-ctx.Done():
    22  		return nil
    23  	case <-envoyHUP:
    24  	}
    25  
    26  	// Try to run envoy directly, but fallback to running it inside docker if there is
    27  	// no envoy executable available.
    28  	if IsEnvoyAvailable() {
    29  		cmd := subcommand(ctx, "envoy", GetEnvoyFlags()...)
    30  		if envbool("DEV_SHUTUP_ENVOY") {
    31  			cmd.Stdout = nil
    32  			cmd.Stderr = nil
    33  		}
    34  		return cmd.Run()
    35  	} else {
    36  		// For some reason docker only sometimes passes the signal onto the process inside
    37  		// the container, so we setup this cleanup function so that in the docker case we
    38  		// can do a docker kill, just to be sure it is really dead and we don't leave an
    39  		// envoy lying around.
    40  
    41  		// Create a label unique to this invocation so we can use it to do a docker
    42  		// kill for cleanup.
    43  		label := fmt.Sprintf("amb-envoy-label-%d", os.Getpid())
    44  
    45  		snapdir := GetSnapshotDir()
    46  
    47  		group := dgroup.NewGroup(ctx, dgroup.GroupConfig{
    48  			ShutdownOnNonError: true,
    49  		})
    50  		group.Go("envoy", func(ctx context.Context) error {
    51  			dockerFlags := []string{
    52  				"--label=" + label,
    53  				// FIXME(lukeshu): --network=host doesn't work on macOS, so that
    54  				// will inconvenience our macOS-using contributors.
    55  				"--network=host",
    56  				"--volume=" + snapdir + ":" + snapdir,
    57  				"--volume=" + GetEnvoyBootstrapFile() + ":" + GetEnvoyBootstrapFile(),
    58  			}
    59  			if envbool("DEV_ENVOY_DOCKER_PRIVILEGED") {
    60  				dockerFlags = append(dockerFlags,
    61  					"--privileged")
    62  			}
    63  			cmd, err := envoytest.LocalEnvoyCmd(ctx, dockerFlags, GetEnvoyFlags())
    64  			if err != nil {
    65  				return err
    66  			}
    67  			return cmd.Run()
    68  		})
    69  		group.Go("cleanup", func(ctx context.Context) error {
    70  			<-ctx.Done()
    71  			ctx = dcontext.HardContext(ctx)
    72  
    73  			for {
    74  				cids, err := cidsForLabel(ctx, label)
    75  				if err != nil {
    76  					return err
    77  				}
    78  				if len(cids) == 0 {
    79  					return nil
    80  				}
    81  
    82  				// Give the container one second to exit
    83  				tctx, cancel := context.WithTimeout(ctx, 1*time.Second)
    84  				defer cancel()
    85  				wait := subcommand(tctx, "docker", append([]string{"wait"}, cids...)...)
    86  				wait.Stdout = nil
    87  				if err := wait.Run(); err != nil {
    88  					var exitErr *dexec.ExitError
    89  					if errors.As(err, &exitErr) {
    90  						dlog.Errorf(ctx, "docker wait: %v\n%s", err, string(exitErr.Stderr))
    91  					} else {
    92  						return err
    93  					}
    94  				}
    95  
    96  				cids, err = cidsForLabel(ctx, label)
    97  				if err != nil {
    98  					return err
    99  				}
   100  				if len(cids) == 0 {
   101  					return nil
   102  				}
   103  
   104  				kill := subcommand(ctx, "docker", append([]string{"kill"}, cids...)...)
   105  				kill.Stdout = nil
   106  				if err := wait.Run(); err != nil {
   107  					var exitErr *dexec.ExitError
   108  					if errors.As(err, &exitErr) {
   109  						dlog.Errorf(ctx, "docker kill: %v\n%s", err, string(exitErr.Stderr))
   110  					} else {
   111  						return err
   112  					}
   113  				}
   114  			}
   115  		})
   116  		return group.Wait()
   117  	}
   118  }
   119  

View as plain text