Skip to content

Commit

Permalink
Move healthz to metrics server & add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xinbinhuang authored and binh-stripe committed Oct 26, 2022
1 parent 54a70d5 commit 550ed09
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 14 deletions.
2 changes: 1 addition & 1 deletion charts/caddy-ingress-controller/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ spec:
initialDelaySeconds: 3
periodSeconds: 10
httpGet:
port: 80
port: 9765
path: /healthz
{{- with .Values.nodeSelector }}
nodeSelector:
Expand Down
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ require (
k8s.io/client-go v0.23.6
)

require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.4.0 // indirect
)

require (
filippo.io/edwards25519 v1.0.0-rc.1 // indirect
github.com/AndreasBriese/bbloom v0.0.0-20190825152654-46b345b51c96 // indirect
Expand Down Expand Up @@ -100,6 +105,7 @@ require (
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stoewer/go-strcase v1.2.0 // indirect
github.com/stretchr/testify v1.8.0
github.com/tailscale/tscert v0.0.0-20220125204807-4509a5fbaf74 // indirect
github.com/urfave/cli v1.22.5 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
Expand Down Expand Up @@ -128,7 +134,7 @@ require (
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
howett.net/plist v1.0.0 // indirect
k8s.io/klog/v2 v2.30.0 // indirect
k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,8 @@ github.com/streadway/handy v0.0.0-20190108123426-d5acb3125c2a/go.mod h1:qNTQ5P5J
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v0.0.0-20170130113145-4d4bfba8f1d1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand All @@ -1083,6 +1085,8 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tailscale/tscert v0.0.0-20220125204807-4509a5fbaf74 h1:uFx5aih29p2IaRUF0lJwtVViCXStlvnPPE3NEmM4Ivs=
github.com/tailscale/tscert v0.0.0-20220125204807-4509a5fbaf74/go.mod h1:hL4gB6APAasMR2NNi/JHzqKkxW3EPQlFgLEq9PMi2t0=
Expand Down Expand Up @@ -1862,6 +1866,8 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
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=
grpc.go4.org v0.0.0-20170609214715-11d0a25b4919/go.mod h1:77eQGdRu53HpSqPFJFmuJdjuHRquDANNeA4x7B8WQ9o=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
37 changes: 37 additions & 0 deletions internal/caddy/convert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package caddy

import (
"encoding/json"
"os"
"testing"

"github.com/stretchr/testify/require"

"github.com/caddyserver/ingress/pkg/store"
)

func TestConvertToCaddyConfig(t *testing.T) {
tests := []struct {
name string
expectedConfigPath string
}{
{
name: "default",
expectedConfigPath: "./test_data/default.json",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cfg, err := Converter{}.ConvertToCaddyConfig(store.NewStore(store.Options{}, &store.PodInfo{}))
require.NoError(t, err)

cfgJson, err := json.Marshal(cfg)
require.NoError(t, err)

expectedCfg, err := os.ReadFile(test.expectedConfigPath)
require.NoError(t, err)

require.JSONEq(t, string(expectedCfg), string(cfgJson))
})
}
}
2 changes: 1 addition & 1 deletion internal/caddy/global/healthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (p HealthzPlugin) GlobalHandler(config *converter.Config, store *store.Stor
}},
}

config.GetHTTPServer().Routes = append(config.GetHTTPServer().Routes, healthzRoute)
config.GetMetricsServer().Routes = append(config.GetMetricsServer().Routes, healthzRoute)
return nil
}

Expand Down
17 changes: 6 additions & 11 deletions internal/caddy/global/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,15 @@ func init() {
}

func (p MetricsPlugin) GlobalHandler(config *converter.Config, store *store.Store) error {
httpApp := config.Apps["http"].(*caddyhttp.App)

if store.ConfigMap.Metrics {
httpApp.Servers[converter.MetricsServer] = &caddyhttp.Server{
Listen: []string{":9765"},
AutoHTTPS: &caddyhttp.AutoHTTPSConfig{Disabled: true},
Routes: []caddyhttp.Route{{
HandlersRaw: []json.RawMessage{json.RawMessage(`{ "handler": "metrics" }`)},
MatcherSetsRaw: []caddy.ModuleMap{{
"path": caddyconfig.JSON(caddyhttp.MatchPath{"/metrics"}, nil),
}},
metricsRoute := caddyhttp.Route{
HandlersRaw: []json.RawMessage{json.RawMessage(`{ "handler": "metrics" }`)},
MatcherSetsRaw: []caddy.ModuleMap{{
"path": caddyconfig.JSON(caddyhttp.MatchPath{"/metrics"}, nil),
}},
}


config.GetMetricsServer().Routes = append(config.GetMetricsServer().Routes, metricsRoute)
}
return nil
}
Expand Down
27 changes: 27 additions & 0 deletions internal/caddy/test_data/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"admin": {},
"storage": { "module": "secret_store", "namespace": "", "leaseId": "" },
"apps": {
"http": {
"servers": {
"ingress_server": {
"listen": [":80", ":443"],
"tls_connection_policies": [{}],
"automatic_https": {}
},
"metrics_server": {
"listen": [":9765"],
"routes": [
{
"match": [{ "path": ["/healthz"] }],
"handle": [{ "handler": "static_response", "status_code": 200 }]
}
],
"automatic_https": { "disable": true }
}
}
},
"tls": {}
},
"logging": {}
}
8 changes: 8 additions & 0 deletions pkg/converter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func (c Config) GetHTTPServer() *caddyhttp.Server {
return c.Apps["http"].(*caddyhttp.App).Servers[HttpServer]
}

func (c Config) GetMetricsServer() *caddyhttp.Server {
return c.Apps["http"].(*caddyhttp.App).Servers[MetricsServer]
}

func (c Config) GetTLSApp() *caddytls.TLS {
return c.Apps["tls"].(*caddytls.TLS)
}
Expand All @@ -50,6 +54,10 @@ func NewConfig() *Config {
&caddytls.ConnectionPolicy{},
},
},
MetricsServer: {
Listen: []string{":9765"},
AutoHTTPS: &caddyhttp.AutoHTTPSConfig{Disabled: true},
},
},
},
},
Expand Down

0 comments on commit 550ed09

Please sign in to comment.