Skip to content

Commit

Permalink
chore: replace echo with stdlib's plain old http server
Browse files Browse the repository at this point in the history
  • Loading branch information
omissis committed Jul 20, 2023
1 parent 23157ca commit c9da939
Show file tree
Hide file tree
Showing 19 changed files with 312 additions and 115 deletions.
37 changes: 36 additions & 1 deletion .rules/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,39 @@ linters:
linters-settings:
decorder:
disable-init-func-first-check: false
depguard:
# Rules to apply.
#
# Variables:
# - File Variables
# you can still use and exclamation mark ! in front of a variable to say not to use it.
# Example !$test will match any file that is not a go test file.
#
# `$all` - matches all go files
# `$test` - matches all go test files
#
# - Package Variables
#
# `$gostd` - matches all of go's standard library (Pulled from `GOROOT`)
#
# Default: Only allow $gostd in all files.
rules:
# Name of a rule.
main:
# List of file globs that will match this list of settings to compare against.
# Default: $all
files: []
# List of allowed packages.
allow:
- $gostd
- github.com/itchyny/gojq
- github.com/omissis
- github.com/spf13/cobra
- github.com/spf13/pflag
- github.com/spf13/viper
- k8s.io
# Packages that are not allowed where the value is a suggestion.
deny: []
errcheck:
check-type-assertions: true
check-blank: true
Expand Down Expand Up @@ -430,7 +463,6 @@ linters-settings:
ignore-names:
- err
ignore-decls:
- c echo.Context
- t testing.T
- e error
- i int
Expand Down Expand Up @@ -473,6 +505,9 @@ issues:
- path: main\.go
linters:
- gochecknoglobals
- path: internal/app/container.go
linters:
- gomnd

