Skip to content

Commit 46b032c

Browse files
authored
Allow setting of PDB for per-node (#2932)
* create PDB automatically for per-node * resolved * fix ref
1 parent f551b8f commit 46b032c

File tree

5 files changed

+99
-29
lines changed

5 files changed

+99
-29
lines changed

.chloggen/resolve-easy-miss.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: bug_fix
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
5+
component: target-allocator
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: Fixes a bug that didn't automatically create a PDB for a TA with per-node strategy
9+
10+
# One or more tracking issues related to the change
11+
issues: [2900]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext:

apis/v1beta1/collector_webhook.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ func (c CollectorWebhook) Default(_ context.Context, obj runtime.Object) error {
133133
}
134134

135135
// if pdb isn't provided for target allocator and it's enabled
136-
// using a valid strategy (consistent-hashing),
136+
// using a valid strategy (consistent-hashing, per-node),
137137
// we set MaxUnavailable 1, which will work even if there is
138138
// just one replica, not blocking node drains but preventing
139139
// out-of-the-box from disruption generated by them with replicas > 1
140140
if otelcol.Spec.TargetAllocator.Enabled &&
141-
otelcol.Spec.TargetAllocator.AllocationStrategy == TargetAllocatorAllocationStrategyConsistentHashing &&
141+
otelcol.Spec.TargetAllocator.AllocationStrategy != TargetAllocatorAllocationStrategyLeastWeighted &&
142142
otelcol.Spec.TargetAllocator.PodDisruptionBudget == nil {
143143
otelcol.Spec.TargetAllocator.PodDisruptionBudget = &PodDisruptionBudgetSpec{
144144
MaxUnavailable: &intstr.IntOrString{

apis/v1beta1/collector_webhook_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,56 @@ func TestCollectorDefaultingWebhook(t *testing.T) {
360360
},
361361
},
362362
},
363+
{
364+
name: "Defined PDB for target allocator per-node",
365+
otelcol: OpenTelemetryCollector{
366+
Spec: OpenTelemetryCollectorSpec{
367+
Mode: ModeDeployment,
368+
TargetAllocator: TargetAllocatorEmbedded{
369+
Enabled: true,
370+
AllocationStrategy: TargetAllocatorAllocationStrategyPerNode,
371+
PodDisruptionBudget: &PodDisruptionBudgetSpec{
372+
MinAvailable: &intstr.IntOrString{
373+
Type: intstr.String,
374+
StrVal: "10%",
375+
},
376+
},
377+
},
378+
},
379+
},
380+
expected: OpenTelemetryCollector{
381+
ObjectMeta: metav1.ObjectMeta{
382+
Labels: map[string]string{
383+
"app.kubernetes.io/managed-by": "opentelemetry-operator",
384+
},
385+
},
386+
Spec: OpenTelemetryCollectorSpec{
387+
Mode: ModeDeployment,
388+
OpenTelemetryCommonFields: OpenTelemetryCommonFields{
389+
Replicas: &one,
390+
ManagementState: ManagementStateManaged,
391+
PodDisruptionBudget: &PodDisruptionBudgetSpec{
392+
MaxUnavailable: &intstr.IntOrString{
393+
Type: intstr.Int,
394+
IntVal: 1,
395+
},
396+
},
397+
},
398+
UpgradeStrategy: UpgradeStrategyAutomatic,
399+
TargetAllocator: TargetAllocatorEmbedded{
400+
Enabled: true,
401+
Replicas: &one,
402+
AllocationStrategy: TargetAllocatorAllocationStrategyPerNode,
403+
PodDisruptionBudget: &PodDisruptionBudgetSpec{
404+
MinAvailable: &intstr.IntOrString{
405+
Type: intstr.String,
406+
StrVal: "10%",
407+
},
408+
},
409+
},
410+
},
411+
},
412+
},
363413
{
364414
name: "Undefined PDB for target allocator and consistent-hashing strategy",
365415
otelcol: OpenTelemetryCollector{

internal/manifests/targetallocator/poddisruptionbudget.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ func PodDisruptionBudget(params manifests.Params) (*policyV1.PodDisruptionBudget
3636
// defaulter doesn't set PodDisruptionBudget if the strategy isn't valid,
3737
// if PodDisruptionBudget != nil and stategy isn't correct, users have set
3838
// it wrongly
39-
if params.TargetAllocator.Spec.AllocationStrategy != v1beta1.TargetAllocatorAllocationStrategyConsistentHashing {
39+
if params.TargetAllocator.Spec.AllocationStrategy != v1beta1.TargetAllocatorAllocationStrategyConsistentHashing &&
40+
params.TargetAllocator.Spec.AllocationStrategy != v1beta1.TargetAllocatorAllocationStrategyPerNode {
4041
params.Log.V(4).Info("current allocation strategy not compatible, skipping podDisruptionBudget creation")
4142
return nil, fmt.Errorf("target allocator pdb has been configured but the allocation strategy isn't not compatible")
4243
}

internal/manifests/targetallocator/poddisruptionbudget_test.go

+29-26
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package targetallocator
1616

1717
import (
18+
"fmt"
1819
"testing"
1920

2021
"github.com/stretchr/testify/assert"
@@ -66,35 +67,37 @@ var tests = []test{
6667

6768
func TestPDBWithValidStrategy(t *testing.T) {
6869
for _, test := range tests {
69-
t.Run(test.name, func(t *testing.T) {
70-
targetAllocator := v1alpha1.TargetAllocator{
71-
ObjectMeta: metav1.ObjectMeta{
72-
Name: "my-instance",
73-
},
74-
Spec: v1alpha1.TargetAllocatorSpec{
75-
OpenTelemetryCommonFields: v1beta1.OpenTelemetryCommonFields{
76-
PodDisruptionBudget: &v1beta1.PodDisruptionBudgetSpec{
77-
MinAvailable: test.MinAvailable,
78-
MaxUnavailable: test.MaxUnavailable,
70+
for _, strategy := range []v1beta1.TargetAllocatorAllocationStrategy{v1beta1.TargetAllocatorAllocationStrategyPerNode, v1beta1.TargetAllocatorAllocationStrategyConsistentHashing} {
71+
t.Run(fmt.Sprintf("%s-%s", strategy, test.name), func(t *testing.T) {
72+
targetAllocator := v1alpha1.TargetAllocator{
73+
ObjectMeta: metav1.ObjectMeta{
74+
Name: "my-instance",
75+
},
76+
Spec: v1alpha1.TargetAllocatorSpec{
77+
OpenTelemetryCommonFields: v1beta1.OpenTelemetryCommonFields{
78+
PodDisruptionBudget: &v1beta1.PodDisruptionBudgetSpec{
79+
MinAvailable: test.MinAvailable,
80+
MaxUnavailable: test.MaxUnavailable,
81+
},
7982
},
83+
AllocationStrategy: strategy,
8084
},
81-
AllocationStrategy: v1beta1.TargetAllocatorAllocationStrategyConsistentHashing,
82-
},
83-
}
84-
configuration := config.New()
85-
pdb, err := PodDisruptionBudget(manifests.Params{
86-
Log: logger,
87-
Config: configuration,
88-
TargetAllocator: targetAllocator,
89-
})
85+
}
86+
configuration := config.New()
87+
pdb, err := PodDisruptionBudget(manifests.Params{
88+
Log: logger,
89+
Config: configuration,
90+
TargetAllocator: targetAllocator,
91+
})
9092

91-
// verify
92-
assert.NoError(t, err)
93-
assert.Equal(t, "my-instance-targetallocator", pdb.Name)
94-
assert.Equal(t, "my-instance-targetallocator", pdb.Labels["app.kubernetes.io/name"])
95-
assert.Equal(t, test.MinAvailable, pdb.Spec.MinAvailable)
96-
assert.Equal(t, test.MaxUnavailable, pdb.Spec.MaxUnavailable)
97-
})
93+
// verify
94+
assert.NoError(t, err)
95+
assert.Equal(t, "my-instance-targetallocator", pdb.Name)
96+
assert.Equal(t, "my-instance-targetallocator", pdb.Labels["app.kubernetes.io/name"])
97+
assert.Equal(t, test.MinAvailable, pdb.Spec.MinAvailable)
98+
assert.Equal(t, test.MaxUnavailable, pdb.Spec.MaxUnavailable)
99+
})
100+
}
98101
}
99102
}
100103

0 commit comments

Comments
 (0)