Skip to content
Draft
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
7 changes: 6 additions & 1 deletion build/logcollector/entrypoint.sh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[shellcheck (suggestion)] reported by reviewdog 🐶

Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@

export PATH="$PATH":/opt/fluent-bit/bin

LOGROTATE_CONF_DIR=""
if [ -f /opt/percona/logcollector/logrotate/conf.d ]; then
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition uses "-f" which checks if the path is a regular file, but "/opt/percona/logcollector/logrotate/conf.d" is expected to be a directory. This should use "-d" to check if it's a directory instead. With "-f", this condition will always be false even when the directory exists.

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition uses "-f" flag which checks if the path is a regular file, but the path "/opt/percona/logcollector/logrotate/conf.d" is expected to be a directory. This should use "-d" flag instead to check if it's a directory.

Copilot uses AI. Check for mistakes.
LOGROTATE_CONF_DIR="/opt/percona/logcollector/logrotate/conf.d"
fi

if [ "$1" = 'logrotate' ]; then
if [[ $EUID != 1001 ]]; then

Check warning on line 13 in build/logcollector/entrypoint.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] build/logcollector/entrypoint.sh#L13 <ShellCheck.SC3010>

In POSIX sh, [[ ]] is undefined.
Raw output
./build/logcollector/entrypoint.sh:13:5: warning: In POSIX sh, [[ ]] is undefined. (ShellCheck.SC3010)

Check warning on line 13 in build/logcollector/entrypoint.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] build/logcollector/entrypoint.sh#L13 <ShellCheck.SC3028>

In POSIX sh, EUID is undefined.
Raw output
./build/logcollector/entrypoint.sh:13:8: warning: In POSIX sh, EUID is undefined. (ShellCheck.SC3028)
# logrotate requires UID in /etc/passwd
sed -e "s^x:1001:^x:$EUID:^" /etc/passwd >/tmp/passwd

Check warning on line 15 in build/logcollector/entrypoint.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] build/logcollector/entrypoint.sh#L15 <ShellCheck.SC3028>

In POSIX sh, EUID is undefined.
Raw output
./build/logcollector/entrypoint.sh:15:23: warning: In POSIX sh, EUID is undefined. (ShellCheck.SC3028)
cat /tmp/passwd >/etc/passwd
rm -rf /tmp/passwd
fi
exec go-cron "0 0 * * *" sh -c "logrotate -s /data/db/logs/logrotate.status /opt/percona/logcollector/logrotate/logrotate.conf;"
exec go-cron "0 0 * * *" sh -c "logrotate -s /data/db/logs/logrotate.status /opt/percona/logcollector/logrotate/logrotate.conf ${LOGROTATE_CONF_DIR};"
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logrotate command is passed a directory path in LOGROTATE_CONF_DIR, but logrotate expects configuration file paths, not directories. This should use a glob pattern like "${LOGROTATE_CONF_DIR}/*" to include all files in the directory, or the directory should be included via an "include" directive in the main configuration file.

Copilot uses AI. Check for mistakes.
else
if [ "$1" = 'fluent-bit' ]; then
fluentbit_opt+='-c /opt/percona/logcollector/fluentbit/fluentbit.conf'

Check warning on line 22 in build/logcollector/entrypoint.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] build/logcollector/entrypoint.sh#L22 <ShellCheck.SC3024>

In POSIX sh, += is undefined.
Raw output
./build/logcollector/entrypoint.sh:22:3: warning: In POSIX sh, += is undefined. (ShellCheck.SC3024)
fi

exec "$@" $fluentbit_opt

Check notice on line 25 in build/logcollector/entrypoint.sh

View workflow job for this annotation

GitHub Actions / shellcheck

[shellcheck] build/logcollector/entrypoint.sh#L25 <ShellCheck.SC2086>

Double quote to prevent globbing and word splitting.
Raw output
./build/logcollector/entrypoint.sh:25:12: info: Double quote to prevent globbing and word splitting. (ShellCheck.SC2086)
fi
12 changes: 12 additions & 0 deletions config/crd/bases/psmdb.percona.com_perconaservermongodbs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,18 @@ spec:
type: string
imagePullPolicy:
type: string
logRotate:
properties:
configuration:
type: string
extraConfig:
properties:
name:
default: ""
type: string
type: object
x-kubernetes-map-type: atomic
type: object
resources:
properties:
claims:
Expand Down
12 changes: 12 additions & 0 deletions deploy/bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,18 @@ spec:
type: string
imagePullPolicy:
type: string
logRotate:
properties:
configuration:
type: string
extraConfig:
properties:
name:
default: ""
type: string
type: object
x-kubernetes-map-type: atomic
type: object
resources:
properties:
claims:
Expand Down
12 changes: 12 additions & 0 deletions deploy/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,18 @@ spec:
type: string
imagePullPolicy:
type: string
logRotate:
properties:
configuration:
type: string
extraConfig:
properties:
name:
default: ""
type: string
type: object
x-kubernetes-map-type: atomic
type: object
resources:
properties:
claims:
Expand Down
12 changes: 12 additions & 0 deletions deploy/cw-bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,18 @@ spec:
type: string
imagePullPolicy:
type: string
logRotate:
properties:
configuration:
type: string
extraConfig:
properties:
name:
default: ""
type: string
type: object
x-kubernetes-map-type: atomic
type: object
resources:
properties:
claims:
Expand Down
12 changes: 12 additions & 0 deletions e2e-tests/version-service/conf/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,18 @@ spec:
type: string
imagePullPolicy:
type: string
logRotate:
properties:
configuration:
type: string
extraConfig:
properties:
name:
default: ""
type: string
type: object
x-kubernetes-map-type: atomic
type: object
resources:
properties:
claims:
Expand Down
9 changes: 9 additions & 0 deletions pkg/apis/psmdb/v1/psmdb_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,15 @@ type LogCollectorSpec struct {
Configuration string `json:"configuration,omitempty"`
ContainerSecurityContext *corev1.SecurityContext `json:"containerSecurityContext,omitempty"`
ImagePullPolicy corev1.PullPolicy `json:"imagePullPolicy,omitempty"`
LogRotate *LogRotateSpec `json:"logRotate,omitempty"`
}

