Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

operator: Add default PodAntiAffinity to Ingester #9329

Merged
merged 1 commit into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions operator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## Main
- [9329](https://github.com/grafana/loki/pull/9329) **JoaoBraveCoding**: Add default PodAntiAffinity to Ingester
- [9262](https://github.com/grafana/loki/pull/9262) **btaani**: Add PodDisruptionBudget to the Ruler
- [9260](https://github.com/grafana/loki/pull/9260) **JoaoBraveCoding**: Add PodDisruptionBudgets to the ingestion path
- [9188](https://github.com/grafana/loki/pull/9188) **aminesnow**: Add PodDisruptionBudgets to the query path
Expand Down
25 changes: 15 additions & 10 deletions operator/internal/manifests/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -909,19 +909,19 @@ func TestBuildAll_WithFeatureGates_LokiStackAlerts(t *testing.T) {

func TestBuildAll_WithFeatureGates_DefaultNodeAffinity(t *testing.T) {
tt := []struct {
desc string
nodeAffinity bool
wantAffinity *corev1.Affinity
desc string
nodeAffinity bool
wantNodeAffinity *corev1.NodeAffinity
}{
{
desc: "disabled",
nodeAffinity: false,
wantAffinity: nil,
desc: "disabled",
nodeAffinity: false,
wantNodeAffinity: nil,
},
{
desc: "enabled",
nodeAffinity: true,
wantAffinity: defaultAffinity(true),
desc: "enabled",
nodeAffinity: true,
wantNodeAffinity: defaultNodeAffinity(true),
},
}

Expand Down Expand Up @@ -956,7 +956,12 @@ func TestBuildAll_WithFeatureGates_DefaultNodeAffinity(t *testing.T) {
continue
}

require.Equal(t, tc.wantAffinity, gotAffinity,
var gotNodeAffinity *corev1.NodeAffinity
if gotAffinity != nil {
gotNodeAffinity = gotAffinity.NodeAffinity
}

require.Equal(t, tc.wantNodeAffinity, gotNodeAffinity,
"kind", raw.GetObjectKind().GroupVersionKind(),
"name", raw.GetName())
}
Expand Down
6 changes: 3 additions & 3 deletions operator/internal/manifests/compactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ func BuildCompactor(opts Options) ([]client.Object, error) {

// NewCompactorStatefulSet creates a statefulset object for a compactor.
func NewCompactorStatefulSet(opts Options) *appsv1.StatefulSet {
l := ComponentLabels(LabelCompactorComponent, opts.Name)
a := commonAnnotations(opts.ConfigSHA1, opts.CertRotationRequiredAt)
podSpec := corev1.PodSpec{
Affinity: defaultAffinity(opts.Gates.DefaultNodeAffinity),
Affinity: configureAffinity(l, opts.Gates.DefaultNodeAffinity),
Volumes: []corev1.Volume{
{
Name: configVolumeName,
Expand Down Expand Up @@ -129,8 +131,6 @@ func NewCompactorStatefulSet(opts Options) *appsv1.StatefulSet {
podSpec.NodeSelector = opts.Stack.Template.Compactor.NodeSelector
}

l := ComponentLabels(LabelCompactorComponent, opts.Name)
a := commonAnnotations(opts.ConfigSHA1, opts.CertRotationRequiredAt)
return &appsv1.StatefulSet{
TypeMeta: metav1.TypeMeta{
Kind: "StatefulSet",
Expand Down
7 changes: 3 additions & 4 deletions operator/internal/manifests/distributor.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ func BuildDistributor(opts Options) ([]client.Object, error) {

// NewDistributorDeployment creates a deployment object for a distributor
func NewDistributorDeployment(opts Options) *appsv1.Deployment {
l := ComponentLabels(LabelDistributorComponent, opts.Name)
a := commonAnnotations(opts.ConfigSHA1, opts.CertRotationRequiredAt)
podSpec := corev1.PodSpec{
Affinity: defaultAffinity(opts.Gates.DefaultNodeAffinity),
Affinity: configureAffinity(l, opts.Gates.DefaultNodeAffinity),
Volumes: []corev1.Volume{
{
Name: configVolumeName,
Expand Down Expand Up @@ -125,9 +127,6 @@ func NewDistributorDeployment(opts Options) *appsv1.Deployment {
podSpec.NodeSelector = opts.Stack.Template.Distributor.NodeSelector
}

l := ComponentLabels(LabelDistributorComponent, opts.Name)
a := commonAnnotations(opts.ConfigSHA1, opts.CertRotationRequiredAt)

return &appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
Kind: "Deployment",
Expand Down
7 changes: 3 additions & 4 deletions operator/internal/manifests/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,11 @@ func BuildGateway(opts Options) ([]client.Object, error) {

// NewGatewayDeployment creates a deployment object for a lokiStack-gateway
func NewGatewayDeployment(opts Options, sha1C string) *appsv1.Deployment {
l := ComponentLabels(LabelGatewayComponent, opts.Name)
a := commonAnnotations(sha1C, opts.CertRotationRequiredAt)
podSpec := corev1.PodSpec{
ServiceAccountName: GatewayName(opts.Name),
Affinity: defaultAffinity(opts.Gates.DefaultNodeAffinity),
Affinity: configureAffinity(l, opts.Gates.DefaultNodeAffinity),
Volumes: []corev1.Volume{
{
Name: "rbac",
Expand Down Expand Up @@ -205,9 +207,6 @@ func NewGatewayDeployment(opts Options, sha1C string) *appsv1.Deployment {
podSpec.NodeSelector = opts.Stack.Template.Gateway.NodeSelector
}

l := ComponentLabels(LabelGatewayComponent, opts.Name)
a := commonAnnotations(sha1C, opts.CertRotationRequiredAt)

return &appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
Kind: "Deployment",
Expand Down
7 changes: 3 additions & 4 deletions operator/internal/manifests/indexgateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ func BuildIndexGateway(opts Options) ([]client.Object, error) {

// NewIndexGatewayStatefulSet creates a statefulset object for an index-gateway
func NewIndexGatewayStatefulSet(opts Options) *appsv1.StatefulSet {
l := ComponentLabels(LabelIndexGatewayComponent, opts.Name)
a := commonAnnotations(opts.ConfigSHA1, opts.CertRotationRequiredAt)
podSpec := corev1.PodSpec{
Affinity: defaultAffinity(opts.Gates.DefaultNodeAffinity),
Affinity: configureAffinity(l, opts.Gates.DefaultNodeAffinity),
Volumes: []corev1.Volume{
{
Name: configVolumeName,
Expand Down Expand Up @@ -131,9 +133,6 @@ func NewIndexGatewayStatefulSet(opts Options) *appsv1.StatefulSet {
podSpec.NodeSelector = opts.Stack.Template.IndexGateway.NodeSelector
}

l := ComponentLabels(LabelIndexGatewayComponent, opts.Name)
a := commonAnnotations(opts.ConfigSHA1, opts.CertRotationRequiredAt)

return &appsv1.StatefulSet{
TypeMeta: metav1.TypeMeta{
Kind: "StatefulSet",
Expand Down
6 changes: 3 additions & 3 deletions operator/internal/manifests/ingester.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ func BuildIngester(opts Options) ([]client.Object, error) {

// NewIngesterStatefulSet creates a deployment object for an ingester
func NewIngesterStatefulSet(opts Options) *appsv1.StatefulSet {
l := ComponentLabels(LabelIngesterComponent, opts.Name)
a := commonAnnotations(opts.ConfigSHA1, opts.CertRotationRequiredAt)
podSpec := corev1.PodSpec{
Affinity: defaultAffinity(opts.Gates.DefaultNodeAffinity),
Affinity: configureAffinity(l, opts.Gates.DefaultNodeAffinity),
Volumes: []corev1.Volume{
{
Name: configVolumeName,
Expand Down Expand Up @@ -141,8 +143,6 @@ func NewIngesterStatefulSet(opts Options) *appsv1.StatefulSet {
podSpec.NodeSelector = opts.Stack.Template.Ingester.NodeSelector
}

l := ComponentLabels(LabelIngesterComponent, opts.Name)
a := commonAnnotations(opts.ConfigSHA1, opts.CertRotationRequiredAt)
return &appsv1.StatefulSet{
TypeMeta: metav1.TypeMeta{
Kind: "StatefulSet",
Expand Down
38 changes: 38 additions & 0 deletions operator/internal/manifests/ingester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import (
"github.com/stretchr/testify/require"
policyv1 "k8s.io/api/policy/v1"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

v1 "github.com/grafana/loki/operator/apis/config/v1"

lokiv1 "github.com/grafana/loki/operator/apis/loki/v1"
"github.com/grafana/loki/operator/internal/manifests"
"github.com/grafana/loki/operator/internal/manifests/internal"
Expand Down Expand Up @@ -133,3 +137,37 @@ func TestBuildIngester_PodDisruptionBudget(t *testing.T) {
})
}
}

func TestIngesterPodAntiAffinity(t *testing.T) {
sts := manifests.NewIngesterStatefulSet(manifests.Options{
Name: "abcd",
Namespace: "efgh",
Stack: lokiv1.LokiStackSpec{
StorageClassName: "standard",
Template: &lokiv1.LokiTemplateSpec{
Ingester: &lokiv1.LokiComponentSpec{
Replicas: 1,
},
},
},
})
expectedPodAntiAffinity := &corev1.PodAntiAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{
{
Weight: 100,
PodAffinityTerm: corev1.PodAffinityTerm{
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app.kubernetes.io/component": manifests.LabelIngesterComponent,
"app.kubernetes.io/instance": "abcd",

},
},
TopologyKey: "kubernetes.io/hostname",
},
},
},
}
require.NotNil(t, sts.Spec.Template.Spec.Affinity)
require.Equal(t, expectedPodAntiAffinity, sts.Spec.Template.Spec.Affinity.PodAntiAffinity)
}
7 changes: 3 additions & 4 deletions operator/internal/manifests/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ func BuildQuerier(opts Options) ([]client.Object, error) {

// NewQuerierDeployment creates a deployment object for a querier
func NewQuerierDeployment(opts Options) *appsv1.Deployment {
l := ComponentLabels(LabelQuerierComponent, opts.Name)
a := commonAnnotations(opts.ConfigSHA1, opts.CertRotationRequiredAt)
podSpec := corev1.PodSpec{
Affinity: defaultAffinity(opts.Gates.DefaultNodeAffinity),
Affinity: configureAffinity(l, opts.Gates.DefaultNodeAffinity),
Volumes: []corev1.Volume{
{
Name: configVolumeName,
Expand Down Expand Up @@ -131,9 +133,6 @@ func NewQuerierDeployment(opts Options) *appsv1.Deployment {
podSpec.NodeSelector = opts.Stack.Template.Querier.NodeSelector
}

l := ComponentLabels(LabelQuerierComponent, opts.Name)
a := commonAnnotations(opts.ConfigSHA1, opts.CertRotationRequiredAt)

return &appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
Kind: "Deployment",
Expand Down
7 changes: 3 additions & 4 deletions operator/internal/manifests/query-frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ func BuildQueryFrontend(opts Options) ([]client.Object, error) {

// NewQueryFrontendDeployment creates a deployment object for a query-frontend
func NewQueryFrontendDeployment(opts Options) *appsv1.Deployment {
l := ComponentLabels(LabelQueryFrontendComponent, opts.Name)
a := commonAnnotations(opts.ConfigSHA1, opts.CertRotationRequiredAt)
podSpec := corev1.PodSpec{
Affinity: defaultAffinity(opts.Gates.DefaultNodeAffinity),
Affinity: configureAffinity(l, opts.Gates.DefaultNodeAffinity),
Volumes: []corev1.Volume{
{
Name: configVolumeName,
Expand Down Expand Up @@ -137,9 +139,6 @@ func NewQueryFrontendDeployment(opts Options) *appsv1.Deployment {
podSpec.NodeSelector = opts.Stack.Template.QueryFrontend.NodeSelector
}

l := ComponentLabels(LabelQueryFrontendComponent, opts.Name)
a := commonAnnotations(opts.ConfigSHA1, opts.CertRotationRequiredAt)

return &appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
Kind: "Deployment",
Expand Down
3 changes: 2 additions & 1 deletion operator/internal/manifests/query-frontend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package manifests
import (
"testing"

lokiv1 "github.com/grafana/loki/operator/apis/loki/v1"
"github.com/stretchr/testify/require"
policyv1 "k8s.io/api/policy/v1"

lokiv1 "github.com/grafana/loki/operator/apis/loki/v1"
)

func TestNewQueryFrontendDeployment_SelectorMatchesLabels(t *testing.T) {
Expand Down
7 changes: 3 additions & 4 deletions operator/internal/manifests/ruler.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ func NewRulerStatefulSet(opts Options) *appsv1.StatefulSet {
})
}

l := ComponentLabels(LabelRulerComponent, opts.Name)
a := commonAnnotations(opts.ConfigSHA1, opts.CertRotationRequiredAt)
podSpec := corev1.PodSpec{
Affinity: defaultAffinity(opts.Gates.DefaultNodeAffinity),
Affinity: configureAffinity(l, opts.Gates.DefaultNodeAffinity),
Volumes: []corev1.Volume{
{
Name: configVolumeName,
Expand Down Expand Up @@ -173,9 +175,6 @@ func NewRulerStatefulSet(opts Options) *appsv1.StatefulSet {
podSpec.NodeSelector = opts.Stack.Template.Ruler.NodeSelector
}

l := ComponentLabels(LabelRulerComponent, opts.Name)
a := commonAnnotations(opts.ConfigSHA1, opts.CertRotationRequiredAt)

return &appsv1.StatefulSet{
TypeMeta: metav1.TypeMeta{
Kind: "StatefulSet",
Expand Down
7 changes: 4 additions & 3 deletions operator/internal/manifests/ruler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"math/rand"
"testing"

lokiv1 "github.com/grafana/loki/operator/apis/loki/v1"
"github.com/grafana/loki/operator/internal/manifests"
"github.com/grafana/loki/operator/internal/manifests/openshift"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"

lokiv1 "github.com/grafana/loki/operator/apis/loki/v1"
"github.com/grafana/loki/operator/internal/manifests"
"github.com/grafana/loki/operator/internal/manifests/openshift"
)

func TestNewRulerStatefulSet_HasTemplateConfigHashAnnotation(t *testing.T) {
Expand Down
Loading