Skip to content
Open
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
190 changes: 152 additions & 38 deletions modules/common/probes/probes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// +kubebuilder:object:generate:=true

// The probes package provides utilities for configuring Kubernetes liveness
// and readiness probes

// Package probes provides utilities for configuring Kubernetes liveness and readiness probes
package probes

import (
Expand All @@ -23,61 +26,172 @@ import (
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/validation/field"
"strings"
)

// ProbeConfig - the configuration for liveness and readiness probes
// LivenessPath - Endpoint path for the liveness probe
// ReadinessPath - Endpoint path for the readiness probe
// InitialDelaySeconds - Number of seconds after the container starts before liveness/readiness probes are initiated
// TimeoutSeconds - Number of seconds after which the probe times out
// PeriodSeconds - How often (in seconds) to perform the probe
type ProbeConfig struct {
LivenessPath string
ReadinessPath string
InitialDelaySeconds int32
TimeoutSeconds int32
PeriodSeconds int32
func (p *ProbeConf) merge(overrides ProbeConf) {
// Override path if provided
if overrides.Path != "" {
p.Path = overrides.Path
}
// Override timing values if they are non-zero
if overrides.InitialDelaySeconds > 0 {
p.InitialDelaySeconds = overrides.InitialDelaySeconds
}
if overrides.TimeoutSeconds > 0 {
p.TimeoutSeconds = overrides.TimeoutSeconds
}
if overrides.PeriodSeconds > 0 {
p.PeriodSeconds = overrides.PeriodSeconds
}
if overrides.FailureThreshold > 0 {
p.FailureThreshold = overrides.FailureThreshold
}
}

// SetProbes - configures and returns liveness and readiness probes based on the provided settings
func SetProbes(port int, disableNonTLSListeners bool, config ProbeConfig) (*v1.Probe, *v1.Probe, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not used in the current operators, so good to do this change without deprecate them first

// CreateProbeSet - creates all probes at once using the interface
func CreateProbeSet(
port int32,
scheme *v1.URIScheme,
overrides ProbeOverrides,
defaults OverrideSpec,
) (*ProbeSet, error) {

if port < 1 || port > 65535 {
return nil, nil, fmt.Errorf("%w: %d", util.ErrInvalidPort, port)
livenessProbe, err := SetProbeConf(
port,
scheme,
func() ProbeConf {
if defaults.LivenessProbes == nil {
defaults.LivenessProbes = &ProbeConf{}
}
baseConf := *defaults.LivenessProbes
if p := overrides.GetLivenessProbes(); p != nil {
baseConf.merge(*p)
}
return baseConf
}(),
)

// Could not process probes config
if err != nil {
return nil, err
}

var scheme v1.URIScheme
if disableNonTLSListeners {
scheme = v1.URISchemeHTTPS
} else {
scheme = v1.URISchemeHTTP
readinessProbe, err := SetProbeConf(
port,
scheme,
func() ProbeConf {
if defaults.ReadinessProbes == nil {
defaults.ReadinessProbes = &ProbeConf{}
}
baseConf := *defaults.ReadinessProbes
if p := overrides.GetReadinessProbes(); p != nil {
baseConf.merge(*p)
}
return baseConf
}(),
)

// Could not process probes config
if err != nil {
return nil, err
}

livenessProbe := &v1.Probe{
ProbeHandler: v1.ProbeHandler{
HTTPGet: &v1.HTTPGetAction{
Path: config.LivenessPath,
Port: intstr.FromInt(port),
Scheme: scheme,
},
},
InitialDelaySeconds: config.InitialDelaySeconds,
TimeoutSeconds: config.TimeoutSeconds,
PeriodSeconds: config.PeriodSeconds,
startupProbe, err := SetProbeConf(
port,
scheme,
func() ProbeConf {
if defaults.StartupProbes == nil {
defaults.StartupProbes = &ProbeConf{}
}
baseConf := *defaults.StartupProbes
if p := overrides.GetStartupProbes(); p != nil {
baseConf.merge(*p)
}
return baseConf
}(),
)

// Could not process probes config
if err != nil {
return nil, err
}

readinessProbe := &v1.Probe{
return &ProbeSet{
Liveness: livenessProbe,
Readiness: readinessProbe,
Startup: startupProbe,
}, nil
}

// SetProbeConf configures and returns liveness and readiness probes based on
// the provided settings
func SetProbeConf(port int32, scheme *v1.URIScheme, config ProbeConf) (*v1.Probe, error) {
if port < 1 || port > 65535 {
return nil, fmt.Errorf("%w: %d", util.ErrInvalidPort, port)
}
probe := &v1.Probe{
ProbeHandler: v1.ProbeHandler{
HTTPGet: &v1.HTTPGetAction{
Path: config.ReadinessPath,
Port: intstr.FromInt(port),
Scheme: scheme,
Path: config.Path,
Port: intstr.FromInt32(port),
},
},
InitialDelaySeconds: config.InitialDelaySeconds,
TimeoutSeconds: config.TimeoutSeconds,
PeriodSeconds: config.PeriodSeconds,
FailureThreshold: config.FailureThreshold,
}
if scheme != nil {
probe.HTTPGet.Scheme = *scheme
}
return probe, nil
}

// ValidateProbeConf - This function can be used at webhooks level to explicitly
// validate the overrides
func ValidateProbeConf(basePath *field.Path, config *ProbeConf) field.ErrorList {
errorList := field.ErrorList{}
// nothing to validate, return an empty errorList
if config == nil {
return errorList
}
// Path validation: fail is explicitly set as an empty string
// or the endpoint does't start with "/"
if config.Path != "" && !strings.HasPrefix(config.Path, "/") {
err := field.Invalid(basePath.Child("path"), config.Path,
"path must start with '/' if specified")
errorList = append(errorList, err)
}

// InitialDelaySeconds validation: must be > 0
if config.InitialDelaySeconds < 0 {
err := field.Invalid(basePath.Child("initialDelaySeconds"), config.InitialDelaySeconds,
"initialDelaySeconds must be non-negative")
errorList = append(errorList, err)
}

// TimeoutSeconds validation: fail if it's a negative number
if config.TimeoutSeconds != 0 && config.TimeoutSeconds < 1 {
err := field.Invalid(basePath.Child("timeoutSeconds"), config.TimeoutSeconds,
"timeoutSeconds must be at least 1 second when set")
errorList = append(errorList, err)
}

// PeriodSeconds validation: fail if it's set as a negative number
if config.PeriodSeconds != 0 && config.PeriodSeconds < 1 {
err := field.Invalid(basePath.Child("periodSeconds"), config.PeriodSeconds,
"periodSeconds must be at least 1 second when set")
errorList = append(errorList, err)
}

// FailureThreshold validation: fail if it's set as a negative number
if config.FailureThreshold != 0 && config.FailureThreshold < 1 {
err := field.Invalid(basePath.Child("failureThreshold"), config.FailureThreshold,
"failureThreshold must be at least 1 when set")
errorList = append(errorList, err)
}

return livenessProbe, readinessProbe, nil
return errorList
}
Loading