Skip to content

Commit fafa2bc

Browse files
committed
Refactor output formatting in Compare-KubeSnapshots.ps1
Signed-off-by: PixelRobots <22979170+PixelRobots@users.noreply.github.com>
1 parent 081d650 commit fafa2bc

File tree

3 files changed

+73
-78
lines changed

3 files changed

+73
-78
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ 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.4] - 2024-10-22
8+
9+
### Added
10+
- **Separate Comparison Switches**: Introduced two distinct switches for comparison:
11+
- `-CompareWithCluster`: Allows users to compare a snapshot against the current state of the Kubernetes cluster without requiring a namespace.
12+
- `-CompareSnapshots`: Allows users to compare two snapshots, enabling more flexible comparison options.
13+
14+
### Changed
15+
- **Refined Operation Logic**: Improved the internal logic to handle operations more clearly using `switch` statements, ensuring that each operation (Snapshot, Compare, Restore) is handled independently without unnecessary dependencies.
16+
- **Improved Error Messaging**: Enhanced error messages for missing required parameters specific to each operation, providing clearer feedback to users.
17+
18+
### Fixed
19+
- **Namespace Requirement for Compare Operations**: Resolved the issue where comparing snapshots against the cluster incorrectly enforced namespace requirements, allowing users to perform comparisons without specifying a namespace.
20+
721
## [0.0.3] - 2024-10-22
822

923
### Changed

KubeSnapIt.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = 'KubeSnapIt.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '0.0.3'
15+
ModuleVersion = '0.0.4'
1616

1717
# Supported PSEditions
1818
# CompatiblePSEditions = @()

KubeSnapIt.psm1

Lines changed: 58 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ else {
6868
}
6969

