diff --git a/README.md b/README.md index d38501a..30d45d3 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,14 @@ The following policy must be attached to the AWS role to be executed. } ``` +## Environment Variable + +|name |required|default|description | +|----------------|--------|-------|--------------------------------------------------| +|ALERT_HOURS |no | 2160 | Time to determine "alert" status for EOL dates | +|WARNING_HOURS |no | 4320 | Time to determine "warning" status for EOL dates | +|AWS_API_INTERVAL|no | 300 | Interval between calls to the AWS API | + ## Datadog Autodiscovery If you use Datadog, you can use [Kubernetes Integration Autodiscovery](https://docs.datadoghq.com/agent/kubernetes/integrations/?tab=kubernetes) feature. diff --git a/main.go b/main.go index 397bd58..45da6ce 100644 --- a/main.go +++ b/main.go @@ -204,6 +204,36 @@ func getInterval() (int, error) { return integerGithubAPIInterval, nil } +func getAlertHours() (int, error) { + const defaultAlertHours = 2160 // 90 days * 24 hour + alertHours := os.Getenv("ALERT_HOURS") + if len(alertHours) == 0 { + return defaultAlertHours, nil + } + + integerAlertHours, err := strconv.Atoi(alertHours) + if err != nil { + return 0, fmt.Errorf("failed to read Alert Hours: %w", err) + } + + return integerAlertHours, nil +} + +func getWarningHours() (int, error) { + const defaultWarningHours = 4320 // 180 days * 24 hour + warningHours := os.Getenv("WARNING_HOURS") + if len(warningHours) == 0 { + return defaultWarningHours, nil + } + + integerWarningHours, err := strconv.Atoi(warningHours) + if err != nil { + return 0, fmt.Errorf("failed to read Warning Hours: %w", err) + } + + return integerWarningHours, nil +} + func getRDSClusters() ([]RDSInfo, error) { sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, @@ -339,8 +369,16 @@ func validateEOLStatus(rdsInfo RDSInfo, minimumSupportedInfos []MinimumSupported func validateEOLDate(validDate string, now time.Time) (string, error) { var layout = "2006-01-02" var eolStatus string - const alertHours = 30 * 24 // 30 Days - const warningHours = 90 * 24 // 90 Days + + alertHours, err := getAlertHours() + if err != nil { + return "", fmt.Errorf("failed to get Alert Hour: %w", err) + } + + warningHours, err := getWarningHours() + if err != nil { + return "", fmt.Errorf("failed to get Warning Hour: %w", err) + } dueDate, err := time.Parse(layout, validDate) if err != nil { @@ -350,9 +388,9 @@ func validateEOLDate(validDate string, now time.Time) (string, error) { switch { case now.After(dueDate): eolStatus = "expired" - case now.After(dueDate.Add(-1 * alertHours * time.Hour)): + case now.After(dueDate.Add(-1 * time.Duration(alertHours) * time.Hour)): eolStatus = "alert" - case now.After(dueDate.Add(-1 * warningHours * time.Hour)): + case now.After(dueDate.Add(-1 * time.Duration(warningHours) * time.Hour)): eolStatus = "warning" default: eolStatus = "ok" diff --git a/main_test.go b/main_test.go index d83c6be..03edbfd 100644 --- a/main_test.go +++ b/main_test.go @@ -26,10 +26,10 @@ func TestValidateEOLDate(t *testing.T) { {name: "Expired", validDate: "2020-12-31", now: now, out: "expired"}, {name: "Alert", validDate: "2021-01-01", now: now, out: "alert"}, {name: "Alert", validDate: "2021-01-02", now: now, out: "alert"}, - {name: "Warning", validDate: "2021-01-30", now: now, out: "alert"}, - {name: "Warning", validDate: "2021-01-31", now: now, out: "warning"}, - {name: "Warning", validDate: "2021-03-31", now: now, out: "warning"}, - {name: "OK", validDate: "2021-04-01", now: now, out: "ok"}, + {name: "Warning", validDate: "2021-03-30", now: now, out: "alert"}, + {name: "Warning", validDate: "2021-04-01", now: now, out: "warning"}, + {name: "Warning", validDate: "2021-06-29", now: now, out: "warning"}, + {name: "OK", validDate: "2021-06-30", now: now, out: "ok"}, } for _, tt := range tests {