-
Notifications
You must be signed in to change notification settings - Fork 60
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
add migration for dv #455
add migration for dv #455
Conversation
WalkthroughThe changes involve updating the versioning logic in the Changes
Sequence Diagram(s)sequenceDiagram
participant Installer
participant MigrationScript
participant Kubernetes
Installer->>MigrationScript: Check current version
MigrationScript->>Kubernetes: Retrieve PVCs and Secrets
Kubernetes-->>MigrationScript: Provide PVCs and Secrets
MigrationScript->>MigrationScript: Apply labels to PVCs and DVs
MigrationScript->>Kubernetes: Create/Update ConfigMap
Kubernetes-->>MigrationScript: Confirm ConfigMap update
MigrationScript->>Installer: Migration complete
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 3
🧹 Outside diff range and nitpick comments (2)
scripts/installer.sh (2)
Line range hint
9-20
: Consider enhancing migration process safety.The current migration implementation could benefit from additional safety measures:
- No atomic transaction handling - if a migration fails mid-way, the system could be left in an inconsistent state
- Missing rollback mechanism
- Limited logging for debugging purposes
Consider implementing these improvements:
- Add a backup mechanism before running migrations
- Implement rollback functionality
- Enhanced logging with timestamps and status details
- Add a dry-run mode for testing migrations
Would you like me to provide a detailed implementation for these improvements?
Line range hint
14-19
: Improve error handling in migration loop.The current migration loop lacks robust error handling:
- No timeout protection against infinite loops
- Missing explicit error handling for kubectl commands
- No validation of migration success
Consider applying this improvement:
current_version=$(kubectl get configmap -n cozy-system cozystack-version -o jsonpath='{.data.version}') || true + start_time=$(date +%s) until [ "$current_version" = "$VERSION" ]; do + # Add timeout after 30 minutes + current_time=$(date +%s) + if [ $((current_time - start_time)) -gt 1800 ]; then + echo "Error: Migration timeout after 30 minutes" + exit 1 + fi echo "run migration: $current_version --> $VERSION" - scripts/migrations/$current_version + if ! scripts/migrations/$current_version; then + echo "Error: Migration $current_version failed" + exit 1 + fi current_version=$(kubectl get configmap -n cozy-system cozystack-version -o jsonpath='{.data.version}') + if [ -z "$current_version" ]; then + echo "Error: Failed to get current version after migration" + exit 1 + fi done
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (2)
- scripts/installer.sh (1 hunks)
- scripts/migrations/5 (1 hunks)
🔇 Additional comments (2)
scripts/migrations/5 (2)
1-3
: LGTM! Clear script purpose declaration.The shebang and comment clearly indicate this is a migration script from version 5 to 6.
1-15
: Verify the migration's idempotency.The script should be idempotent to handle potential reruns safely.
Let's verify the current version and existing labels:
✅ Verification successful
Let me analyze the migration script for idempotency without relying on
kubectl
commands, as they're not available in the sandbox environment.Looking at the script's operations:
The first part (
kubectl get secret ... | while read ...
) applies labels to PVCs and DVs:
- Uses
--overwrite
flag for labels, making it safe to rerun- Temporary files are properly cleaned up with
rm -f
The second part creates/updates the version configmap:
- Uses
kubectl apply -f-
which is idempotent by design- The version is set to a fixed value of 6
The migration script is safely idempotent
The script is idempotent because:
- Label operations use
--overwrite
flag which safely updates existing labels- ConfigMap creation uses
kubectl apply
which is idempotent by design- Temporary files are properly cleaned up
- No destructive operations are performed that could cause issues on reruns
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Check current version and existing labels to ensure idempotent migration # Test 1: Check if version 6 already exists echo "Checking current version..." kubectl get configmap -n cozy-system cozystack-version -o jsonpath='{.data.version}' # Test 2: Check for existing cluster labels echo -e "\nChecking existing cluster labels..." kubectl get pvc,dv -A -l 'cluster.x-k8s.io/cluster-name' -o jsonpath='{range .items[*]}{.kind}/{.metadata.name}: {.metadata.labels.cluster\.x-k8s\.io/cluster-name}{"\n"}{end}'Length of output: 565
rm -f "$config" | ||
done | ||
|
||
kubectl create configmap -n cozy-system cozystack-version --from-literal=version=6 --dry-run=client -o yaml | kubectl apply -f- |
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.
Add error handling for version update.
The version update command should verify success.
Apply this improvement:
-kubectl create configmap -n cozy-system cozystack-version --from-literal=version=6 --dry-run=client -o yaml | kubectl apply -f-
+if ! kubectl create configmap -n cozy-system cozystack-version --from-literal=version=6 --dry-run=client -o yaml | kubectl apply -f-; then
+ echo "Failed to update version ConfigMap"
+ exit 1
+fi
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
kubectl create configmap -n cozy-system cozystack-version --from-literal=version=6 --dry-run=client -o yaml | kubectl apply -f- | |
if ! kubectl create configmap -n cozy-system cozystack-version --from-literal=version=6 --dry-run=client -o yaml | kubectl apply -f-; then | |
echo "Failed to update version ConfigMap" | |
exit 1 | |
fi |
# Fix tenant-kubernetes PVCs | ||
kubectl get secret -l kamaji.clastix.io/project=kamaji,kamaji.clastix.io/component=admin-kubeconfig -A --output=go-template='{{ range .items }}{{ printf "%s %s %s %s\n" .metadata.namespace .metadata.name (index .metadata.labels "kamaji.clastix.io/name") (index .data "super-admin.conf") }}{{ end }}' | while read NAMESPACE NAME CLUSTER CONFIGB64; do | ||
config=$(mktemp) | ||
echo "$CONFIGB64" | base64 -d > "$config" | ||
kubectl get pv --kubeconfig="$config" --output=go-template='{{ range .items }}{{ printf "%s\n" .metadata.name }}{{ end }}' | while read PVC; do | ||
(set -x; kubectl label pvc --overwrite -n "$NAMESPACE" "$PVC" "cluster.x-k8s.io/cluster-name=$CLUSTER") | ||
(set -x; kubectl label dv --overwrite -n "$NAMESPACE" "$PVC" "cluster.x-k8s.io/cluster-name=$CLUSTER") | ||
done | ||
rm -f "$config" | ||
done |
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.
Add error handling and safety measures.
While the core logic is correct, the script could benefit from additional error handling and safety measures:
- Missing error handling for kubectl commands
- No validation of base64 decoded content
- Temporary file could persist if script fails between creation and cleanup
Consider applying these improvements:
#!/bin/sh
+set -euo pipefail
+
+cleanup() {
+ [ -f "$config" ] && rm -f "$config"
+}
+trap cleanup EXIT
+
# Migration 5 --> 6
# Fix tenant-kubernetes PVCs
kubectl get secret -l kamaji.clastix.io/project=kamaji,kamaji.clastix.io/component=admin-kubeconfig -A --output=go-template='{{ range .items }}{{ printf "%s %s %s %s\n" .metadata.namespace .metadata.name (index .metadata.labels "kamaji.clastix.io/name") (index .data "super-admin.conf") }}{{ end }}' | while read NAMESPACE NAME CLUSTER CONFIGB64; do
+ [ -z "$NAMESPACE" ] || [ -z "$CLUSTER" ] || [ -z "$CONFIGB64" ] && echo "Missing required values" && exit 1
config=$(mktemp)
- echo "$CONFIGB64" | base64 -d > "$config"
+ if ! echo "$CONFIGB64" | base64 -d > "$config"; then
+ echo "Failed to decode kubeconfig"
+ exit 1
+ fi
+ if ! kubectl --kubeconfig="$config" cluster-info > /dev/null 2>&1; then
+ echo "Invalid kubeconfig"
+ exit 1
+ fi
kubectl get pv --kubeconfig="$config" --output=go-template='{{ range .items }}{{ printf "%s\n" .metadata.name }}{{ end }}' | while read PVC; do
- (set -x; kubectl label pvc --overwrite -n "$NAMESPACE" "$PVC" "cluster.x-k8s.io/cluster-name=$CLUSTER")
- (set -x; kubectl label dv --overwrite -n "$NAMESPACE" "$PVC" "cluster.x-k8s.io/cluster-name=$CLUSTER")
+ if ! kubectl label pvc --overwrite -n "$NAMESPACE" "$PVC" "cluster.x-k8s.io/cluster-name=$CLUSTER"; then
+ echo "Failed to label PVC $PVC"
+ exit 1
+ fi
+ if ! kubectl label dv --overwrite -n "$NAMESPACE" "$PVC" "cluster.x-k8s.io/cluster-name=$CLUSTER"; then
+ echo "Failed to label DV $PVC"
+ exit 1
+ fi
done
- rm -f "$config"
done
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
# Fix tenant-kubernetes PVCs | |
kubectl get secret -l kamaji.clastix.io/project=kamaji,kamaji.clastix.io/component=admin-kubeconfig -A --output=go-template='{{ range .items }}{{ printf "%s %s %s %s\n" .metadata.namespace .metadata.name (index .metadata.labels "kamaji.clastix.io/name") (index .data "super-admin.conf") }}{{ end }}' | while read NAMESPACE NAME CLUSTER CONFIGB64; do | |
config=$(mktemp) | |
echo "$CONFIGB64" | base64 -d > "$config" | |
kubectl get pv --kubeconfig="$config" --output=go-template='{{ range .items }}{{ printf "%s\n" .metadata.name }}{{ end }}' | while read PVC; do | |
(set -x; kubectl label pvc --overwrite -n "$NAMESPACE" "$PVC" "cluster.x-k8s.io/cluster-name=$CLUSTER") | |
(set -x; kubectl label dv --overwrite -n "$NAMESPACE" "$PVC" "cluster.x-k8s.io/cluster-name=$CLUSTER") | |
done | |
rm -f "$config" | |
done | |
#!/bin/sh | |
set -euo pipefail | |
cleanup() { | |
[ -f "$config" ] && rm -f "$config" | |
} | |
trap cleanup EXIT | |
# Migration 5 --> 6 | |
# Fix tenant-kubernetes PVCs | |
kubectl get secret -l kamaji.clastix.io/project=kamaji,kamaji.clastix.io/component=admin-kubeconfig -A --output=go-template='{{ range .items }}{{ printf "%s %s %s %s\n" .metadata.namespace .metadata.name (index .metadata.labels "kamaji.clastix.io/name") (index .data "super-admin.conf") }}{{ end }}' | while read NAMESPACE NAME CLUSTER CONFIGB64; do | |
[ -z "$NAMESPACE" ] || [ -z "$CLUSTER" ] || [ -z "$CONFIGB64" ] && echo "Missing required values" && exit 1 | |
config=$(mktemp) | |
if ! echo "$CONFIGB64" | base64 -d > "$config"; then | |
echo "Failed to decode kubeconfig" | |
exit 1 | |
fi | |
if ! kubectl --kubeconfig="$config" cluster-info > /dev/null 2>&1; then | |
echo "Invalid kubeconfig" | |
exit 1 | |
fi | |
kubectl get pv --kubeconfig="$config" --output=go-template='{{ range .items }}{{ printf "%s\n" .metadata.name }}{{ end }}' | while read PVC; do | |
if ! kubectl label pvc --overwrite -n "$NAMESPACE" "$PVC" "cluster.x-k8s.io/cluster-name=$CLUSTER"; then | |
echo "Failed to label PVC $PVC" | |
exit 1 | |
fi | |
if ! kubectl label dv --overwrite -n "$NAMESPACE" "$PVC" "cluster.x-k8s.io/cluster-name=$CLUSTER"; then | |
echo "Failed to label DV $PVC" | |
exit 1 | |
fi | |
done | |
done |
@@ -3,7 +3,7 @@ set -o pipefail | |||
set -e | |||
|
|||
BUNDLE=$(set -x; kubectl get configmap -n cozy-system cozystack -o 'go-template={{index .data "bundle-name"}}') | |||
VERSION=5 | |||
VERSION=6 |
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.
💡 Codebase verification
Migration script 5 exists but is not executable
The migration script for version 5 exists at scripts/migrations/5
but lacks executable permissions (shown as -rw-r--r--
), while all previous migration scripts (1-4) are properly executable. This will cause the migration process to fail since the installer script attempts to execute these files directly.
scripts/migrations/5
needs executable permissions to be set usingchmod +x
🔗 Analysis chain
Verify migration script existence.
The version bump from 5 to 6 requires a corresponding migration script at scripts/migrations/5
to handle the transition.
Let me try to gather more information about the migration scripts in the repository to ensure we're looking at the correct path.
Let me check one more thing to specifically look for migration scripts in any numbered directories, as the installer.sh suggests they should exist with version numbers.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Verify migration script exists and is executable
# Expected: File should exist and be executable
if [ -x "scripts/migrations/5" ]; then
echo "Migration script exists and is executable"
head -n 5 "scripts/migrations/5" # Show first few lines for context
else
echo "ERROR: Missing or non-executable migration script"
exit 1
fi
Length of output: 151
Script:
#!/bin/bash
# Search for migration scripts in the repository
echo "=== Looking for migration-related files ==="
fd -t f "migration" -x ls -l {}
echo -e "\n=== Searching for version-related content in installer.sh ==="
cat scripts/installer.sh
echo -e "\n=== Searching for migration-related patterns in the codebase ==="
rg -l "migration" --type sh
Length of output: 3695
Script:
#!/bin/bash
# Look for numbered migration scripts
echo "=== Looking for numbered migration scripts ==="
fd -t f "^[0-9]+$" scripts/migrations
echo "=== Checking migrations directory structure ==="
ls -la scripts/migrations 2>/dev/null || echo "migrations directory not found"
echo "=== Checking if migrations directory exists ==="
if [ -d "scripts/migrations" ]; then
echo "Directory exists"
tree scripts/migrations
else
echo "Directory does not exist"
fi
Length of output: 1023
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.
LGTM
Merged, thanks |
Summary by CodeRabbit
New Features
Bug Fixes