Skip to content

Commit c6245a1

Browse files
committed
feat: add loglevel
Signed-off-by: Armando Ruocco <[email protected]>
1 parent 6a7589a commit c6245a1

File tree

5 files changed

+114
-2
lines changed

5 files changed

+114
-2
lines changed

api/v1/objectstore_types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,15 @@ type InstanceSidecarConfiguration struct {
4141
// AdditionalContainerArgs is an optional list of command-line arguments
4242
// to be passed to the sidecar container when it starts.
4343
// The provided arguments are appended to the container’s default arguments.
44+
// +kubebuilder:validation:XValidation:rule="self == null || !self.exists(a, a.startsWith('--log-level'))",reason="FieldValueForbidden",message="do not set --log-level in additionalContainerArgs; use spec.instanceSidecarConfiguration.logLevel"
4445
// +optional
4546
AdditionalContainerArgs []string `json:"additionalContainerArgs,omitempty"`
47+
48+
// The instances' log level, one of the following values: error, warning, info (default), debug, trace
49+
// +kubebuilder:default:=info
50+
// +kubebuilder:validation:Enum:=error;warning;info;debug;trace
51+
// +optional
52+
LogLevel string `json:"logLevel,omitempty"`
4653
}
4754

4855
// ObjectStoreSpec defines the desired state of ObjectStore.

config/crd/bases/barmancloud.cnpg.io_objectstores.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,11 @@ spec:
399399
items:
400400
type: string
401401
type: array
402+
x-kubernetes-validations:
403+
- message: do not set --log-level in additionalContainerArgs;
404+
use spec.instanceSidecarConfiguration.logLevel
405+
reason: FieldValueForbidden
406+
rule: self == null || !self.exists(a, a.startsWith('--log-level'))
402407
env:
403408
description: The environment to be explicitly passed to the sidecar
404409
items:
@@ -519,6 +524,17 @@ spec:
519524
- name
520525
type: object
521526
type: array
527+
logLevel:
528+
default: info
529+
description: 'The instances'' log level, one of the following
530+
values: error, warning, info (default), debug, trace'
531+
enum:
532+
- error
533+
- warning
534+
- info
535+
- debug
536+
- trace
537+
type: string
522538
resources:
523539
description: Resources define cpu/memory requests and limits for
524540
the sidecar that runs in the instance pods.