# Independently of option `exclude` we use default exclude patterns,
# it can be disabled by this option.
Expand Down
2 changes: 1 addition & 1 deletion Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ helm_resource(
k8s_resource(
workload="kube-apiserver-proxy",
links=[
link("https://api.kube-apiserver-proxy.dev/", "kube-apiserver-proxy"),
link("https://api.kube-apiserver-proxy.dev/api/v1/pods", "kube-apiserver-proxy"),
],
labels="projects",
trigger_mode=TRIGGER_MODE_AUTO
Expand Down
10 changes: 1 addition & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ go 1.20
require (
github.com/google/go-cmp v0.5.9
github.com/itchyny/gojq v0.12.13
github.com/labstack/echo v3.3.10+incompatible
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.4
go.uber.org/mock v0.1.0
k8s.io/api v0.27.3
k8s.io/apimachinery v0.27.3
k8s.io/client-go v0.27.3
k8s.io/kubectl v0.27.3
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/emicklei/go-restful/v3 v3.10.2 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
Expand All @@ -36,11 +35,8 @@ require (
github.com/itchyny/timefmt-go v0.1.5 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/labstack/gommon v0.4.0 // indirect
github.com/magiconair/properties v1.8.7 // 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.19 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand All @@ -51,9 +47,6 @@ require (
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/oauth2 v0.7.0 // indirect
golang.org/x/sys v0.8.0 // indirect
Expand All @@ -66,7 +59,6 @@ require (
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.27.3 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect
Expand Down
25 changes: 0 additions & 25 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
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=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
github.com/emicklei/go-restful/v3 v3.10.2 h1:hIovbnmBTLjHXkqEBUz3HGpXZdM7ZrE9fJIZIqlJLqE=
github.com/emicklei/go-restful/v3 v3.10.2/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
Expand Down Expand Up @@ -187,21 +185,10 @@ 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 v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg=
github.com/labstack/echo v3.3.10+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s=
github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down Expand Up @@ -251,11 +238,6 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8=
github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
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.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
Expand All @@ -278,7 +260,6 @@ golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa h1:zuSxTR4o9y82ebqCUJYNGJbGPo6sKVl54f/TVDObg1c=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down Expand Up @@ -407,12 +388,7 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down Expand Up @@ -603,7 +579,6 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
54 changes: 35 additions & 19 deletions internal/app/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package app
import (
"fmt"
"net/http"
"time"

"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"k8s.io/client-go/rest"

httpx "github.com/omissis/kube-apiserver-proxy/internal/x/http"
"github.com/omissis/kube-apiserver-proxy/pkg/kube"
"github.com/omissis/kube-apiserver-proxy/pkg/kube/proxy"
)
Expand All @@ -22,24 +22,27 @@ type ContainerFactoryFunc func() (*Container, error)

func NewDefaultParameters() Parameters {
return Parameters{
APIServerTimeout: 5 * time.Second,
APIServerHost: "0.0.0.0",
APIServerPort: apiServerPort,
APIAllowedOrigins: []string{"http://localhost:3000", "http://kasp.dev"},
}
}

type Parameters struct {
APIServerTimeout time.Duration
APIServerHost string
APIServerPort uint16
APIAllowedOrigins []string
KubeconfigPath string
}

type services struct {
echo *echo.Echo
httpServer *http.Server
httpServeMux *httpx.ServeMux
k8sRESTClientFactory *kube.DefaultRESTClientFactory
k8sHTTProxy *proxy.HTTP
k8sHttpClient *http.Client
k8sHTTPClient *http.Client
k8sRESTConfigFactory *kube.DefaultRESTConfigFactory
}

Expand All @@ -54,19 +57,32 @@ type Container struct {
services
}

func (c *Container) Echo() *echo.Echo {
if c.echo == nil {
c.echo = echo.New()
c.echo.Debug = true
func (c *Container) HTTPServeMux() *httpx.ServeMux {
if c.httpServeMux == nil {
c.httpServeMux = httpx.NewServeMux(nil)

if len(c.Parameters.APIAllowedOrigins) > 0 {
c.echo.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: c.Parameters.APIAllowedOrigins,
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept},
}))
c.httpServeMux.Use(
httpx.CORSMuxMiddleware(httpx.CORSConfig{
AllowOrigins: c.Parameters.APIAllowedOrigins,
}),
)
}
}

return c.httpServeMux
}

func (c *Container) HTTPServer() *http.Server {
if c.httpServer == nil {
c.httpServer = &http.Server{
Addr: fmt.Sprintf("%s:%d", c.APIServerHost, c.APIServerPort),
Handler: c.HTTPServeMux(),
ReadHeaderTimeout: c.APIServerTimeout,
}
}

return c.echo
return c.httpServer
}

func (c *Container) K8sHTTPProxy() *proxy.HTTP {
Expand All @@ -83,7 +99,7 @@ func (c *Container) RESTClientFactory() *kube.DefaultRESTClientFactory {
if c.k8sRESTClientFactory == nil {
c.k8sRESTClientFactory = kube.NewDefaultRESTClientFactory(
c.RESTConfigFactory(),
c.K8sHttpClient(),
c.K8sHTTPClient(),
c.KubeconfigPath,
)
}
Expand All @@ -99,17 +115,17 @@ func (c *Container) RESTConfigFactory() *kube.DefaultRESTConfigFactory {
return c.k8sRESTConfigFactory
}

func (c *Container) K8sHttpClient() *http.Client {
if c.k8sHttpClient == nil {
k8sHttpClient, err := rest.HTTPClientFor(c.K8sRestConfig())
func (c *Container) K8sHTTPClient() *http.Client {
if c.k8sHTTPClient == nil {
k8sHTTPClient, err := rest.HTTPClientFor(c.K8sRestConfig())
if err != nil {
panic(err)
}

c.k8sHttpClient = k8sHttpClient
c.k8sHTTPClient = k8sHTTPClient
}

return c.k8sHttpClient
return c.k8sHTTPClient
}

func (c *Container) K8sRestConfig() *rest.Config {
Expand Down
30 changes: 21 additions & 9 deletions internal/app/container_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build unit

package app_test

import (
Expand All @@ -8,26 +10,36 @@ import (
"github.com/omissis/kube-apiserver-proxy/internal/app"
)

func TestEcho(t *testing.T) {
func TestHTTPServeMux(t *testing.T) {
t.Parallel()

container := app.NewContainer()

assert.NotNil(t, container.Echo())
assert.NotNil(t, container.HTTPServeMux())
}

func TestK8sHTTPProxy(t *testing.T) {
func TestHTTPServer(t *testing.T) {
t.Parallel()

container := app.NewContainer()

assert.NotNil(t, container.K8sHTTPProxy())
assert.NotNil(t, container.HTTPServer())
}

func TestRESTClientFactory(t *testing.T) {
t.Parallel()
// TODO: enable this test
// func TestK8sHTTPProxy(t *testing.T) {
// t.Parallel()

container := app.NewContainer()
// container := app.NewContainer()

assert.NotNil(t, container.RESTClientFactory())
}
// assert.NotNil(t, container.K8sHTTPProxy())
// }

// TODO: enable this test
// func TestRESTClientFactory(t *testing.T) {
// t.Parallel()

// container := app.NewContainer()

// assert.NotNil(t, container.RESTClientFactory())
// }
Loading

0 comments on commit c9da939

Please sign in to comment.