98 lines
3.2 KiB
Go
98 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"os/signal"
|
|
"strings"
|
|
"syscall"
|
|
|
|
"proxy-pool/internal/config"
|
|
"proxy-pool/internal/gateway/bootstrap"
|
|
)
|
|
|
|
const (
|
|
configEnvironment = "PROXY_POOL_CONFIG"
|
|
controlPlaneAddressEnvironment = "PROXY_POOL_CONTROL_PLANE_ADDRESS"
|
|
clusterIDEnvironment = "PROXY_POOL_CLUSTER_ID"
|
|
workerIDEnvironment = "PROXY_POOL_WORKER_ID"
|
|
instanceIDEnvironment = "PROXY_POOL_INSTANCE_ID"
|
|
zoneEnvironment = "PROXY_POOL_ZONE"
|
|
)
|
|
|
|
type environmentLookup func(string) string
|
|
type gatewayRun func(context.Context, bootstrap.Options) error
|
|
|
|
func main() {
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer stop()
|
|
os.Exit(execute(ctx, os.Args[1:], os.Getenv, bootstrap.Run, os.Stderr))
|
|
}
|
|
|
|
func execute(
|
|
ctx context.Context,
|
|
args []string,
|
|
getenv environmentLookup,
|
|
run gatewayRun,
|
|
stderr io.Writer,
|
|
) int {
|
|
flags := flag.NewFlagSet("proxy-gateway", flag.ContinueOnError)
|
|
flags.SetOutput(stderr)
|
|
configPath := flags.String("config", "", "configuration file path")
|
|
controlPlaneAddress := flags.String("control-plane", "", "remote Controller control-plane address")
|
|
clusterID := flags.String("cluster-id", "", "cluster identifier")
|
|
workerID := flags.String("worker-id", "", "unique Worker identifier")
|
|
instanceID := flags.String("instance-id", "", "unique process instance identifier")
|
|
zone := flags.String("zone", "", "availability zone identifier")
|
|
if err := flags.Parse(args); err != nil {
|
|
if errors.Is(err, flag.ErrHelp) {
|
|
return 0
|
|
}
|
|
return 2
|
|
}
|
|
if flags.NArg() != 0 {
|
|
_, _ = fmt.Fprintln(stderr, "proxy-gateway: unexpected positional arguments")
|
|
return 2
|
|
}
|
|
if getenv != nil {
|
|
setIfEmpty(configPath, getenv(configEnvironment))
|
|
setIfEmpty(controlPlaneAddress, getenv(controlPlaneAddressEnvironment))
|
|
setIfEmpty(clusterID, getenv(clusterIDEnvironment))
|
|
setIfEmpty(workerID, getenv(workerIDEnvironment))
|
|
setIfEmpty(instanceID, getenv(instanceIDEnvironment))
|
|
setIfEmpty(zone, getenv(zoneEnvironment))
|
|
}
|
|
if ctx == nil || run == nil || !validValue(*configPath) || !validValue(*controlPlaneAddress) ||
|
|
!validValue(*clusterID) || !validValue(*workerID) || !validValue(*instanceID) || !validValue(*zone) {
|
|
_, _ = fmt.Fprintf(stderr,
|
|
"proxy-gateway: -config, -control-plane, -cluster-id, -worker-id, -instance-id and -zone are required; "+
|
|
"environment fallbacks: %s, %s, %s, %s, %s, %s\n",
|
|
configEnvironment, controlPlaneAddressEnvironment, clusterIDEnvironment, workerIDEnvironment, instanceIDEnvironment, zoneEnvironment,
|
|
)
|
|
return 2
|
|
}
|
|
err := run(ctx, bootstrap.Options{
|
|
ConfigPath: *configPath, Resolver: config.OSResolver{}, ControlPlaneAddress: *controlPlaneAddress,
|
|
ClusterID: *clusterID, WorkerID: *workerID, InstanceID: *instanceID, Zone: *zone,
|
|
})
|
|
if err == nil || (errors.Is(err, context.Canceled) && ctx.Err() != nil) {
|
|
return 0
|
|
}
|
|
_, _ = fmt.Fprintf(stderr, "proxy-gateway: %v\n", err)
|
|
return 1
|
|
}
|
|
|
|
func setIfEmpty(target *string, value string) {
|
|
if target != nil && *target == "" {
|
|
*target = value
|
|
}
|
|
}
|
|
|
|
func validValue(value string) bool {
|
|
return value != "" && strings.TrimSpace(value) == value
|
|
}
|