Skip to content

Commit

Permalink
feat #196: Add Go templating for probe http port
Browse files Browse the repository at this point in the history
  • Loading branch information
F1bonacc1 committed Aug 9, 2024
1 parent c3a16e8 commit 2ad462d
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 30 deletions.
3 changes: 2 additions & 1 deletion process-compose.override.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ vars:
VERSION: v0.75.0
PWD: /tmp
DISABLED: true
PORT: 80

processes:
process0:
Expand Down Expand Up @@ -144,7 +145,7 @@ processes:
http_get:
host: 127.0.0.1
path: "/"
port: 80
port: "{{.PORT}}"
initial_delay_seconds: 5
period_seconds: 10
timeout_seconds: 5
Expand Down
46 changes: 26 additions & 20 deletions src/health/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package health
import (
"fmt"
"net/url"
"strconv"
"strings"
)

Expand All @@ -22,18 +23,19 @@ type ExecProbe struct {
}

type HttpProbe struct {
Host string `yaml:"host,omitempty"`
Path string `yaml:"path,omitempty"`
Scheme string `yaml:"scheme,omitempty"`
Port int `yaml:"port,omitempty"`
Host string `yaml:"host,omitempty"`
Path string `yaml:"path,omitempty"`
Scheme string `yaml:"scheme,omitempty"`
Port string `yaml:"port,omitempty"`
numPort int
}

func (h HttpProbe) getUrl() (*url.URL, error) {
func (h *HttpProbe) getUrl() (*url.URL, error) {
urlStr := ""
if h.Port != 0 {
urlStr = fmt.Sprintf("%s://%s:%d%s", h.Scheme, h.Host, h.Port, h.Path)
if h.numPort != 0 {
urlStr = fmt.Sprintf("%s://%s:%d%s", h.Scheme, h.Host, h.numPort, h.Path)
}
if h.Port == 0 {
if h.numPort == 0 {
urlStr = fmt.Sprintf("%s://%s%s", h.Scheme, h.Host, h.Path)
}
return url.Parse(urlStr)
Expand All @@ -56,24 +58,28 @@ func (p *Probe) validateAndSetDefaults() {
p.FailureThreshold = 3
}

p.validateAndSetHttpDefaults()
if p.HttpGet != nil {
p.HttpGet.validateAndSetHttpDefaults()
}
}

func (p *Probe) validateAndSetHttpDefaults() {
if p.HttpGet == nil {
return
func (p *HttpProbe) validateAndSetHttpDefaults() {
if len(strings.TrimSpace(p.Host)) == 0 {
p.Host = "127.0.0.1"
}
if len(strings.TrimSpace(p.HttpGet.Host)) == 0 {
p.HttpGet.Host = "127.0.0.1"
if len(strings.TrimSpace(p.Scheme)) == 0 {
p.Scheme = "http"
}
if len(strings.TrimSpace(p.HttpGet.Scheme)) == 0 {
p.HttpGet.Scheme = "http"
if len(strings.TrimSpace(p.Path)) == 0 {
p.Path = "/"
}
if len(strings.TrimSpace(p.HttpGet.Path)) == 0 {
p.HttpGet.Path = "/"
if p.Port == "" {
p.numPort = 0
} else {
p.numPort, _ = strconv.Atoi(p.Port)
}
if p.HttpGet.Port < 1 || p.HttpGet.Port > 65535 {
if p.numPort < 1 || p.numPort > 65535 {
// if undefined or wrong value - will be treated as undefined
p.HttpGet.Port = 0
p.numPort = 0
}
}
7 changes: 4 additions & 3 deletions src/health/probe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestHttpProbe_getUrl(t *testing.T) {
Host string
Path string
Scheme string
Port int
Port string
}
tUrl, _ := url.Parse("http://google.com/isAlive")
tsUrl, _ := url.Parse("https://google.com:443/isAlive")
Expand All @@ -119,7 +119,7 @@ func TestHttpProbe_getUrl(t *testing.T) {
Host: "google.com",
Path: "/isAlive",
Scheme: "http",
Port: 0,
Port: "0",
},
want: tUrl,
wantErr: false,
Expand All @@ -130,7 +130,7 @@ func TestHttpProbe_getUrl(t *testing.T) {
Host: "google.com",
Path: "/isAlive",
Scheme: "https",
Port: 443,
Port: "443",
},
want: tsUrl,
wantErr: false,
Expand All @@ -144,6 +144,7 @@ func TestHttpProbe_getUrl(t *testing.T) {
Scheme: tt.fields.Scheme,
Port: tt.fields.Port,
}
h.validateAndSetHttpDefaults()
got, err := h.getUrl()
if (err != nil) != tt.wantErr {
t.Errorf("HttpProbe.getUrl() error = %v, wantErr %v", err, tt.wantErr)
Expand Down
10 changes: 5 additions & 5 deletions src/loader/merger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func getBaseProcess() *types.ProcessConfig {
Host: "127.0.0.1",
Path: "/is",
Scheme: "http",
Port: 80,
Port: "80",
},
InitialDelay: 5,
PeriodSeconds: 4,
Expand Down Expand Up @@ -87,7 +87,7 @@ func getOverrideProcess() *types.ProcessConfig {
Host: "google.com",
Path: "/isAlive",
Scheme: "https",
Port: 443,
Port: "443",
},
InitialDelay: 1,
PeriodSeconds: 2,
Expand All @@ -100,7 +100,7 @@ func getOverrideProcess() *types.ProcessConfig {
Host: "google.com",
Path: "/isAlive",
Scheme: "https",
Port: 443,
Port: "443",
},
InitialDelay: 1,
PeriodSeconds: 2,
Expand Down Expand Up @@ -154,7 +154,7 @@ func getMergedProcess() *types.ProcessConfig {
Host: "google.com",
Path: "/isAlive",
Scheme: "https",
Port: 443,
Port: "443",
},
InitialDelay: 1,
PeriodSeconds: 2,
Expand All @@ -167,7 +167,7 @@ func getMergedProcess() *types.ProcessConfig {
Host: "google.com",
Path: "/isAlive",
Scheme: "https",
Port: 443,
Port: "443",
},
InitialDelay: 1,
PeriodSeconds: 2,
Expand Down
1 change: 1 addition & 0 deletions src/loader/mutators.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,6 @@ func renderProbe(probe *health.Probe, tpl *templater.Templater, vars types.Vars)
probe.HttpGet.Path = tpl.RenderWithExtraVars(probe.HttpGet.Path, vars)
probe.HttpGet.Host = tpl.RenderWithExtraVars(probe.HttpGet.Host, vars)
probe.HttpGet.Scheme = tpl.RenderWithExtraVars(probe.HttpGet.Scheme, vars)
probe.HttpGet.Port = tpl.RenderWithExtraVars(probe.HttpGet.Port, vars)
}
}
1 change: 1 addition & 0 deletions www/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ Variables in Process Compose rely on [Go template engine](https://pkg.go.dev/tex
* `processes.process.<probe>.http_get.host`
* `processes.process.<probe>.http_get.path`
* `processes.process.<probe>.http_get.scheme`
* `processes.process.<probe>.http_get.port`

### Local (Per Process)

Expand Down
2 changes: 1 addition & 1 deletion www/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ markdown_extensions:
emoji_generator: !!python/name:materialx.emoji.to_svg

copyright: |
&copy; 2023 <a href="https://github.com/f1bonacc1" target="_blank" rel="noopener">Eugene Berger</a>
&copy; 2024 <a href="https://github.com/f1bonacc1" target="_blank" rel="noopener">Eugene Berger</a>

0 comments on commit 2ad462d

Please sign in to comment.