7070
function Invoke-KubeSnapIt {
71-
7271
# START PARAM BLOCK
7372
[CmdletBinding()]
7473
param (
@@ -81,8 +80,8 @@ function Invoke-KubeSnapIt {
8180
[string]$Objects = "",
8281
[switch]$DryRun,
8382
[switch]$Restore,
84-
[switch]$Compare,
85-
[switch]$CompareWithCluster,
83+
[switch]$CompareWithCluster, # Switch for comparing with the cluster
84+
[switch]$CompareSnapshots, # Switch for comparing two snapshots
8685
[switch]$Force,
8786
[Alias("h")] [switch]$Help
8887
)
@@ -101,26 +100,20 @@ function Invoke-KubeSnapIt {
101100
Write-Host " -Objects Comma-separated list of specific objects in the kind/name format (e.g., pod/my-pod, deployment/my-deployment)."
102101
Write-Host " -DryRun Simulate taking or restoring the snapshot without saving or applying files."
103102
Write-Host " -Restore Restore snapshots from the specified directory or file."
104-
Write-Host " -Compare Compare two snapshots or compare a snapshot with the current cluster state."
105-
Write-Host " -CompareWithCluster Compare a snapshot with the current cluster state instead of another snapshot."
103+
Write-Host " -CompareWithCluster Compare a snapshot with the current cluster state."
104+
Write-Host " -CompareSnapshots Compare two snapshots."
106105
Write-Host " -Force Force the action without prompting for confirmation."
107-
Write-Host " -Verbose Show detailed output during the snapshot, restore, or comparison process."
108106
Write-Host " -Help Display this help message."
109107
return
110108
}
111109

112-
# Show-KubeSnapItBanner
113-
114110
# Check if kubectl is installed for actions that require it
115111
if (!(Get-Command "kubectl" -ErrorAction SilentlyContinue)) {
116112
Write-Host "'kubectl' is not installed or not found in your system's PATH." -ForegroundColor Red
117113
Write-Host "Please install 'kubectl' before running KubeSnapIt." -ForegroundColor Red
118114
Write-Host "You can install 'kubectl' from: https://kubernetes.io/docs/tasks/tools/install-kubectl/" -ForegroundColor Yellow
119115
exit
120116
}
121-
else {
122-
Write-Verbose "'kubectl' is installed."
123-
}
124117

125118
# Get the current Kubernetes context
126119
$context = kubectl config current-context
@@ -147,90 +140,78 @@ function Invoke-KubeSnapIt {
147140
exit
148141
}
149142

150-
# Handle Restore operation
151-
if ($Restore) {
152-
if (-not $InputPath) {
153-
Write-Host "Error: You must specify an input path for the restore operation." -ForegroundColor Red
143+
# Determine the operation type using switch
144+
switch ($true) {
145+
# Handle Restore operation
146+
{ $Restore } {
147+
if (-not $InputPath) {
148+
Write-Host "Error: You must specify an input path for the restore operation." -ForegroundColor Red
149+
return
150+
}
151+
# Call the Restore-KubeSnapshot function
152+
Restore-KubeSnapshot -InputPath $InputPath -Force $Force -Verbose:$Verbose
154153
return
155154
}
156155

157-
# Call the Restore-KubeSnapshot function
158-
Restore-KubeSnapshot `
159-
-InputPath $InputPath `
160-
-Force $Force `
161-
-Verbose:$Verbose
162-
return
163-
}
156+
# Handle Compare with Cluster operation
157+
{ $CompareWithCluster } {
158+
if (-not $InputPath) {
159+
Write-Host "Error: You must specify a snapshot path for comparison." -ForegroundColor Red
160+
return
161+
}
164162

165-
# Handle Compare operation
166-
if ($Compare) {
167-
if (-not $InputPath) {
168-
Write-Host "Error: You must specify a snapshot path for comparison." -ForegroundColor Red
163+
Write-Verbose "Comparing snapshot with current cluster state: $InputPath"
164+
Compare-Files -LocalFile $InputPath -Verbose:$Verbose
169165
return
170166
}
171167

172-
# Comparison with the current cluster state
173-
if ($CompareWithCluster) {
174-
Write-Verbose "Comparing snapshot with current cluster state: $InputPath"
175-
Compare-Files `
176-
-LocalFile $InputPath `
177-
-Verbose:$Verbose
178-
}
179-
# Comparison between two snapshots
180-
elseif ($ComparePath) {
181-
Write-Verbose "Comparing two snapshots: $InputPath and $ComparePath"
182-
Compare-Files `
183-
-LocalFile $InputPath `
184-
-CompareFile $ComparePath `
185-
-Verbose:$Verbose
186-
}
187-
else {
188-
Write-Host "Error: You must specify either -CompareWithCluster or a second snapshot for comparison (-ComparePath)." -ForegroundColor Red
168+
# Handle Compare Snapshots operation
169+
{ $CompareSnapshots } {
170+
if (-not $InputPath) {
171+
Write-Host "Error: You must specify a snapshot path for comparison." -ForegroundColor Red
172+
return
173+
}
174+
175+
# Ensure ComparePath is provided for snapshot comparison
176+
if (-not [string]::IsNullOrWhiteSpace($ComparePath)) {
177+
Write-Verbose "Comparing two snapshots: $InputPath and $ComparePath"
178+
Compare-Files -LocalFile $InputPath -CompareFile $ComparePath -Verbose:$Verbose
179+
return
180+
} else {
181+
Write-Host "Error: You must specify a second snapshot for comparison (-ComparePath)." -ForegroundColor Red
182+
return
183+
}
189184
}
190-
return
191-
}
192185

193-
# Handle Snapshot operation (default)
194-
if ($Namespace -or $AllNamespaces) {
195-
# Set output path and create directory if it doesn't exist
196-
try {
186+
# Handle Snapshot operation
187+
{ $Namespace -or $AllNamespaces } {
188+
# Set output path and create directory if it doesn't exist
197189
if (-not (Test-Path -Path $OutputPath)) {
198190
New-Item -Path $OutputPath -ItemType Directory -Force | Out-Null
199191
Write-Verbose "Output directory created: $OutputPath"
200192
}
201-
else {
202-
Write-Verbose "Output directory already exists: $OutputPath"
193+
194+
Write-Verbose "Starting snapshot process..."
195+
if ($DryRun) {
196+
Write-Host "Dry run enabled. No files will be saved." -ForegroundColor Yellow
203197
}
204-
}
205-
catch {
206-
Write-Host "Error creating output directory: $_" -ForegroundColor Red
207-
return # Exit the function if directory creation fails
208-
}
209198

210-
Write-Verbose "Starting snapshot process..."
211-
if ($DryRun) {
212-
Write-Host "Dry run enabled. No files will be saved." -ForegroundColor Yellow
199+
# Call the snapshot function
200+
try {
201+
Save-KubeSnapshot -Namespace $Namespace -AllNamespaces:$AllNamespaces -Labels $Labels -Objects $Objects -OutputPath $OutputPath -DryRun:$DryRun -Verbose:$Verbose
202+
} catch {
203+
Write-Host "Error occurred during the snapshot process: $_" -ForegroundColor Red
204+
}
205+
return
213206
}
214207

215-
# Call the snapshot function
216-
try {
217-
Save-KubeSnapshot `
218-
-Namespace $Namespace `
219-
-AllNamespaces:$AllNamespaces `
220-
-Labels $Labels `
221-
-Objects $Objects `
222-
-OutputPath $OutputPath `
223-
-DryRun:$DryRun `
224-
-Verbose:$Verbose
225-
}
226-
catch {
227-
Write-Host "Error occurred during the snapshot process: $_" -ForegroundColor Red
208+
# If none of the operations match, display an error
209+
default {
210+
Write-Host "Error: You must specify either -Restore, -CompareWithCluster, or -CompareSnapshots with a valid operation." -ForegroundColor Red
211+
return
228212
}
229213
}
230-
else {
231-
Write-Host "Error: You must specify either -Namespace or -AllNamespaces." -ForegroundColor Red
232-
return
233-
}
234214
}
235215

236-
# MARKER: FUNCTION CALL
216+
217+
Invoke-KubeSnapIt -InputPath "C:\Users\rhooper\OneDrive - Intercept\Documents\Git\KubeDeck\snapshot\ConfigMap_task-scheduler-configmap_2024-10-22_15-04-47.yaml" -CompareWithCluster -verbose

0 commit comments

Comments
 (0)