Skip to content

Commit

Permalink
feat: cli flags and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
chronark committed Mar 3, 2025
1 parent 6f92d8e commit 53f0fc2
Show file tree
Hide file tree
Showing 15 changed files with 720 additions and 299 deletions.
308 changes: 210 additions & 98 deletions apps/engineering/content/architecture/services/api/config.mdx

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions deployment/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ services:
deploy:
replicas: 3
endpoint_mode: vip

command: ["api", "--config", "config.docker.json"]
command: ["api"]
build:
context: ../go
dockerfile: ./Dockerfile
Expand All @@ -54,11 +53,14 @@ services:
- redis
- clickhouse
environment:
GOSSIP_PORT: 9090
RPC_PORT: 9091
DATABASE_PRIMARY_DSN: "mysql://unkey:password@tcp(mysql:3900)/unkey?parseTime=true"
CLICKHOUSE_URL: "clickhouse://default:password@clickhouse:9000"
REDIS_URL: "redis://redis:6379"
UNKEY_HTTP_PORT: 7070
UNKEY_CLUSTER: true
UNKEY_CLUSTER_GOSSIP_PORT: 9090
UNKEY_CLUSTER_RPC_PORT: 9091
# UNKEY_CLUSTER_ADVERTISE_ADDR_STATIC: "${HOSTNAME}"
UNKEY_CLUSTER_DISCOVERY_REDIS_URL: "redis://redis:6379"
UNKEY_DATABASE_PRIMARY_DSN: "mysql://unkey:password@tcp(mysql:3900)/unkey?parseTime=true"
UNKEY_CLICKHOUSE_URL: "clickhouse://default:password@clickhouse:9000"

redis:
image: redis:latest
Expand Down
9 changes: 1 addition & 8 deletions go/.golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ linters-settings:
- "^golang.org/x/tools/go/analysis.Analyzer$"
- "^google.golang.org/protobuf/.+Options$"
- "^gopkg.in/yaml.v3.Node$"
- "^github.com/urfave/cli/v2.*$"
- "^github.com/urfave/cli/v3.*$"

lll:
line-length: 120
Expand Down Expand Up @@ -210,12 +210,6 @@ linters-settings:
# Default: ""
context: "scope"

tenv:
# The option `all` will run against whole test files (`_test.go`) regardless of method/function signatures.
# Otherwise, only methods that take `*testing.T`, `*testing.B`, and `testing.TB` as arguments are checked.
# Default: false
all: true

linters:
disable-all: true
enable:
Expand Down Expand Up @@ -281,7 +275,6 @@ linters:
- spancheck # checks for mistakes with OpenTelemetry/Census spans
- sqlclosecheck # checks that sql.Rows and sql.Stmt are closed
# - stylecheck # is a replacement for golint
- tenv # detects using os.Setenv instead of t.Setenv since Go1.17
# - testableexamples # checks if examples are testable (have an expected output)
# - testifylint # checks usage of github.com/stretchr/testify
- tparallel # detects inappropriate usage of t.Parallel() method in your Go test codes
Expand Down
2 changes: 0 additions & 2 deletions go/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ RUN go build -o bin/unkey -ldflags "-X 'github.com/unkeyed/unkey/go/pkg/version.
FROM gcr.io/distroless/static-debian12
# FROM scratch
COPY --from=builder /go/src/github.com/unkeyed/unkey/go/bin/unkey /
COPY --from=builder /go/src/github.com/unkeyed/unkey/go/config.docker.json /
COPY --from=builder /go/src/github.com/unkeyed/unkey/go/config.ecs.json /


ENTRYPOINT [ "/unkey"]
167 changes: 124 additions & 43 deletions go/cmd/api/config.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,128 @@
package api

import (
"github.com/unkeyed/unkey/go/pkg/assert"
"github.com/urfave/cli/v3"
)

