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 15, 2023
1 parent 6fc0597 commit a3562eb
Show file tree
Hide file tree
Showing 13 changed files with 1,461 additions and 22 deletions.
13 changes: 12 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ lint: golangci-lint

.PHONY: test
## Execute tests
test: manager manifests envtest
test: manager manifests generate-mocks envtest
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./api/... ./controllers/... ./pkg/... -coverprofile cover.out

.PHONY: infinispan-test
Expand Down Expand Up @@ -159,6 +159,11 @@ generate: controller-gen rice
$(RICE) embed-go -i controllers/grafana.go -i pkg/templates/templates.go
find . -type f -name 'rice-box.go' -exec sed -i "s|time.Unix(.*, 0)|time.Unix(1620137619, 0)|" {} \;

.PHONY: generate-mocks
## Generate testing mocks
generate-mocks: mockgen
$(MOCKGEN) -source=./pkg/reconcile/pipeline/infinispan/api.go -destination=./pkg/reconcile/pipeline/infinispan/api_mocks.go -package=infinispan

.PHONY: operator-build
## Build the operator image
operator-build: manager
Expand Down Expand Up @@ -199,6 +204,12 @@ export GO_JUNIT_REPORT = $(shell pwd)/bin/go-junit-report
go-junit-report:
$(call go-get-tool,$(GO_JUNIT_REPORT),github.com/jstemmer/go-junit-report@latest)

export MOCKGEN = $(shell pwd)/bin/mockgen
.PHONY: mockgen
## Download mockgen locally if necessary
mockgen:
$(call go-get-tool,$(MOCKGEN),github.com/golang/mock/[email protected])

ENVTEST = $(shell pwd)/bin/setup-envtest
.PHONY: envtest
## Download envtest-setup locally if necessary.
Expand Down
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 SchedulingSpec 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 scheduling.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
Scheduling *SchedulingSpec `json:"scheduling,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.Scheduling == nil {
i.Spec.Scheduling = &SchedulingSpec{}
}

if i.Spec.Scheduling.Affinity == nil {
if i.Spec.Affinity != nil {
i.Spec.Scheduling.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.Scheduling.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 @@ -636,6 +637,43 @@ var _ = Describe("Infinispan Webhooks", func() {
statusDetailCause{"FieldValueForbidden", "spec.security.endpointEncryption.certServiceName", ".certServiceName cannot be configured with Encryption .type=Secret"},
)
})

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.Scheduling.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.Scheduling != nil && ispn.Spec.Scheduling.Affinity != nil {
return ispn.Spec.Scheduling.Affinity
}
return ispn.Spec.Affinity
}

func (ispn *Infinispan) PriorityClassName() string {
if ispn.Spec.Scheduling != nil {
return ispn.Spec.Scheduling.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 a3562eb

Please sign in to comment.