Skip to content
29 changes: 20 additions & 9 deletions api/v1/runtimecomponent_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,15 @@ type RuntimeComponentServiceAccount struct {
type RuntimeComponentProbes struct {
// Periodic probe of container liveness. Container will be restarted if the probe fails.
// +operator-sdk:csv:customresourcedefinitions:order=3,type=spec,displayName="Liveness Probe"
Liveness *corev1.Probe `json:"liveness,omitempty"`
Liveness *common.BaseComponentProbe `json:"liveness,omitempty"`

// Periodic probe of container service readiness. Container will be removed from service endpoints if the probe fails.
// +operator-sdk:csv:customresourcedefinitions:order=2,type=spec,displayName="Readiness Probe"
Readiness *corev1.Probe `json:"readiness,omitempty"`
Readiness *common.BaseComponentProbe `json:"readiness,omitempty"`

// Probe to determine successful initialization. If specified, other probes are not executed until this completes successfully.
// +operator-sdk:csv:customresourcedefinitions:order=1,type=spec,displayName="Startup Probe"
Startup *corev1.Probe `json:"startup,omitempty"`
Startup *common.BaseComponentProbe `json:"startup,omitempty"`
}

// Configure pods to run on particular Nodes.
Expand Down Expand Up @@ -611,18 +611,18 @@ func (cr *RuntimeComponent) GetProbes() common.BaseComponentProbes {
}

// GetLivenessProbe returns liveness probe
func (p *RuntimeComponentProbes) GetLivenessProbe() *corev1.Probe {
return p.Liveness
func (p *RuntimeComponentProbes) GetLivenessProbe(ba common.BaseComponent) *corev1.Probe {
return common.ConvertBaseComponentProbeToCoreProbe(p.Liveness, p.GetDefaultLivenessProbe(ba))
}

// GetReadinessProbe returns readiness probe
func (p *RuntimeComponentProbes) GetReadinessProbe() *corev1.Probe {
return p.Readiness
func (p *RuntimeComponentProbes) GetReadinessProbe(ba common.BaseComponent) *corev1.Probe {
return common.ConvertBaseComponentProbeToCoreProbe(p.Readiness, p.GetDefaultReadinessProbe(ba))
}

// GetStartupProbe returns startup probe
func (p *RuntimeComponentProbes) GetStartupProbe() *corev1.Probe {
return p.Startup
func (p *RuntimeComponentProbes) GetStartupProbe(ba common.BaseComponent) *corev1.Probe {
return common.ConvertBaseComponentProbeToCoreProbe(p.Startup, p.GetDefaultStartupProbe(ba))
}

// GetDefaultLivenessProbe returns default values for liveness probe
Expand Down Expand Up @@ -765,6 +765,17 @@ func (cr *RuntimeComponent) GetManageTLS() *bool {
return cr.Spec.ManageTLS
}

func (cr *RuntimeComponent) GetManagedPort() int {
return int(cr.GetService().GetPort())
Copy link
Member

Choose a reason for hiding this comment

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

Validate that the default value Operator sets (when user hasn't configured Service port) is retrieved here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added nil check on GetService() and fallback to 8080

}

func (cr *RuntimeComponent) GetManagedScheme() corev1.URIScheme {
if cr.GetManageTLS() != nil && *cr.GetManageTLS() {
Copy link
Member

Choose a reason for hiding this comment

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

Handle the default value of manageTLS (when nil)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added check for when GetManageTLS() == nil

return corev1.URISchemeHTTPS
}
return corev1.URISchemeHTTP
}

// GetDeployment returns deployment settings
func (cr *RuntimeComponent) GetDeployment() common.BaseComponentDeployment {
if cr.Spec.Deployment == nil {
Expand Down
6 changes: 3 additions & 3 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions api/v1beta2/runtimecomponent_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,17 +488,17 @@ func (cr *RuntimeComponent) GetProbes() common.BaseComponentProbes {
}

// GetLivenessProbe returns liveness probe
func (p *RuntimeComponentProbes) GetLivenessProbe() *corev1.Probe {
func (p *RuntimeComponentProbes) GetLivenessProbe(ba common.BaseComponent) *corev1.Probe {
return p.Liveness
}

// GetReadinessProbe returns readiness probe
func (p *RuntimeComponentProbes) GetReadinessProbe() *corev1.Probe {
func (p *RuntimeComponentProbes) GetReadinessProbe(ba common.BaseComponent) *corev1.Probe {
return p.Readiness
}

// GetStartupProbe returns startup probe
func (p *RuntimeComponentProbes) GetStartupProbe() *corev1.Probe {
func (p *RuntimeComponentProbes) GetStartupProbe(ba common.BaseComponent) *corev1.Probe {
return p.Startup
}

Expand Down
86 changes: 80 additions & 6 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,88 @@ import (
"k8s.io/apimachinery/pkg/util/intstr"
)

func ConvertBaseComponentProbeToCoreProbe(bcp *BaseComponentProbe, defaultProbe *corev1.Probe) *corev1.Probe {
if bcp == nil {
return nil
}
return CustomizeBaseComponentProbeDefaults(bcp, defaultProbe)
}

func CustomizeBaseComponentProbeDefaults(config *BaseComponentProbe, defaultProbe *corev1.Probe) *corev1.Probe {
probe := defaultProbe
if probe == nil {
probe = &corev1.Probe{}
}
if config == nil {
return probe
}
if config.BaseComponentProbeHandler.Exec != nil {
probe.ProbeHandler.Exec = config.BaseComponentProbeHandler.Exec
}
if config.BaseComponentProbeHandler.GRPC != nil {
probe.ProbeHandler.GRPC = config.BaseComponentProbeHandler.GRPC
}
if config.BaseComponentProbeHandler.TCPSocket != nil {
probe.ProbeHandler.TCPSocket = config.BaseComponentProbeHandler.TCPSocket
}
if config.BaseComponentProbeHandler.HTTPGet != nil {
probe.ProbeHandler.HTTPGet = convertOptionalHTTPGetActionToHTTPGetAction(config.BaseComponentProbeHandler.HTTPGet, probe.ProbeHandler.HTTPGet)
}
if config.InitialDelaySeconds != 0 {
probe.InitialDelaySeconds = config.InitialDelaySeconds
}
if config.TimeoutSeconds != 0 {
probe.TimeoutSeconds = config.TimeoutSeconds
}
if config.PeriodSeconds != 0 {
probe.PeriodSeconds = config.PeriodSeconds
}
if config.SuccessThreshold != 0 {
probe.SuccessThreshold = config.SuccessThreshold
}
if config.FailureThreshold != 0 {
probe.FailureThreshold = config.FailureThreshold
}
if config.TerminationGracePeriodSeconds != nil {
probe.TerminationGracePeriodSeconds = config.TerminationGracePeriodSeconds
}
return probe
}

func convertOptionalHTTPGetActionToHTTPGetAction(optAction *OptionalHTTPGetAction, defaultHTTPGetAction *corev1.HTTPGetAction) *corev1.HTTPGetAction {
action := defaultHTTPGetAction
if action == nil {
action = &corev1.HTTPGetAction{}
}
if optAction == nil {
return action
}
if optAction.Host != nil {
action.Host = *optAction.Host
}
if optAction.Path != nil {
action.Path = *optAction.Path
}
if optAction.Port != nil {
action.Port = *optAction.Port
}
if optAction.Scheme != nil {
action.Scheme = *optAction.Scheme
}
if optAction.HTTPHeaders != nil {
action.HTTPHeaders = *optAction.HTTPHeaders
}
return action
}

// GetDefaultMicroProfileStartupProbe returns the default values for MicroProfile Health-based startup probe.
func GetDefaultMicroProfileStartupProbe(ba BaseComponent) *corev1.Probe {
return &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/health/started",
Port: intstr.FromInt(int(ba.GetService().GetPort())),
Scheme: "HTTPS",
Port: intstr.FromInt(ba.GetManagedPort()),
Scheme: ba.GetManagedScheme(),
},
},
PeriodSeconds: 10,
Expand All @@ -27,8 +101,8 @@ func GetDefaultMicroProfileReadinessProbe(ba BaseComponent) *corev1.Probe {
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/health/ready",
Port: intstr.FromInt(int(ba.GetService().GetPort())),
Scheme: "HTTPS",
Port: intstr.FromInt(ba.GetManagedPort()),
Scheme: ba.GetManagedScheme(),
},
},
InitialDelaySeconds: 10,
Expand All @@ -44,8 +118,8 @@ func GetDefaultMicroProfileLivenessProbe(ba BaseComponent) *corev1.Probe {
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/health/live",
Port: intstr.FromInt(int(ba.GetService().GetPort())),
Scheme: "HTTPS",
Port: intstr.FromInt(ba.GetManagedPort()),
Scheme: ba.GetManagedScheme(),
},
},
InitialDelaySeconds: 60,
Expand Down
93 changes: 90 additions & 3 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
v1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)

// StatusConditionType ...
Expand Down Expand Up @@ -195,11 +196,95 @@ type BaseComponentStatefulSet interface {
GetAnnotations() map[string]string
}

// This struct is taken from the Probe specification in https://github.com/kubernetes/api/blob/v0.33.4/core/v1/types.go
// +kubebuilder:object:generate=true
type BaseComponentProbe struct {
Copy link
Member

Choose a reason for hiding this comment

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

This can be removed

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed the structs

// The action taken to determine the health of a container
BaseComponentProbeHandler `json:",inline"`
// Number of seconds after the container has started before liveness probes are initiated.
// More info: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
// +optional
InitialDelaySeconds int32 `json:"initialDelaySeconds,omitempty"`
// Number of seconds after which the probe times out.
// Defaults to 1 second. Minimum value is 1.
// More info: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
// +optional
TimeoutSeconds int32 `json:"timeoutSeconds,omitempty"`
// How often (in seconds) to perform the probe.
// Default to 10 seconds. Minimum value is 1.
// +optional
PeriodSeconds int32 `json:"periodSeconds,omitempty"`
// Minimum consecutive successes for the probe to be considered successful after having failed.
// Defaults to 1. Must be 1 for liveness and startup. Minimum value is 1.
// +optional
SuccessThreshold int32 `json:"successThreshold,omitempty"`
// Minimum consecutive failures for the probe to be considered failed after having succeeded.
// Defaults to 3. Minimum value is 1.
// +optional
FailureThreshold int32 `json:"failureThreshold,omitempty"`
// Optional duration in seconds the pod needs to terminate gracefully upon probe failure.
// The grace period is the duration in seconds after the processes running in the pod are sent
// a termination signal and the time when the processes are forcibly halted with a kill signal.
// Set this value longer than the expected cleanup time for your process.
// If this value is nil, the pod's terminationGracePeriodSeconds will be used. Otherwise, this
// value overrides the value provided by the pod spec.
// Value must be non-negative integer. The value zero indicates stop immediately via
// the kill signal (no opportunity to shut down).
// This is a beta field and requires enabling ProbeTerminationGracePeriod feature gate.
// Minimum value is 1. spec.terminationGracePeriodSeconds is used if unset.
// +optional
TerminationGracePeriodSeconds *int64 `json:"terminationGracePeriodSeconds,omitempty"`
}

// This struct is taken from the ProbeHandler specification in https://github.com/kubernetes/api/blob/v0.33.4/core/v1/types.go
// +kubebuilder:object:generate=true
type BaseComponentProbeHandler struct {
// Exec specifies the action to take.
// +optional
Exec *corev1.ExecAction `json:"exec,omitempty"`
// HTTPGet specifies the http request to perform.
// +optional
HTTPGet *OptionalHTTPGetAction `json:"httpGet,omitempty"`
// TCPSocket specifies an action involving a TCP port.
// +optional
TCPSocket *corev1.TCPSocketAction `json:"tcpSocket,omitempty"`

// GRPC specifies an action involving a GRPC port.
// This is a beta field and requires enabling GRPCContainerProbe feature gate.
// +featureGate=GRPCContainerProbe
// +optional
GRPC *corev1.GRPCAction `json:"grpc,omitempty"`
}

// This struct is based upon the HTTPGetAction specification in https://github.com/kubernetes/api/blob/v0.33.4/core/v1/types.go
// +kubebuilder:object:generate=true
type OptionalHTTPGetAction struct {
// Path to access on the HTTP server.
// +optional
Path *string `json:"path,omitempty"`
// Name or number of the port to access on the container.
// Number must be in the range 1 to 65535.
// Name must be an IANA_SVC_NAME.
// +optional
Port *intstr.IntOrString `json:"port,omitempty"`
// Host name to connect to, defaults to the pod IP. You probably want to set
// "Host" in httpHeaders instead.
// +optional
Host *string `json:"host,omitempty"`
// Scheme to use for connecting to the host.
// Defaults to HTTP.
// +optional
Scheme *corev1.URIScheme `json:"scheme,omitempty"`
// Custom headers to set in the request. HTTP allows repeated headers.
// +optional
HTTPHeaders *[]corev1.HTTPHeader `json:"httpHeaders,omitempty"`
}

// BaseComponentProbes describes the probes for application container
type BaseComponentProbes interface {
GetLivenessProbe() *corev1.Probe
GetReadinessProbe() *corev1.Probe
GetStartupProbe() *corev1.Probe
GetLivenessProbe(ba BaseComponent) *corev1.Probe
GetReadinessProbe(ba BaseComponent) *corev1.Probe
GetStartupProbe(ba BaseComponent) *corev1.Probe

GetDefaultLivenessProbe(ba BaseComponent) *corev1.Probe
GetDefaultReadinessProbe(ba BaseComponent) *corev1.Probe
Expand Down Expand Up @@ -257,6 +342,8 @@ type BaseComponent interface {
GetTopologySpreadConstraints() BaseComponentTopologySpreadConstraints
GetSecurityContext() *corev1.SecurityContext
GetManageTLS() *bool
GetManagedPort() int
GetManagedScheme() corev1.URIScheme
GetDisableServiceLinks() *bool
GetTolerations() []corev1.Toleration
GetDNS() BaseComponentDNS
Expand Down
Loading