type nodeConfig struct {
Platform string `json:"platform,omitempty" description:"Cloud platform identifier (e.g., aws, gcp, hetzner)"`
Image string `json:"image,omitempty" description:"Container image identifier including repository and tag"`
HttpPort int `json:"httpPort" default:"7070" description:"HTTP port for the API server to listen on"`
Schema string `json:"$schema,omitempty" description:"JSON Schema URI for configuration validation"`
Region string `json:"region,omitempty" description:"Geographic region identifier where this node is deployed"`
Heartbeat *struct {
URL string `json:"url" minLength:"1" description:"Complete URL endpoint where heartbeat signals will be sent"`
Interval int `json:"interval" min:"1" description:"Time between heartbeat signals in seconds"`
} `json:"heartbeat,omitempty" description:"Configuration for health check heartbeat mechanism"`

Cluster *struct {
NodeID string `json:"nodeId,omitempty" description:"Unique identifier for this node within the cluster"`
AdvertiseAddr struct {
Static *string `json:"static,omitempty" description:"Static IP address or hostname for node discovery"`
AwsEcsMetadata *bool `json:"awsEcsMetadata,omitempty" description:"Enable automatic address discovery using AWS ECS container metadata"`
} `json:"advertiseAddr" description:"Node address advertisement configuration for cluster communication"`
RpcPort string `json:"rpcPort" default:"7071" description:"Port used for internal RPC communication between nodes"`
GossipPort string `json:"gossipPort" default:"7072" description:"Port used for cluster membership and failure detection"`
Discovery *struct {
Static *struct {
Addrs []string `json:"addrs" minLength:"1" description:"List of seed node addresses for static cluster configuration"`
} `json:"static,omitempty" description:"Static cluster membership configuration"`
Redis *struct {
URL string `json:"url" minLength:"1" description:"Redis connection string for dynamic cluster discovery"`
} `json:"redis,omitempty" description:"Redis-based cluster discovery configuration"`
} `json:"discovery,omitempty" description:"Cluster node discovery mechanism configuration"`
} `json:"cluster,omitempty" description:"Distributed cluster configuration settings"`

Logs *struct {
Color bool `json:"color" description:"Enable ANSI color codes in log output"`
} `json:"logs,omitempty"`
Clickhouse *struct {
Url string `json:"url" minLength:"1" description:"ClickHouse database connection string"`
} `json:"clickhouse,omitempty"`

Database struct {
Primary string `json:"primary" description:"Primary database connection string for read and write operations"`
ReadonlyReplica string `json:"readonlyReplica,omitempty" description:"Optional read-replica database connection string for read operations"`
} `json:"database"`

Otel *struct {
OtlpEndpoint string `json:"otlpEndpoint" description:"OpenTelemetry collector endpoint for metrics, traces, and logs"`
} `json:"otel,omitempty" description:"OpenTelemetry observability configuration"`
// Platform identifies the cloud platform where the node is running (e.g., aws, gcp, hetzner)
Platform string

// Image specifies the container image identifier including repository and tag
Image string

// HttpPort defines the HTTP port for the API server to listen on (default: 7070)
HttpPort int

// Region identifies the geographic region where this node is deployed
Region string

// --- Cluster configuration ---

ClusterEnabled bool

// ClusterNodeID is the unique identifier for this node within the cluster
ClusterNodeID string

// --- Advertise Address configuration ---

// ClusterAdvertiseAddrStatic is a static IP address or hostname for node discovery
ClusterAdvertiseAddrStatic string

// ClusterAdvertiseAddrAwsEcsMetadata enables automatic address discovery using AWS ECS container metadata
ClusterAdvertiseAddrAwsEcsMetadata bool

// ClusterRpcPort is the port used for internal RPC communication between nodes (default: 7071)
ClusterRpcPort int

// ClusterGossipPort is the port used for cluster membership and failure detection (default: 7072)
ClusterGossipPort int

// --- Discovery configuration ---

// ClusterDiscoveryStaticAddrs lists seed node addresses for static cluster configuration
ClusterDiscoveryStaticAddrs []string

// ClusterDiscoveryRedisURL provides a Redis connection string for dynamic cluster discovery
ClusterDiscoveryRedisURL string

// --- Logs configuration ---

// LogsColor enables ANSI color codes in log output
LogsColor bool

// --- ClickHouse configuration ---

// ClickhouseURL is the ClickHouse database connection string
ClickhouseURL string

// --- Database configuration ---

// DatabasePrimary is the primary database connection string for read and write operations
DatabasePrimary string

// DatabaseReadonlyReplica is an optional read-replica database connection string for read operations
DatabaseReadonlyReplica string

// --- OpenTelemetry configuration ---

// OtelOtlpEndpoint specifies the OpenTelemetry collector endpoint for metrics, traces, and logs
OtelOtlpEndpoint string
}

func (c nodeConfig) Validate() error {

if c.ClusterEnabled {
err := assert.Multi(
assert.NotEmpty(c.ClusterNodeID),
assert.Greater(c.ClusterRpcPort, 0),
assert.Greater(c.ClusterGossipPort, 0),
assert.True(c.ClusterAdvertiseAddrStatic != "" || c.ClusterAdvertiseAddrAwsEcsMetadata),
assert.True(len(c.ClusterDiscoveryStaticAddrs) > 0 || c.ClusterDiscoveryRedisURL != ""),
)
if err != nil {
return err
}
}

return nil
}

// ConfigFromFlags creates a nodeConfig from CLI flags
func configFromFlags(cmd *cli.Command) nodeConfig {

config := nodeConfig{
// Basic configuration
Platform: cmd.String("platform"),
Image: cmd.String("image"),
HttpPort: int(cmd.Int("http-port")),
Region: cmd.String("region"),

// Database configuration
DatabasePrimary: cmd.String("database-primary"),
DatabaseReadonlyReplica: cmd.String("database-readonly-replica"),

// Logs
LogsColor: cmd.Bool("color"),

// ClickHouse
ClickhouseURL: cmd.String("clickhouse-url"),

// OpenTelemetry configuration
OtelOtlpEndpoint: cmd.String("otel-otlp-endpoint"),

// Cluster
ClusterEnabled: cmd.Bool("cluster"),
ClusterNodeID: cmd.String("cluster-node-id"),
ClusterRpcPort: int(cmd.Int("cluster-rpc-port")),
ClusterGossipPort: int(cmd.Int("cluster-gossip-port")),
ClusterAdvertiseAddrStatic: cmd.String("cluster-advertise-addr-static"),
ClusterAdvertiseAddrAwsEcsMetadata: cmd.Bool("cluster-advertise-addr-aws-ecs-metadata"),
ClusterDiscoveryStaticAddrs: cmd.StringSlice("cluster-discovery-static-addrs"),
ClusterDiscoveryRedisURL: cmd.String("cluster-discovery-redis-url"),
}

return config

}
Loading

0 comments on commit 53f0fc2

Please sign in to comment.