Skip to content

Commit

Permalink
Set pod affinity to spread services across OCP workers
Browse files Browse the repository at this point in the history
Pod affinity is actually an anti-affinity rule intended to spread
pods across all OCP workers. However, we don't want this rule to
apply to all cinder pods in general. The anti-affinity rule now
applies to each specific cinder service (api, scheduler, volume
and backup).
  • Loading branch information
ASBishop committed Feb 12, 2024
1 parent 6572d1b commit d169dc3
Show file tree
Hide file tree
Showing 15 changed files with 45 additions and 81 deletions.
2 changes: 1 addition & 1 deletion api/v1beta1/cinder_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ limitations under the License.
package v1beta1

import (
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
)

// CinderDefaults -
Expand Down
2 changes: 1 addition & 1 deletion controllers/cinderapi_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ func (r *CinderAPIReconciler) reconcileNormal(ctx context.Context, instance *cin
//
serviceLabels := map[string]string{
common.AppSelector: cinder.ServiceName,
common.ComponentSelector: cinderapi.Component,
common.ComponentSelector: cinderapi.ComponentName,
}

//
Expand Down
2 changes: 1 addition & 1 deletion controllers/cinderbackup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func (r *CinderBackupReconciler) reconcileNormal(ctx context.Context, instance *
//
serviceLabels := map[string]string{
common.AppSelector: cinder.ServiceName,
common.ComponentSelector: cinderbackup.Component,
common.ComponentSelector: cinderbackup.ComponentName,
}

//
Expand Down
2 changes: 1 addition & 1 deletion controllers/cinderscheduler_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ func (r *CinderSchedulerReconciler) reconcileNormal(ctx context.Context, instanc
//
serviceLabels := map[string]string{
common.AppSelector: cinder.ServiceName,
common.ComponentSelector: cinderscheduler.Component,
common.ComponentSelector: cinderscheduler.ComponentName,
}

//
Expand Down
4 changes: 2 additions & 2 deletions controllers/cindervolume_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,8 @@ func (r *CinderVolumeReconciler) reconcileNormal(ctx context.Context, instance *
//
serviceLabels := map[string]string{
common.AppSelector: cinder.ServiceName,
common.ComponentSelector: cindervolume.Component,
cindervolume.Backend: instance.Name[len(cindervolume.Component)+1:],
common.ComponentSelector: cindervolume.ComponentName,
cindervolume.Backend: instance.Name[len(cindervolume.ComponentName)+1:],
}

//
Expand Down
22 changes: 21 additions & 1 deletion pkg/cinder/funcs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package cinder

import "sigs.k8s.io/controller-runtime/pkg/client"
import (
common "github.com/openstack-k8s-operators/lib-common/modules/common"
"github.com/openstack-k8s-operators/lib-common/modules/common/affinity"

corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// GetOwningCinderName - Given a CinderAPI, CinderScheduler, CinderBackup or CinderVolume
// object, returning the parent Cinder object that created it (if any)
Expand All @@ -27,3 +33,17 @@ func GetNetworkAttachmentAddrs(namespace string, networkAttachments []string, ne

return networkAttachmentAddrs
}

// GetPodAffinity - Returns a corev1.Affinity reference for the specified component.
func GetPodAffinity(componentName string) *corev1.Affinity {
// If possible two pods of the same component (e.g cinder-api) should not
// run on the same worker node. If this is not possible they get still
// created on the same worker node.
return affinity.DistributePods(
common.ComponentSelector,
[]string{
componentName,
},
corev1.LabelHostname,
)
}
4 changes: 2 additions & 2 deletions pkg/cinderapi/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ limitations under the License.
package cinderapi

const (
// Component -
Component = "cinder-api"
// ComponentName -
ComponentName = "cinder-api"

//LogFile -
LogFile = "/var/log/cinder/cinder-api.log"
Expand Down
18 changes: 2 additions & 16 deletions pkg/cinderapi/statefuleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
cinderv1beta1 "github.com/openstack-k8s-operators/cinder-operator/api/v1beta1"
cinder "github.com/openstack-k8s-operators/cinder-operator/pkg/cinder"
common "github.com/openstack-k8s-operators/lib-common/modules/common"
"github.com/openstack-k8s-operators/lib-common/modules/common/affinity"
"github.com/openstack-k8s-operators/lib-common/modules/common/env"
"github.com/openstack-k8s-operators/lib-common/modules/common/service"
"github.com/openstack-k8s-operators/lib-common/modules/common/tls"
Expand Down Expand Up @@ -162,7 +161,7 @@ func StatefulSet(
Resources: instance.Spec.Resources,
},
{
Name: cinder.ServiceName + "-api",
Name: ComponentName,
Command: []string{
"/bin/bash",
},
Expand All @@ -178,26 +177,13 @@ func StatefulSet(
LivenessProbe: livenessProbe,
},
},
Affinity: cinder.GetPodAffinity(ComponentName),
NodeSelector: instance.Spec.NodeSelector,
Volumes: volumes,
},
},
},
}

// 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.
statefulset.Spec.Template.Spec.Affinity = affinity.DistributePods(
common.AppSelector,
[]string{
cinder.ServiceName,
},
corev1.LabelHostname,
)
if instance.Spec.NodeSelector != nil && len(instance.Spec.NodeSelector) > 0 {
statefulset.Spec.Template.Spec.NodeSelector = instance.Spec.NodeSelector
}

return statefulset, nil
}
4 changes: 2 additions & 2 deletions pkg/cinderbackup/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ limitations under the License.
package cinderbackup

const (
// Component -
Component = "cinder-backup"
// ComponentName -
ComponentName = "cinder-backup"
)
18 changes: 2 additions & 16 deletions pkg/cinderbackup/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
cinderv1 "github.com/openstack-k8s-operators/cinder-operator/api/v1beta1"
cinder "github.com/openstack-k8s-operators/cinder-operator/pkg/cinder"
common "github.com/openstack-k8s-operators/lib-common/modules/common"
"github.com/openstack-k8s-operators/lib-common/modules/common/affinity"
"github.com/openstack-k8s-operators/lib-common/modules/common/env"

appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -137,7 +136,7 @@ func StatefulSet(
HostPID: true,
Containers: []corev1.Container{
{
Name: cinder.ServiceName + "-backup",
Name: ComponentName,
Command: []string{
"/bin/bash",
},
Expand All @@ -164,26 +163,13 @@ func StatefulSet(
VolumeMounts: volumeMounts,
},
},
Affinity: cinder.GetPodAffinity(ComponentName),
NodeSelector: instance.Spec.NodeSelector,
Volumes: volumes,
},
},
},
}

