Skip to content

Commit

Permalink
Add AntiAffinity and NodeSelector capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
lmiccini committed Aug 12, 2024
1 parent 53a96f9 commit 8e55a15
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 7 deletions.
6 changes: 6 additions & 0 deletions apis/bases/memcached.openstack.org_memcacheds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ spec:
description: Name of the memcached 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 memcached cluster
Expand Down
4 changes: 4 additions & 0 deletions apis/memcached/v1beta1/memcached_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ type MemcachedSpecCore struct {
// Size of the memcached 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 memcached service
Expand Down
7 changes: 7 additions & 0 deletions apis/memcached/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/memcached.openstack.org_memcacheds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ spec:
description: Name of the memcached 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 memcached cluster
Expand Down
9 changes: 5 additions & 4 deletions pkg/memcached/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package memcached

import (
memcachedv1 "github.com/openstack-k8s-operators/infra-operator/apis/memcached/v1beta1"
common "github.com/openstack-k8s-operators/lib-common/modules/common"
labels "github.com/openstack-k8s-operators/lib-common/modules/common/labels"
service "github.com/openstack-k8s-operators/lib-common/modules/common/service"
corev1 "k8s.io/api/core/v1"
Expand All @@ -10,16 +11,16 @@ import (
// HeadlessService exposes all memcached repliscas for a memcached CR
func HeadlessService(m *memcachedv1.Memcached) *corev1.Service {
labels := labels.GetLabels(m, "memcached", map[string]string{
"owner": "infra-operator",
"cr": m.GetName(),
"app": m.GetName(),
common.OwnerSelector: "infra-operator",
"cr": m.GetName(),
common.AppSelector: m.GetName(),
})
details := &service.GenericServiceDetails{
Name: m.GetName(),
Namespace: m.GetNamespace(),
Labels: labels,
Selector: map[string]string{
"app": m.GetName(),
common.AppSelector: m.GetName(),
},
Ports: []corev1.ServicePort{
{Name: "memcached", Protocol: "TCP", Port: MemcachedPort},
Expand Down
22 changes: 19 additions & 3 deletions pkg/memcached/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package memcached

import (
memcachedv1 "github.com/openstack-k8s-operators/infra-operator/apis/memcached/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 All @@ -12,9 +14,9 @@ import (
// StatefulSet returns a Stateful resource for the Memcached CR
func StatefulSet(m *memcachedv1.Memcached) *appsv1.StatefulSet {
matchls := map[string]string{
"app": m.Name,
"cr": m.Name,
"owner": "infra-operator",
common.AppSelector: m.Name,
"cr": m.Name,
common.OwnerSelector: "infra-operator",
}
ls := labels.GetLabels(m, "memcached", matchls)
runAsUser := int64(0)
Expand Down Expand Up @@ -92,5 +94,19 @@ func StatefulSet(m *memcachedv1.Memcached) *appsv1.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.
sfs.Spec.Template.Spec.Affinity = affinity.DistributePods(
common.AppSelector,
[]string{
m.Name,
},
corev1.LabelHostname,
)
if m.Spec.NodeSelector != nil && len(m.Spec.NodeSelector) > 0 {
sfs.Spec.Template.Spec.NodeSelector = m.Spec.NodeSelector
}

return sfs
}

0 comments on commit 8e55a15

Please sign in to comment.