Skip to content

Commit d3f1892

Browse files
authored
Set extraArgs in the end of args processing (#250)
Set extraArgs in the end of args processing to let them overwrite default values
1 parent ec7f117 commit d3f1892

File tree

2 files changed

+48
-34
lines changed

2 files changed

+48
-34
lines changed

internal/controller/factory/statefulset.go

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"slices"
2424
"strconv"
2525

26-
"github.com/aenix-io/etcd-operator/internal/log"
2726
appsv1 "k8s.io/api/apps/v1"
2827
corev1 "k8s.io/api/core/v1"
2928
"k8s.io/apimachinery/pkg/api/resource"
@@ -34,6 +33,7 @@ import (
3433

3534
etcdaenixiov1alpha1 "github.com/aenix-io/etcd-operator/api/v1alpha1"
3635
"github.com/aenix-io/etcd-operator/internal/k8sutils"
36+
"github.com/aenix-io/etcd-operator/internal/log"
3737
)
3838

3939
const (
@@ -256,36 +256,6 @@ func generateEtcdCommand() []string {
256256
func generateEtcdArgs(cluster *etcdaenixiov1alpha1.EtcdCluster) []string {
257257
args := []string{}
258258

259-
if value, ok := cluster.Spec.Options["quota-backend-bytes"]; !ok || value == "" {
260-
var size resource.Quantity
261-
if cluster.Spec.Storage.EmptyDir != nil {
262-
if cluster.Spec.Storage.EmptyDir.SizeLimit != nil {
263-
size = *cluster.Spec.Storage.EmptyDir.SizeLimit
264-
}
265-
} else {
266-
size = *cluster.Spec.Storage.VolumeClaimTemplate.Spec.Resources.Requests.Storage()
267-
}
268-
quota := float64(size.Value()) * defaultBackendQuotaBytesFraction
269-
quota = math.Floor(quota)
270-
if quota > 0 {
271-
if cluster.Spec.Options == nil {
272-
cluster.Spec.Options = make(map[string]string, 1)
273-
}
274-
cluster.Spec.Options["quota-backend-bytes"] = strconv.FormatInt(int64(quota), 10)
275-
}
276-
}
277-
278-
for name, value := range cluster.Spec.Options {
279-
flag := "--" + name
280-
if len(value) == 0 {
281-
args = append(args, flag)
282-
283-
continue
284-
}
285-
286-
args = append(args, fmt.Sprintf("%s=%s", flag, value))
287-
}
288-
289259
peerTlsSettings := []string{"--peer-auto-tls"}
290260

291261
if cluster.Spec.Security != nil && cluster.Spec.Security.TLS.PeerSecret != "" {
@@ -335,7 +305,41 @@ func generateEtcdArgs(cluster *etcdaenixiov1alpha1.EtcdCluster) []string {
335305
args = append(args, clientTlsSettings...)
336306
args = append(args, autoCompactionSettings...)
337307

338-
slices.Sort(args)
308+
extraArgs := []string{}
309+
310+
if value, ok := cluster.Spec.Options["quota-backend-bytes"]; !ok || value == "" {
311+
var size resource.Quantity
312+
if cluster.Spec.Storage.EmptyDir != nil {
313+
if cluster.Spec.Storage.EmptyDir.SizeLimit != nil {
314+
size = *cluster.Spec.Storage.EmptyDir.SizeLimit
315+
}
316+
} else {
317+
size = *cluster.Spec.Storage.VolumeClaimTemplate.Spec.Resources.Requests.Storage()
318+
}
319+
quota := float64(size.Value()) * defaultBackendQuotaBytesFraction
320+
quota = math.Floor(quota)
321+
if quota > 0 {
322+
if cluster.Spec.Options == nil {
323+
cluster.Spec.Options = make(map[string]string, 1)
324+
}
325+
cluster.Spec.Options["quota-backend-bytes"] = strconv.FormatInt(int64(quota), 10)
326+
}
327+
}
328+
329+
for name, value := range cluster.Spec.Options {
330+
flag := "--" + name
331+
if len(value) == 0 {
332+
extraArgs = append(extraArgs, flag)
333+
334+
continue
335+
}
336+
337+
extraArgs = append(extraArgs, fmt.Sprintf("%s=%s", flag, value))
338+
}
339+
340+
// Sort the extra args to ensure a deterministic order
341+
slices.Sort(extraArgs)
342+
args = append(args, extraArgs...)
339343

340344
return args
341345
}

internal/controller/factory/statefulset_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ var _ = Describe("CreateOrUpdateStatefulSet handler", func() {
6666
},
6767
Spec: etcdaenixiov1alpha1.EtcdClusterSpec{
6868
Replicas: ptr.To(int32(3)),
69+
Options: map[string]string{
70+
"foo": "bar",
71+
"key1": "value1",
72+
"key2": "value2",
73+
},
6974
},
7075
}
7176
Expect(k8sClient.Create(ctx, &etcdcluster)).Should(Succeed())
@@ -181,9 +186,14 @@ var _ = Describe("CreateOrUpdateStatefulSet handler", func() {
181186
By("Checking the extraArgs", func() {
182187
Expect(statefulSet.Spec.Template.Spec.Containers[0].Args).To(Equal(generateEtcdArgs(&etcdcluster)))
183188
By("Checking args are sorted", func() {
184-
argsClone := slices.Clone(statefulSet.Spec.Template.Spec.Containers[0].Args)
189+
// Check that only the extra args are sorted, which means we need to check elements starting from n,
190+
// where n is the length of the default args. So we subtract the length of the extra args from the all args.
191+
// For example: if we have 3 extra args and 10 total args, we need to check elements starting from 10-3 = 7,
192+
// because the first 7 elements are default args and the elements args[7], args[8], and args[9] are extra args.
193+
n := len(statefulSet.Spec.Template.Spec.Containers[0].Args) - len(etcdcluster.Spec.Options)
194+
argsClone := slices.Clone(statefulSet.Spec.Template.Spec.Containers[0].Args[n:])
185195
slices.Sort(argsClone)
186-
Expect(statefulSet.Spec.Template.Spec.Containers[0].Args).To(Equal(argsClone))
196+
Expect(statefulSet.Spec.Template.Spec.Containers[0].Args[n:]).To(Equal(argsClone))
187197
})
188198
})
189199

0 commit comments

Comments
 (0)