-
Notifications
You must be signed in to change notification settings - Fork 73
WINC-1859: Implement log file rotation for WICD #4177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jrvaldes
wants to merge
2
commits into
openshift:master
Choose a base branch
from
jrvaldes:WINC-1859-wicd-logFileMaxSizeMB
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package logconfig | ||
|
|
||
| import ( | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| ) | ||
|
|
||
| func init() { | ||
| log := ctrl.Log.WithName("logconfig").WithName("init") | ||
|
|
||
| var err error | ||
| logFileSize, err = getEnvQuantityString(LogFileSizeEnvVar) | ||
| if err != nil { | ||
| log.Error(err, "cannot load environment variable", "name", LogFileSizeEnvVar) | ||
| } | ||
|
|
||
| logFileAge, err = getEnvDurationString(LogFileAgeEnvVar) | ||
| if err != nil { | ||
| log.Error(err, "cannot load environment variable", "name", LogFileAgeEnvVar) | ||
| } | ||
|
|
||
| flushInterval, err = getEnvDurationString(LogFlushIntervalEnvVar) | ||
| if err != nil { | ||
| log.Error(err, "cannot load environment variable", "name", LogFlushIntervalEnvVar) | ||
| } | ||
| } | ||
|
jrvaldes marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package logconfig | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "k8s.io/apimachinery/pkg/api/resource" | ||
| ) | ||
|
|
||
| const ( | ||
| // LogFileSizeEnvVar is the environment variable name for log file size limit | ||
| LogFileSizeEnvVar = "SERVICES_LOG_FILE_SIZE" | ||
| // LogFileAgeEnvVar is the environment variable name for log file age retention | ||
| LogFileAgeEnvVar = "SERVICES_LOG_FILE_AGE" | ||
| // LogFlushIntervalEnvVar is the environment variable name for log flush interval | ||
| LogFlushIntervalEnvVar = "SERVICES_LOG_FLUSH_INTERVAL" | ||
| ) | ||
|
|
||
| // logFileSize, logFileAge, and flushInterval hold the configuration values | ||
| // for log file size, log file age, and flush interval respectively. | ||
| var logFileSize, logFileAge, flushInterval string | ||
|
|
||
| // GetLogFileSizeMegabytes returns the configured log file size resource value in megabytes | ||
| func GetLogFileSizeMegabytes() uint64 { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can still keep this as GetLogFileSize if there isn't going to be another function that will need to return size not in megabytes. Function doc can state that the value will be in megabytes. |
||
| if logFileSize == "" { | ||
| return 0 | ||
| } | ||
| q, err := resource.ParseQuantity(logFileSize) | ||
| if err != nil { | ||
| return 0 | ||
| } | ||
| mb := q.ScaledValue(resource.Mega) | ||
| // check overflow | ||
| if mb < 0 { | ||
| return 0 | ||
| } | ||
| return uint64(mb) | ||
| } | ||
|
|
||
| // GenerateKubeLogRunnerCmd returns the command string to run the given commandPath with kube-log-runner | ||
| // logging to the given logfilePath. Log rotation parameters logFileSize, logFileAge, and flushInterval | ||
| // are read via environment variables. | ||
| func GenerateKubeLogRunnerCmd(kubeLogRunnerPath, commandPath, logfilePath string) string { | ||
| cmdBuilder := strings.Builder{} | ||
| cmdBuilder.WriteString(kubeLogRunnerPath) | ||
|
|
||
| cmdBuilder.WriteString(" -log-file=") | ||
| cmdBuilder.WriteString(logfilePath) | ||
|
|
||
| if logFileSize != "" { | ||
| cmdBuilder.WriteString(" -log-file-size=") | ||
| cmdBuilder.WriteString(logFileSize) | ||
| } | ||
|
|
||
| if logFileAge != "" { | ||
| cmdBuilder.WriteString(" -log-file-age=") | ||
| cmdBuilder.WriteString(logFileAge) | ||
| } | ||
|
|
||
| if flushInterval != "" { | ||
| cmdBuilder.WriteString(" -flush-interval=") | ||
| cmdBuilder.WriteString(flushInterval) | ||
| } | ||
|
|
||
| cmdBuilder.WriteString(" " + commandPath) | ||
|
|
||
| return cmdBuilder.String() | ||
| } | ||
|
|
||
| // getEnvQuantityString returns the string value of the environment variable for the given key | ||
| // if it represents a valid and non-negative quantity, otherwise returns error | ||
| func getEnvQuantityString(key string) (string, error) { | ||
| value := os.Getenv(key) | ||
| value = strings.TrimSpace(value) | ||
| if value == "" { | ||
| // not present | ||
| return "", nil | ||
| } | ||
| // validate value as quantity | ||
| q, err := resource.ParseQuantity(value) | ||
| if err != nil { | ||
| return "", fmt.Errorf("invalid quantity value for %s: %w", key, err) | ||
| } | ||
| if q.Sign() < 0 { | ||
| return "", fmt.Errorf("quantity cannot be negative for %s", key) | ||
| } | ||
| return value, nil | ||
| } | ||
|
|
||
| // getEnvDurationString returns the string value of the environment variable for the given key | ||
| // if it represents a valid and non-negative duration, otherwise returns error | ||
| func getEnvDurationString(key string) (string, error) { | ||
| value := os.Getenv(key) | ||
| value = strings.TrimSpace(value) | ||
| if value == "" { | ||
| return "", nil | ||
| } | ||
| if strings.HasPrefix(value, "-") { | ||
| return "", fmt.Errorf("duration cannot be negative for %s", key) | ||
| } | ||
|
|
||
| // validate value as duration | ||
| if _, err := time.ParseDuration(value); err != nil { | ||
| return "", fmt.Errorf("invalid duration value for %s: %w", key, err) | ||
| } | ||
| return value, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't this be default unlimited and then if set to a value other than 0, then set to that value?