Skip to content

Commit

Permalink
Add Affinity and NodeSelector to Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
lmiccini committed Oct 25, 2024
1 parent 3e23dc6 commit 69c5587
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
6 changes: 6 additions & 0 deletions apis/bases/redis.openstack.org_redises.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ spec:
description: Name of the redis container image to run (will be set
to environmental default if empty)
type: string
nodeSelector:
additionalProperties:
type: string
description: NodeSelector to target subset of worker nodes running
this service
type: object
replicas:
default: 1
description: Size of the redis cluster
Expand Down
13 changes: 9 additions & 4 deletions apis/redis/v1beta1/redis_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ const (
// RedisContainerImage is the fall-back container image for Redis
RedisContainerImage = "quay.io/podified-antelope-centos9/openstack-redis:current-podified"

// CrMaxLengthCorrection - DNS1123LabelMaxLength (63) - CrMaxLengthCorrection used in validation to
// omit issue with statefulset pod label "controller-revision-hash": "<statefulset_name>-<hash>"
// Int32 is a 10 character + hyphen = 11
CrMaxLengthCorrection = 11
// CrMaxLengthCorrection - DNS1123LabelMaxLength (63) - CrMaxLengthCorrection used in validation to
// omit issue with statefulset pod label "controller-revision-hash": "<statefulset_name>-<hash>"
// Int32 is a 10 character + hyphen = 11
CrMaxLengthCorrection = 11
)

// RedisSpec defines the desired state of Redis
Expand All @@ -50,6 +50,11 @@ type RedisSpecCore struct {
// +kubebuilder:default=1
// Size of the redis cluster
Replicas *int32 `json:"replicas"`

// +kubebuilder:validation:Optional
// NodeSelector to target subset of worker nodes running this service
NodeSelector map[string]string `json:"nodeSelector,omitempty"`

// +kubebuilder:validation:Optional
// +operator-sdk:csv:customresourcedefinitions:type=spec
// TLS settings for Redis service and internal Redis replication
Expand Down
7 changes: 7 additions & 0 deletions apis/redis/v1beta1/zz_generated.deepcopy.go

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

6 changes: 6 additions & 0 deletions config/crd/bases/redis.openstack.org_redises.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ spec:
description: Name of the redis container image to run (will be set
to environmental default if empty)
type: string
nodeSelector:
additionalProperties:
type: string
description: NodeSelector to target subset of worker nodes running
this service
type: object
replicas:
default: 1
description: Size of the redis cluster
Expand Down
15 changes: 15 additions & 0 deletions pkg/redis/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

redisv1 "github.com/openstack-k8s-operators/infra-operator/apis/redis/v1beta1"
common "github.com/openstack-k8s-operators/lib-common/modules/common"
"github.com/openstack-k8s-operators/lib-common/modules/common/affinity"
labels "github.com/openstack-k8s-operators/lib-common/modules/common/labels"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -141,5 +142,19 @@ func StatefulSet(
},
}

// If possible two pods of the same service should not
// run on the same worker node. If this is not possible
// the get still created on the same worker node.
sts.Spec.Template.Spec.Affinity = affinity.DistributePods(
common.AppSelector,
[]string{
r.Name,
},
corev1.LabelHostname,
)
if r.Spec.NodeSelector != nil && len(r.Spec.NodeSelector) > 0 {
sts.Spec.Template.Spec.NodeSelector = r.Spec.NodeSelector
}

return sts
}

0 comments on commit 69c5587

Please sign in to comment.