Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

service discovery #2882

Merged
merged 10 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,9 @@ tasks:
dir: internal/db
cmds:
- pnpm drizzle-kit generate --dialect=mysql

nuke-docker:
cmds:
- docker stop $(docker ps -aq) || true
- docker system prune -af
- docker volume prune --all -f
6 changes: 4 additions & 2 deletions apps/agent/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.22-alpine AS builder
FROM golang:1.22 AS builder



Expand All @@ -10,12 +10,14 @@ COPY . .
ARG VERSION
RUN go build -o bin/unkey -ldflags "-X 'github.com/unkeyed/unkey/apps/agent/pkg/version.Version=${VERSION}'" ./cmd/main.go

FROM golang:1.22-alpine
FROM golang:1.22
WORKDIR /usr/local/bin
COPY --from=builder /go/src/github.com/unkeyed/unkey/apps/agent/bin/unkey .
COPY --from=builder /go/src/github.com/unkeyed/unkey/apps/agent/config.production.json .
COPY --from=builder /go/src/github.com/unkeyed/unkey/apps/agent/config.staging.json .
COPY --from=builder /go/src/github.com/unkeyed/unkey/apps/agent/config.docker.json .
COPY --from=builder /go/src/github.com/unkeyed/unkey/apps/agent/config.apprunner.production.json .
COPY --from=builder /go/src/github.com/unkeyed/unkey/apps/agent/config.apprunner.staging.json .
COPY --from=builder /go/src/github.com/unkeyed/unkey/apps/agent/pkg/openapi/openapi.json ./pkg/openapi/openapi.json

CMD [ "/usr/local/bin/unkey", "agent"]
4 changes: 3 additions & 1 deletion apps/agent/cmd/agent/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (

func setupLogging(cfg config.Agent) (logging.Logger, error) {

logger := logging.New(nil)
logger := logging.New(&logging.Config{
Color: cfg.Logging.Color,
})

// runId is unique per start of the agent, this is useful for differnetiating logs between
// deployments
Expand Down
52 changes: 52 additions & 0 deletions apps/agent/config.apprunner.production.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"$schema": "schema.json",
"platform": "aws",
"image": "${DOCKER_IMAGE}",
"nodeId": "",
"port": "${PORT}",
"rpcPort": "${RPC_PORT}",
"region": "aws::${AWS_REGION}",
"authToken": "${AUTH_TOKEN}",
"logging": {
"color": false,
"axiom": {
"dataset": "agent",
"token": "${AXIOM_TOKEN}"
}
},
"tracing": {
"axiom": {
"dataset": "tracing",
"token": "${AXIOM_TOKEN}"
}
},
"metrics": {
"axiom": {
"dataset": "metrics",
"token": "${AXIOM_TOKEN}"
}
},
"services": {
"vault": {
"s3Url": "${VAULT_S3_URL}",
"s3Bucket": "${VAULT_S3_BUCKET}",
"s3AccessKeyId": "${VAULT_S3_ACCESS_KEY_ID}",
"s3AccessKeySecret": "${VAULT_S3_ACCESS_KEY_SECRET}",
"masterKeys": "${VAULT_MASTER_KEYS}"
}
},
"cluster": {
"authToken": "${AUTH_TOKEN}",
"serfAddr": "[${FLY_PRIVATE_IP}]:${SERF_PORT}",
"rpcAddr": "http://${FLY_PRIVATE_IP}:${RPC_PORT}",
"join": {
"env": {
"addrs": []
}
}
},
"heartbeat": {
"interval": 60,
"url": "${HEARTBEAT_URL}"
}
}
32 changes: 32 additions & 0 deletions apps/agent/config.apprunner.staging.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"$schema": "schema.json",
"platform": "aws",
"image": "${DOCKER_IMAGE}",
"nodeId": "",
"port": "${PORT}",
"rpcPort": "${RPC_PORT}",
"region": "aws::${AWS_REGION}",
"authToken": "${AUTH_TOKEN}",
"logging": {
"color": false
},
"services": {
"vault": {
"s3Url": "${VAULT_S3_URL}",
"s3Bucket": "${VAULT_S3_BUCKET}",
"s3AccessKeyId": "${VAULT_S3_ACCESS_KEY_ID}",
"s3AccessKeySecret": "${VAULT_S3_ACCESS_KEY_SECRET}",
"masterKeys": "${VAULT_MASTER_KEYS}"
}
},
"cluster": {
"authToken": "${AUTH_TOKEN}",
"serfAddr": "[${FLY_PRIVATE_IP}]:${SERF_PORT}",
"rpcAddr": "http://${FLY_PRIVATE_IP}:${RPC_PORT}",
"join": {
"env": {
"addrs": []
}
}
}
}
1 change: 0 additions & 1 deletion apps/agent/pkg/api/validation/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ func (v *Validator) Body(r *http.Request, dest any) (openapi.ValidationError, bo
Type: "TODO docs link",
}, false
}
fmt.Printf("body %+v\n", dest)

return openapi.ValidationError{}, true

