File: healthcheck_config.go

package info (click to toggle)
podman 5.7.0%2Bds2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,824 kB
  • sloc: sh: 4,700; python: 2,798; perl: 1,885; ansic: 1,484; makefile: 977; ruby: 42; csh: 8
file content (165 lines) | stat: -rw-r--r-- 5,538 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//go:build !remote

package libpod

import (
	"errors"
	"strings"
	"time"

	"github.com/containers/podman/v5/libpod/define"
	"github.com/containers/podman/v5/pkg/specgenutil"
	"go.podman.io/image/v5/manifest"
)

type HealthCheckConfig struct {
	*manifest.Schema2HealthConfig
}

type StartupHealthCheckConfig struct {
	*define.StartupHealthCheck
}

type IHealthCheckConfig interface {
	SetTo(config *ContainerConfig)
	IsStartup() bool
	IsNil() bool
	IsTimeChanged(oldInterval time.Duration) bool
	GetInterval() time.Duration
	SetCurrentConfigTo(healthCheckOptions *define.HealthCheckOptions)
	IsHealthCheckCommandSet(updateHealthCheckConfig define.UpdateHealthCheckConfig) bool
	SetNewHealthCheckOptions(updateHealthCheckConfig define.UpdateHealthCheckConfig, healthCheckOptions *define.HealthCheckOptions) bool
}

func (h *HealthCheckConfig) SetTo(config *ContainerConfig) {
	config.HealthCheckConfig = h.Schema2HealthConfig
}

func (h *StartupHealthCheckConfig) SetTo(config *ContainerConfig) {
	config.StartupHealthCheckConfig = h.StartupHealthCheck
}

func (h *HealthCheckConfig) IsNil() bool {
	return h.Schema2HealthConfig == nil
}

func (h *StartupHealthCheckConfig) IsNil() bool {
	return h.StartupHealthCheck == nil
}

func (h *HealthCheckConfig) IsStartup() bool {
	return false
}

func (h *StartupHealthCheckConfig) IsStartup() bool {
	return true
}

func (h *HealthCheckConfig) IsTimeChanged(oldInterval time.Duration) bool {
	return h.Interval != oldInterval
}

func (h *StartupHealthCheckConfig) IsTimeChanged(oldInterval time.Duration) bool {
	return h.Interval != oldInterval
}

func (h *HealthCheckConfig) GetInterval() time.Duration {
	return h.Interval
}

func (h *StartupHealthCheckConfig) GetInterval() time.Duration {
	return h.Interval
}

func (h *HealthCheckConfig) SetCurrentConfigTo(healthCheckOptions *define.HealthCheckOptions) {
	healthCheckOptions.Cmd = strings.Join(h.Test, " ")
	healthCheckOptions.Interval = h.Interval.String()
	healthCheckOptions.Retries = h.Retries
	healthCheckOptions.Timeout = h.Timeout.String()
	healthCheckOptions.StartPeriod = h.StartPeriod.String()
}

func (h *StartupHealthCheckConfig) SetCurrentConfigTo(healthCheckOptions *define.HealthCheckOptions) {
	healthCheckOptions.Cmd = strings.Join(h.Test, " ")
	healthCheckOptions.Interval = h.Interval.String()
	healthCheckOptions.Retries = h.Retries
	healthCheckOptions.Timeout = h.Timeout.String()
	healthCheckOptions.Successes = h.Successes
}

func (h *HealthCheckConfig) IsHealthCheckCommandSet(updateHealthCheckConfig define.UpdateHealthCheckConfig) bool {
	return updateHealthCheckConfig.IsHealthCheckCommandSet(h.Schema2HealthConfig)
}

func (h *StartupHealthCheckConfig) IsHealthCheckCommandSet(updateHealthCheckConfig define.UpdateHealthCheckConfig) bool {
	return updateHealthCheckConfig.IsStartupHealthCheckCommandSet(h.StartupHealthCheck)
}

func (h *HealthCheckConfig) SetNewHealthCheckOptions(updateHealthCheckConfig define.UpdateHealthCheckConfig, healthCheckOptions *define.HealthCheckOptions) bool {
	return updateHealthCheckConfig.SetNewHealthCheckConfigTo(healthCheckOptions)
}

func (h *StartupHealthCheckConfig) SetNewHealthCheckOptions(updateHealthCheckConfig define.UpdateHealthCheckConfig, healthCheckOptions *define.HealthCheckOptions) bool {
	return updateHealthCheckConfig.SetNewStartupHealthCheckConfigTo(healthCheckOptions)
}

func GetNewHealthCheckConfig(originalHealthCheckConfig IHealthCheckConfig, updateHealthCheckConfig define.UpdateHealthCheckConfig) (IHealthCheckConfig, bool, error) {
	if originalHealthCheckConfig.IsHealthCheckCommandSet(updateHealthCheckConfig) {
		return nil, false, errors.New("startup healthcheck command is not set")
	}

	healthCheckOptions := define.HealthCheckOptions{
		Cmd:         "",
		Interval:    define.DefaultHealthCheckInterval,
		Retries:     int(define.DefaultHealthCheckRetries),
		Timeout:     define.DefaultHealthCheckTimeout,
		StartPeriod: define.DefaultHealthCheckStartPeriod,
		Successes:   0,
	}

	if originalHealthCheckConfig.IsStartup() {
		healthCheckOptions.Retries = 0
	}

	if !originalHealthCheckConfig.IsNil() {
		originalHealthCheckConfig.SetCurrentConfigTo(&healthCheckOptions)
	}

	noHealthCheck := false
	if updateHealthCheckConfig.NoHealthCheck != nil {
		noHealthCheck = *updateHealthCheckConfig.NoHealthCheck
	}

	changed := originalHealthCheckConfig.SetNewHealthCheckOptions(updateHealthCheckConfig, &healthCheckOptions)

	if noHealthCheck && changed {
		return nil, false, errors.New("cannot specify both --no-healthcheck and other HealthCheck flags")
	}

	if noHealthCheck {
		if originalHealthCheckConfig.IsStartup() {
			return &StartupHealthCheckConfig{StartupHealthCheck: nil}, true, nil
		}
		return &HealthCheckConfig{Schema2HealthConfig: &manifest.Schema2HealthConfig{Test: []string{"NONE"}}}, true, nil
	}

	newHealthCheckConfig, err := specgenutil.MakeHealthCheckFromCli(
		healthCheckOptions.Cmd,
		healthCheckOptions.Interval,
		uint(healthCheckOptions.Retries),
		healthCheckOptions.Timeout,
		healthCheckOptions.StartPeriod,
		true,
	)
	if err != nil {
		return nil, false, err
	}

	if originalHealthCheckConfig.IsStartup() {
		newStartupHealthCheckConfig := new(define.StartupHealthCheck)
		newStartupHealthCheckConfig.Schema2HealthConfig = *newHealthCheckConfig
		newStartupHealthCheckConfig.Successes = healthCheckOptions.Successes
		return &StartupHealthCheckConfig{StartupHealthCheck: newStartupHealthCheckConfig}, changed, nil
	}
	return &HealthCheckConfig{Schema2HealthConfig: newHealthCheckConfig}, changed, nil
}