Skip to content

Commit d357060

Browse files
committed
services: add checks flag
* checks flags allows to define http and tcp healthchecks
1 parent 2540c55 commit d357060

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

docs/reference.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ koyeb apps init NAME [flags]
225225
### Options
226226

227227
```
228+
--checks strings HTTP healthcheck (<port>:http:<path>) and TCP healthcheck (<port>:tcp)
228229
--docker string Docker image
229230
--docker-args strings Docker args
230231
--docker-command string Docker command
@@ -898,6 +899,7 @@ koyeb services create NAME [flags]
898899

899900
```
900901
-a, --app string App
902+
--checks strings HTTP healthcheck (<port>:http:<path>) and TCP healthcheck (<port>:tcp)
901903
--docker string Docker image
902904
--docker-args strings Docker args
903905
--docker-command string Docker command
@@ -1179,6 +1181,7 @@ koyeb services update NAME [flags]
11791181
### Options
11801182

11811183
```
1184+
--checks strings HTTP healthcheck (<port>:http:<path>) and TCP healthcheck (<port>:tcp)
11821185
--docker string Docker image
11831186
--docker-args strings Docker args
11841187
--docker-command string Docker command

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/gofrs/uuid v4.3.0+incompatible
1010
github.com/gorilla/websocket v1.5.0
1111
github.com/iancoleman/strcase v0.2.0
12-
github.com/koyeb/koyeb-api-client-go v0.0.0-20221026085506-07c8c0a7b717
12+
github.com/koyeb/koyeb-api-client-go v0.0.0-20230130102659-26de8f39c858
1313
github.com/logrusorgru/aurora v2.0.3+incompatible
1414
github.com/manifoldco/promptui v0.9.0
1515
github.com/mitchellh/go-homedir v1.1.0

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxv
241241
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
242242
github.com/koyeb/koyeb-api-client-go v0.0.0-20221026085506-07c8c0a7b717 h1:/x2pFgpZtC936I+VV+DFEt0XnWTio6cO2/HuCd0Vn38=
243243
github.com/koyeb/koyeb-api-client-go v0.0.0-20221026085506-07c8c0a7b717/go.mod h1:+oQfFj2WL3gi9Pb+UHbob4D7xaT52mPfKyH1UvWa4PQ=
244+
github.com/koyeb/koyeb-api-client-go v0.0.0-20221103082152-736202b94397 h1:/w+PLg5VsAh485pwQrVefY+/So6sSNDdkDZre13bjP8=
245+
github.com/koyeb/koyeb-api-client-go v0.0.0-20221103082152-736202b94397/go.mod h1:+oQfFj2WL3gi9Pb+UHbob4D7xaT52mPfKyH1UvWa4PQ=
246+
github.com/koyeb/koyeb-api-client-go v0.0.0-20230130102659-26de8f39c858 h1:BcXBg0dWO84Ng1/yWlasGPfz0OA1cg3vwOdDqk1bht4=
247+
github.com/koyeb/koyeb-api-client-go v0.0.0-20230130102659-26de8f39c858/go.mod h1:+oQfFj2WL3gi9Pb+UHbob4D7xaT52mPfKyH1UvWa4PQ=
244248
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
245249
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
246250
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=

pkg/koyeb/services.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package koyeb
22

33
import (
44
"context"
5+
"fmt"
56
"strconv"
67
"strings"
78

@@ -197,6 +198,7 @@ func addServiceDefinitionFlags(flags *pflag.FlagSet) {
197198
flags.String("instance-type", "nano", "Instance type")
198199
flags.Int64("min-scale", 1, "Min scale")
199200
flags.Int64("max-scale", 1, "Max scale")
201+
flags.StringSlice("checks", []string{""}, "HTTP healthcheck (<port>:http:<path>) and TCP healthcheck (<port>:tcp)")
200202
}
201203

202204
func parseServiceDefinitionFlags(flags *pflag.FlagSet, definition *koyeb.DeploymentDefinition, useDefault bool) error {
@@ -292,6 +294,46 @@ func parseServiceDefinitionFlags(flags *pflag.FlagSet, definition *koyeb.Deploym
292294
definition.SetScalings([]koyeb.DeploymentScaling{*scaling})
293295
}
294296

297+
if flags.Lookup("checks").Changed {
298+
checks, _ := flags.GetStringSlice("checks")
299+
healthchecks := []koyeb.DeploymentHealthCheck{}
300+
301+
for _, c := range checks {
302+
healthcheck := koyeb.NewDeploymentHealthCheck()
303+
components := strings.Split(c, ":")
304+
componentsCount := len(components)
305+
if componentsCount < 2 || componentsCount > 3 {
306+
return fmt.Errorf(`Invalid checks: "%s", must be either "<port>:http:<path>" or "<port>:tcp"`, c)
307+
}
308+
309+
healthcheckType := components[1]
310+
portStr := components[0]
311+
port, err := strconv.Atoi(portStr)
312+
if err != nil {
313+
return errors.Errorf(`Invalid port: "%s"`, portStr)
314+
}
315+
316+
switch healthcheckType {
317+
case "http":
318+
if componentsCount < 3 {
319+
return errors.New("Missing path definition for http check")
320+
}
321+
HTTPHealthCheck := koyeb.NewHTTPHealthCheck()
322+
HTTPHealthCheck.Port = koyeb.PtrInt64(int64(port))
323+
HTTPHealthCheck.Path = koyeb.PtrString(components[2])
324+
healthcheck.SetHttp(*HTTPHealthCheck)
325+
case "tcp":
326+
TCPHealthCheck := koyeb.NewTCPHealthCheck()
327+
TCPHealthCheck.Port = koyeb.PtrInt64(int64(port))
328+
healthcheck.SetTcp(*TCPHealthCheck)
329+
default:
330+
return fmt.Errorf(`Invalid healthcheck: "%s", must be either "http" or "tcp"`, healthcheckType)
331+
}
332+
healthchecks = append(healthchecks, *healthcheck)
333+
}
334+
definition.SetHealthChecks(healthchecks)
335+
}
336+
295337
// Docker
296338
if useDefault && !flags.Lookup("git").Changed || flags.Lookup("docker").Changed && !flags.Lookup("git").Changed {
297339
createDockerSource := koyeb.NewDockerSourceWithDefaults()

0 commit comments

Comments
 (0)