Expand Down
1 change: 1 addition & 0 deletions apps/agent/pkg/config/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type Agent struct {
Image string `json:"image,omitempty" description:"The image this agent is running"`
AuthToken string `json:"authToken" minLength:"1" description:"The token to use for http authentication"`
Logging *struct {
Color bool `json:"color,omitempty"`
Axiom *struct {
Dataset string `json:"dataset" minLength:"1" description:"The dataset to send logs to"`
Token string `json:"token" minLength:"1" description:"The token to use for authentication"`
Expand Down
3 changes: 2 additions & 1 deletion apps/agent/pkg/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const timeFormat = "2006-01-02T15:04:05.000MST"
type Config struct {
Debug bool
Writer []io.Writer
Color bool
}

func init() {
Expand All @@ -32,7 +33,7 @@ func New(config *Config) Logger {
config = &Config{}
}

consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: timeFormat}
consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: timeFormat, NoColor: !config.Color}

writers := []io.Writer{consoleWriter}
if len(config.Writer) > 0 {
Expand Down
8 changes: 0 additions & 8 deletions apps/agent/services/vault/storage/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,6 @@ func NewS3(config S3Config) (Storage, error) {
}

client := awsS3.NewFromConfig(cfg)
logger.Info().Msg("creating bucket if necessary")
logger.Info().Msgf("url: %s", config.S3URL)
_, err = client.CreateBucket(context.Background(), &awsS3.CreateBucketInput{
Bucket: aws.String(config.S3Bucket),
})
if err != nil && !strings.Contains(err.Error(), "BucketAlreadyOwnedByYou") {
return nil, fault.Wrap(err, fmsg.With("failed to create bucket"))
}

logger.Info().Msg("s3 storage initialized")

Expand Down
17 changes: 4 additions & 13 deletions deployment/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,21 @@ services:
ports:
- 3900:3900

api_lb:
container_name: api_lb
image: nginx:latest
volumes:
- ./nginx.apiv2.conf:/etc/nginx/nginx.conf:ro
depends_on:
- apiv2
ports:
- 7070:7070

apiv2:
deploy:
replicas: 3
endpoint_mode: vip


command: [ "api", "--config", "config.docker.json"]
build:
context: ../go
dockerfile: ./Dockerfile
ports:
- "7070:7070"
depends_on:
- mysql
- clickhouse
- redis
environment:
PORT: 7070
DATABASE_PRIMARY_DSN: "mysql://unkey:password@tcp(mysql:3900)/unkey"
CLICKHOUSE_URL: "clickhouse://default:password@clickhouse:9000"
REDIS_URL: "redis://redis:6379"
Expand Down
1 change: 1 addition & 0 deletions go/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ WORKDIR /usr/local/bin
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 .


ENTRYPOINT [ "/usr/local/bin/unkey"]
13 changes: 10 additions & 3 deletions go/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
version: '3'
version: "3"

tasks:
install:
cmd:
go mod tidy
cmd: go mod tidy
fmt:
cmds:
- go fmt ./...
Expand All @@ -16,7 +15,15 @@ tasks:
cmds:
- go build -o unkey ./main.go

generate:
cmds:
- go generate ./...
- buf generate

lint:
cmds:
- golangci-lint run

simulate:
cmds:
- go run github.com/jellevandenhooff/gosim/cmd/gosim test -v -run TestGosim
27 changes: 22 additions & 5 deletions go/cmd/api/config.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
package api

type nodeConfig struct {
Platform string `json:"platform,omitempty" description:"The platform this agent is running on"`
NodeId string `json:"nodeId,omitempty" description:"A unique node id"`
Image string `json:"image,omitempty" description:"The image this agent is running"`

Platform string `json:"platform,omitempty" description:"The platform this agent is running on"`
Image string `json:"image,omitempty" description:"The image this agent is running"`
HttpPort int `json:"httpPort" default:"7070" description:"Port to listen on"`
Schema string `json:"$schema,omitempty" description:"Make jsonschema happy"`
Region string `json:"region,omitempty" description:"The region this agent is running in"`
Port string `json:"port,omitempty" default:"8080" description:"Port to listen on"`
Heartbeat *struct {
URL string `json:"url" minLength:"1" description:"URL to send heartbeat to"`
Interval int `json:"interval" min:"1" description:"Interval in seconds to send heartbeat"`
} `json:"heartbeat,omitempty" description:"Send heartbeat to a URL"`

Cluster *struct {
NodeID string `json:"nodeId,omitempty" description:"A unique node id"`
AdvertiseAddr string `json:"advertiseAddr,omitempty" description:"The address to advertise to other nodes"`
RpcPort int `json:"rpcPort" default:"7071" description:"The port used for RPC"`
GossipPort int `json:"gossipPort" default:"7072" description:"The port used for gossip"`
Discovery *struct {
Static *struct {
Addrs []string `json:"addrs" minLength:"1" description:"List of node addresses"`
} `json:"static,omitempty" description:"Static cluster discovery configuration"`
AwsCloudmap *struct {
ServiceName string `json:"serviceName" minLength:"1" description:"Cloudmap service name"`
Region string `json:"region" minLength:"1" description:"Cloudmap region"`
} `json:"awsCloudmap,omitempty" description:"Cloudmap cluster discovery configuration"`
} `json:"discovery,omitempty" description:"Cluster discovery configuration, only one supported: static, cloudmap"`
} `json:"cluster,omitempty" description:"Cluster configuration"`

Logs *struct {
Color bool `json:"color" description:"Display color in logs"`
} `json:"logs,omitempty"`
RedisUrl string `json:"redisUrl"`
Clickhouse *struct {
Url string `json:"url" minLength:"1"`
Expand Down
Loading
Loading