Skip to content
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

feat: create NS only if needed #252

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

paolomainardi
Copy link
Member

@paolomainardi paolomainardi commented Nov 25, 2024

PR Type

Enhancement


Description

  • Enhanced namespace creation logic to be more explicit and informative:
    • Added check for namespace existence before attempting to create it
    • Replaced silent failure (|| true) with proper conditional logic
    • Added user feedback when namespace already exists
  • Improved script reliability and debugging experience by providing clear status messages

Changes walkthrough 📝

Relevant files
Enhancement
functions.bash
Improve namespace creation with existence check                   

scripts/src/functions.bash

  • Modified prepare-namespace function to check if namespace exists
    before creating it
  • Added informative message when namespace already exists
  • Removed the || true fallback approach in favor of explicit condition
    check
  • +5/-1     

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    @sparkfabrik-ai-bot
    Copy link

    sparkfabrik-ai-bot bot commented Nov 25, 2024

    PR Reviewer Guide 🔍

    (Review updated until commit 43526c0)

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Duplicate Message
    The message about namespace existence is printed twice - once unconditionally at line 99 and again conditionally at line 106. This could be confusing for users.

    Error Handling
    The error message for namespace creation failure could be more descriptive by including the specific error returned from kubectl create command

    @paolomainardi
    Copy link
    Member Author

    /review

    @paolomainardi
    Copy link
    Member Author

    /improve

    @sparkfabrik-ai-bot
    Copy link

    Persistent review updated to latest commit 331724d

    @paolomainardi
    Copy link
    Member Author

    /improve

    @paolomainardi
    Copy link
    Member Author

    /review

    @sparkfabrik-ai-bot
    Copy link

    Persistent review updated to latest commit 43526c0

    @paolomainardi
    Copy link
    Member Author

    /improve

    @paolomainardi
    Copy link
    Member Author

    /improve

    @sparkfabrik-ai-bot
    Copy link

    sparkfabrik-ai-bot bot commented Nov 25, 2024

    PR Code Suggestions ✨

    Latest suggestions up to 31bb8eb

    CategorySuggestion                                                                                                                                    Score
    Enhancement
    Improve error handling with more descriptive error messages and troubleshooting hints

    Add error handling for the case when kubectl command fails due to network issues or
    invalid configuration.

    scripts/src/functions.bash [101-104]

     if ! kubectl auth can-i get namespace >/dev/null 2>&1; then
       echo "Failed to check namespace: cluster connection or permissions issue"
    +  echo "Please verify your kubeconfig and network connection"
       exit 1
     fi
    Suggestion importance[1-10]: 5

    Why: Adding troubleshooting hints improves user experience by providing actionable guidance when permissions or connectivity issues occur. However, the existing error message already conveys the core issue.

    5
    Best practice
    Quote command arguments and redirections to prevent word splitting issues

    Add quotes around the command substitution in the redirection to prevent word
    splitting and globbing issues.

    scripts/src/functions.bash [100]

    -if ! kubectl get ns "$KUBE_NAMESPACE" >/dev/null 2>&1; then
    +if ! kubectl get ns "${KUBE_NAMESPACE}" > "/dev/null" 2>&1; then
    Suggestion importance[1-10]: 3

    Why: While quoting redirections can provide marginal safety benefits, the current code is already safe with proper quoting of variables. The improvement is minimal and mostly stylistic.

    3

    Previous suggestions

    Suggestions up to commit c0ac49c
    CategorySuggestion                                                                                                                                    Score
    Best practice
    Add error handling to distinguish between non-existent namespace and cluster connectivity issues

    Add error handling for the kubectl get command to catch potential connection or
    authentication issues with the cluster.

    scripts/src/functions.bash [100]

     if ! kubectl get ns "$KUBE_NAMESPACE" >/dev/null 2>&1; then
    +  if ! kubectl auth can-i get namespace >/dev/null 2>&1; then
    +    echo "Failed to check namespace: cluster connection or permissions issue"
    +    exit 1
    +  fi
    Suggestion importance[1-10]: 8

    Why: This is a valuable security enhancement that helps distinguish between different failure modes (connectivity vs. non-existent namespace), enabling better error diagnosis and user feedback.

    8
    Improve error message formatting and safety by properly quoting variables

    Quote the namespace variable in error messages to prevent potential word splitting
    and make the output more readable when namespace contains spaces.

    scripts/src/functions.bash [102]

    -echo "Failed to create namespace $KUBE_NAMESPACE"
    +echo "Failed to create namespace \"${KUBE_NAMESPACE}\""
    Suggestion importance[1-10]: 5

    Why: Proper quoting in error messages is a good practice that prevents potential issues with special characters in namespace names and improves output readability.

    5
    Enhancement
    Use modern bash conditional syntax for better script reliability

    Use double brackets [[ ]] instead of single brackets [ ] for better compatibility
    and functionality in bash scripts.

    scripts/src/functions.bash [95]

    -if [ -z "${KUBE_NAMESPACE}" ]; then
    +if [[ -z "${KUBE_NAMESPACE}" ]]; then
    Suggestion importance[1-10]: 4

    Why: While using double brackets is a better bash practice offering more features and fewer pitfalls, in this specific context the improvement is minor as the current usage is safe.

    4
    Suggestions up to commit 43526c0
    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Remove misleading log message that incorrectly states a namespace exists before actually checking its existence

    Remove the premature echo statement before checking if the namespace exists, as it's
    misleading when the namespace doesn't exist yet.

    scripts/src/functions.bash [99-100]

    -echo "Namespace \"$KUBE_NAMESPACE\" already exists."
     if ! kubectl get ns "$KUBE_NAMESPACE" >/dev/null 2>&1; then
    Suggestion importance[1-10]: 9

    Why: The suggestion correctly identifies a logical error where the code incorrectly states a namespace exists before actually checking. This could cause confusion and incorrect debugging.

    9
    Enhancement
    Add success message to provide clear feedback about successful operations

    Add a success message when namespace is created to provide better feedback about the
    operation's outcome.

    scripts/src/functions.bash [101-104]

     if ! kubectl create ns "$KUBE_NAMESPACE"; then
       echo "Failed to create namespace $KUBE_NAMESPACE"
       exit 1
     fi
    +echo "Successfully created namespace \"$KUBE_NAMESPACE\""
    Suggestion importance[1-10]: 6

    Why: Adding success feedback improves observability and debugging experience, though it's not critical for functionality since the command's success can be inferred from the absence of error messages.

    6
    Best practice
    Quote variables in error messages to prevent shell interpretation and improve readability

    Quote the namespace variable in error messages to prevent potential shell
    interpretation and make the output more readable when the namespace contains special
    characters.

    scripts/src/functions.bash [102]

    -echo "Failed to create namespace $KUBE_NAMESPACE"
    +echo "Failed to create namespace \"$KUBE_NAMESPACE\""
    Suggestion importance[1-10]: 5

    Why: While proper quoting of variables is a good practice for shell scripts, the current context already has the variable within double quotes which provides adequate protection. The additional escaping would only marginally improve readability.

    5
    Suggestions up to commit 331724d
    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add error handling for command execution failures to prevent silent errors and improve script reliability

    Add error handling for the kubectl create command to catch potential failures other
    than namespace already exists

    scripts/src/functions.bash [100-102]

     if ! kubectl get ns "$KUBE_NAMESPACE" >/dev/null 2>&1; then
    -  kubectl create ns "$KUBE_NAMESPACE"
    +  if ! kubectl create ns "$KUBE_NAMESPACE"; then
    +    echo "Failed to create namespace $KUBE_NAMESPACE"
    +    exit 1
    +  fi
     else
    Suggestion importance[1-10]: 8

    Why: Adding error handling for kubectl create is important for reliability and debugging. The suggestion properly handles potential failures and exits with an error message instead of silently continuing.

    8
    Best practice
    Properly quote variables in string outputs to prevent potential shell expansion issues

    Quote the namespace variable in the echo statement to prevent potential word
    splitting or glob expansion issues

    scripts/src/functions.bash [103]

    -echo "Namespace $KUBE_NAMESPACE already exists."
    +echo "Namespace \"$KUBE_NAMESPACE\" already exists."
    Suggestion importance[1-10]: 3

    Why: While proper variable quoting is a good practice, in this echo statement the risk is minimal since the variable is already within double quotes. The improvement adds only marginal value to code safety.

    3
    Suggestions up to commit 852d602
    CategorySuggestion                                                                                                                                    Score
    Possible bug
    Fix incorrect if-else block termination syntax in shell script

    Fix the incorrect 'end' keyword in the if-else block. In Bash, the correct syntax is
    'fi' to close an if statement.

    scripts/src/functions.bash [100-104]

     if ! kubectl get ns "$KUBE_NAMESPACE" >/dev/null 2>&1; then
       kubectl create ns "$KUBE_NAMESPACE"
     else
       echo "Namespace $KUBE_NAMESPACE already exists."
    -end
    +fi
    Suggestion importance[1-10]: 10

    Why: This is a critical bug fix as using 'end' instead of 'fi' to close an if statement in Bash will cause a syntax error and break script execution. The suggestion correctly identifies and fixes this syntax error.

    10

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    1 participant