Skip to content

Commit 0501e18

Browse files
armrugbartolini
andauthored
feat: introduce logLevel setting to control verbosity (#536)
This commit adds a new `logLevel` field to the plugin configuration, allowing users to select the desired log verbosity for the instances (e.g. error, warning, info, debug, trace). Closes #514 Signed-off-by: Armando Ruocco <[email protected]> Signed-off-by: Gabriele Bartolini <[email protected]> Co-authored-by: Gabriele Bartolini <[email protected]>
1 parent 77aa6e0 commit 0501e18

File tree

8 files changed

+121
-4
lines changed

8 files changed

+121
-4
lines changed

.wordlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ DigitalOcean
1313
Docusaurus
1414
EDB
1515
EKS
16+
Enum
1617
EnvVar
1718
GCP
1819
GKE

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.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 log level for PostgreSQL instances. Valid values are: `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.exists(a, a.startsWith(''--log-level''))'
402407
env:
403408
description: The environment to be explicitly passed to the sidecar
404409
items:
@@ -557,6 +562,17 @@ spec:
557562
- name
558563
type: object
559564
type: array
565+
logLevel:
566+
default: info
567+
description: 'The log level for PostgreSQL instances. Valid values
568+
are: `error`, `warning`, `info` (default), `debug`, `trace`'
569+
enum:
570+
- error
571+
- warning
572+
- info
573+
- debug
574+
- trace
575+
type: string
560576
resources:
561577
description: Resources define cpu/memory requests and limits for
562578
the sidecar that runs in the instance pods.

hack/examples/minio-store.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ metadata:
55
spec:
66
retentionPolicy: "1m"
77
instanceSidecarConfiguration:
8+
logLevel: "debug"
89
retentionPolicyIntervalSeconds: 1800
910
resources:
1011
requests:
@@ -13,8 +14,6 @@ spec:
1314
limits:
1415
memory: "512Mi"
1516
cpu: "500m"
16-
additionalContainerArgs:
17-
- --log-level=debug
1817
configuration:
1918
endpointCA:
2019
name: minio-server-tls

internal/cnpgi/operator/lifecycle.go

Lines changed: 25 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,12 @@ 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 := barmanObjectStore.Spec.InstanceSidecarConfiguration.AdditionalContainerArgs
261+
args = append(
262+
args,
263+
collectTypedAdditionalArgs(&barmanObjectStore)...,
264+
)
265+
return args, nil
248266
}
249267

250268
if len(pluginConfiguration.RecoveryBarmanObjectName) > 0 {
@@ -253,7 +271,12 @@ func (impl LifecycleImplementation) collectAdditionalInstanceArgs(
253271
return nil, fmt.Errorf("while getting recovery barman object store %s: %w",
254272
pluginConfiguration.GetRecoveryBarmanObjectKey().String(), err)
255273
}
256-
return barmanObjectStore.Spec.InstanceSidecarConfiguration.AdditionalContainerArgs, nil
274+
args := barmanObjectStore.Spec.InstanceSidecarConfiguration.AdditionalContainerArgs
275+
args = append(
276+
args,
277+
collectTypedAdditionalArgs(&barmanObjectStore)...,
278+
)
279+
return args, nil
257280
}
258281

259282
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,11 @@ spec:
398398
items:
399399
type: string
400400
type: array
401+
x-kubernetes-validations:
402+
- message: do not set --log-level in additionalContainerArgs;
403+
use spec.instanceSidecarConfiguration.logLevel
404+
reason: FieldValueForbidden
405+
rule: '!self.exists(a, a.startsWith(''--log-level''))'
401406
env:
402407
description: The environment to be explicitly passed to the sidecar
403408
items:
@@ -556,6 +561,17 @@ spec:
556561
- name
557562
type: object
558563
type: array
564+
logLevel:
565+
default: info
566+
description: 'The log level for PostgreSQL instances. Valid values
567+
are: `error`, `warning`, `info` (default), `debug`, `trace`'
568+
enum:
569+
- error
570+
- warning
571+
- info
572+
- debug
573+
- trace
574+
type: string
559575
resources:
560576
description: Resources define cpu/memory requests and limits for
561577
the sidecar that runs in the instance pods.

web/docs/plugin-barman-cloud.v1.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ _Appears in:_
3030
| `retentionPolicyIntervalSeconds` _integer_ | The retentionCheckInterval defines the frequency at which the<br />system checks and enforces retention policies. | | 1800 | |
3131
| `resources` _[ResourceRequirements](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.32/#resourcerequirements-v1-core)_ | Resources define cpu/memory requests and limits for the sidecar that runs in the instance pods. | | | |
3232
| `additionalContainerArgs` _string array_ | AdditionalContainerArgs is an optional list of command-line arguments<br />to be passed to the sidecar container when it starts.<br />The provided arguments are appended to the container’s default arguments. | | | |
33+
| `logLevel` _string_ | The log level for PostgreSQL instances. Valid values are: `error`, `warning`, `info` (default), `debug`, `trace` | | info | Enum: [error warning info debug trace] <br /> |
3334

3435

3536
#### ObjectStore

0 commit comments

Comments
 (0)