Skip to content

Commit

Permalink
Add default affinity rules for Redis (#508)
Browse files Browse the repository at this point in the history
* Add default affinity rules

Signed-off-by: Fahim Abrar <fahimabrar@appscode.com>

* Add comments

Signed-off-by: Tamal Saha <tamal@appscode.com>

* Add shard template label to affinity for clustered redis

Signed-off-by: Tamal Saha <tamal@appscode.com>

Co-authored-by: Tamal Saha <tamal@appscode.com>
  • Loading branch information
faem and tamalsaha committed Mar 9, 2020
1 parent ab738ac commit e6f72c3
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion apis/kubedb/v1alpha1/redis_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ import (

"github.com/appscode/go/types"
apps "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
core_util "kmodules.xyz/client-go/core/v1"
meta_util "kmodules.xyz/client-go/meta"
appcat "kmodules.xyz/custom-resources/apis/appcatalog/v1alpha1"
mona "kmodules.xyz/monitoring-agent-api/api/v1"
ofst "kmodules.xyz/offshoot-api/api/v1"
)

const RedisShardAffinityTemplateVar = "SHARD_INDEX"

func (_ Redis) CustomResourceDefinition() *apiextensions.CustomResourceDefinition {
return crds.MustCustomResourceDefinition(SchemeGroupVersion.WithResource(ResourcePluralRedis))
}
Expand Down Expand Up @@ -147,7 +153,7 @@ func (r *Redis) GetMonitoringVendor() string {
return ""
}

func (r *Redis) SetDefaults() {
func (r *Redis) SetDefaults(topology *core_util.Topology) {
if r == nil {
return
}
Expand Down Expand Up @@ -182,9 +188,61 @@ func (r *Redis) SetDefaults() {
r.Spec.PodTemplate.Spec.ServiceAccountName = r.OffshootName()
}

labels := r.OffshootSelectors()
if r.Spec.Mode == RedisModeCluster {
labels[RedisShardKey] = r.ShardNodeTemplate()
}
r.setDefaultAffinity(&r.Spec.PodTemplate, labels, topology)

r.Spec.Monitor.SetDefaults()
}

func (e *RedisSpec) GetSecrets() []string {
return nil
}

func (r *Redis) setDefaultAffinity(podTemplate *ofst.PodTemplateSpec, labels map[string]string, topology *core_util.Topology) {
if podTemplate == nil {
return
} else if podTemplate.Spec.Affinity != nil {
topology.ConvertAffinity(podTemplate.Spec.Affinity)
return
}

podTemplate.Spec.Affinity = &corev1.Affinity{
PodAntiAffinity: &corev1.PodAntiAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{
// Prefer to not schedule multiple pods on the same node
{
Weight: 100,
PodAffinityTerm: corev1.PodAffinityTerm{
Namespaces: []string{r.Namespace},
LabelSelector: &metav1.LabelSelector{
MatchLabels: labels,
},

TopologyKey: corev1.LabelHostname,
},
},
// Prefer to not schedule multiple pods on the node with same zone
{
Weight: 50,
PodAffinityTerm: corev1.PodAffinityTerm{
Namespaces: []string{r.Namespace},
LabelSelector: &metav1.LabelSelector{
MatchLabels: labels,
},
TopologyKey: topology.LabelZone,
},
},
},
},
}
}

func (r Redis) ShardNodeTemplate() string {
if r.Spec.Mode == RedisModeStandalone {
panic("shard template is not applicable to a standalone redis server")
}
return fmt.Sprintf("${%s}", RedisShardAffinityTemplateVar)
}

0 comments on commit e6f72c3

Please sign in to comment.