diff --git a/api/v1/kubegres_types.go b/api/v1/kubegres_types.go index d557d3e3..b4a05c34 100644 --- a/api/v1/kubegres_types.go +++ b/api/v1/kubegres_types.go @@ -63,6 +63,7 @@ type Volume struct { type Probe struct { LivenessProbe *v1.Probe `json:"livenessProbe,omitempty"` ReadinessProbe *v1.Probe `json:"readinessProbe,omitempty"` + StartupProbe *v1.Probe `json:"startupProbe,omitempty"` } type KubegresSpec struct { diff --git a/api/v1/zz_generated.deepcopy.go b/api/v1/zz_generated.deepcopy.go index 34f27dc6..c9f97839 100644 --- a/api/v1/zz_generated.deepcopy.go +++ b/api/v1/zz_generated.deepcopy.go @@ -283,6 +283,11 @@ func (in *Probe) DeepCopyInto(out *Probe) { *out = new(corev1.Probe) (*in).DeepCopyInto(*out) } + if in.StartupProbe != nil { + in, out := &in.StartupProbe, &out.StartupProbe + *out = new(corev1.Probe) + (*in).DeepCopyInto(*out) + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Probe. diff --git a/config/crd/bases/kubegres.reactive-tech.io_kubegres.yaml b/config/crd/bases/kubegres.reactive-tech.io_kubegres.yaml index 48485874..4052802f 100644 --- a/config/crd/bases/kubegres.reactive-tech.io_kubegres.yaml +++ b/config/crd/bases/kubegres.reactive-tech.io_kubegres.yaml @@ -488,6 +488,156 @@ spec: format: int32 type: integer type: object + startupProbe: + description: Probe describes a health check to be performed against + a container to determine whether it is alive or ready to receive + traffic. + properties: + exec: + description: Exec specifies the action to take. + properties: + command: + description: Command is the command line to execute inside + the container, the working directory for the command is + root ('/') in the container's filesystem. The command + is simply exec'd, it is not run inside a shell, so traditional + shell instructions ('|', etc) won't work. To use a shell, + you need to explicitly call out to that shell. Exit + status of 0 is treated as live/healthy and non-zero + is unhealthy. + items: + type: string + type: array + type: object + failureThreshold: + description: Minimum consecutive failures for the probe to + be considered failed after having succeeded. Defaults to + 3. Minimum value is 1. + format: int32 + type: integer + grpc: + description: GRPC specifies an action involving a GRPC port. + This is a beta field and requires enabling GRPCContainerProbe + feature gate. + properties: + port: + description: Port number of the gRPC service. Number must + be in the range 1 to 65535. + format: int32 + type: integer + service: + description: "Service is the name of the service to place + in the gRPC HealthCheckRequest (see https://github.com/grpc/grpc/blob/master/doc/health-checking.md). + \n If this is not specified, the default behavior is + defined by gRPC." + type: string + required: + - port + type: object + httpGet: + description: HTTPGet specifies the http request to perform. + properties: + host: + description: Host name to connect to, defaults to the + pod IP. You probably want to set "Host" in httpHeaders + instead. + type: string + httpHeaders: + description: Custom headers to set in the request. HTTP + allows repeated headers. + items: + description: HTTPHeader describes a custom header to + be used in HTTP probes + properties: + name: + description: The header field name + type: string + value: + description: The header field value + type: string + required: + - name + - value + type: object + type: array + path: + description: Path to access on the HTTP server. + type: string + port: + anyOf: + - type: integer + - type: string + description: 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. + x-kubernetes-int-or-string: true + scheme: + description: Scheme to use for connecting to the host. + Defaults to HTTP. + type: string + required: + - port + type: object + initialDelaySeconds: + description: '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' + format: int32 + type: integer + periodSeconds: + description: How often (in seconds) to perform the probe. + Default to 10 seconds. Minimum value is 1. + format: int32 + type: integer + successThreshold: + description: 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. + format: int32 + type: integer + tcpSocket: + description: TCPSocket specifies an action involving a TCP + port. + properties: + host: + description: 'Optional: Host name to connect to, defaults + to the pod IP.' + type: string + port: + anyOf: + - type: integer + - type: string + description: Number or name of the port to access on the + container. Number must be in the range 1 to 65535. Name + must be an IANA_SVC_NAME. + x-kubernetes-int-or-string: true + required: + - port + type: object + terminationGracePeriodSeconds: + description: 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. + format: int64 + type: integer + timeoutSeconds: + description: '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' + format: int32 + type: integer + type: object type: object replicas: format: int32 diff --git a/controllers/ctx/resources/ResourcesContext.go b/controllers/ctx/resources/ResourcesContext.go index f3e283d4..00535c85 100644 --- a/controllers/ctx/resources/ResourcesContext.go +++ b/controllers/ctx/resources/ResourcesContext.go @@ -166,6 +166,7 @@ func addStatefulSetSpecEnforcers(rc *ResourcesContext) { securityContextSpecEnforcer := statefulset_spec.CreateSecurityContextSpecEnforcer(rc.KubegresContext) livenessProbeSpecEnforcer := statefulset_spec.CreateLivenessProbeSpecEnforcer(rc.KubegresContext) readinessProbeSpecEnforcer := statefulset_spec.CreateReadinessProbeSpecEnforcer(rc.KubegresContext) + startupProbeSpecEnforcer := statefulset_spec.CreateStartupProbeSpecEnforcer(rc.KubegresContext) rc.StatefulSetsSpecsEnforcer = statefulset_spec.CreateStatefulSetsSpecsEnforcer(rc.KubegresContext) rc.StatefulSetsSpecsEnforcer.AddSpecEnforcer(&imageSpecEnforcer) @@ -179,6 +180,7 @@ func addStatefulSetSpecEnforcers(rc *ResourcesContext) { rc.StatefulSetsSpecsEnforcer.AddSpecEnforcer(&securityContextSpecEnforcer) rc.StatefulSetsSpecsEnforcer.AddSpecEnforcer(&livenessProbeSpecEnforcer) rc.StatefulSetsSpecsEnforcer.AddSpecEnforcer(&readinessProbeSpecEnforcer) + rc.StatefulSetsSpecsEnforcer.AddSpecEnforcer(&startupProbeSpecEnforcer) rc.AllStatefulSetsSpecEnforcer = statefulset_spec.CreateAllStatefulSetsSpecEnforcer(rc.KubegresContext, rc.ResourcesStates, rc.BlockingOperation, rc.StatefulSetsSpecsEnforcer) } diff --git a/controllers/spec/enforcer/statefulset_spec/StartupProbeSpecEnforcer.go b/controllers/spec/enforcer/statefulset_spec/StartupProbeSpecEnforcer.go new file mode 100644 index 00000000..1ca3e048 --- /dev/null +++ b/controllers/spec/enforcer/statefulset_spec/StartupProbeSpecEnforcer.go @@ -0,0 +1,69 @@ +/* +Copyright 2021 Reactive Tech Limited. +"Reactive Tech Limited" is a company located in England, United Kingdom. +https://www.reactive-tech.io + +Lead Developer: Alex Arica + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +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. +*/ + +package statefulset_spec + +import ( + apps "k8s.io/api/apps/v1" + "reactive-tech.io/kubegres/controllers/ctx" + "reactive-tech.io/kubegres/controllers/states" + "reflect" +) + +type StartupProbeSpecEnforcer struct { + kubegresContext ctx.KubegresContext + resourcesStates states.ResourcesStates +} + +func CreateStartupProbeSpecEnforcer(kubegresContext ctx.KubegresContext) StartupProbeSpecEnforcer { + return StartupProbeSpecEnforcer{kubegresContext: kubegresContext} +} + +func (r *StartupProbeSpecEnforcer) GetSpecName() string { + return "StartupProbe" +} + +func (r *StartupProbeSpecEnforcer) CheckForSpecDifference(statefulSet *apps.StatefulSet) StatefulSetSpecDifference { + current := statefulSet.Spec.Template.Spec.Containers[0].StartupProbe + expected := r.kubegresContext.Kubegres.Spec.Probe.StartupProbe + + if expected == nil { + return StatefulSetSpecDifference{} + } + + if !reflect.DeepEqual(current, expected) { + return StatefulSetSpecDifference{ + SpecName: r.GetSpecName(), + Current: current.String(), + Expected: expected.String(), + } + } + + return StatefulSetSpecDifference{} +} + +func (r *StartupProbeSpecEnforcer) EnforceSpec(statefulSet *apps.StatefulSet) (wasSpecUpdated bool, err error) { + statefulSet.Spec.Template.Spec.Containers[0].StartupProbe = r.kubegresContext.Kubegres.Spec.Probe.StartupProbe + return true, nil +} + +func (r *StartupProbeSpecEnforcer) OnSpecEnforcedSuccessfully(statefulSet *apps.StatefulSet) error { + return nil +} diff --git a/controllers/spec/template/yaml/PrimaryStatefulSetTemplate.yaml b/controllers/spec/template/yaml/PrimaryStatefulSetTemplate.yaml index 8e52ffb1..f0e673fb 100644 --- a/controllers/spec/template/yaml/PrimaryStatefulSetTemplate.yaml +++ b/controllers/spec/template/yaml/PrimaryStatefulSetTemplate.yaml @@ -63,6 +63,17 @@ spec: apiVersion: v1 fieldPath: status.podIP + startupProbe: + exec: + command: + - sh + - -c + - exec pg_isready -U $POSTGRES_USER -h $POD_IP + failureThreshold: 15 + initialDelaySeconds: 10 + periodSeconds: 60 + successThreshold: 1 + livenessProbe: exec: command: diff --git a/controllers/spec/template/yaml/ReplicaStatefulSetTemplate.yaml b/controllers/spec/template/yaml/ReplicaStatefulSetTemplate.yaml index cb4bbe94..a60b0e7c 100644 --- a/controllers/spec/template/yaml/ReplicaStatefulSetTemplate.yaml +++ b/controllers/spec/template/yaml/ReplicaStatefulSetTemplate.yaml @@ -85,6 +85,17 @@ spec: apiVersion: v1 fieldPath: status.podIP + startupProbe: + exec: + command: + - sh + - -c + - exec pg_isready -U $POSTGRES_USER -h $POD_IP + failureThreshold: 15 + initialDelaySeconds: 10 + periodSeconds: 60 + successThreshold: 1 + livenessProbe: exec: command: diff --git a/controllers/spec/template/yaml/Templates.go b/controllers/spec/template/yaml/Templates.go index 537a9f2b..fde8d2db 100644 --- a/controllers/spec/template/yaml/Templates.go +++ b/controllers/spec/template/yaml/Templates.go @@ -400,6 +400,17 @@ spec: apiVersion: v1 fieldPath: status.podIP + startupProbe: + exec: + command: + - sh + - -c + - exec pg_isready -U $POSTGRES_USER -h $POD_IP + failureThreshold: 15 + initialDelaySeconds: 10 + periodSeconds: 60 + successThreshold: 1 + livenessProbe: exec: command: @@ -548,6 +559,17 @@ spec: apiVersion: v1 fieldPath: status.podIP + startupProbe: + exec: + command: + - sh + - -c + - exec pg_isready -U $POSTGRES_USER -h $POD_IP + failureThreshold: 15 + initialDelaySeconds: 10 + periodSeconds: 60 + successThreshold: 1 + livenessProbe: exec: command: diff --git a/kubegres.yaml b/kubegres.yaml index 278fac09..33e731f9 100644 --- a/kubegres.yaml +++ b/kubegres.yaml @@ -367,6 +367,108 @@ spec: format: int32 type: integer type: object + startupProbe: + description: Probe describes a health check to be performed against a container to determine whether it is alive or ready to receive traffic. + properties: + exec: + description: Exec specifies the action to take. + properties: + command: + description: Command is the command line to execute inside the container, the working directory for the command is root ('/') in the container's filesystem. The command is simply exec'd, it is not run inside a shell, so traditional shell instructions ('|', etc) won't work. To use a shell, you need to explicitly call out to that shell. Exit status of 0 is treated as live/healthy and non-zero is unhealthy. + items: + type: string + type: array + type: object + failureThreshold: + description: Minimum consecutive failures for the probe to be considered failed after having succeeded. Defaults to 3. Minimum value is 1. + format: int32 + type: integer + grpc: + description: GRPC specifies an action involving a GRPC port. This is a beta field and requires enabling GRPCContainerProbe feature gate. + properties: + port: + description: Port number of the gRPC service. Number must be in the range 1 to 65535. + format: int32 + type: integer + service: + description: "Service is the name of the service to place in the gRPC HealthCheckRequest (see https://github.com/grpc/grpc/blob/master/doc/health-checking.md). \n If this is not specified, the default behavior is defined by gRPC." + type: string + required: + - port + type: object + httpGet: + description: HTTPGet specifies the http request to perform. + properties: + host: + description: Host name to connect to, defaults to the pod IP. You probably want to set "Host" in httpHeaders instead. + type: string + httpHeaders: + description: Custom headers to set in the request. HTTP allows repeated headers. + items: + description: HTTPHeader describes a custom header to be used in HTTP probes + properties: + name: + description: The header field name + type: string + value: + description: The header field value + type: string + required: + - name + - value + type: object + type: array + path: + description: Path to access on the HTTP server. + type: string + port: + anyOf: + - type: integer + - type: string + description: 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. + x-kubernetes-int-or-string: true + scheme: + description: Scheme to use for connecting to the host. Defaults to HTTP. + type: string + required: + - port + type: object + initialDelaySeconds: + description: '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' + format: int32 + type: integer + periodSeconds: + description: How often (in seconds) to perform the probe. Default to 10 seconds. Minimum value is 1. + format: int32 + type: integer + successThreshold: + description: 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. + format: int32 + type: integer + tcpSocket: + description: TCPSocket specifies an action involving a TCP port. + properties: + host: + description: 'Optional: Host name to connect to, defaults to the pod IP.' + type: string + port: + anyOf: + - type: integer + - type: string + description: Number or name of the port to access on the container. Number must be in the range 1 to 65535. Name must be an IANA_SVC_NAME. + x-kubernetes-int-or-string: true + required: + - port + type: object + terminationGracePeriodSeconds: + description: 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. + format: int64 + type: integer + timeoutSeconds: + description: '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' + format: int32 + type: integer + type: object type: object replicas: format: int32