Skip to content

Commit

Permalink
fix logRotate and fix path naming add auditlogrotation (#1591)
Browse files Browse the repository at this point in the history
  • Loading branch information
nammn authored Jul 22, 2024
1 parent 604ef86 commit 35e9ae1
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 5 deletions.
3 changes: 3 additions & 0 deletions api/v1/mongodbcommunity_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,9 @@ type AgentConfiguration struct {
// LogRotate if enabled, will enable LogRotate for all processes.
LogRotate *automationconfig.CrdLogRotate `json:"logRotate,omitempty"`
// +optional
// AuditLogRotate if enabled, will enable AuditLogRotate for all processes.
AuditLogRotate *automationconfig.CrdLogRotate `json:"AuditLogRotate,omitempty"`
// +optional
// SystemLog configures system log of mongod
SystemLog *automationconfig.SystemLog `json:"systemLog,omitempty"`
}
Expand Down
1 change: 1 addition & 0 deletions controllers/mongodb_tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ func TestAutomationConfigIsCorrectlyConfiguredWithTLS(t *testing.T) {
assert.Equal(t, "/tmp/test", process.Args26.Get("systemLog.path").String())
assert.Equal(t, "file", process.Args26.Get("systemLog.destination").String())
assert.Equal(t, process.LogRotate, automationconfig.ConvertCrdLogRotateToAC(mdb.Spec.AgentConfiguration.LogRotate))
assert.Equal(t, process.AuditLogRotate, automationconfig.ConvertCrdLogRotateToAC(mdb.Spec.AgentConfiguration.AuditLogRotate))
}
})

Expand Down
2 changes: 1 addition & 1 deletion controllers/replica_set_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ func buildAutomationConfig(mdb mdbv1.MongoDBCommunity, auth automationconfig.Aut
AddModifications(getMongodConfigModification(mdb)).
AddModifications(modifications...).
AddProcessModification(func(_ int, p *automationconfig.Process) {
automationconfig.ConfigureAgentConfiguration(mdb.Spec.AgentConfiguration.SystemLog, mdb.Spec.AgentConfiguration.LogRotate, p)
automationconfig.ConfigureAgentConfiguration(mdb.Spec.AgentConfiguration.SystemLog, mdb.Spec.AgentConfiguration.LogRotate, mdb.Spec.AgentConfiguration.AuditLogRotate, p)
}).
Build()
}
Expand Down
3 changes: 3 additions & 0 deletions controllers/replicaset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ func newTestReplicaSetWithSystemLogAndLogRotate() mdbv1.MongoDBCommunity {
LogRotate: &automationconfig.CrdLogRotate{
SizeThresholdMB: "1",
},
AuditLogRotate: &automationconfig.CrdLogRotate{
SizeThresholdMB: "1",
},
SystemLog: &automationconfig.SystemLog{
Destination: automationconfig.File,
Path: "/tmp/test",
Expand Down
12 changes: 10 additions & 2 deletions pkg/automationconfig/automation_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ type Process struct {
ProcessType ProcessType `json:"processType"`
Version string `json:"version"`
AuthSchemaVersion int `json:"authSchemaVersion"`
LogRotate *AcLogRotate `json:"LogRotate,omitempty"`
LogRotate *AcLogRotate `json:"logRotate,omitempty"`
AuditLogRotate *AcLogRotate `json:"auditLogRotate,omitempty"`
}

func (p *Process) SetPort(port int) *Process {
Expand Down Expand Up @@ -180,6 +181,12 @@ func (p *Process) SetLogRotate(lr *CrdLogRotate) *Process {
return p
}

// SetAuditLogRotate sets the acLogRotate by converting the CrdLogRotate to an acLogRotate.
func (p *Process) SetAuditLogRotate(lr *CrdLogRotate) *Process {
p.AuditLogRotate = ConvertCrdLogRotateToAC(lr)
return p
}

// ConvertCrdLogRotateToAC converts a CrdLogRotate to an AcLogRotate representation.
func ConvertCrdLogRotateToAC(lr *CrdLogRotate) *AcLogRotate {
if lr == nil {
Expand Down Expand Up @@ -478,7 +485,7 @@ func FromBytes(acBytes []byte) (AutomationConfig, error) {
return ac, nil
}

func ConfigureAgentConfiguration(systemLog *SystemLog, logRotate *CrdLogRotate, p *Process) {
func ConfigureAgentConfiguration(systemLog *SystemLog, logRotate *CrdLogRotate, auditLR *CrdLogRotate, p *Process) {
if systemLog != nil {
p.SetSystemLog(*systemLog)
}
Expand All @@ -491,6 +498,7 @@ func ConfigureAgentConfiguration(systemLog *SystemLog, logRotate *CrdLogRotate,
zap.S().Warn("Configuring LogRotate with systemLog.Destination = Syslog will not work")
}
p.SetLogRotate(logRotate)
p.SetAuditLogRotate(auditLR)
}

}
8 changes: 6 additions & 2 deletions pkg/automationconfig/automation_config_secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestEnsureSecret(t *testing.T) {
ctx := context.Background()

desiredAutomationConfig, err = NewBuilder().SetMembers(3).AddProcessModification(func(i_ int, p *Process) {
p.SetLogRotate(&CrdLogRotate{
lr := &CrdLogRotate{
SizeThresholdMB: "0.001",
LogRotate: LogRotate{
TimeThresholdHrs: 1,
Expand All @@ -53,7 +53,9 @@ func TestEnsureSecret(t *testing.T) {
IncludeAuditLogsWithMongoDBLogs: false,
},
PercentOfDiskspace: "1",
})
}
p.SetLogRotate(lr)
p.SetAuditLogRotate(lr)
}).Build()
assert.NoError(t, err)

Expand All @@ -72,7 +74,9 @@ func TestEnsureSecret(t *testing.T) {
acFromBytes, err := FromBytes(bytes)
assert.NoError(t, err)
assert.Equal(t, 0.001, acFromBytes.Processes[0].LogRotate.SizeThresholdMB)
assert.Equal(t, 0.001, acFromBytes.Processes[0].AuditLogRotate.SizeThresholdMB)
assert.Equal(t, float64(1), acFromBytes.Processes[0].LogRotate.PercentOfDiskspace)
assert.Equal(t, float64(1), acFromBytes.Processes[0].AuditLogRotate.PercentOfDiskspace)
})

t.Run("test LogRotate marshal and unmarshal if not set", func(t *testing.T) {
Expand Down

0 comments on commit 35e9ae1

Please sign in to comment.