Skip to content

Commit

Permalink
Support for initialDelaySeconds and init containers (#9)
Browse files Browse the repository at this point in the history
* Added support for initialDelaySeconds and InitContainers

* renamed inits section in initContainers, generated files using operator-sdk v0.10.0
  • Loading branch information
step76 authored and nrvnrvn committed Sep 25, 2019
1 parent 7c5065d commit 381d91f
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 22 deletions.
20 changes: 20 additions & 0 deletions deploy/crds/k8s_v1alpha1_redis_crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ spec:
image:
description: Image is a standard path for a Container image
type: string
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
resources:
description: Resources describes the compute resource requirements
type: object
Expand All @@ -86,6 +91,11 @@ spec:
items:
type: object
type: array
initContainers:
description: Pod initContainers
items:
type: object
type: array
password:
properties:
secretKeyRef:
Expand All @@ -104,6 +114,11 @@ spec:
image:
description: Image is a standard path for a Container image
type: string
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
resources:
description: Resources describes the compute resource requirements
type: object
Expand Down Expand Up @@ -131,6 +146,11 @@ spec:
items:
type: object
type: array
volumes:
description: Volumes for StatefulSet
items:
type: object
type: array
required:
- replicas
- redis
Expand Down
36 changes: 31 additions & 5 deletions example/k8s_v1alpha1_redis_cr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ spec:
annotations:
cluster-autoscaler.kubernetes.io/safe-to-evict: "true"
seccomp.security.alpha.kubernetes.io/pod: runtime/default
securityContext:
runAsUser: 7777777
runAsGroup: 7777777
fsGroup: 7777777
runAsNonRoot: true

# dataVolumeClaimTemplate allows to define a persistent volume template for Redis. (optional)
# If omitted, emptyDir will be used.
Expand All @@ -86,6 +81,7 @@ spec:
# More info: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.14/#container-v1-core
redis:
image: redis:5-alpine
initialDelaySeconds: 10
resources:
limits:
cpu: 100m
Expand All @@ -96,6 +92,10 @@ spec:
drop:
- all
readOnlyRootFilesystem: true
runAsUser: 7777777
runAsGroup: 7777777
fsGroup: 7777777
runAsNonRoot: true

# Redis exporter container definition (optional)
exporter:
Expand All @@ -110,3 +110,29 @@ spec:
drop:
- all
readOnlyRootFilesystem: true
runAsUser: 7777777
runAsGroup: 7777777
fsGroup: 7777777
runAsNonRoot: true

# To disable THP
volumes:
- name: sys
hostPath:
path: /sys
initContainers:
- name: disable-thp
image: busybox
command: ["sh", "-c"]
args:
- |-
set -e
set -o pipefail
echo never > /rootfs/sys/kernel/mm/transparent_hugepage/enabled
grep -q -F [never] /sys/kernel/mm/transparent_hugepage/enabled
volumeMounts:
- name: sys
mountPath: /rootfs/sys
type: Directory
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/amaizfinance/redis-operator
go 1.13

require (
github.com/NYTimes/gziphandler v1.0.1 // indirect
github.com/cenkalti/backoff/v3 v3.0.0
github.com/go-openapi/spec v0.19.0
github.com/go-redis/redis v6.15.5+incompatible
Expand Down
9 changes: 9 additions & 0 deletions pkg/apis/k8s/v1alpha1/redis_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,17 @@ type RedisSpec struct {
PriorityClassName string `json:"priorityClassName,omitempty"`
// DataVolumeClaimTemplate for StatefulSet
DataVolumeClaimTemplate corev1.PersistentVolumeClaim `json:"dataVolumeClaimTemplate,omitempty"`
// Volumes for StatefulSet
Volumes []corev1.Volume `json:"volumes,omitempty"`

// Redis container specification
Redis ContainerSpec `json:"redis"`

// Exporter container specification
Exporter ContainerSpec `json:"exporter,omitempty"`

// Pod initContainers
InitContainers []corev1.Container `json:"initContainers,omitempty"`
}

// Password allows to refer to a Secret containing password for Redis
Expand Down Expand Up @@ -104,6 +109,10 @@ type ContainerSpec struct {
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
// SecurityContext holds security configuration that will be applied to a container
SecurityContext *corev1.SecurityContext `json:"securityContext,omitempty"`
// 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"`
}

// RedisStatus contains the observed state of Redis
Expand Down
14 changes: 14 additions & 0 deletions pkg/apis/k8s/v1alpha1/zz_generated.deepcopy.go

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

63 changes: 48 additions & 15 deletions pkg/apis/k8s/v1alpha1/zz_generated.openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import (

func GetOpenAPIDefinitions(ref common.ReferenceCallback) map[string]common.OpenAPIDefinition {
return map[string]common.OpenAPIDefinition{
"./pkg/apis/k8s/v1alpha1.ContainerSpec": schema_pkg_apis_k8s_v1alpha1_ContainerSpec(ref),
"./pkg/apis/k8s/v1alpha1.Password": schema_pkg_apis_k8s_v1alpha1_Password(ref),
"./pkg/apis/k8s/v1alpha1.Redis": schema_pkg_apis_k8s_v1alpha1_Redis(ref),
"./pkg/apis/k8s/v1alpha1.RedisList": schema_pkg_apis_k8s_v1alpha1_RedisList(ref),
"./pkg/apis/k8s/v1alpha1.RedisSpec": schema_pkg_apis_k8s_v1alpha1_RedisSpec(ref),
"./pkg/apis/k8s/v1alpha1.RedisStatus": schema_pkg_apis_k8s_v1alpha1_RedisStatus(ref),
"github.com/amaizfinance/redis-operator/pkg/apis/k8s/v1alpha1.ContainerSpec": schema_pkg_apis_k8s_v1alpha1_ContainerSpec(ref),
"github.com/amaizfinance/redis-operator/pkg/apis/k8s/v1alpha1.Password": schema_pkg_apis_k8s_v1alpha1_Password(ref),
"github.com/amaizfinance/redis-operator/pkg/apis/k8s/v1alpha1.Redis": schema_pkg_apis_k8s_v1alpha1_Redis(ref),
"github.com/amaizfinance/redis-operator/pkg/apis/k8s/v1alpha1.RedisList": schema_pkg_apis_k8s_v1alpha1_RedisList(ref),
"github.com/amaizfinance/redis-operator/pkg/apis/k8s/v1alpha1.RedisSpec": schema_pkg_apis_k8s_v1alpha1_RedisSpec(ref),
"github.com/amaizfinance/redis-operator/pkg/apis/k8s/v1alpha1.RedisStatus": schema_pkg_apis_k8s_v1alpha1_RedisStatus(ref),
}
}

Expand Down Expand Up @@ -59,6 +59,13 @@ func schema_pkg_apis_k8s_v1alpha1_ContainerSpec(ref common.ReferenceCallback) co
Ref: ref("k8s.io/api/core/v1.SecurityContext"),
},
},
"initialDelaySeconds": {
SchemaProps: spec.SchemaProps{
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",
Type: []string{"integer"},
Format: "int32",
},
},
},
Required: []string{"image"},
},
Expand Down Expand Up @@ -117,20 +124,20 @@ func schema_pkg_apis_k8s_v1alpha1_Redis(ref common.ReferenceCallback) common.Ope
},
"spec": {
SchemaProps: spec.SchemaProps{
Ref: ref("./pkg/apis/k8s/v1alpha1.RedisSpec"),
Ref: ref("github.com/amaizfinance/redis-operator/pkg/apis/k8s/v1alpha1.RedisSpec"),
},
},
"status": {
SchemaProps: spec.SchemaProps{
Ref: ref("./pkg/apis/k8s/v1alpha1.RedisStatus"),
Ref: ref("github.com/amaizfinance/redis-operator/pkg/apis/k8s/v1alpha1.RedisStatus"),
},
},
},
Required: []string{"spec"},
},
},
Dependencies: []string{
"./pkg/apis/k8s/v1alpha1.RedisSpec", "./pkg/apis/k8s/v1alpha1.RedisStatus", "k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta"},
"github.com/amaizfinance/redis-operator/pkg/apis/k8s/v1alpha1.RedisSpec", "github.com/amaizfinance/redis-operator/pkg/apis/k8s/v1alpha1.RedisStatus", "k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta"},
}
}

