Skip to content

Commit ddeaaa7

Browse files
committed
Update CHANGELOG and improve parameter validation for SnapshotHelm functionality
Signed-off-by: PixelRobots <[email protected]>
1 parent 1f0d0b3 commit ddeaaa7

File tree

3 files changed

+45
-49
lines changed

3 files changed

+45
-49
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
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).
66

7+
## [0.0.8] - 2024-11-01
8+
9+
### Fixed:
10+
- 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.
11+
- Improved parameter validation to prevent execution without necessary inputs, reducing potential errors in Helm snapshot and comparison functions.
12+
713
## [0.0.7] - 2024-11-01
814

915
### Added

KubeSnapIt.psm1

+38-48
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@ if (-not $foundScripts -and (Test-Path -Path $krewStorageDir)) {
5151
. $script.FullName # Call the script
5252
$foundScripts = $true
5353
}
54-
} else {
54+
}
55+
else {
5556
Write-Verbose "No Private directory found for the latest version: $($latestVersionDir.Name)."
5657
}
57-
} else {
58+
}
59+
else {
5860
Write-Verbose "No version directories found in $krewStorageDir."
5961
}
6062
}
@@ -79,8 +81,8 @@ function Invoke-KubeSnapIt {
7981
[string]$Objects = "",
8082
[switch]$DryRun,
8183
[switch]$Restore,
82-
[switch]$CompareWithCluster, # Switch for comparing with the cluster
83-
[switch]$CompareSnapshots, # Switch for comparing two snapshots
84+
[switch]$CompareWithCluster,
85+
[switch]$CompareSnapshots,
8486
[switch]$Force,
8587
[switch]$UI,
8688
[switch]$SnapshotHelm,
@@ -105,7 +107,7 @@ function Invoke-KubeSnapIt {
105107
Write-Host " -CompareWithCluster Compare a snapshot with the current cluster state."
106108
Write-Host " -CompareSnapshots Compare two snapshots."
107109
Write-Host " -Force Force the action without prompting for confirmation."
108-
Write-Host " -SnapshotHelm Backup Helm releases and their values."
110+
Write-Host " -SnapshotHelm Backup Helm releases and their values."
109111
Write-Host " -Help Display this help message."
110112
return
111113
}
@@ -139,7 +141,8 @@ function Invoke-KubeSnapIt {
139141
Write-Host ""
140142
Write-Host "=========================================" -ForegroundColor Cyan
141143
Write-Host ""
142-
} else {
144+
}
145+
else {
143146
Write-Host "You are not connected to any Kubernetes cluster." -ForegroundColor Red
144147
Write-Host "Please configure a Kubernetes cluster to connect to." -ForegroundColor Red
145148
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
@@ -149,88 +152,75 @@ function Invoke-KubeSnapIt {
149152

150153
# Determine the operation type using switch
151154
switch ($true) {
152-
# Handle Restore operation
153155
{ $Restore } {
154156
if (-not $InputPath) {
155-
Write-Host "Error: You must specify an input path for the restore operation." -ForegroundColor Red
157+
Write-Host "Error: Input path required for restore." -ForegroundColor Red
156158
return
157159
}
158-
# Call the Restore-KubeSnapshot function
159160
Restore-KubeSnapshot -InputPath $InputPath -Force $Force -Verbose:$Verbose
160161
return
161162
}
162163

163-
# Handle Compare with Cluster operation
164164
{ $CompareWithCluster } {
165165
if (-not $InputPath) {
166-
Write-Host "Error: You must specify a snapshot path for comparison." -ForegroundColor Red
166+
Write-Host "Error: Snapshot path required for comparison." -ForegroundColor Red
167167
return
168168
}
169-
170-
Write-Verbose "Comparing snapshot with current cluster state: $InputPath"
171169
Compare-Files -LocalFile $InputPath -Verbose:$Verbose
172170
return
173171
}
174172

175-
# Handle Compare Snapshots operation
176173
{ $CompareSnapshots } {
177-
if (-not $InputPath) {
178-
Write-Host "Error: You must specify a snapshot path for comparison." -ForegroundColor Red
174+
if (-not $InputPath -or -not $ComparePath) {
175+
Write-Host "Error: Both -InputPath and -ComparePath required for snapshot comparison." -ForegroundColor Red
179176
return
180177
}
178+
Compare-Files -LocalFile $InputPath -CompareFile $ComparePath -Verbose:$Verbose
179+
return
180+
}
181181

182-
# Ensure ComparePath is provided for snapshot comparison
183-
if (-not [string]::IsNullOrWhiteSpace($ComparePath)) {
184-
Write-Verbose "Comparing two snapshots: $InputPath and $ComparePath"
185-
Compare-Files -LocalFile $InputPath -CompareFile $ComparePath -Verbose:$Verbose
186-
return
187-
} else {
188-
Write-Host "Error: You must specify a second snapshot for comparison (-ComparePath)." -ForegroundColor Red
182+
{ $SnapshotHelm } {
183+
if (-not ($Namespace -or $AllNamespaces -or $AllNonSystemNamespaces)) {
184+
Write-Host "Error: -Namespace, -AllNamespaces, or -AllNonSystemNamespaces is required with -SnapshotHelm." -ForegroundColor Red
189185
return
190186
}
191-
}
192-
193-
# Handle Snapshot operation
194-
{ $Namespace -or $AllNamespaces -or $AllNonSystemNamespaces } {
195-
# Set output path and create directory if it doesn't exist
196187
if (-not (Test-Path -Path $OutputPath)) {
197188
New-Item -Path $OutputPath -ItemType Directory -Force | Out-Null
198189
Write-Verbose "Output directory created: $OutputPath"
199190
}
191+
Write-Verbose "Starting Helm backup..."
192+
if ($DryRun) { Write-Host "Dry run enabled. No files will be saved." -ForegroundColor Yellow }
200193

201-
Write-Verbose "Starting snapshot process..."
202-
if ($DryRun) {
203-
Write-Host "Dry run enabled. No files will be saved." -ForegroundColor Yellow
204-
}
205-
206-
# Call the snapshot function
194+
# Helm backup function call
207195
try {
208-
Save-KubeSnapshot -Namespace $Namespace -AllNamespaces:$AllNamespaces -AllNonSystemNamespaces:$AllNonSystemNamespaces -Labels $Labels -Objects $Objects -OutputPath $OutputPath -DryRun:$DryRun -Verbose:$Verbose
209-
} catch {
210-
Write-Host "Error occurred during the snapshot process: $_" -ForegroundColor Red
196+
Save-HelmBackup -Namespace $Namespace -AllNamespaces:$AllNamespaces -AllNonSystemNamespaces:$AllNonSystemNamespaces -OutputPath $OutputPath -DryRun:$DryRun -Verbose:$Verbose
197+
}
198+
catch {
199+
Write-Host "Error during Helm backup: $_" -ForegroundColor Red
211200
}
212201
return
213202
}
214203

215-
# Handle Helm backup operation
216-
{ $SnapshotHelm } {
217-
Write-Verbose "Starting Helm backup process..."
218-
if ($DryRun) {
219-
Write-Host "Dry run enabled. No files will be saved." -ForegroundColor Yellow
204+
{ $Namespace -or $AllNamespaces -or $AllNonSystemNamespaces } {
205+
if (-not (Test-Path -Path $OutputPath)) {
206+
New-Item -Path $OutputPath -ItemType Directory -Force | Out-Null
207+
Write-Verbose "Output directory created: $OutputPath"
220208
}
209+
Write-Verbose "Starting snapshot..."
210+
if ($DryRun) { Write-Host "Dry run enabled. No files will be saved." -ForegroundColor Yellow }
221211

222-
# Call the Helm backup function
212+
# Snapshot function call
223213
try {
224-
Save-HelmBackup -Namespace $Namespace -AllNamespaces:$AllNamespaces -AllNonSystemNamespaces:$AllNonSystemNamespaces -OutputPath $OutputPath -DryRun:$DryRun -Verbose:$Verbose
225-
} catch {
226-
Write-Host "Error occurred during the Helm backup process: $_" -ForegroundColor Red
214+
Save-KubeSnapshot -Namespace $Namespace -AllNamespaces:$AllNamespaces -AllNonSystemNamespaces:$AllNonSystemNamespaces -Labels $Labels -Objects $Objects -OutputPath $OutputPath -DryRun:$DryRun -Verbose:$Verbose
215+
}
216+
catch {
217+
Write-Host "Error during snapshot: $_" -ForegroundColor Red
227218
}
228219
return
229220
}
230221

231-
# If none of the operations match, display an error
232222
default {
233-
Write-Host "Error: You must specify either -Restore, -CompareWithCluster, -CompareSnapshots, or -SnapshotHelm with a valid operation." -ForegroundColor Red
223+
Write-Host "Error: Specify -Restore, -CompareWithCluster, -CompareSnapshots, or -SnapshotHelm with necessary parameters." -ForegroundColor Red
234224
return
235225
}
236226
}

Private/Save-HelmBackup.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function Save-HelmBackup {
9292
Write-Host "Dry run: Found Helm release '$releaseName' in namespace '$releaseNamespace'."
9393
} else {
9494
# Fetch values for each release
95-
$helmGetValuesCmd = "get values $releaseName $namespaceOption"
95+
$helmGetValuesCmd = "get values $releaseName $namespaceOption -o yaml"
9696
Write-Verbose "Running command: helm $helmGetValuesCmd"
9797
$valuesOutput = Invoke-HelmCommand $helmGetValuesCmd
9898

0 commit comments

Comments
 (0)