type LogRotateSpec struct {
// Configuration allows overriding the default logrotate configuration.
Configuration string `json:"configuration,omitempty"`
// ExtraConfig allows specifying logrotate configuration file in addition to the main configuration file.
// This should be a reference to a ConfigMap in the same namespace.
ExtraConfig corev1.LocalObjectReference `json:"extraConfig,omitempty"`
}

func (cr *PerconaServerMongoDB) IsLogCollectorEnabled() bool {
Expand Down
21 changes: 21 additions & 0 deletions pkg/apis/psmdb/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions pkg/controller/perconaservermongodb/psmdb_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/percona/percona-server-mongodb-operator/pkg/psmdb/backup"
psmdbconfig "github.com/percona/percona-server-mongodb-operator/pkg/psmdb/config"
"github.com/percona/percona-server-mongodb-operator/pkg/psmdb/logcollector"
"github.com/percona/percona-server-mongodb-operator/pkg/psmdb/logcollector/logrotate"
"github.com/percona/percona-server-mongodb-operator/pkg/psmdb/pmm"
"github.com/percona/percona-server-mongodb-operator/pkg/psmdb/secret"
"github.com/percona/percona-server-mongodb-operator/pkg/psmdb/tls"
Expand Down Expand Up @@ -350,6 +351,12 @@ func (r *ReconcilePerconaServerMongoDB) Reconcile(ctx context.Context, request r
}
}

if cr.CompareVersion("1.22.0") >= 0 {
if err := r.reconcileLogRotateConfigMaps(ctx, cr); err != nil {
return reconcile.Result{}, errors.Wrap(err, "reconcile log rotate config map")
}
}

err = r.reconcileUsers(ctx, cr, repls)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to reconcile users")
Expand Down Expand Up @@ -1249,6 +1256,40 @@ func (r *ReconcilePerconaServerMongoDB) reconcileLogCollectorConfigMaps(ctx cont
return nil
}

func (r *ReconcilePerconaServerMongoDB) reconcileLogRotateConfigMaps(ctx context.Context, cr *api.PerconaServerMongoDB) error {
if !cr.IsLogCollectorEnabled() {
if err := deleteConfigMapIfExists(ctx, r.client, cr, logrotate.ConfigMapName(cr.Name)); err != nil {
return errors.Wrap(err, "failed to delete log rotate config map when log collector is disabled")
}
return nil
}

if cr.Spec.LogCollector == nil || cr.Spec.LogCollector.LogRotate == nil || cr.Spec.LogCollector.LogRotate.Configuration == "" {
if err := deleteConfigMapIfExists(ctx, r.client, cr, logrotate.ConfigMapName(cr.Name)); err != nil {
return errors.Wrap(err, "failed to delete log rotate config map when the configuration is empty")
}
return nil
}

cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: logrotate.ConfigMapName(cr.Name),
Namespace: cr.GetNamespace(),
Labels: naming.ClusterLabels(cr),
},
Data: map[string]string{
logrotate.MongodbConfig: cr.Spec.LogCollector.LogRotate.Configuration,
},
}

err := r.createOrUpdateConfigMap(ctx, cr, cm)
if err != nil {
return errors.Wrap(err, "create or update config map")
}

return nil
}

func deleteConfigMapIfExists(ctx context.Context, cl client.Client, cr *api.PerconaServerMongoDB, cmName string) error {
configMap := &corev1.ConfigMap{}

Expand Down
35 changes: 27 additions & 8 deletions pkg/controller/perconaservermongodb/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1"
"github.com/percona/percona-server-mongodb-operator/pkg/naming"
"github.com/percona/percona-server-mongodb-operator/pkg/psmdb"
psmdbconfig "github.com/percona/percona-server-mongodb-operator/pkg/psmdb/config"
"github.com/percona/percona-server-mongodb-operator/pkg/psmdb/logcollector"
"github.com/percona/percona-server-mongodb-operator/pkg/psmdb/logcollector/logrotate"
)