// 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.
statefulset.Spec.Template.Spec.Affinity = affinity.DistributePods(
common.AppSelector,
[]string{
cinder.ServiceName,
},
corev1.LabelHostname,
)
if instance.Spec.NodeSelector != nil && len(instance.Spec.NodeSelector) > 0 {
statefulset.Spec.Template.Spec.NodeSelector = instance.Spec.NodeSelector
}

return statefulset
}
4 changes: 2 additions & 2 deletions pkg/cinderscheduler/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ limitations under the License.
package cinderscheduler

const (
// Component -
Component = "cinder-scheduler"
// ComponentName -
ComponentName = "cinder-scheduler"
)
18 changes: 2 additions & 16 deletions pkg/cinderscheduler/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
cinderv1 "github.com/openstack-k8s-operators/cinder-operator/api/v1beta1"
cinder "github.com/openstack-k8s-operators/cinder-operator/pkg/cinder"
common "github.com/openstack-k8s-operators/lib-common/modules/common"
"github.com/openstack-k8s-operators/lib-common/modules/common/affinity"
"github.com/openstack-k8s-operators/lib-common/modules/common/env"

appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -123,7 +122,7 @@ func StatefulSet(
ServiceAccountName: instance.Spec.ServiceAccount,
Containers: []corev1.Container{
{
Name: cinder.ServiceName + "-scheduler",
Name: ComponentName,
Command: []string{
"/bin/bash",
},
Expand All @@ -149,26 +148,13 @@ func StatefulSet(
VolumeMounts: volumeMounts,
},
},
Affinity: cinder.GetPodAffinity(ComponentName),
NodeSelector: instance.Spec.NodeSelector,
Volumes: volumes,
},
},
},
}

// 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.
statefulset.Spec.Template.Spec.Affinity = affinity.DistributePods(
common.AppSelector,
[]string{
cinder.ServiceName,
},
corev1.LabelHostname,
)
if instance.Spec.NodeSelector != nil && len(instance.Spec.NodeSelector) > 0 {
statefulset.Spec.Template.Spec.NodeSelector = instance.Spec.NodeSelector
}

return statefulset
}
4 changes: 2 additions & 2 deletions pkg/cindervolume/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ limitations under the License.
package cindervolume

const (
// Component -
Component = "cinder-volume"
// ComponentName -
ComponentName = "cinder-volume"
// Backend -
Backend = "backend"
)
18 changes: 2 additions & 16 deletions pkg/cindervolume/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
cinderv1 "github.com/openstack-k8s-operators/cinder-operator/api/v1beta1"
cinder "github.com/openstack-k8s-operators/cinder-operator/pkg/cinder"
common "github.com/openstack-k8s-operators/lib-common/modules/common"
"github.com/openstack-k8s-operators/lib-common/modules/common/affinity"
"github.com/openstack-k8s-operators/lib-common/modules/common/env"

appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -137,7 +136,7 @@ func StatefulSet(
HostPID: true,
Containers: []corev1.Container{
{
Name: cinder.ServiceName + "-volume",
Name: ComponentName,
Command: []string{
"/bin/bash",
},
Expand All @@ -164,26 +163,13 @@ func StatefulSet(
VolumeMounts: volumeMounts,
},
},
Affinity: cinder.GetPodAffinity(ComponentName),
NodeSelector: instance.Spec.NodeSelector,
Volumes: volumes,
},
},
},
}

// 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.
statefulset.Spec.Template.Spec.Affinity = affinity.DistributePods(
common.AppSelector,
[]string{
cinder.ServiceName,
},
corev1.LabelHostname,
)
if instance.Spec.NodeSelector != nil && len(instance.Spec.NodeSelector) > 0 {
statefulset.Spec.Template.Spec.NodeSelector = instance.Spec.NodeSelector
}

return statefulset
}
4 changes: 2 additions & 2 deletions test/kuttl/common/assert_sample_deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ spec:
- podAffinityTerm:
labelSelector:
matchExpressions:
- key: service
- key: component
operator: In
values:
- cinder
- cinder-api
topologyKey: kubernetes.io/hostname
weight: 1
containers:
Expand Down

0 comments on commit d169dc3

Please sign in to comment.