@@ -119,18 +119,29 @@ func NewConfig(spec *v1beta1.InstrumentationSpec) *Config {
119119// provided configmap
120120func AddLogrotateConfig (ctx context.Context , spec * v1beta1.InstrumentationSpec ,
121121 outInstanceConfigMap * corev1.ConfigMap , logFilePath , postrotateScript string ,
122- ) {
123- var logrotateConfig string
122+ ) error {
123+ var err error
124+ var retentionPeriod * v1beta1.Duration
125+
124126 if outInstanceConfigMap .Data == nil {
125127 outInstanceConfigMap .Data = make (map [string ]string )
126128 }
127129
130+ // If retentionPeriod is set in the spec, use that value; otherwise, we want
131+ // to use a reasonably short duration. Defaulting to 1 day.
128132 if spec != nil && spec .Logs != nil && spec .Logs .RetentionPeriod != nil {
129- logrotateConfig = generateLogrotateConfig (logFilePath , spec .Logs .RetentionPeriod ,
130- postrotateScript )
133+ retentionPeriod = spec .Logs .RetentionPeriod
134+ } else {
135+ retentionPeriod , err = v1beta1 .NewDuration ("1d" )
136+ if err != nil {
137+ return err
138+ }
131139 }
132140
133- outInstanceConfigMap .Data ["logrotate.conf" ] = logrotateConfig
141+ outInstanceConfigMap .Data ["logrotate.conf" ] = generateLogrotateConfig (logFilePath ,
142+ retentionPeriod , postrotateScript )
143+
144+ return err
134145}
135146
136147// generateLogrotateConfig generates a configuration string for logrotate based
@@ -150,11 +161,14 @@ func generateLogrotateConfig(logFilePath string, retentionPeriod *v1beta1.Durati
150161}
151162
152163// parseDurationForLogrotate takes a retention period and returns the rotate
153- // number and interval string that should be used in the logrotate config
164+ // number and interval string that should be used in the logrotate config.
165+ // If the retentionPeriod is less than 24 hours, the function will return the
166+ // number of hours and "hourly"; otherwise, we will round up to the nearest day
167+ // and return the day count and "daily"
154168func parseDurationForLogrotate (retentionPeriod * v1beta1.Duration ) (int , string ) {
155169 hours := math .Round (retentionPeriod .AsDuration ().Hours ())
156170 if hours < 24 {
157171 return int (hours ), "hourly"
158172 }
159- return int (math .Round (hours / 24 )), "daily"
173+ return int (math .Ceil (hours / 24 )), "daily"
160174}
0 commit comments