func (r *ReconcilePerconaServerMongoDB) reconcileStatefulSet(ctx context.Context, cr *api.PerconaServerMongoDB, rs *api.ReplsetSpec, ls map[string]string) (*appsv1.StatefulSet, error) {
Expand Down Expand Up @@ -118,6 +120,19 @@ func (r *ReconcilePerconaServerMongoDB) getStatefulsetFromReplset(ctx context.Co
return nil, errors.Wrap(err, "check if log collection custom configuration exists")
}

logRotateCustomConfig, err := r.getCustomConfig(ctx, cr.Namespace, logrotate.ConfigMapName(cr.Name))
if err != nil {
return nil, errors.Wrap(err, "check if log rotate configuration exists")
}

logRotateExtraConfig := psmdbconfig.CustomConfig{}
if cr.Spec.LogCollector != nil && cr.Spec.LogCollector.LogRotate != nil && cr.Spec.LogCollector.LogRotate.ExtraConfig.Name != "" {
logRotateExtraConfig, err = r.getCustomConfig(ctx, cr.Namespace, cr.Spec.LogCollector.LogRotate.ExtraConfig.Name)
if err != nil {
return nil, errors.Wrap(err, "check if log rotate extra configuration exists")
}
}

usersSecret := new(corev1.Secret)
err = r.client.Get(ctx, types.NamespacedName{Name: api.UserSecretName(cr), Namespace: cr.Namespace}, usersSecret)
if client.IgnoreNotFound(err) != nil {
Expand All @@ -130,16 +145,20 @@ func (r *ReconcilePerconaServerMongoDB) getStatefulsetFromReplset(ctx context.Co
return nil, errors.Wrap(err, "check ssl secrets")
}

configs := psmdb.StatefulConfigParams{
MongoDConf: mongodCustomConfig,
LogCollectionConf: logCollectionCustomConfig,
LogRotateConf: logRotateCustomConfig,
LogRotateExtraConf: logRotateExtraConfig,
}
secrets := psmdb.StatefulSpecSecretParams{
UsersSecret: usersSecret,
SSLSecret: sslSecret,
}
sfsSpec, err := psmdb.StatefulSpec(
ctx, cr, rs, ls, r.initImage,
psmdb.StatefulConfigParams{
MongoDConf: mongodCustomConfig,
LogCollectionConf: logCollectionCustomConfig,
},
psmdb.StatefulSpecSecretParams{
UsersSecret: usersSecret,
SSLSecret: sslSecret,
},
configs,
secrets,
)
if err != nil {
return nil, errors.Wrapf(err, "create StatefulSet.Spec %s", sfs.Name)
Expand Down
73 changes: 2 additions & 71 deletions pkg/psmdb/logcollector/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package logcollector

import (
"fmt"
"strconv"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"

api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1"
"github.com/percona/percona-server-mongodb-operator/pkg/psmdb/config"
"github.com/percona/percona-server-mongodb-operator/pkg/psmdb/logcollector/logrotate"
)

const (
Expand All @@ -35,7 +35,7 @@ func Containers(cr *api.PerconaServerMongoDB, mongoPort int32) ([]corev1.Contain
return nil, err
}

logRotationCont, err := logRotationContainer(cr, mongoPort)
logRotationCont, err := logrotate.Container(cr, mongoPort)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -103,72 +103,3 @@ func logContainer(cr *api.PerconaServerMongoDB) (*corev1.Container, error) {

return &container, nil
}

func logRotationContainer(cr *api.PerconaServerMongoDB, mongoPort int32) (*corev1.Container, error) {
if cr.Spec.LogCollector == nil {
return nil, errors.New("logcollector can't be nil")
}

boolFalse := false

usersSecretName := api.UserSecretName(cr)

envs := []corev1.EnvVar{
{
Name: "MONGODB_HOST",
Value: "localhost",
},
{
Name: "MONGODB_PORT",
Value: strconv.Itoa(int(mongoPort)),
},
{
Name: "MONGODB_USER",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
Key: "MONGODB_CLUSTER_ADMIN_USER_ESCAPED",
LocalObjectReference: corev1.LocalObjectReference{
Name: usersSecretName,
},
Optional: &boolFalse,
},
},
},
{
Name: "MONGODB_PASSWORD",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
Key: "MONGODB_CLUSTER_ADMIN_PASSWORD_ESCAPED",
LocalObjectReference: corev1.LocalObjectReference{
Name: usersSecretName,
},
Optional: &boolFalse,
},
},
},
}

container := corev1.Container{
Name: "logrotate",
Image: cr.Spec.LogCollector.Image,
Env: envs,
ImagePullPolicy: cr.Spec.LogCollector.ImagePullPolicy,
SecurityContext: cr.Spec.LogCollector.ContainerSecurityContext,
Resources: cr.Spec.LogCollector.Resources,
Args: []string{
"logrotate",
},
Command: []string{"/opt/percona/logcollector/entrypoint.sh"},
VolumeMounts: []corev1.VolumeMount{
{
Name: config.MongodDataVolClaimName,
MountPath: config.MongodContainerDataDir,
},
{
Name: config.BinVolumeName,
MountPath: config.BinMountPath,
},
},
}
return &container, nil
}
Loading
Loading