Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backend): Support configuring connection keepalive parameters #1275

Merged
merged 7 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
)

require (
github.com/fastly/go-fastly/v9 v9.7.0
github.com/fastly/go-fastly/v9 v9.8.0
github.com/hashicorp/cap v0.7.0
github.com/kennygrant/sanitize v1.2.4
github.com/mholt/archiver v3.1.1+incompatible
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5/go.mod h1:qssHWj6
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
github.com/dustinkirkland/golang-petname v0.0.0-20231002161417-6a283f1aaaf2 h1:S6Dco8FtAhEI/qkg/00H6RdEGC+MCy5GPiQ+xweNRFE=
github.com/dustinkirkland/golang-petname v0.0.0-20231002161417-6a283f1aaaf2/go.mod h1:8AuBTZBRSFqEYBPYULd+NN474/zZBLP+6WeT5S9xlAc=
github.com/fastly/go-fastly/v9 v9.7.0 h1:RIHKQcsUT6n5kZHeDES47FsUWtoJm69/40tk5P8fi8Q=
github.com/fastly/go-fastly/v9 v9.7.0/go.mod h1:5w2jgJBZqQEebOwM/rRg7wutAcpDTziiMYWb/6qdM7U=
github.com/fastly/go-fastly/v9 v9.8.0 h1:15dtV3fmLlS/8wbdU3tBsW3Tb0Tj/gQrdS4v5mhtDDE=
github.com/fastly/go-fastly/v9 v9.8.0/go.mod h1:5w2jgJBZqQEebOwM/rRg7wutAcpDTziiMYWb/6qdM7U=
github.com/fastly/kingpin v2.1.12-0.20191105091915-95d230a53780+incompatible h1:FhrXlfhgGCS+uc6YwyiFUt04alnjpoX7vgDKJxS6Qbk=
github.com/fastly/kingpin v2.1.12-0.20191105091915-95d230a53780+incompatible/go.mod h1:U8UynVoU1SQaqD2I4ZqgYd5lx3A1ipQYn4aSt2Y5h6c=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
Expand Down
79 changes: 79 additions & 0 deletions pkg/commands/backend/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,31 @@ func TestBackendCreate(t *testing.T) {
},
WantOutput: "Created backend www.test.com (service 123 version 3)",
},
// The following tests verify parsing of the --tcp-ka-enable flag.
{
Arg: "--service-id 123 --version 3 --address 127.0.0.1 --name www.test.com --tcp-ka-enabled=true",
API: mock.API{
ListVersionsFn: testutil.ListVersions,
CreateBackendFn: createBackendOK,
},
WantOutput: "Created backend www.test.com (service 123 version 3)",
},
{
Arg: "--service-id 123 --version 3 --address 127.0.0.1 --name www.test.com --tcp-ka-enabled=false",
API: mock.API{
ListVersionsFn: testutil.ListVersions,
CreateBackendFn: createBackendOK,
},
WantOutput: "Created backend www.test.com (service 123 version 3)",
},
{
Arg: "--service-id 123 --version 3 --address 127.0.0.1 --name www.test.com --tcp-ka-enabled=invalid",
API: mock.API{
ListVersionsFn: testutil.ListVersions,
CreateBackendFn: createBackendOK,
},
WantError: "'tcp-ka-enable' flag must be one of the following [true, false]",
},
}
testutil.RunScenarios(t, []string{root.CommandName, "create"}, scenarios)
}
Expand Down Expand Up @@ -258,6 +283,37 @@ func TestBackendUpdate(t *testing.T) {
},
WantOutput: "Updated backend www.example.com (service 123 version 4)",
},
// The following tests verify parsing of the --tcp-ka-enable flag.
{
Arg: "--service-id 123 --version 1 --name www.test.com --tcp-ka-enabled=true --autoclone",
API: mock.API{
ListVersionsFn: testutil.ListVersions,
CloneVersionFn: testutil.CloneVersionResult(4),
GetBackendFn: getBackendOK,
UpdateBackendFn: updateBackendOK,
},
WantOutput: "Updated backend (service 123 version 4)",
},
{
Arg: "--service-id 123 --version 1 --name www.test.com --tcp-ka-enabled=false --autoclone",
API: mock.API{
ListVersionsFn: testutil.ListVersions,
CloneVersionFn: testutil.CloneVersionResult(4),
GetBackendFn: getBackendOK,
UpdateBackendFn: updateBackendOK,
},
WantOutput: "Updated backend (service 123 version 4)",
},
{
Arg: "--service-id 123 --version 1 --name www.test.com --tcp-ka-enabled=invalid --autoclone",
API: mock.API{
ListVersionsFn: testutil.ListVersions,
CloneVersionFn: testutil.CloneVersionResult(4),
GetBackendFn: getBackendOK,
UpdateBackendFn: updateBackendOK,
},
WantError: "'tcp-ka-enable' flag must be one of the following [true, false]",
},
}
testutil.RunScenarios(t, []string{root.CommandName, "update"}, scenarios)
}
Expand Down Expand Up @@ -377,6 +433,10 @@ var listBackendsJSONOutput = strings.TrimSpace(`
"ServiceID": "123",
"ServiceVersion": 1,
"Shield": null,
"TCPKeepAliveEnable": null,
"TCPKeepAliveIntvl": null,
"TCPKeepAliveProbes": null,
"TCPKeepAliveTime": null,
"UpdatedAt": null,
"UseSSL": null,
"Weight": null
Expand Down Expand Up @@ -412,6 +472,10 @@ var listBackendsJSONOutput = strings.TrimSpace(`
"ServiceID": "123",
"ServiceVersion": 1,
"Shield": null,
"TCPKeepAliveEnable": null,
"TCPKeepAliveIntvl": null,
"TCPKeepAliveProbes": null,
"TCPKeepAliveTime": null,
"UpdatedAt": null,
"UseSSL": null,
"Weight": null
Expand Down Expand Up @@ -456,6 +520,11 @@ var listBackendsVerboseOutput = strings.Join([]string{
" Min TLS version: ",
" Max TLS version: ",
" SSL ciphers: ",
" HTTP KeepAlive Timeout: 0",
" TCP KeepAlive Enabled: unset",
" TCP KeepAlive Interval: 0",
" TCP KeepAlive Probes: 0",
" TCP KeepAlive Timeout: 0",
" Backend 2/2",
" Name: example.com",
" Comment: example",
Expand All @@ -480,6 +549,11 @@ var listBackendsVerboseOutput = strings.Join([]string{
" Min TLS version: ",
" Max TLS version: ",
" SSL ciphers: ",
" HTTP KeepAlive Timeout: 0",
" TCP KeepAlive Enabled: unset",
" TCP KeepAlive Interval: 0",
" TCP KeepAlive Probes: 0",
" TCP KeepAlive Timeout: 0",
}, "\n") + "\n\n"

func getBackendOK(i *fastly.GetBackendInput) (*fastly.Backend, error) {
Expand Down Expand Up @@ -523,6 +597,11 @@ var describeBackendOutput = strings.Join([]string{
"Min TLS version: ",
"Max TLS version: ",
"SSL ciphers: ",
"HTTP KeepAlive Timeout: 0",
"TCP KeepAlive Enabled: unset",
"TCP KeepAlive Interval: 0",
"TCP KeepAlive Probes: 0",
"TCP KeepAlive Timeout: 0",
}, "\n") + "\n"

func updateBackendOK(i *fastly.UpdateBackendInput) (*fastly.Backend, error) {
Expand Down
43 changes: 41 additions & 2 deletions pkg/commands/backend/create.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package backend

import (
"errors"
"io"
"net"

"github.com/fastly/go-fastly/v9/fastly"

"github.com/fastly/cli/pkg/argparser"
"github.com/fastly/cli/pkg/errors"
fsterr "github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/global"
"github.com/fastly/cli/pkg/text"
)
Expand Down Expand Up @@ -45,6 +46,11 @@ type CreateCommand struct {
sslClientCert argparser.OptionalString
sslClientKey argparser.OptionalString
sslSNIHostname argparser.OptionalString
tcpKaEnable argparser.OptionalString
tcpKaInterval argparser.OptionalInt
tcpKaProbes argparser.OptionalInt
tcpKaTime argparser.OptionalInt
httpKaTime argparser.OptionalInt
useSSL argparser.OptionalBool
weight argparser.OptionalInt
}
Expand Down Expand Up @@ -107,6 +113,11 @@ func NewCreateCommand(parent argparser.Registerer, g *global.Data) *CreateComman
c.CmdClause.Flag("ssl-client-cert", "Client certificate attached to origin").Action(c.sslClientCert.Set).StringVar(&c.sslClientCert.Value)
c.CmdClause.Flag("ssl-client-key", "Client key attached to origin").Action(c.sslClientKey.Set).StringVar(&c.sslClientKey.Value)
c.CmdClause.Flag("ssl-sni-hostname", "Overrides ssl_hostname, but only for SNI in the handshake. Does not affect cert validation at all.").Action(c.sslSNIHostname.Set).StringVar(&c.sslSNIHostname.Value)
c.CmdClause.Flag("tcp-ka-enabled", "Enable TCP keepalive probes [true, false]").Action(c.tcpKaEnable.Set).StringVar(&c.tcpKaEnable.Value)
c.CmdClause.Flag("tcp-ka-interval", "Configure how long to wait between sending each TCP keepalive probe.").Action(c.tcpKaInterval.Set).IntVar(&c.tcpKaInterval.Value)
c.CmdClause.Flag("tcp-ka-probes", "Configure how many unacknowledged TCP keepalive probes to send before considering the connection dead.").Action(c.tcpKaProbes.Set).IntVar(&c.tcpKaProbes.Value)
c.CmdClause.Flag("tcp-ka-time", "Configure how long to wait after the last sent data before sending TCP keepalive probes.").Action(c.tcpKaTime.Set).IntVar(&c.tcpKaTime.Value)
c.CmdClause.Flag("http-ka-time", "Configure how long to keep idle HTTP keepalive connections in the connection pool.").Action(c.httpKaTime.Set).IntVar(&c.httpKaTime.Value)
c.CmdClause.Flag("use-ssl", "Whether or not to use SSL to reach the backend").Action(c.useSSL.Set).BoolVar(&c.useSSL.Value)
c.CmdClause.Flag("weight", "Weight used to load balance this backend against others").Action(c.weight.Set).IntVar(&c.weight.Value)

Expand All @@ -127,7 +138,7 @@ func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
if err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]any{
"Service ID": serviceID,
"Service Version": errors.ServiceVersion(serviceVersion),
"Service Version": fsterr.ServiceVersion(serviceVersion),
})
return err
}
Expand Down Expand Up @@ -203,6 +214,34 @@ func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
if c.sslSNIHostname.WasSet {
input.SSLSNIHostname = &c.sslSNIHostname.Value
}
if c.tcpKaEnable.WasSet {
var tcpKaEnable bool

switch c.tcpKaEnable.Value {
case "true":
tcpKaEnable = true
case "false":
tcpKaEnable = false
default:
err := errors.New("'tcp-ka-enable' flag must be one of the following [true, false]")
c.Globals.ErrLog.Add(err)
return err
kpfleming marked this conversation as resolved.
Show resolved Hide resolved
}

input.TCPKeepAliveEnable = &tcpKaEnable
}
if c.tcpKaInterval.WasSet {
input.TCPKeepAliveIntvl = &c.tcpKaInterval.Value
}
if c.tcpKaProbes.WasSet {
input.TCPKeepAliveProbes = &c.tcpKaProbes.Value
}
if c.tcpKaTime.WasSet {
input.TCPKeepAliveTime = &c.tcpKaTime.Value
}
if c.httpKaTime.WasSet {
input.KeepAliveTime = &c.httpKaTime.Value
}
if c.weight.WasSet {
input.Weight = &c.weight.Value
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/commands/backend/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ func (c *DescribeCommand) print(out io.Writer, b *fastly.Backend) error {
fmt.Fprintf(out, "Min TLS version: %v\n", fastly.ToValue(b.MinTLSVersion))
fmt.Fprintf(out, "Max TLS version: %v\n", fastly.ToValue(b.MaxTLSVersion))
fmt.Fprintf(out, "SSL ciphers: %v\n", fastly.ToValue(b.SSLCiphers))
fmt.Fprintf(out, "HTTP KeepAlive Timeout: %v\n", fastly.ToValue(b.KeepAliveTime))
if b.TCPKeepAliveEnable == nil {
fmt.Fprintf(out, "TCP KeepAlive Enabled: unset\n")
} else {
fmt.Fprintf(out, "TCP KeepAlive Enabled: %v\n", fastly.ToValue(b.TCPKeepAliveEnable))
}
fmt.Fprintf(out, "TCP KeepAlive Interval: %v\n", fastly.ToValue(b.TCPKeepAliveIntvl))
fmt.Fprintf(out, "TCP KeepAlive Probes: %v\n", fastly.ToValue(b.TCPKeepAliveProbes))
fmt.Fprintf(out, "TCP KeepAlive Timeout: %v\n", fastly.ToValue(b.TCPKeepAliveTime))

return nil
}
45 changes: 43 additions & 2 deletions pkg/commands/backend/update.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package backend

import (
"errors"
"io"

"github.com/fastly/go-fastly/v9/fastly"

"github.com/fastly/cli/pkg/argparser"
"github.com/fastly/cli/pkg/errors"
fsterr "github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/global"
"github.com/fastly/cli/pkg/text"
)
Expand Down Expand Up @@ -43,6 +44,11 @@ type UpdateCommand struct {
SSLClientKey argparser.OptionalString
SSLSNIHostname argparser.OptionalString
Shield argparser.OptionalString
TCPKaEnable argparser.OptionalString
TCPKaInterval argparser.OptionalInt
TCPKaProbes argparser.OptionalInt
TCPKaTime argparser.OptionalInt
HTTPKaTime argparser.OptionalInt
UseSSL argparser.OptionalBool
Weight argparser.OptionalInt
}
Expand Down Expand Up @@ -105,6 +111,11 @@ func NewUpdateCommand(parent argparser.Registerer, g *global.Data) *UpdateComman
c.CmdClause.Flag("ssl-client-cert", "Client certificate attached to origin").Action(c.SSLClientCert.Set).StringVar(&c.SSLClientCert.Value)
c.CmdClause.Flag("ssl-client-key", "Client key attached to origin").Action(c.SSLClientKey.Set).StringVar(&c.SSLClientKey.Value)
c.CmdClause.Flag("ssl-sni-hostname", "Overrides ssl_hostname, but only for SNI in the handshake. Does not affect cert validation at all.").Action(c.SSLSNIHostname.Set).StringVar(&c.SSLSNIHostname.Value)
c.CmdClause.Flag("tcp-ka-enabled", "Enable TCP keepalive probes [true, false]").Action(c.TCPKaEnable.Set).StringVar(&c.TCPKaEnable.Value)
c.CmdClause.Flag("tcp-ka-interval", "Configure how long to wait between sending each TCP keepalive probe.").Action(c.TCPKaInterval.Set).IntVar(&c.TCPKaInterval.Value)
c.CmdClause.Flag("tcp-ka-probes", "Configure how many unacknowledged TCP keepalive probes to send before considering the connection dead.").Action(c.TCPKaProbes.Set).IntVar(&c.TCPKaProbes.Value)
c.CmdClause.Flag("tcp-ka-time", "Configure how long to wait after the last sent data before sending TCP keepalive probes.").Action(c.TCPKaTime.Set).IntVar(&c.TCPKaTime.Value)
c.CmdClause.Flag("http-ka-time", "Configure how long to keep idle HTTP keepalive connections in the connection pool.").Action(c.HTTPKaTime.Set).IntVar(&c.HTTPKaTime.Value)
c.CmdClause.Flag("use-ssl", "Whether or not to use SSL to reach the backend").Action(c.UseSSL.Set).BoolVar(&c.UseSSL.Value)
c.CmdClause.Flag("weight", "Weight used to load balance this backend against others").Action(c.Weight.Set).IntVar(&c.Weight.Value)
return &c
Expand All @@ -124,7 +135,7 @@ func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error {
if err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]any{
"Service ID": serviceID,
"Service Version": errors.ServiceVersion(serviceVersion),
"Service Version": fsterr.ServiceVersion(serviceVersion),
})
return err
}
Expand Down Expand Up @@ -236,6 +247,36 @@ func (c *UpdateCommand) Exec(_ io.Reader, out io.Writer) error {
input.SSLCiphers = &c.SSLCiphers.Value
}

if c.TCPKaEnable.WasSet {
var tcpKaEnable bool

switch c.TCPKaEnable.Value {
case "true":
tcpKaEnable = true
case "false":
tcpKaEnable = false
default:
err := errors.New("'tcp-ka-enable' flag must be one of the following [true, false]")
c.Globals.ErrLog.Add(err)
return err
kpfleming marked this conversation as resolved.
Show resolved Hide resolved
}

input.TCPKeepAliveEnable = &tcpKaEnable
}
if c.TCPKaInterval.WasSet {
input.TCPKeepAliveIntvl = &c.TCPKaInterval.Value
}
if c.TCPKaProbes.WasSet {
input.TCPKeepAliveProbes = &c.TCPKaProbes.Value
}
if c.TCPKaTime.WasSet {
input.TCPKeepAliveTime = &c.TCPKaTime.Value
}

if c.HTTPKaTime.WasSet {
input.KeepAliveTime = &c.HTTPKaTime.Value
}

b, err := c.Globals.APIClient.UpdateBackend(input)
if err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]any{
Expand Down
9 changes: 9 additions & 0 deletions pkg/text/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,13 @@ func PrintBackend(out io.Writer, prefix string, b *fastly.Backend) {
fmt.Fprintf(out, "Min TLS version: %v\n", fastly.ToValue(b.MinTLSVersion))
fmt.Fprintf(out, "Max TLS version: %v\n", fastly.ToValue(b.MaxTLSVersion))
fmt.Fprintf(out, "SSL ciphers: %v\n", fastly.ToValue(b.SSLCiphers))
fmt.Fprintf(out, "HTTP KeepAlive Timeout: %v\n", fastly.ToValue(b.KeepAliveTime))
if b.TCPKeepAliveEnable == nil {
fmt.Fprintf(out, "TCP KeepAlive Enabled: unset\n")
} else {
fmt.Fprintf(out, "TCP KeepAlive Enabled: %v\n", fastly.ToValue(b.TCPKeepAliveEnable))
}
fmt.Fprintf(out, "TCP KeepAlive Interval: %v\n", fastly.ToValue(b.TCPKeepAliveIntvl))
fmt.Fprintf(out, "TCP KeepAlive Probes: %v\n", fastly.ToValue(b.TCPKeepAliveProbes))
fmt.Fprintf(out, "TCP KeepAlive Timeout: %v\n", fastly.ToValue(b.TCPKeepAliveTime))
}
Loading