You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains 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
Sensitive information exposure: The module creates a kubernetes_secret_v1 resource (lines 39-52 in main.tf) that generates a service account token. While this is a common practice, it's important to ensure that access to this token is strictly controlled and that it's not exposed in logs or other outputs. Additionally, the use of wait_for_service_account_token = true means the token will be available immediately, which could potentially be a security risk if not properly managed.
⚡ Key issues to review
Security Concern The kubernetes_secret_v1 resource is creating a service account token, which could potentially be a security risk if not properly managed.
Resource Naming The use of generate_name for the kubernetes_secret_v1 resource may lead to unexpected behavior or difficulties in resource management.
Default Configuration The default working hours schedules (scale_down at 20:00 and scale_up at 07:30) may not be suitable for all use cases and should be reviewed.
for KUBE_NAMESPACE in $(echo "${NAMESPACES}" | tr "," " "); do
echo "Processing namespace: ${KUBE_NAMESPACE}"
for DEPLOYMENT in $(kubectl -n "${KUBE_NAMESPACE}" get deployments -l "${DEPLOYMENTS_LABEL_SELECTOR}" -o jsonpath='{.items[*].metadata.name}'); do
echo "Processing deployment: ${KUBE_NAMESPACE}/${DEPLOYMENT}"
if [ "${DRY_RUN}" -eq "1" ]; then
echo "DRY-RUN: kubectl -n ${KUBE_NAMESPACE} scale --replicas ${GO_TO_REPLICAS} deployment ${DEPLOYMENT}"
continue
fi
echo "Scaling deployment ${KUBE_NAMESPACE}/${DEPLOYMENT} to ${GO_TO_REPLICAS} replicas"
- kubectl -n "${KUBE_NAMESPACE}" scale --replicas "${GO_TO_REPLICAS}" deployment "${DEPLOYMENT}"+ if ! kubectl -n "${KUBE_NAMESPACE}" scale --replicas "${GO_TO_REPLICAS}" deployment "${DEPLOYMENT}"; then+ echo "ERROR: Failed to scale deployment ${KUBE_NAMESPACE}/${DEPLOYMENT}" >&2+ exit 1+ fi+ echo "Successfully scaled deployment ${KUBE_NAMESPACE}/${DEPLOYMENT}"
done
done
Suggestion importance[1-10]: 9
Why: Improved error handling and logging in the bash script is crucial for effective debugging and monitoring of scaling operations.
9
Add validation rules for cron schedule variables to ensure they are valid expressions
Consider adding validation rules for critical variables to ensure they meet specific criteria. This can help prevent configuration errors and improve the module's robustness.
variable "working_hours_scale_down_schedule" {
description = "Cron schedule to scale down the Deployments. Remember that this is relative to the timezone defined in the `cronjob_timezone` variable."
type = string
default = "0 20 * * *"
+ validation {+ condition = can(regex("^[0-9*/-]+ [0-9*/-]+ [0-9*/-]+ [0-9*/-]+ [0-9*/-]+$", var.working_hours_scale_down_schedule))+ error_message = "The working_hours_scale_down_schedule must be a valid cron expression."+ }
}
variable "working_hours_scale_up_schedule" {
description = "Cron schedule to scale up the Deployments. Remember that this is relative to the timezone defined in the `cronjob_timezone` variable."
type = string
default = "30 7 * * 1-5"
+ validation {+ condition = can(regex("^[0-9*/-]+ [0-9*/-]+ [0-9*/-]+ [0-9*/-]+ [0-9*/-]+$", var.working_hours_scale_up_schedule))+ error_message = "The working_hours_scale_up_schedule must be a valid cron expression."+ }
}
Suggestion importance[1-10]: 8
Why: Adding validation rules for cron schedules prevents configuration errors, significantly improving the module's reliability.
8
Add a failure policy to the CronJob resource to handle potential job failures
Consider adding a failure_policy to the CronJob resource to specify how the job should behave if it fails. This can help manage potential issues with the scaling operations.
resource "kubernetes_manifest" "scale_down" {
manifest = yamldecode(
templatefile(
"${path.module}/files/k8s-working-hours-cronjob.yaml.tftpl",
{
name = "${var.working_hours_resource_prefix}-scale-down"
namespace = local.final_namespace
labels = local.k8s_full_labels
suspend = var.working_hours_suspend
schedule = var.working_hours_scale_down_schedule
timezone = var.cronjob_timezone
image = var.working_hours_docker_image
config_map_app = kubernetes_config_map_v1.app.metadata[0].name
config_map_app_env = kubernetes_config_map_v1.app_env.metadata[0].name
service_account = kubernetes_service_account_v1.this.metadata[0].name
+ failure_policy = "Retry"
# This is the scale down script, so we want to scale down the replicas to 0.
go_to_replicas = 0
}
)
)
}
Suggestion importance[1-10]: 7
Why: Adding a failure policy improves the robustness of the CronJob, helping to manage potential issues with scaling operations.
7
Best practice
Use the most recent stable API version for Kubernetes resources
Consider using a more specific API version for the Kubernetes resources. Instead of 'kubernetes_*_v1', use the most recent stable API version for each resource type. This ensures compatibility with the latest Kubernetes versions and access to newer features.
variable "working_hours_scale_down_schedule" {
description = "Cron schedule to scale down the Deployments. Remember that this is relative to the timezone defined in the `cronjob_timezone` variable."
type = string
default = "0 20 * * *"
+ validation {+ condition = can(regex("^([0-59]|[0-5][0-9]) ([0-23]|[0-1][0-9]|2[0-3]) ([1-31]|[1-2][0-9]|3[0-1]) ([1-12]|0[1-9]) ([0-7]|[0-6])$", var.working_hours_scale_down_schedule))+ error_message = "The working_hours_scale_down_schedule must be a valid cron expression."+ }
}
variable "working_hours_scale_up_schedule" {
description = "Cron schedule to scale up the Deployments. Remember that this is relative to the timezone defined in the `cronjob_timezone` variable."
type = string
default = "30 7 * * 1-5"
+ validation {+ condition = can(regex("^([0-59]|[0-5][0-9]) ([0-23]|[0-1][0-9]|2[0-3]) ([1-31]|[1-2][0-9]|3[0-1]) ([1-12]|0[1-9]) ([0-7]|[0-6])$", var.working_hours_scale_up_schedule))+ error_message = "The working_hours_scale_up_schedule must be a valid cron expression."+ }
}
Suggestion importance[1-10]: 7
Why: Adding validation for cron expressions is a good practice to catch potential errors early, improving code robustness.
7
Maintainability
Use more descriptive resource names to improve code readability and maintainability
Consider using a more descriptive name for the 'kubernetes_manifest' resources to improve readability and maintainability.
-resource "kubernetes_manifest" "scale_down" {+resource "kubernetes_manifest" "working_hours_scale_down_cronjob" {
manifest = yamldecode(
templatefile(
"${path.module}/files/k8s-working-hours-cronjob.yaml.tftpl",
{
name = "${var.working_hours_resource_prefix}-scale-down"
namespace = local.final_namespace
labels = local.k8s_full_labels
suspend = var.working_hours_suspend
schedule = var.working_hours_scale_down_schedule
timezone = var.cronjob_timezone
image = var.working_hours_docker_image
config_map_app = kubernetes_config_map_v1.app.metadata[0].name
config_map_app_env = kubernetes_config_map_v1.app_env.metadata[0].name
service_account = kubernetes_service_account_v1.this.metadata[0].name
# This is the scale down script, so we want to scale down the replicas to 0.
go_to_replicas = 0
}
)
)
}
Suggestion importance[1-10]: 6
Why: The suggestion improves code readability, but the current naming is not incorrect and the benefit is relatively minor.
6
Best practice
Use more specific Kubernetes resource types to improve compatibility and maintainability
Consider using a more specific API version for the Kubernetes resources. For example, instead of 'kubernetes_namespace_v1', use 'kubernetes_namespace'.
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.
PR Type
Enhancement
Description
Changes walkthrough 📝
main.tf
Implement Kubernetes Application Sleep Cycles
main.tf
bindings
configuration
outputs.tf
Update outputs for Kubernetes resources
outputs.tf
variables.tf
Define variables for Kubernetes Sleep Cycles module
variables.tf
settings
k8s-working-hours-cronjob.yaml.tftpl
Add CronJob template for working hours
files/k8s-working-hours-cronjob.yaml.tftpl
working-hours.sh
Add scaling script for working hours
files/working-hours.sh
versions.tf
Update provider and version requirements
versions.tf
README.md
Update README for Kubernetes Sleep Cycles module
README.md