internal/cnpgi/operator/lifecycle.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,19 @@ func (impl LifecycleImplementation) collectAdditionalInstanceArgs(
236236
ctx context.Context,
237237
pluginConfiguration *config.PluginConfiguration,
238238
) ([]string, error) {
239+
collectTypedAdditionalArgs := func(store *barmancloudv1.ObjectStore) []string {
240+
if store == nil {
241+
return nil
242+
}
243+
244+
var args []string
245+
if len(store.Spec.InstanceSidecarConfiguration.LogLevel) > 0 {
246+
args = append(args, fmt.Sprintf("--log-level=%s", store.Spec.InstanceSidecarConfiguration.LogLevel))
247+
}
248+
249+
return args
250+
}
251+
239252
// Prefer the cluster object store (backup/archive). If not set, fallback to the recovery object store.
240253
// If neither is configured, no additional args are provided.
241254
if len(pluginConfiguration.BarmanObjectName) > 0 {
@@ -244,7 +257,11 @@ func (impl LifecycleImplementation) collectAdditionalInstanceArgs(
244257
return nil, fmt.Errorf("while getting barman object store %s: %w",
245258
pluginConfiguration.GetBarmanObjectKey().String(), err)
246259
}
247-
return barmanObjectStore.Spec.InstanceSidecarConfiguration.AdditionalContainerArgs, nil
260+
args := append(
261+
barmanObjectStore.Spec.InstanceSidecarConfiguration.AdditionalContainerArgs,
262+
collectTypedAdditionalArgs(&barmanObjectStore)...,
263+
)
264+
return args, nil
248265
}
249266

250267
if len(pluginConfiguration.RecoveryBarmanObjectName) > 0 {
@@ -253,7 +270,11 @@ func (impl LifecycleImplementation) collectAdditionalInstanceArgs(
253270
return nil, fmt.Errorf("while getting recovery barman object store %s: %w",
254271
pluginConfiguration.GetRecoveryBarmanObjectKey().String(), err)
255272
}
256-
return barmanObjectStore.Spec.InstanceSidecarConfiguration.AdditionalContainerArgs, nil
273+
args := append(
274+
barmanObjectStore.Spec.InstanceSidecarConfiguration.AdditionalContainerArgs,
275+
collectTypedAdditionalArgs(&barmanObjectStore)...,
276+
)
277+
return args, nil
257278
}
258279

259280
return nil, nil

internal/cnpgi/operator/lifecycle_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,60 @@ var _ = Describe("LifecycleImplementation", func() {
313313
Expect(err.Error()).To(ContainSubstring(ns + "/" + pc.RecoveryBarmanObjectName))
314314
Expect(args).To(BeNil())
315315
})
316+
317+
It("includes --log-level from primary object store when set", func(ctx SpecContext) {
318+
ns := "test-ns"
319+
cluster := &cnpgv1.Cluster{ObjectMeta: metav1.ObjectMeta{Name: "c", Namespace: ns}}
320+
pc := &config.PluginConfiguration{
321+
Cluster: cluster,
322+
BarmanObjectName: "primary-store",
323+
}
324+
store := &barmancloudv1.ObjectStore{
325+
TypeMeta: metav1.TypeMeta{Kind: "ObjectStore", APIVersion: barmancloudv1.GroupVersion.String()},
326+
ObjectMeta: metav1.ObjectMeta{Name: pc.BarmanObjectName, Namespace: ns},
327+
Spec: barmancloudv1.ObjectStoreSpec{
328+
InstanceSidecarConfiguration: barmancloudv1.InstanceSidecarConfiguration{
329+
AdditionalContainerArgs: []string{"--alpha"},
330+
LogLevel: "debug",
331+
},
332+
},
333+
}
334+
s := runtime.NewScheme()
335+
_ = barmancloudv1.AddToScheme(s)
336+
cli := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(store).Build()
337+
338+
impl := LifecycleImplementation{Client: cli}
339+
args, err := impl.collectAdditionalInstanceArgs(ctx, pc)
340+
Expect(err).NotTo(HaveOccurred())
341+
Expect(args).To(Equal([]string{"--alpha", "--log-level=debug"}))
342+
})
343+
344+
It("includes --log-level from recovery object store when primary not set", func(ctx SpecContext) {
345+
ns := "test-ns"
346+
cluster := &cnpgv1.Cluster{ObjectMeta: metav1.ObjectMeta{Name: "c", Namespace: ns}}
347+
pc := &config.PluginConfiguration{
348+
Cluster: cluster,
349+
BarmanObjectName: "",
350+
RecoveryBarmanObjectName: "reco-store",
351+
}
352+
store := &barmancloudv1.ObjectStore{
353+
TypeMeta: metav1.TypeMeta{Kind: "ObjectStore", APIVersion: barmancloudv1.GroupVersion.String()},
354+
ObjectMeta: metav1.ObjectMeta{Name: pc.RecoveryBarmanObjectName, Namespace: ns},
355+
Spec: barmancloudv1.ObjectStoreSpec{
356+
InstanceSidecarConfiguration: barmancloudv1.InstanceSidecarConfiguration{
357+
LogLevel: "info",
358+
},
359+
},
360+
}
361+
s := runtime.NewScheme()
362+
_ = barmancloudv1.AddToScheme(s)
363+
cli := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(store).Build()
364+
365+
impl := LifecycleImplementation{Client: cli}
366+
args, err := impl.collectAdditionalInstanceArgs(ctx, pc)
367+
Expect(err).NotTo(HaveOccurred())
368+
Expect(args).To(Equal([]string{"--log-level=info"}))
369+
})
316370
})
317371
})
318372

manifest.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,10 @@ spec:
398398
items:
399399
type: string
400400
type: array
401+
x-kubernetes-validations:
402+
- message: do not set --log-level in additionalContainerArgs; use spec.instanceSidecarConfiguration.logLevel
403+
reason: FieldValueForbidden
404+
rule: self == null || !self.exists(a, a.startsWith('--log-level'))
401405
env:
402406
description: The environment to be explicitly passed to the sidecar
403407
items:
@@ -518,6 +522,16 @@ spec:
518522
- name
519523
type: object
520524
type: array
525+
logLevel:
526+
default: info
527+
description: "The instances' log level, one of the following values: error, warning, info (default), debug, trace"
528+
enum:
529+
- error
530+
- warning
531+
- info
532+
- debug
533+
- trace
534+
type: string
521535
resources:
522536
description: Resources define cpu/memory requests and limits for
523537
the sidecar that runs in the instance pods.

0 commit comments

Comments
 (0)