Skip to content

Commit

Permalink
Update CHANGELOG and improve parameter validation for SnapshotHelm fu…
Browse files Browse the repository at this point in the history
…nctionality

Signed-off-by: PixelRobots <22979170+PixelRobots@users.noreply.github.com>
  • Loading branch information
PixelRobots committed Nov 4, 2024
1 parent 1f0d0b3 commit ddeaaa7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 49 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.0.8] - 2024-11-01

### Fixed:
- Resolved an issue where `SnapshotHelm` could be triggered without specifying a namespace-related option. Now requires either `-Namespace`, `-AllNamespaces`, or `-AllNonSystemNamespaces` when `SnapshotHelm` is used.
- Improved parameter validation to prevent execution without necessary inputs, reducing potential errors in Helm snapshot and comparison functions.

## [0.0.7] - 2024-11-01

### Added
Expand Down
86 changes: 38 additions & 48 deletions KubeSnapIt.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ if (-not $foundScripts -and (Test-Path -Path $krewStorageDir)) {
. $script.FullName # Call the script
$foundScripts = $true
}
} else {
}
else {
Write-Verbose "No Private directory found for the latest version: $($latestVersionDir.Name)."
}
} else {
}
else {
Write-Verbose "No version directories found in $krewStorageDir."
}
}
Expand All @@ -79,8 +81,8 @@ function Invoke-KubeSnapIt {
[string]$Objects = "",
[switch]$DryRun,
[switch]$Restore,
[switch]$CompareWithCluster, # Switch for comparing with the cluster
[switch]$CompareSnapshots, # Switch for comparing two snapshots
[switch]$CompareWithCluster,
[switch]$CompareSnapshots,
[switch]$Force,
[switch]$UI,
[switch]$SnapshotHelm,
Expand All @@ -105,7 +107,7 @@ function Invoke-KubeSnapIt {
Write-Host " -CompareWithCluster Compare a snapshot with the current cluster state."
Write-Host " -CompareSnapshots Compare two snapshots."
Write-Host " -Force Force the action without prompting for confirmation."
Write-Host " -SnapshotHelm Backup Helm releases and their values."
Write-Host " -SnapshotHelm Backup Helm releases and their values."
Write-Host " -Help Display this help message."
return
}
Expand Down Expand Up @@ -139,7 +141,8 @@ function Invoke-KubeSnapIt {
Write-Host ""
Write-Host "=========================================" -ForegroundColor Cyan
Write-Host ""
} else {
}
else {
Write-Host "You are not connected to any Kubernetes cluster." -ForegroundColor Red
Write-Host "Please configure a Kubernetes cluster to connect to." -ForegroundColor Red
Write-Host "Instructions to set up a cluster can be found here: https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/" -ForegroundColor Yellow
Expand All @@ -149,88 +152,75 @@ function Invoke-KubeSnapIt {

# Determine the operation type using switch
switch ($true) {
# Handle Restore operation
{ $Restore } {
if (-not $InputPath) {
Write-Host "Error: You must specify an input path for the restore operation." -ForegroundColor Red
Write-Host "Error: Input path required for restore." -ForegroundColor Red
return
}
# Call the Restore-KubeSnapshot function
Restore-KubeSnapshot -InputPath $InputPath -Force $Force -Verbose:$Verbose
return
}

# Handle Compare with Cluster operation
{ $CompareWithCluster } {
if (-not $InputPath) {
Write-Host "Error: You must specify a snapshot path for comparison." -ForegroundColor Red
Write-Host "Error: Snapshot path required for comparison." -ForegroundColor Red
return
}

Write-Verbose "Comparing snapshot with current cluster state: $InputPath"
Compare-Files -LocalFile $InputPath -Verbose:$Verbose
return
}

# Handle Compare Snapshots operation
{ $CompareSnapshots } {
if (-not $InputPath) {
Write-Host "Error: You must specify a snapshot path for comparison." -ForegroundColor Red
if (-not $InputPath -or -not $ComparePath) {
Write-Host "Error: Both -InputPath and -ComparePath required for snapshot comparison." -ForegroundColor Red
return
}
Compare-Files -LocalFile $InputPath -CompareFile $ComparePath -Verbose:$Verbose
return
}

# Ensure ComparePath is provided for snapshot comparison
if (-not [string]::IsNullOrWhiteSpace($ComparePath)) {
Write-Verbose "Comparing two snapshots: $InputPath and $ComparePath"
Compare-Files -LocalFile $InputPath -CompareFile $ComparePath -Verbose:$Verbose
return
} else {
Write-Host "Error: You must specify a second snapshot for comparison (-ComparePath)." -ForegroundColor Red
{ $SnapshotHelm } {
if (-not ($Namespace -or $AllNamespaces -or $AllNonSystemNamespaces)) {
Write-Host "Error: -Namespace, -AllNamespaces, or -AllNonSystemNamespaces is required with -SnapshotHelm." -ForegroundColor Red
return
}
}

# Handle Snapshot operation
{ $Namespace -or $AllNamespaces -or $AllNonSystemNamespaces } {
# Set output path and create directory if it doesn't exist
if (-not (Test-Path -Path $OutputPath)) {
New-Item -Path $OutputPath -ItemType Directory -Force | Out-Null
Write-Verbose "Output directory created: $OutputPath"
}
Write-Verbose "Starting Helm backup..."
if ($DryRun) { Write-Host "Dry run enabled. No files will be saved." -ForegroundColor Yellow }

Write-Verbose "Starting snapshot process..."
if ($DryRun) {
Write-Host "Dry run enabled. No files will be saved." -ForegroundColor Yellow
}

# Call the snapshot function
# Helm backup function call
try {
Save-KubeSnapshot -Namespace $Namespace -AllNamespaces:$AllNamespaces -AllNonSystemNamespaces:$AllNonSystemNamespaces -Labels $Labels -Objects $Objects -OutputPath $OutputPath -DryRun:$DryRun -Verbose:$Verbose
} catch {
Write-Host "Error occurred during the snapshot process: $_" -ForegroundColor Red
Save-HelmBackup -Namespace $Namespace -AllNamespaces:$AllNamespaces -AllNonSystemNamespaces:$AllNonSystemNamespaces -OutputPath $OutputPath -DryRun:$DryRun -Verbose:$Verbose
}
catch {
Write-Host "Error during Helm backup: $_" -ForegroundColor Red
}
return
}

# Handle Helm backup operation
{ $SnapshotHelm } {
Write-Verbose "Starting Helm backup process..."
if ($DryRun) {
Write-Host "Dry run enabled. No files will be saved." -ForegroundColor Yellow
{ $Namespace -or $AllNamespaces -or $AllNonSystemNamespaces } {
if (-not (Test-Path -Path $OutputPath)) {
New-Item -Path $OutputPath -ItemType Directory -Force | Out-Null
Write-Verbose "Output directory created: $OutputPath"
}
Write-Verbose "Starting snapshot..."
if ($DryRun) { Write-Host "Dry run enabled. No files will be saved." -ForegroundColor Yellow }

# Call the Helm backup function
# Snapshot function call
try {
Save-HelmBackup -Namespace $Namespace -AllNamespaces:$AllNamespaces -AllNonSystemNamespaces:$AllNonSystemNamespaces -OutputPath $OutputPath -DryRun:$DryRun -Verbose:$Verbose
} catch {
Write-Host "Error occurred during the Helm backup process: $_" -ForegroundColor Red
Save-KubeSnapshot -Namespace $Namespace -AllNamespaces:$AllNamespaces -AllNonSystemNamespaces:$AllNonSystemNamespaces -Labels $Labels -Objects $Objects -OutputPath $OutputPath -DryRun:$DryRun -Verbose:$Verbose
}
catch {
Write-Host "Error during snapshot: $_" -ForegroundColor Red
}
return
}

# If none of the operations match, display an error
default {
Write-Host "Error: You must specify either -Restore, -CompareWithCluster, -CompareSnapshots, or -SnapshotHelm with a valid operation." -ForegroundColor Red
Write-Host "Error: Specify -Restore, -CompareWithCluster, -CompareSnapshots, or -SnapshotHelm with necessary parameters." -ForegroundColor Red
return
}
}
Expand Down
2 changes: 1 addition & 1 deletion Private/Save-HelmBackup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function Save-HelmBackup {
Write-Host "Dry run: Found Helm release '$releaseName' in namespace '$releaseNamespace'."
} else {
# Fetch values for each release
$helmGetValuesCmd = "get values $releaseName $namespaceOption"
$helmGetValuesCmd = "get values $releaseName $namespaceOption -o yaml"
Write-Verbose "Running command: helm $helmGetValuesCmd"
$valuesOutput = Invoke-HelmCommand $helmGetValuesCmd

Expand Down

0 comments on commit ddeaaa7

Please sign in to comment.