Expand Down Expand Up @@ -161,7 +168,7 @@ func schema_pkg_apis_k8s_v1alpha1_RedisList(ref common.ReferenceCallback) common
Items: &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Ref: ref("./pkg/apis/k8s/v1alpha1.Redis"),
Ref: ref("github.com/amaizfinance/redis-operator/pkg/apis/k8s/v1alpha1.Redis"),
},
},
},
Expand All @@ -172,7 +179,7 @@ func schema_pkg_apis_k8s_v1alpha1_RedisList(ref common.ReferenceCallback) common
},
},
Dependencies: []string{
"./pkg/apis/k8s/v1alpha1.Redis"},
"github.com/amaizfinance/redis-operator/pkg/apis/k8s/v1alpha1.Redis"},
}
}

Expand Down Expand Up @@ -205,7 +212,7 @@ func schema_pkg_apis_k8s_v1alpha1_RedisSpec(ref common.ReferenceCallback) common
},
"password": {
SchemaProps: spec.SchemaProps{
Ref: ref("./pkg/apis/k8s/v1alpha1.Password"),
Ref: ref("github.com/amaizfinance/redis-operator/pkg/apis/k8s/v1alpha1.Password"),
},
},
"annotations": {
Expand Down Expand Up @@ -280,24 +287,50 @@ func schema_pkg_apis_k8s_v1alpha1_RedisSpec(ref common.ReferenceCallback) common
Ref: ref("k8s.io/api/core/v1.PersistentVolumeClaim"),
},
},
"volumes": {
SchemaProps: spec.SchemaProps{
Description: "Volumes for StatefulSet",
Type: []string{"array"},
Items: &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Ref: ref("k8s.io/api/core/v1.Volume"),
},
},
},
},
},
"redis": {
SchemaProps: spec.SchemaProps{
Description: "Redis container specification",
Ref: ref("./pkg/apis/k8s/v1alpha1.ContainerSpec"),
Ref: ref("github.com/amaizfinance/redis-operator/pkg/apis/k8s/v1alpha1.ContainerSpec"),
},
},
"exporter": {
SchemaProps: spec.SchemaProps{
Description: "Exporter container specification",
Ref: ref("./pkg/apis/k8s/v1alpha1.ContainerSpec"),
Ref: ref("github.com/amaizfinance/redis-operator/pkg/apis/k8s/v1alpha1.ContainerSpec"),
},
},
"initContainers": {
SchemaProps: spec.SchemaProps{
Description: "Pod initContainers",
Type: []string{"array"},
Items: &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Ref: ref("k8s.io/api/core/v1.Container"),
},
},
},
},
},
},
Required: []string{"replicas", "redis"},
},
},
Dependencies: []string{
"./pkg/apis/k8s/v1alpha1.ContainerSpec", "./pkg/apis/k8s/v1alpha1.Password", "k8s.io/api/core/v1.Affinity", "k8s.io/api/core/v1.LocalObjectReference", "k8s.io/api/core/v1.PersistentVolumeClaim", "k8s.io/api/core/v1.PodSecurityContext", "k8s.io/api/core/v1.Toleration"},
"github.com/amaizfinance/redis-operator/pkg/apis/k8s/v1alpha1.ContainerSpec", "github.com/amaizfinance/redis-operator/pkg/apis/k8s/v1alpha1.Password", "k8s.io/api/core/v1.Affinity", "k8s.io/api/core/v1.Container", "k8s.io/api/core/v1.LocalObjectReference", "k8s.io/api/core/v1.PersistentVolumeClaim", "k8s.io/api/core/v1.PodSecurityContext", "k8s.io/api/core/v1.Toleration", "k8s.io/api/core/v1.Volume"},
}
}

