Skip to content

Commit

Permalink
Support different port for service checks
Browse files Browse the repository at this point in the history
  • Loading branch information
dangra committed Jan 17, 2025
1 parent 9bbba45 commit a24a095
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 5 deletions.
2 changes: 2 additions & 0 deletions internal/appconfig/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,15 @@ func TestToDefinition(t *testing.T) {
},
"tcp_checks": []any{
map[string]any{
"port": int64(1001),
"interval": "21s",
"timeout": "4s",
"grace_period": "1s",
},
},
"http_checks": []any{
map[string]any{
"port": int64(2020),
"interval": "1m21s",
"timeout": "7s",
"grace_period": "2s",
Expand Down
11 changes: 11 additions & 0 deletions internal/appconfig/machines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,17 @@ func TestToMachineConfig_services(t *testing.T) {
InternalPort: 1004,
Autostart: nil,
Autostop: nil,
Checks: []fly.MachineCheck{
{
Port: fly.Pointer(9090),
Type: fly.Pointer("tcp"),
}, {
Port: fly.Pointer(2020),
Type: fly.Pointer("http"),
HTTPMethod: fly.Pointer("GET"),
HTTPPath: fly.Pointer("/"),
},
},
},
}

Expand Down
2 changes: 2 additions & 0 deletions internal/appconfig/serde_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ func TestLoadTOMLAppConfigReferenceFormat(t *testing.T) {

TCPChecks: []*ServiceTCPCheck{
{
Port: fly.Pointer(1001),
Interval: fly.MustParseDuration("21s"),
Timeout: fly.MustParseDuration("4s"),
GracePeriod: fly.MustParseDuration("1s"),
Expand All @@ -575,6 +576,7 @@ func TestLoadTOMLAppConfigReferenceFormat(t *testing.T) {

HTTPChecks: []*ServiceHTTPCheck{
{
Port: fly.Pointer(2020),
Interval: fly.MustParseDuration("81s"),
Timeout: fly.MustParseDuration("7s"),
GracePeriod: fly.MustParseDuration("2s"),
Expand Down
15 changes: 10 additions & 5 deletions internal/appconfig/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"

"github.com/samber/lo"
fly "github.com/superfly/fly-go"
"github.com/superfly/flyctl/internal/sentry"
)
Expand All @@ -26,12 +25,14 @@ type Service struct {
}

type ServiceTCPCheck struct {
Port *int `json:"port,omitempty" toml:"port,omitempty"`
Interval *fly.Duration `json:"interval,omitempty" toml:"interval,omitempty"`
Timeout *fly.Duration `json:"timeout,omitempty" toml:"timeout,omitempty"`
GracePeriod *fly.Duration `toml:"grace_period,omitempty" json:"grace_period,omitempty"`
}

type ServiceHTTPCheck struct {
Port *int `json:"port,omitempty" toml:"port,omitempty"`
Interval *fly.Duration `json:"interval,omitempty" toml:"interval,omitempty"`
Timeout *fly.Duration `json:"timeout,omitempty" toml:"timeout,omitempty"`
GracePeriod *fly.Duration `toml:"grace_period,omitempty" json:"grace_period,omitempty"`
Expand Down Expand Up @@ -122,7 +123,13 @@ func (svc *Service) toMachineService() *fly.MachineService {
}

func (chk *ServiceHTTPCheck) toMachineCheck() *fly.MachineCheck {
var httpHeaders []fly.MachineHTTPHeader
for k, v := range chk.HTTPHeaders {
httpHeaders = append(httpHeaders, fly.MachineHTTPHeader{Name: k, Values: []string{v}})
}

return &fly.MachineCheck{
Port: chk.Port,
Type: fly.Pointer("http"),
Interval: chk.Interval,
Timeout: chk.Timeout,
Expand All @@ -132,10 +139,7 @@ func (chk *ServiceHTTPCheck) toMachineCheck() *fly.MachineCheck {
HTTPProtocol: chk.HTTPProtocol,
HTTPSkipTLSVerify: chk.HTTPTLSSkipVerify,
HTTPTLSServerName: chk.HTTPTLSServerName,
HTTPHeaders: lo.MapToSlice(
chk.HTTPHeaders, func(k string, v string) fly.MachineHTTPHeader {
return fly.MachineHTTPHeader{Name: k, Values: []string{v}}
}),
HTTPHeaders: httpHeaders,
}
}

Expand All @@ -145,6 +149,7 @@ func (chk *ServiceHTTPCheck) String(port int) string {

func (chk *ServiceTCPCheck) toMachineCheck() *fly.MachineCheck {
return &fly.MachineCheck{
Port: chk.Port,
Type: fly.Pointer("tcp"),
Interval: chk.Interval,
Timeout: chk.Timeout,
Expand Down
2 changes: 2 additions & 0 deletions internal/appconfig/testdata/full-reference.toml
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,13 @@ host_dedication_id = "06031957"
idle_timeout = 600

[[services.tcp_checks]]
port = 1001
interval = "21s"
timeout = "4s"
grace_period = "1s"

[[services.http_checks]]
port = 2020
interval = "1m21s"
timeout = "7s"
grace_period = "2s"
Expand Down
8 changes: 8 additions & 0 deletions internal/appconfig/testdata/tomachine-services.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ primary_region = "scl"
[[services]]
protocol = "tcp"
internal_port = 1004

[[services.tcp_checks]]
port = 9090

[[services.http_checks]]
port = 2020
method = "GET"
path = "/"

0 comments on commit a24a095

Please sign in to comment.