Skip to content

Commit

Permalink
Merge pull request spotahome#327 from hoffoo/term-grace
Browse files Browse the repository at this point in the history
add terminationGracePeriodSeconds option
  • Loading branch information
ese committed Jan 12, 2022
2 parents bf700a2 + 70f08be commit 9869cc4
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 32 deletions.
48 changes: 27 additions & 21 deletions api/redisfailover/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// RedisFailover represents a Redis failover
// +kubebuilder:printcolumn:name="NAME",type="string",JSONPath=".metadata.name"
// +kubebuilder:printcolumn:name="REDIS",type="integer",JSONPath=".spec.redis.replicas"
// +kubebuilder:printcolumn:name="SENTINELS",type="integer",JSONPath=".spec.sentinel.replicas"
// +kubebuilder:printcolumn:name="AGE",type="date",JSONPath=".metadata.creationTimestamp"
// +kubebuilder:resource:singular=redisfailover,path=redisfailovers,shortName=rf,scope=Namespaced
type RedisFailover struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand All @@ -32,27 +37,28 @@ type RedisCommandRename struct {

// RedisSettings defines the specification of the redis cluster
type RedisSettings struct {
Image string `json:"image,omitempty"`
ImagePullPolicy corev1.PullPolicy `json:"imagePullPolicy,omitempty"`
Replicas int32 `json:"replicas,omitempty"`
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
CustomConfig []string `json:"customConfig,omitempty"`
CustomCommandRenames []RedisCommandRename `json:"customCommandRenames,omitempty"`
Command []string `json:"command,omitempty"`
ShutdownConfigMap string `json:"shutdownConfigMap,omitempty"`
Storage RedisStorage `json:"storage,omitempty"`
Exporter RedisExporter `json:"exporter,omitempty"`
Affinity *corev1.Affinity `json:"affinity,omitempty"`
SecurityContext *corev1.PodSecurityContext `json:"securityContext,omitempty"`
ImagePullSecrets []corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
Tolerations []corev1.Toleration `json:"tolerations,omitempty"`
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
PodAnnotations map[string]string `json:"podAnnotations,omitempty"`
ServiceAnnotations map[string]string `json:"serviceAnnotations,omitempty"`
HostNetwork bool `json:"hostNetwork,omitempty"`
DNSPolicy corev1.DNSPolicy `json:"dnsPolicy,omitempty"`
PriorityClassName string `json:"priorityClassName,omitempty"`
ServiceAccountName string `json:"serviceAccountName,omitempty"`
Image string `json:"image,omitempty"`
ImagePullPolicy corev1.PullPolicy `json:"imagePullPolicy,omitempty"`
Replicas int32 `json:"replicas,omitempty"`
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
CustomConfig []string `json:"customConfig,omitempty"`
CustomCommandRenames []RedisCommandRename `json:"customCommandRenames,omitempty"`
Command []string `json:"command,omitempty"`
ShutdownConfigMap string `json:"shutdownConfigMap,omitempty"`
Storage RedisStorage `json:"storage,omitempty"`
Exporter RedisExporter `json:"exporter,omitempty"`
Affinity *corev1.Affinity `json:"affinity,omitempty"`
SecurityContext *corev1.PodSecurityContext `json:"securityContext,omitempty"`
ImagePullSecrets []corev1.LocalObjectReference `json:"imagePullSecrets,omitempty"`
Tolerations []corev1.Toleration `json:"tolerations,omitempty"`
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
PodAnnotations map[string]string `json:"podAnnotations,omitempty"`
ServiceAnnotations map[string]string `json:"serviceAnnotations,omitempty"`
HostNetwork bool `json:"hostNetwork,omitempty"`
DNSPolicy corev1.DNSPolicy `json:"dnsPolicy,omitempty"`
PriorityClassName string `json:"priorityClassName,omitempty"`
ServiceAccountName string `json:"serviceAccountName,omitempty"`
TerminationGracePeriodSeconds int64 `json:"terminationGracePeriod,omitempty"`
}

// SentinelSettings defines the specification of the sentinel cluster
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,25 @@ spec:
kind: RedisFailover
listKind: RedisFailoverList
plural: redisfailovers
shortNames:
- rf
singular: redisfailover
scope: Namespaced
versions:
- name: v1
- additionalPrinterColumns:
- jsonPath: .metadata.name
name: NAME
type: string
- jsonPath: .spec.redis.replicas
name: REDIS
type: integer
- jsonPath: .spec.sentinel.replicas
name: SENTINELS
type: integer
- jsonPath: .metadata.creationTimestamp
name: AGE
type: date
name: v1
schema:
openAPIV3Schema:
description: RedisFailover represents a Redis failover
Expand Down Expand Up @@ -1600,6 +1615,9 @@ spec:
type: object
type: object
type: object
terminationGracePeriod:
format: int64
type: integer
tolerations:
items:
description: The pod this Toleration is attached to tolerates
Expand Down Expand Up @@ -2925,6 +2943,7 @@ spec:
type: object
served: true
storage: true
subresources: {}
status:
acceptedNames:
kind: ""
Expand Down
21 changes: 20 additions & 1 deletion manifests/databases.spotahome.com_redisfailovers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,25 @@ spec:
kind: RedisFailover
listKind: RedisFailoverList
plural: redisfailovers
shortNames:
- rf
singular: redisfailover
scope: Namespaced
versions:
- name: v1
- additionalPrinterColumns:
- jsonPath: .metadata.name
name: NAME
type: string
- jsonPath: .spec.redis.replicas
name: REDIS
type: integer
- jsonPath: .spec.sentinel.replicas
name: SENTINELS
type: integer
- jsonPath: .metadata.creationTimestamp
name: AGE
type: date
name: v1
schema:
openAPIV3Schema:
description: RedisFailover represents a Redis failover
Expand Down Expand Up @@ -1600,6 +1615,9 @@ spec:
type: object
type: object
type: object
terminationGracePeriod:
format: int64
type: integer
tolerations:
items:
description: The pod this Toleration is attached to tolerates
Expand Down Expand Up @@ -2925,6 +2943,7 @@ spec:
type: object
served: true
storage: true
subresources: {}
status:
acceptedNames:
kind: ""
Expand Down
27 changes: 18 additions & 9 deletions operator/redisfailover/service/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ func generateRedisStatefulSet(rf *redisfailoverv1.RedisFailover, labels map[stri
labels = util.MergeLabels(labels, selectorLabels)
volumeMounts := getRedisVolumeMounts(rf)
volumes := getRedisVolumes(rf)
terminationGracePeriodSeconds := getTerminationGracePeriodSeconds(rf)

ss := &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -274,15 +275,16 @@ func generateRedisStatefulSet(rf *redisfailoverv1.RedisFailover, labels map[stri
Annotations: rf.Spec.Redis.PodAnnotations,
},
Spec: corev1.PodSpec{
Affinity: getAffinity(rf.Spec.Redis.Affinity, labels),
Tolerations: rf.Spec.Redis.Tolerations,
NodeSelector: rf.Spec.Redis.NodeSelector,
SecurityContext: getSecurityContext(rf.Spec.Redis.SecurityContext),
HostNetwork: rf.Spec.Redis.HostNetwork,
DNSPolicy: getDnsPolicy(rf.Spec.Redis.DNSPolicy),
ImagePullSecrets: rf.Spec.Redis.ImagePullSecrets,
PriorityClassName: rf.Spec.Redis.PriorityClassName,
ServiceAccountName: rf.Spec.Redis.ServiceAccountName,
Affinity: getAffinity(rf.Spec.Redis.Affinity, labels),
Tolerations: rf.Spec.Redis.Tolerations,
NodeSelector: rf.Spec.Redis.NodeSelector,
SecurityContext: getSecurityContext(rf.Spec.Redis.SecurityContext),
HostNetwork: rf.Spec.Redis.HostNetwork,
DNSPolicy: getDnsPolicy(rf.Spec.Redis.DNSPolicy),
ImagePullSecrets: rf.Spec.Redis.ImagePullSecrets,
PriorityClassName: rf.Spec.Redis.PriorityClassName,
ServiceAccountName: rf.Spec.Redis.ServiceAccountName,
TerminationGracePeriodSeconds: &terminationGracePeriodSeconds,
Containers: []corev1.Container{
{
Name: "redis",
Expand Down Expand Up @@ -794,3 +796,10 @@ func pullPolicy(specPolicy corev1.PullPolicy) corev1.PullPolicy {
}
return specPolicy
}

func getTerminationGracePeriodSeconds(rf *redisfailoverv1.RedisFailover) int64 {
if rf.Spec.Redis.TerminationGracePeriodSeconds > 0 {
return rf.Spec.Redis.TerminationGracePeriodSeconds
}
return 30
}

0 comments on commit 9869cc4

Please sign in to comment.