Expand Down
16 changes: 14 additions & 2 deletions pkg/controller/redis/object_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@ func generateStatefulSet(r *k8sv1alpha1.Redis, password string) *appsv1.Stateful
},
}}

// append external volumes
if r.Spec.Volumes != nil {
volumes = append(volumes, r.Spec.Volumes...)
}

// redis container goes first
containers := []corev1.Container{{
Name: redisName,
Expand All @@ -276,8 +281,14 @@ func generateStatefulSet(r *k8sv1alpha1.Redis, password string) *appsv1.Stateful
MountPath: configMapMountPath,
SubPath: configFileName,
}},
LivenessProbe: &corev1.Probe{Handler: corev1.Handler{Exec: &corev1.ExecAction{Command: []string{"redis-cli", "ping"}}}},
ReadinessProbe: &corev1.Probe{Handler: corev1.Handler{Exec: &corev1.ExecAction{Command: []string{"redis-cli", "ping"}}}},
LivenessProbe: &corev1.Probe{
Handler: corev1.Handler{Exec: &corev1.ExecAction{Command: []string{"redis-cli", "ping"}}},
InitialDelaySeconds: r.Spec.Redis.InitialDelaySeconds,
},
ReadinessProbe: &corev1.Probe{
Handler: corev1.Handler{Exec: &corev1.ExecAction{Command: []string{"redis-cli", "ping"}}},
InitialDelaySeconds: r.Spec.Redis.InitialDelaySeconds,
},
ImagePullPolicy: corev1.PullAlways,
SecurityContext: r.Spec.Redis.SecurityContext,
}}
Expand Down Expand Up @@ -382,6 +393,7 @@ func generateStatefulSet(r *k8sv1alpha1.Redis, password string) *appsv1.Stateful
Spec: corev1.PodSpec{
Volumes: volumes,
Containers: containers,
InitContainers: r.Spec.InitContainers,
ServiceAccountName: r.Spec.ServiceAccountName,
SecurityContext: r.Spec.SecurityContext,
ImagePullSecrets: r.Spec.ImagePullSecrets,
Expand Down

0 comments on commit 381d91f

Please sign in to comment.