Skip to content

Commit

Permalink
Allow StatefulSet PriorityClass to be configured. Resolves infinispan…
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanemerson committed Aug 11, 2023
1 parent 5f676d4 commit 3c57e1d
Show file tree
Hide file tree
Showing 8 changed files with 744 additions and 20 deletions.
10 changes: 10 additions & 0 deletions api/v1/infinispan_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,13 @@ type JmxSpec struct {
Enabled bool `json:"enabled,omitempty"`
}

type StatefulSetSpec struct {
// +optional
Affinity *corev1.Affinity `json:"affinity,omitempty"`
// +optional
PriorityClassName string `json:"PriorityClassName,omitempty"`
}

// InfinispanSpec defines the desired state of Infinispan
type InfinispanSpec struct {
// The number of nodes in the Infinispan cluster.
Expand All @@ -438,6 +445,7 @@ type InfinispanSpec struct {
// +optional
Autoscale *Autoscale `json:"autoscale,omitempty"`
// +optional
// Deprecated. Use statefulSet.affinity instead
Affinity *corev1.Affinity `json:"affinity,omitempty"`
// +optional
CloudEvents *InfinispanCloudEvents `json:"cloudEvents,omitempty"`
Expand All @@ -452,6 +460,8 @@ type InfinispanSpec struct {
ConfigListener *ConfigListenerSpec `json:"configListener,omitempty"`
// +optional
Jmx *JmxSpec `json:"jmx,omitempty"`
// +optional
StatefulSet *StatefulSetSpec `json:"statefulSet,omitempty"`
}

// InfinispanUpgradesSpec defines the Infinispan upgrade strategy
Expand Down
41 changes: 25 additions & 16 deletions api/v1/infinispan_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,33 @@ func (i *Infinispan) Default() {
}
}

if i.Spec.Affinity == nil {
// The user hasn't configured Affinity, so we utilise the default strategy of preferring pods are deployed on distinct nodes
i.Spec.Affinity = &corev1.Affinity{
PodAntiAffinity: &corev1.PodAntiAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{{
Weight: 100,
PodAffinityTerm: corev1.PodAffinityTerm{
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"infinispan_cr": i.Name,
"clusterName": i.Name,
"app": "infinispan-pod",
if i.Spec.StatefulSet == nil {
i.Spec.StatefulSet = &StatefulSetSpec{}
}

if i.Spec.StatefulSet.Affinity == nil {
if i.Spec.Affinity != nil {
i.Spec.StatefulSet.Affinity = i.Spec.Affinity
i.Spec.Affinity = nil
} else {
// The user hasn't configured Affinity, so we utilise the default strategy of preferring pods are deployed on distinct nodes
i.Spec.StatefulSet.Affinity = &corev1.Affinity{
PodAntiAffinity: &corev1.PodAntiAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{{
Weight: 100,
PodAffinityTerm: corev1.PodAffinityTerm{
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"infinispan_cr": i.Name,
"clusterName": i.Name,
"app": "infinispan-pod",
},
},
TopologyKey: "kubernetes.io/hostname",
},
TopologyKey: "kubernetes.io/hostname",
},
}},
},
}},
},
}
}
}

Expand Down
38 changes: 38 additions & 0 deletions api/v1/infinispan_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/infinispan/infinispan-operator/pkg/hash"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"

// +kubebuilder:scaffold:imports
k8serrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -614,6 +615,43 @@ var _ = Describe("Infinispan Webhooks", func() {
statusDetailCause{"FieldValueForbidden", "spec.jmx", "JMX configuration is immutable and cannot be updated after initial Infinispan creation"},
)
})

It("Should transform affinity spec", func() {
affinitySpec := &corev1.Affinity{
PodAntiAffinity: &corev1.PodAntiAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{{
Weight: 100,
PodAffinityTerm: corev1.PodAffinityTerm{
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"infinispan_cr": key.Name,
"clusterName": key.Namespace,
"app": "infinispan-pod",
},
},
TopologyKey: "check-value",
},
}},
},
}

ispn := &Infinispan{
ObjectMeta: metav1.ObjectMeta{
Name: key.Name,
Namespace: key.Namespace,
},
Spec: InfinispanSpec{
Replicas: 1,
Affinity: affinitySpec,
},
}

Expect(k8sClient.Create(ctx, ispn)).Should(Succeed())
updated := &Infinispan{}
Expect(k8sClient.Get(ctx, key, updated)).Should(Succeed())
Expect(updated.Spec.Affinity).Should(BeNil())
Expect(updated.Spec.StatefulSet.Affinity).Should(BeEquivalentTo(affinitySpec))
})
})
})

Expand Down
14 changes: 14 additions & 0 deletions api/v1/types_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -956,3 +956,17 @@ func (ispn *Infinispan) IsJmxExposed() bool {
// TODO update
return ispn.Spec.Jmx != nil && ispn.Spec.Jmx.Enabled
}

func (ispn *Infinispan) Affinity() *corev1.Affinity {
if ispn.Spec.StatefulSet != nil && ispn.Spec.StatefulSet.Affinity != nil {
return ispn.Spec.StatefulSet.Affinity
}
return ispn.Spec.Affinity
}

func (ispn *Infinispan) PriorityClassName() string {
if ispn.Spec.StatefulSet != nil {
return ispn.Spec.StatefulSet.PriorityClassName
}
return ""
}
25 changes: 25 additions & 0 deletions api/v1/zz_generated.deepcopy.go

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

Loading

0 comments on commit 3c57e1d

Please sign in to comment.