6868}
6969
7070function 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