Skip to content
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
82 changes: 24 additions & 58 deletions lib/compose/desired.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"sort"
"strings"

"github.com/kernel/hypeman-go"
)
Expand Down Expand Up @@ -155,78 +154,45 @@ func updateDesiredInstanceImage(instances []desiredInstance, composeName, servic
}

func buildComposeRestartPolicy(restart *composeRestartSpec) hypeman.RestartPolicyParam {
policy := hypeman.RestartPolicyParam{}
if restart.Policy != "" {
policy.Policy = hypeman.RestartPolicyPolicy(strings.ReplaceAll(restart.Policy, "-", "_"))
}
if restart.Backoff != "" {
policy.Backoff = hypeman.String(restart.Backoff)
in := RestartPolicyInput{
Policy: restart.Policy,
Backoff: restart.Backoff,
StableAfter: restart.StableAfter,
}
if restart.MaxAttempts > 0 {
policy.MaxAttempts = hypeman.Int(int64(restart.MaxAttempts))
}
if restart.StableAfter != "" {
policy.StableAfter = hypeman.String(restart.StableAfter)
v := int64(restart.MaxAttempts)
in.MaxAttempts = &v
}
return policy
return BuildRestartPolicyParam(in)
}

func buildComposeHealthCheck(check *composeCheckSpec) hypeman.HealthCheckParam {
health := hypeman.HealthCheckParam{}
if check.Type != "" {
health.Type = hypeman.HealthCheckType(strings.ToLower(check.Type))
in := HealthCheckInput{
Type: check.Type,
Interval: check.Interval,
Timeout: check.Timeout,
StartPeriod: check.StartPeriod,
FailureThreshold: int64(check.FailureThreshold),
SuccessThreshold: int64(check.SuccessThreshold),
}
if check.HTTP != nil {
health.Type = defaultHealthCheckType(health.Type, hypeman.HealthCheckTypeHTTP)
health.HTTP = hypeman.HealthCheckHTTPParam{
Port: int64(check.HTTP.Port),
}
if check.HTTP.Path != "" {
health.HTTP.Path = hypeman.String(check.HTTP.Path)
}
if check.HTTP.Scheme != "" {
health.HTTP.Scheme = hypeman.HealthCheckHTTPScheme(strings.ToLower(check.HTTP.Scheme))
}
if check.HTTP.ExpectedStatus > 0 {
health.HTTP.ExpectedStatus = hypeman.Int(int64(check.HTTP.ExpectedStatus))
in.HTTP = &HealthCheckHTTPInput{
Port: int64(check.HTTP.Port),
Path: check.HTTP.Path,
Scheme: check.HTTP.Scheme,
ExpectedStatus: int64(check.HTTP.ExpectedStatus),
}
}
if check.TCP != nil {
health.Type = defaultHealthCheckType(health.Type, hypeman.HealthCheckTypeTcp)
health.Tcp = hypeman.HealthCheckTcpParam{Port: int64(check.TCP.Port)}
in.TCP = &HealthCheckTCPInput{Port: int64(check.TCP.Port)}
}
if check.Exec != nil {
health.Type = defaultHealthCheckType(health.Type, hypeman.HealthCheckTypeExec)
health.Exec = hypeman.HealthCheckExecParam{
Command: check.Exec.Command,
}
if check.Exec.WorkingDir != "" {
health.Exec.WorkingDir = hypeman.String(check.Exec.WorkingDir)
in.Exec = &HealthCheckExecInput{
Command: check.Exec.Command,
WorkingDir: check.Exec.WorkingDir,
}
}
if check.Interval != "" {
health.Interval = hypeman.String(check.Interval)
}
if check.Timeout != "" {
health.Timeout = hypeman.String(check.Timeout)
}
if check.StartPeriod != "" {
health.StartPeriod = hypeman.String(check.StartPeriod)
}
if check.FailureThreshold > 0 {
health.FailureThreshold = hypeman.Int(int64(check.FailureThreshold))
}
if check.SuccessThreshold > 0 {
health.SuccessThreshold = hypeman.Int(int64(check.SuccessThreshold))
}
return health
}

func defaultHealthCheckType(current, fallback hypeman.HealthCheckType) hypeman.HealthCheckType {
if current != "" {
return current
}
return fallback
return BuildHealthCheckParam(in)
}

func buildComposeIngressInput(instanceName, ingressName string, spec composeIngressRuleSpec) hypeman.IngressNewParams {
Expand Down
123 changes: 123 additions & 0 deletions lib/compose/policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package compose

import (
"strings"

"github.com/kernel/hypeman-go"
)

// HealthCheckInput is a neutral, plain-value description of a workload health
// check. It is shared by compose, the imperative run command, and the update
// subcommands so the param-construction logic does not drift.
type HealthCheckInput struct {
Type string
Interval string
Timeout string
StartPeriod string
FailureThreshold int64
SuccessThreshold int64
HTTP *HealthCheckHTTPInput
TCP *HealthCheckTCPInput
Exec *HealthCheckExecInput
}

type HealthCheckHTTPInput struct {
Port int64
Path string
Scheme string
ExpectedStatus int64
}

type HealthCheckTCPInput struct {
Port int64
}

type HealthCheckExecInput struct {
Command []string
WorkingDir string
}

// RestartPolicyInput is a neutral, plain-value description of a restart policy.
// MaxAttempts is a pointer so an explicit 0 (unlimited) is distinguishable from
// "not provided": nil omits the field, &0 sends an explicit 0.
type RestartPolicyInput struct {
Policy string
Backoff string
MaxAttempts *int64
StableAfter string
}

func BuildHealthCheckParam(in HealthCheckInput) hypeman.HealthCheckParam {
health := hypeman.HealthCheckParam{}
if in.Type != "" {
health.Type = hypeman.HealthCheckType(strings.ToLower(in.Type))
}
if in.HTTP != nil {
health.Type = defaultHealthCheckType(health.Type, hypeman.HealthCheckTypeHTTP)
health.HTTP = hypeman.HealthCheckHTTPParam{
Port: in.HTTP.Port,
}
if in.HTTP.Path != "" {
health.HTTP.Path = hypeman.String(in.HTTP.Path)
}
if in.HTTP.Scheme != "" {
health.HTTP.Scheme = hypeman.HealthCheckHTTPScheme(strings.ToLower(in.HTTP.Scheme))
}
if in.HTTP.ExpectedStatus > 0 {
health.HTTP.ExpectedStatus = hypeman.Int(in.HTTP.ExpectedStatus)
}
}
if in.TCP != nil {
health.Type = defaultHealthCheckType(health.Type, hypeman.HealthCheckTypeTcp)
health.Tcp = hypeman.HealthCheckTcpParam{Port: in.TCP.Port}
}
if in.Exec != nil {
health.Type = defaultHealthCheckType(health.Type, hypeman.HealthCheckTypeExec)
health.Exec = hypeman.HealthCheckExecParam{
Command: in.Exec.Command,
}
if in.Exec.WorkingDir != "" {
health.Exec.WorkingDir = hypeman.String(in.Exec.WorkingDir)
}
}
if in.Interval != "" {
health.Interval = hypeman.String(in.Interval)
}
if in.Timeout != "" {
health.Timeout = hypeman.String(in.Timeout)
}
if in.StartPeriod != "" {
health.StartPeriod = hypeman.String(in.StartPeriod)
}
if in.FailureThreshold > 0 {
health.FailureThreshold = hypeman.Int(in.FailureThreshold)
}
if in.SuccessThreshold > 0 {
health.SuccessThreshold = hypeman.Int(in.SuccessThreshold)
}
return health
}

func BuildRestartPolicyParam(in RestartPolicyInput) hypeman.RestartPolicyParam {
policy := hypeman.RestartPolicyParam{}
if in.Policy != "" {
policy.Policy = hypeman.RestartPolicyPolicy(strings.ReplaceAll(in.Policy, "-", "_"))
}
if in.Backoff != "" {
policy.Backoff = hypeman.String(in.Backoff)
}
if in.MaxAttempts != nil {
policy.MaxAttempts = hypeman.Int(*in.MaxAttempts)
}
Comment thread
cursor[bot] marked this conversation as resolved.
if in.StableAfter != "" {
policy.StableAfter = hypeman.String(in.StableAfter)
}
return policy
}

func defaultHealthCheckType(current, fallback hypeman.HealthCheckType) hypeman.HealthCheckType {
if current != "" {
return current
}
return fallback
}
1 change: 1 addition & 0 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func init() {
&snapshotCmd,
&volumeCmd,
&resourcesCmd,
&healthCmd,
&deviceCmd,
&composeCmd,
{
Expand Down
55 changes: 55 additions & 0 deletions pkg/cmd/healthcmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cmd

import (
"context"
"fmt"
"os"

"github.com/kernel/hypeman-go"
"github.com/kernel/hypeman-go/option"
"github.com/tidwall/gjson"
"github.com/urfave/cli/v3"
)

var healthCmd = cli.Command{
Name: "health",
Usage: "Check API server health",
Description: `Report the health of the hypeman API server.

Examples:
# Check health (default)
hypeman health

# Check health as JSON
hypeman health --format json`,
Action: handleHealth,
HideHelpCommand: true,
}

func handleHealth(ctx context.Context, cmd *cli.Command) error {
client := hypeman.NewClient(getDefaultRequestOptions(cmd)...)

var opts []option.RequestOption
if cmd.Root().Bool("debug") {
opts = append(opts, debugMiddlewareOption)
}

var res []byte
opts = append(opts, option.WithResponseBodyInto(&res))
_, err := client.Health.Check(ctx, opts...)
if err != nil {
return err
}

format := cmd.Root().String("format")
transform := cmd.Root().String("transform")

obj := gjson.ParseBytes(res)

if format == "auto" || format == "" {
fmt.Println(obj.Get("status").String())
return nil
}

return ShowJSON(os.Stdout, "health", obj, format, transform)
}
Loading
Loading