Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SKIP-478 Legge til deployment strategy og startup probe #20

Merged
merged 1 commit into from
Jul 25, 2022
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
34 changes: 20 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ spec:
# Maximum number of replicas the deployment is allowed to scale to
max: 5
# When the average CPU utilization crosses this threshold another replica is started
cpuThresholdPercentage: 80
targetCpuUtilization: 80
# Environment variables that will be set inside the Deployment's pod
env:
# Alternative 1: Keys and values provided directly
Expand Down Expand Up @@ -72,6 +72,14 @@ spec:
mountPath: /var/run/secret
- persistentVolumeClaim: some-pvc
mountPath: /var/run/volume
# Defines an alternative strategy for the Kubernetes deployment is useful for when
# the deafult which is rolling deployments are not usable. Setting type to
# Recreate will take down all the pods before starting new pods, whereas the
# default of RollingUpdate will try to start the new pods before taking down the
# old ones
strategy:
# Valid values: RollingUpdate, Recreate. Default RollingUpdate
type: RollingUpdate
# Liveness probes define a resource that returns 200 OK when the app is running
# as intended. Returning a non-200 code will make kubernetes restart the app.
# Liveness is optional, but when provided path and port is requred
Expand All @@ -94,19 +102,17 @@ spec:
# marking the pod as Running and progressing with the deployment strategy.
# Readiness is optional, but when provided path and port is requred
readiness:
# The path to access on the HTTP server
path: /healthz
# Number of the port to access on the container
port: 8080
# Minimum consecutive failures for the probe to be considered failed after
# having succeeded. Defaults to 3. Minimum value is 1
failureThreshold: 3
# How often (in seconds) to perform the probe. Default to 10 seconds.
# Minimum value is 1
periodSeconds: 10
# Number of seconds after which the probe times out. Defaults to 1 second.
# Minimum value is 1
timeout: 1
# Readiness has the same options as liveness
path: ..
# Kubernetes uses startup probes to know when a container application has started.
# If such a probe is configured, it disables liveness and readiness checks until it
# succeeds, making sure those probes don't interfere with the application startup.
# This can be used to adopt liveness checks on slow starting containers, avoiding them
# getting killed by Kubernetes before they are up and running.
# Startup is optional, but when provided path and port is requred
startup:
# Startup has the same options as liveness
path: ..
# Resource limits to apply to the deployment. It's common to set these to
# prevent the app from swelling in resource usage and consuming all the
# resources of other apps on the cluster.
Expand Down
10 changes: 9 additions & 1 deletion api/v1alpha1/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type ApplicationSpec struct {

Resources corev1.ResourceRequirements `json:"resources,omitempty"`
Replicas Replicas `json:"replicas,omitempty"`
Strategy Strategy `json:"strategy,omitempty"`

Env []corev1.EnvVar `json:"env,omitempty"`
EnvFrom []EnvFrom `json:"envFrom,omitempty"`
Expand All @@ -46,6 +47,7 @@ type ApplicationSpec struct {
Port int `json:"port"`
Liveness *Probe `json:"liveness,omitempty"`
Readiness *Probe `json:"readiness,omitempty"`
Startup *Probe `json:"startup,omitempty"`

Ingresses []string `json:"ingresses,omitempty"`
AccessPolicy AccessPolicy `json:"accessPolicy,omitempty"`
Expand All @@ -58,6 +60,12 @@ type Replicas struct {
TargetCpuUtilization uint `json:"targetCpuUtilization,omitempty"`
}

type Strategy struct {
// +kubebuilder:validation:Enum=RollingUpdate;Recreate
// +kubebuilder:default=RollingUpdate
Type string `json:"type,omitempty"`
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Noen grunn til å introdusere en egen struct bare for denne stringen? Har vi planer om mer rollout strategy options down the line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tenkte det ja, begynte med dette nå så NRL kan få redeploya databasen sin så kan vi legge inn options for RollingUpdate og slikt her etter hvert

type EnvFrom struct {
ConfigMap string `json:"configMap,omitempty"`
Secret string `json:"secret,omitempty"`
Expand Down Expand Up @@ -91,7 +99,7 @@ type InboundPolicy struct {
}

type OutboundPolicy struct {
Rules []InternalRule `json:"rules"`
Rules []InternalRule `json:"rules,omitempty"`
External []ExternalRule `json:"external,omitempty"`
}

Expand Down
21 changes: 21 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

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

27 changes: 25 additions & 2 deletions config/crd/bases/skiperator.kartverket.no_applications.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ spec:
- application
type: object
type: array
required:
- rules
type: object
type: object
command:
Expand Down Expand Up @@ -304,6 +302,31 @@ spec:
to an implementation-defined value. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/'
type: object
type: object
startup:
properties:
failureThreshold:
type: integer
initialDelay:
type: integer
path:
type: string
port:
type: integer
timeout:
type: integer
required:
- path
- port
type: object
strategy:
properties:
type:
default: RollingUpdate
enum:
- RollingUpdate
- Recreate
type: string
type: object
required:
- image
- port
Expand Down
2 changes: 1 addition & 1 deletion config/samples/skiperator_v1alpha1_skip.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ spec:
replicas:
min: 3
max: 5
cpuThresholdPercentage: 80
targetCpuUtilization: 80
env:
- name: ENV
value: PRODUCTION
Expand Down
16 changes: 16 additions & 0 deletions controllers/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"context"

skiperatorv1alpha1 "github.com/kartverket/skiperator/api/v1alpha1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -62,6 +63,11 @@ func (r *DeploymentReconciler) Reconcile(ctx context.Context, req reconcile.Requ
}
deployment.Spec.Replicas = &replicas

deployment.Spec.Strategy.Type = appsv1.DeploymentStrategyType(application.Spec.Strategy.Type)
if application.Spec.Strategy.Type == "Recreate" {
deployment.Spec.Strategy.RollingUpdate = nil
}

deployment.Spec.Template.Spec.Containers = make([]corev1.Container, 1)
container := &deployment.Spec.Template.Spec.Containers[0]
container.Name = application.Name
Expand Down Expand Up @@ -156,6 +162,16 @@ func (r *DeploymentReconciler) Reconcile(ctx context.Context, req reconcile.Requ
container.LivenessProbe.HTTPGet.Port = intstr.FromInt(int(application.Spec.Liveness.Port))
container.LivenessProbe.HTTPGet.Path = application.Spec.Liveness.Path
}
if application.Spec.Startup != nil {
container.StartupProbe = &corev1.Probe{}
container.StartupProbe.InitialDelaySeconds = int32(application.Spec.Startup.InitialDelay)
container.StartupProbe.TimeoutSeconds = int32(application.Spec.Startup.Timeout)
container.StartupProbe.FailureThreshold = int32(application.Spec.Startup.FailureThreshold)

container.StartupProbe.HTTPGet = &corev1.HTTPGetAction{}
container.StartupProbe.HTTPGet.Port = intstr.FromInt(int(application.Spec.Startup.Port))
container.StartupProbe.HTTPGet.Path = application.Spec.Startup.Path
}

return nil
})
Expand Down