Skip to content

Commit

Permalink
use box
Browse files Browse the repository at this point in the history
  • Loading branch information
mycrEEpy committed Aug 5, 2024
1 parent c10717b commit 71eebcf
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 94 deletions.
53 changes: 11 additions & 42 deletions cmd/pgopher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,14 @@ import (
)

var (
logLevel = flag.String("log-level", "INFO", "log level")
logJsonFormat = flag.Bool("log-json", false, "log in json format")
logSource = flag.Bool("log-source", false, "log source code position")
cfgFile = flag.String("config", "pgopher.yml", "config file")
pprofEnabled = flag.Bool("pprof", false, "enable pprof endpoint")
cfgFile = flag.String("config", "pgopher.yml", "config file")
pprofEnabled = flag.Bool("pprof", false, "enable pprof endpoint")
)

func init() {
func main() {
flag.Parse()
setupLogger()
}

func setupLogger() {
level := &slog.LevelVar{}

err := level.UnmarshalText([]byte(*logLevel))
if err != nil {
slog.Error("failed to parse log level", slog.String("err", err.Error()))
os.Exit(1)
}

handler := &slog.HandlerOptions{
AddSource: *logSource,
Level: level,
}

logger := slog.New(slog.NewTextHandler(os.Stderr, handler))

if *logJsonFormat {
logger = slog.New(slog.NewJSONHandler(os.Stderr, handler))
}

slog.SetDefault(logger)
}

func main() {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()

cfg, err := pgopher.LoadConfig(*cfgFile)
Expand All @@ -60,35 +31,33 @@ func main() {
os.Exit(1)
}

slog.Debug("loaded configuration", slog.String("file", *cfgFile), slog.Any("config", cfg))

if *pprofEnabled {
go pprofServer(cfg.PprofListenAddress)
}

s, err := pgopher.NewServer(cfg)
if err != nil {
slog.Error("failed to create pgopher server", slog.String("err", err.Error()))
os.Exit(1)
}

if *pprofEnabled {
go pprofServer(cfg.PprofListenAddress, s.Logger)
}

err = s.Run(ctx)
if err != nil {
slog.Error("failed to run pgopher server", slog.String("err", err.Error()))
os.Exit(1)
}
}

func pprofServer(listenAddr string) {
func pprofServer(listenAddr string, logger *slog.Logger) {
pprofMux := http.NewServeMux()
pprofMux.HandleFunc("/debug/pprof/profile", pprof.Profile)

server := &http.Server{Addr: listenAddr, Handler: pprofMux}

slog.Info("starting pprof server", slog.String("listenAddr", listenAddr))
logger.Info("starting pprof server", slog.String("listenAddr", listenAddr))

err := server.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
slog.Error("pprof server failed", slog.String("err", err.Error()))
logger.Error("pprof server failed", slog.String("err", err.Error()))
}
}
11 changes: 5 additions & 6 deletions deploy/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,13 @@ spec:
- linux
containers:
- args:
- -log-level=INFO
- -log-json=true
- -config=/opt/pgopher/cfg/pgopher.yml
command:
- /opt/pgopher/pgopher
image: ghcr.io/mycreepy/pgopher:0.6.11
livenessProbe:
httpGet:
path: /_live
path: /healthz
port: http
initialDelaySeconds: 15
periodSeconds: 20
Expand All @@ -61,7 +59,7 @@ spec:
name: http
readinessProbe:
httpGet:
path: /_ready
path: /readyz
port: http
initialDelaySeconds: 5
periodSeconds: 10
Expand All @@ -82,9 +80,10 @@ spec:
capabilities:
drop:
- ALL
add:
- NET_BIND_SERVICE
securityContext:
runAsUser: 65534
runAsGroup: 65534
fsGroup: 65534
runAsNonRoot: true
seccompProfile:
type: RuntimeDefault
Expand Down
2 changes: 2 additions & 0 deletions deploy/namespace.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ apiVersion: v1
kind: Namespace
metadata:
name: pgopher
labels:
pod-security.kubernetes.io/enforce: restricted
1 change: 1 addition & 0 deletions deploy/pgopher.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
logLevel: info
listenAddress: ":8000"
pprofListenAddress: ":6060"
profilingTargets:
Expand Down
15 changes: 13 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
module github.com/mycreepy/pgopher

go 1.22.2
go 1.22.0

toolchain go1.22.5

require (
github.com/aws/aws-sdk-go-v2 v1.30.3
github.com/aws/aws-sdk-go-v2/config v1.27.27
github.com/aws/aws-sdk-go-v2/service/s3 v1.58.3
github.com/labstack/echo/v4 v4.12.0
github.com/mycreepy/box v0.4.0
github.com/robfig/cron/v3 v3.0.1
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.30.3
Expand All @@ -30,33 +33,41 @@ require (
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.30.3 // indirect
github.com/aws/smithy-go v1.20.3 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/labstack/echo-contrib v0.17.1 // indirect
github.com/labstack/gommon v0.4.2 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/prometheus/client_golang v1.19.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.53.0 // indirect
github.com/prometheus/procfs v0.13.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/term v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
26 changes: 22 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ github.com/aws/aws-sdk-go-v2/service/sts v1.30.3 h1:ZsDKRLXGWHk8WdtyYMoGNO7bTudr
github.com/aws/aws-sdk-go-v2/service/sts v1.30.3/go.mod h1:zwySh8fpFyXp9yOr/KVzxOl8SRqgf/IDw5aUt9UKFcQ=
github.com/aws/smithy-go v1.20.3 h1:ryHwveWzPV5BIof6fyDvor6V3iUL7nTfiTKXHiW05nE=
github.com/aws/smithy-go v1.20.3/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand All @@ -52,6 +56,8 @@ github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEe
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
Expand Down Expand Up @@ -82,6 +88,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/labstack/echo-contrib v0.17.1 h1:7I/he7ylVKsDUieaGRZ9XxxTYOjfQwVzHzUYrNykfCU=
github.com/labstack/echo-contrib v0.17.1/go.mod h1:SnsCZtwHBAZm5uBSAtQtXQHI3wqEA73hvTn0bYMKnZA=
github.com/labstack/echo/v4 v4.12.0 h1:IKpw49IMryVB2p1a4dzwlhP1O2Tf2E0Ir/450lH+kI0=
github.com/labstack/echo/v4 v4.12.0/go.mod h1:UP9Cr2DJXbOK3Kr9ONYzNowSh7HP0aG0ShAyycHSJvM=
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
Expand All @@ -100,12 +108,22 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/mycreepy/box v0.4.0 h1:q4qAH3Sw4oS5G6z9v5aaKFYmx1pYD8xLLxZnF0nrk40=
github.com/mycreepy/box v0.4.0/go.mod h1:9IQGEZa4LarvKBufmuLfUxrF4KIH9s8/h+CnL3Aw+PU=
github.com/onsi/ginkgo/v2 v2.15.0 h1:79HwNRBAZHOEwrczrgSOPy+eFTTlIGELKy5as+ClttY=
github.com/onsi/ginkgo/v2 v2.15.0/go.mod h1:HlxMHtYF57y6Dpf+mc5529KKmSq9h2FpCF+/ZkwUxKM=
github.com/onsi/gomega v1.31.0 h1:54UJxxj6cPInHS3a35wm6BK/F9nHYueZ1NVujHDrnXE=
github.com/onsi/gomega v1.31.0/go.mod h1:DW9aCi7U6Yi40wNVAvT6kzFnEVEI5n3DloYBiKiT6zk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.19.0 h1:ygXvpU1AoN1MhdzckN+PyD9QJOSD4x7kmXYlnfbA6JU=
github.com/prometheus/client_golang v1.19.0/go.mod h1:ZRM9uEAypZakd+q/x7+gmsvXdURP+DABIEIjnmDdp+k=
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
github.com/prometheus/common v0.53.0 h1:U2pL9w9nmJwJDa4qqLQ3ZaePJ6ZTwt7cMD3AG3+aLCE=
github.com/prometheus/common v0.53.0/go.mod h1:BrxBKv3FWBIGXw89Mg1AeBq7FSyRzXWI3l3e7W3RN5U=
github.com/prometheus/procfs v0.13.0 h1:GqzLlQyfsPbaEHaQkO7tbDlriv/4o5Hudv6OXHGKX7o=
github.com/prometheus/procfs v0.13.0/go.mod h1:cd4PFCR54QLnGKPaKGA6l+cfuNXtht43ZKY6tow0Y1g=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
Expand All @@ -119,8 +137,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
Expand All @@ -141,8 +159,8 @@ golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
golang.org/x/oauth2 v0.10.0 h1:zHCpF2Khkwy4mMB4bv0U37YtJdTGW8jI0glAApi0Kh8=
golang.org/x/oauth2 v0.10.0/go.mod h1:kTpgurOux7LqtuxjuyZa4Gj2gdezIt/jQtGnNFfypQI=
golang.org/x/oauth2 v0.18.0 h1:09qnuIAgzdx1XplqJvW6CQqMCtGZykZWcXzPMPUusvI=
golang.org/x/oauth2 v0.18.0/go.mod h1:Wf7knwG0MPoWIMMBgFlEaSUDaKskp0dCfrlJRJXbBi8=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
2 changes: 1 addition & 1 deletion internal/pgopher/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
func (s *Server) handleProfile(ctx echo.Context) error {
profile := ctx.Param("profile")

logger := slog.With(slog.String("profile", profile))
logger := s.Logger.With(slog.String("profile", profile))

if len(profile) == 0 || strings.Contains(profile, "..") {
logger.Error("invalid profile")
Expand Down
5 changes: 3 additions & 2 deletions internal/pgopher/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"os"
"time"

"github.com/mycreepy/box"
"gopkg.in/yaml.v3"
)

type Config struct {
ListenAddress string `yaml:"listenAddress"`
box.Config `yaml:",inline"`

PprofListenAddress string `yaml:"pprofListenAddress"`
ProfilingTargets []ProfilingTarget `yaml:"profilingTargets"`
Sink Sink `yaml:"sink"`
Expand Down Expand Up @@ -44,7 +46,6 @@ type KubernetesSinkOptions struct {

func DefaultConfig() Config {
return Config{
ListenAddress: ":8000",
PprofListenAddress: "localhost:8010",
ProfilingTargets: make([]ProfilingTarget, 0),
Sink: Sink{
Expand Down
15 changes: 0 additions & 15 deletions internal/pgopher/health.go

This file was deleted.

29 changes: 9 additions & 20 deletions internal/pgopher/pgopher.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,28 @@ import (

awsConfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/labstack/echo/v4"
"github.com/mycreepy/box"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)

type Server struct {
*box.Box

cfg Config
mux *echo.Echo
s3Client *s3.Client
kubeClient *kubernetes.Clientset
}

func NewServer(cfg Config) (*Server, error) {
s := &Server{
Box: box.New(box.WithConfig(cfg.Config), box.WithWebServer()),
cfg: cfg,
mux: echo.New(),
}

s.mux.HideBanner = true
s.mux.HidePort = true
s.Logger.Debug("loaded configuration", slog.Any("config", cfg))

s.mux.GET("/_ready", readinessProbe)
s.mux.GET("/_live", livenessProbe)
s.mux.GET("/api/v1/profile/:profile", s.handleProfile)
s.WebServer.GET("/api/v1/profile/:profile", s.handleProfile)

if cfg.Sink.Type == "s3" {
sdkConfig, err := awsConfig.LoadDefaultConfig(context.Background())
Expand Down Expand Up @@ -66,25 +64,16 @@ func (s *Server) Run(ctx context.Context) error {

go s.startScheduler(ctx, wg)

go func() {
<-ctx.Done()

err := s.mux.Shutdown(context.Background())
if err != nil {
slog.Error("failed to shutdown http server", slog.String("err", err.Error()))
}
}()

slog.Info("starting http server", slog.String("listenAddr", s.cfg.ListenAddress))
s.Logger.Info("starting http server", slog.String("listenAddr", s.cfg.ListenAddress))

err := s.mux.Start(s.cfg.ListenAddress)
err := s.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
return fmt.Errorf("http server failed: %w", err)
}

wg.Wait()

slog.Info("graceful shutdown completed, see you next time!")
s.Logger.Info("graceful shutdown completed, see you next time!")

return nil
}
Loading

0 comments on commit 71eebcf

Please sign in to comment.