...

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

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

     1  package entrypoint
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"os"
     8  	"path"
     9  	"path/filepath"
    10  	"strings"
    11  
    12  	"github.com/datawire/dlib/dexec"
    13  	"github.com/datawire/dlib/dlog"
    14  )
    15  
    16  func GetAgentService() string {
    17  	// Using an agent service is no longer supported, so return empty.
    18  	// For good measure, we also set AGENT_SERVICE to empty in the entrypoint.
    19  	return ""
    20  }
    21  
    22  func GetAmbassadorID() string {
    23  	id := os.Getenv("AMBASSADOR_ID")
    24  	if id != "" {
    25  		return id
    26  	}
    27  	svc := GetAgentService()
    28  	if svc != "" {
    29  		return fmt.Sprintf("intercept-%s", svc)
    30  	}
    31  	return "default"
    32  }
    33  
    34  func GetAmbassadorNamespace() string {
    35  	return env("AMBASSADOR_NAMESPACE", "default")
    36  }
    37  
    38  func GetAmbassadorFieldSelector() string {
    39  	return env("AMBASSADOR_FIELD_SELECTOR", "")
    40  }
    41  
    42  func GetAmbassadorLabelSelector() string {
    43  	return env("AMBASSADOR_LABEL_SELECTOR", "")
    44  }
    45  
    46  func GetAmbassadorRoot() string {
    47  	return env("ambassador_root", "/ambassador")
    48  }
    49  
    50  func GetHomeDir() string {
    51  	return env("HOME", "/tmp/ambassador")
    52  }
    53  
    54  func GetAmbassadorConfigBaseDir() string {
    55  	return env("AMBASSADOR_CONFIG_BASE_DIR", GetAmbassadorRoot())
    56  }
    57  
    58  func GetEnvoyDir() string {
    59  	return env("ENVOY_DIR", path.Join(GetAmbassadorConfigBaseDir(), "envoy"))
    60  }
    61  
    62  func GetEnvoyConcurrency() string {
    63  	return env("ENVOY_CONCURRENCY", "")
    64  }
    65  
    66  func GetEnvoyBootstrapFile() string {
    67  	return env("ENVOY_BOOTSTRAP_FILE", path.Join(GetAmbassadorConfigBaseDir(), "bootstrap-ads.json"))
    68  }
    69  
    70  func GetEnvoyBaseID() string {
    71  	return env("AMBASSADOR_ENVOY_BASE_ID", "0")
    72  }
    73  
    74  func GetAppDir() string {
    75  	return env("APPDIR", GetAmbassadorRoot())
    76  }
    77  
    78  // GetConfigDir returns the path to the directory we should check for
    79  // filesystem config.
    80  func GetConfigDir(demoMode bool) string {
    81  	// XXX There was no way to override the config dir via the environment in
    82  	// entrypoint.sh.
    83  	configDir := env("AMBASSADOR_CONFIG_DIR", path.Join(GetAmbassadorConfigBaseDir(), "ambassador-config"))
    84  
    85  	if demoMode {
    86  		// There is _intentionally_ no way to override the demo-mode config directory,
    87  		// and it is _intentionally_ based on the root directory rather than on
    88  		// AMBASSADOR_CONFIG_BASE_DIR: it's baked into a specific location during
    89  		// the build process.
    90  		configDir = path.Join(GetAmbassadorRoot(), "ambassador-demo-config")
    91  	}
    92  
    93  	return configDir
    94  }
    95  
    96  // ConfigIsPresent checks to see if any configuration is actually present
    97  // in the given configdir.
    98  func ConfigIsPresent(ctx context.Context, configDir string) bool {
    99  	// Is there anything in this directory?
   100  	foundAny := false
   101  
   102  	_ = filepath.Walk(configDir, func(path string, info os.FileInfo, err error) error {
   103  		// If we're handed an error coming in, something has gone wrong and we _must_
   104  		// return the error to avoid a panic. (The most likely error, admittedly, is
   105  		// simply that the toplevel directory doesn't exist.)
   106  		if err != nil {
   107  			// Log it, but if it isn't an os.ErrNoExist().
   108  			if !os.IsNotExist(err) {
   109  				dlog.Errorf(ctx, "Error scanning config file %s: %s", filepath.Join(configDir, path), err)
   110  			}
   111  
   112  			return err
   113  		}
   114  
   115  		if (info.Mode() & os.ModeType) == 0 {
   116  			// This is a regular file, so we can consider this valid config.
   117  			foundAny = true
   118  
   119  			// Return an error in order to short-circuit the rest of the walk.
   120  			// This is kind of an abuse, honestly, but then we also don't want
   121  			// to spend a long time walking crap if someone sets the environment
   122  			// variable incorrectly -- and if we run into an actual error walking
   123  			// the config dir, see the comment above.
   124  			return errors.New("not really an errore")
   125  		}
   126  
   127  		return nil
   128  	})
   129  
   130  	// Done. We don't care what the walk actually returned, we only care
   131  	// about foundAny.
   132  	return foundAny
   133  }
   134  
   135  func GetSnapshotDir() string {
   136  	return env("snapshot_dir", path.Join(GetAmbassadorConfigBaseDir(), "snapshots"))
   137  }
   138  
   139  func GetEnvoyConfigFile() string {
   140  	return env("envoy_config_file", path.Join(GetEnvoyDir(), "envoy.json"))
   141  }
   142  
   143  func GetEnvoyAPIVersion() string {
   144  	return env("AMBASSADOR_ENVOY_API_VERSION", "V3")
   145  }
   146  
   147  func GetAmbassadorDebug() string {
   148  	return env("AMBASSADOR_DEBUG", "")
   149  }
   150  
   151  func isDebug(name string) bool {
   152  	return strings.Contains(GetAmbassadorDebug(), name)
   153  }
   154  
   155  func GetEnvoyFlags() []string {
   156  	result := []string{"-c", GetEnvoyBootstrapFile(), "--base-id", GetEnvoyBaseID()}
   157  	svc := GetAgentService()
   158  	if svc != "" {
   159  		result = append(result, "--drain-time-s", "1")
   160  	} else {
   161  		result = append(result, "--drain-time-s", env("AMBASSADOR_DRAIN_TIME", "600"))
   162  	}
   163  	if isDebug("envoy") {
   164  		result = append(result, "-l", "trace")
   165  	} else {
   166  		result = append(result, "-l", "error")
   167  	}
   168  	concurrency := GetEnvoyConcurrency()
   169  	if concurrency != "" {
   170  		result = append(result, "--concurrency", concurrency)
   171  	}
   172  	envoyAPIVersion := GetEnvoyAPIVersion()
   173  	if strings.ToUpper(envoyAPIVersion) == "V3" {
   174  		result = append(result, "--bootstrap-version", "3")
   175  	} else {
   176  		result = append(result, "--bootstrap-version", "2")
   177  	}
   178  	return result
   179  }
   180  
   181  func GetDiagdBindAddress() string {
   182  	return env("AMBASSADOR_DIAGD_BIND_ADDREASS", "")
   183  }
   184  
   185  func IsDiagdOnly() bool {
   186  	return envbool("DIAGD_ONLY")
   187  }
   188  
   189  // ForceEndpoints reflects AMBASSADOR_FORCE_ENDPOINTS, to determine whether
   190  // we're forcing endpoint watching or (the default) not.
   191  func ForceEndpoints() bool {
   192  	return envbool("AMBASSADOR_FORCE_ENDPOINTS")
   193  }
   194  
   195  func GetDiagdBindPort() string {
   196  	return env("AMBASSADOR_DIAGD_BIND_PORT", "8004")
   197  }
   198  
   199  func IsEnvoyAvailable() bool {
   200  	_, err := dexec.LookPath("envoy")
   201  	return err == nil
   202  }
   203  
   204  func GetDiagdFlags(ctx context.Context, demoMode bool) []string {
   205  	result := []string{"--notices", path.Join(GetAmbassadorConfigBaseDir(), "notices.json")}
   206  
   207  	if isDebug("diagd") {
   208  		result = append(result, "--debug")
   209  	}
   210  
   211  	diagdBind := GetDiagdBindAddress()
   212  	if diagdBind != "" {
   213  		result = append(result, "--host", diagdBind)
   214  	}
   215  
   216  	// XXX: this was not in entrypoint.sh
   217  	result = append(result, "--port", GetDiagdBindPort())
   218  
   219  	cdir := GetConfigDir(demoMode)
   220  
   221  	if (cdir != "") && ConfigIsPresent(ctx, cdir) {
   222  		result = append(result, "--config-path", cdir)
   223  	}
   224  
   225  	if IsDiagdOnly() {
   226  		result = append(result, "--no-checks", "--no-envoy")
   227  	} else {
   228  		result = append(result, "--kick", fmt.Sprintf("kill -HUP %d", os.Getpid()))
   229  		// XXX: this was not in entrypoint.sh
   230  		if !IsEnvoyAvailable() {
   231  			result = append(result, "--no-envoy")
   232  		}
   233  	}
   234  
   235  	return result
   236  }
   237  
   238  func GetDiagdArgs(ctx context.Context, demoMode bool) []string {
   239  	return append(
   240  		[]string{
   241  			GetSnapshotDir(),
   242  			GetEnvoyBootstrapFile(),
   243  			GetEnvoyConfigFile(),
   244  		},
   245  		GetDiagdFlags(ctx, demoMode)...,
   246  	)
   247  }
   248  
   249  func IsAmbassadorSingleNamespace() bool {
   250  	return envbool("AMBASSADOR_SINGLE_NAMESPACE")
   251  }
   252  
   253  func IsEdgeStack() (bool, error) {
   254  	if envbool("EDGE_STACK") {
   255  		return true, nil
   256  	}
   257  	_, err := os.Stat("/ambassador/.edge_stack")
   258  	if err == nil {
   259  		return true, nil
   260  	} else if os.IsNotExist(err) {
   261  		return false, nil
   262  	} else {
   263  		return false, err
   264  	}
   265  }
   266  
   267  func GetLicenseSecretName() string {
   268  	return env("AMBASSADOR_AES_SECRET_NAME", "ambassador-edge-stack")
   269  }
   270  
   271  func GetLicenseSecretNamespace() string {
   272  	return env("AMBASSADOR_AES_SECRET_NAMESPACE", GetAmbassadorNamespace())
   273  }
   274  
   275  func GetCloudConnectTokenResourceName() string {
   276  	return env("AGENT_CONFIG_RESOURCE_NAME", "ambassador-agent-cloud-token")
   277  }
   278  
   279  func GetCloudConnectTokenResourceNamespace() string {
   280  	return env("AGENT_NAMESPACE", GetAmbassadorNamespace())
   281  }
   282  
   283  func GetEventHost() string {
   284  	return env("DEV_AMBASSADOR_EVENT_HOST", fmt.Sprintf("http://localhost:%s", GetDiagdBindPort()))
   285  }
   286  
   287  func GetEventPath() string {
   288  	return env("DEV_AMBASSADOR_EVENT_PATH", fmt.Sprintf("_internal/v0"))
   289  }
   290  
   291  func GetSidecarHost() string {
   292  	return env("DEV_AMBASSADOR_SIDECAR_HOST", "http://localhost:8500")
   293  }
   294  
   295  func GetSidecarPath() string {
   296  	return env("DEV_AMBASSADOR_SIDECAR_PATH", "_internal/v0")
   297  }
   298  
   299  func GetEventUrl() string {
   300  	return fmt.Sprintf("%s/%s/watt", GetEventHost(), GetEventPath())
   301  }
   302  
   303  func GetSidecarUrl() string {
   304  	return fmt.Sprintf("%s/%s/watt", GetSidecarHost(), GetSidecarPath())
   305  }
   306  
   307  func IsKnativeEnabled() bool {
   308  	return strings.ToLower(env("AMBASSADOR_KNATIVE_SUPPORT", "")) == "true"
   309  }
   310  

View as plain text