...

Source file src/github.com/datawire/ambassador/v2/cmd/entrypoint/helpers.go

Documentation: github.com/datawire/ambassador/v2/cmd/entrypoint

     1  package entrypoint
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"os"
     7  	"strings"
     8  
     9  	amb "github.com/datawire/ambassador/v2/pkg/api/getambassador.io/v3alpha1"
    10  	"github.com/datawire/dlib/dexec"
    11  )
    12  
    13  func envbool(name string) bool {
    14  	return os.Getenv(name) != ""
    15  }
    16  
    17  func env(name, defaultValue string) string {
    18  	value := os.Getenv(name)
    19  	if value != "" {
    20  		return value
    21  	} else {
    22  		return defaultValue
    23  	}
    24  }
    25  
    26  func ensureDir(dirname string) error {
    27  	err := os.MkdirAll(dirname, 0700)
    28  	if err != nil && os.IsExist(err) {
    29  		err = nil
    30  	}
    31  	return err
    32  }
    33  
    34  func cidsForLabel(ctx context.Context, label string) ([]string, error) {
    35  	bs, err := dexec.CommandContext(ctx, "docker", "ps", "-q", "-f", "label="+label).CombinedOutput()
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  	return strings.Fields(string(bs)), nil
    40  }
    41  
    42  func subcommand(ctx context.Context, command string, args ...string) *dexec.Cmd {
    43  	cmd := dexec.CommandContext(ctx, command, args...)
    44  	cmd.Stdin = os.Stdin
    45  	cmd.Stdout = os.Stdout
    46  	cmd.Stderr = os.Stderr
    47  	return cmd
    48  }
    49  
    50  func convert(in interface{}, out interface{}) error {
    51  	if out == nil {
    52  		return nil
    53  	}
    54  
    55  	jsonBytes, err := json.Marshal(in)
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	err = json.Unmarshal(jsonBytes, out)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	return nil
    66  }
    67  
    68  // Should we pay attention to a given AmbassadorID set?
    69  //
    70  // XXX Yes, amb.AmbassadorID is a singular name for a plural type. Sigh.
    71  func include(id amb.AmbassadorID) bool {
    72  	// We always pay attention to the "_automatic_" ID -- it gives us a
    73  	// to easily always include certain configuration resources for Edge
    74  	// Stack.
    75  	if len(id) == 1 && id[0] == "_automatic_" {
    76  		return true
    77  	}
    78  
    79  	// It's not "_automatic_", so we have to actually do the work. Grab
    80  	// our AmbassadorID...
    81  	me := GetAmbassadorID()
    82  
    83  	// ...force an empty AmbassadorID to "default", per the documentation...
    84  	if len(id) == 0 {
    85  		id = amb.AmbassadorID{"default"}
    86  	}
    87  
    88  	// ...and then see if our AmbassadorID is in the list.
    89  	for _, name := range id {
    90  		if me == name {
    91  			return true
    92  		}
    93  	}
    94  
    95  	return false
    96  }
    97  

View as plain text