Skip to content

Commit f2d4f6c

Browse files
authored
Merge pull request #5 from KubeDeckio/helm-values
Add SnapshotHelmUsedValues parameter and update documentation for Hel…
2 parents 86b8bdf + 07e86cf commit f2d4f6c

File tree

5 files changed

+346
-129
lines changed

5 files changed

+346
-129
lines changed

CHANGELOG.md

+15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ 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.10] - 2024-12-04
8+
9+
### Added:
10+
- **`-SnapshotHelmUsedValues`**: Introduced a new switch to capture only the used values (default and user-provided) for Helm releases.
11+
- **Combined Helm Snapshots**: Added support for combining `-SnapshotHelm` and `-SnapshotHelmUsedValues` to capture both full Helm snapshots and used values in a single command.
12+
- **Documentation Enhancements**: Expanded PowerShell and Krew plugin usage documentation to reflect the new Helm-related functionality, including examples for individual and combined switches.
13+
14+
### Fixed:
15+
- **Non-System Namespace Handling**: Resolved issues with `AllNonSystemNamespaces` when processing Helm snapshots, ensuring proper filtering of system namespaces.
16+
- **Error Handling in Helm Commands**: Improved error handling for `helm` commands to provide clearer feedback when commands fail or outputs are invalid.
17+
18+
### Changed:
19+
- **Output File Naming**: Standardized file naming conventions for Helm snapshots to include timestamps and clearly distinguish between full snapshots and used values.
20+
- **Verbose Mode Improvements**: Enhanced verbosity messages to provide better visibility into the snapshotting process, particularly for Helm-related operations.
21+
722
## [0.0.9] - 2024-11-04
823

924
### Fixed:

KubeSnapIt.psm1

+62-1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ function Invoke-KubeSnapIt {
8686
[switch]$Force,
8787
[switch]$UI,
8888
[switch]$SnapshotHelm,
89+
[switch]$SnapshotHelmUsedValues,
8990
[Alias("h")] [switch]$Help
9091
)
9192
# END PARAM BLOCK
@@ -108,6 +109,7 @@ function Invoke-KubeSnapIt {
108109
Write-Host " -CompareSnapshots Compare two snapshots."
109110
Write-Host " -Force Force the action without prompting for confirmation."
110111
Write-Host " -SnapshotHelm Backup Helm releases and their values."
112+
Write-Host " -SnapshotHelmUsedValues Backup Helm release values."
111113
Write-Host " -Help Display this help message."
112114
return
113115
}
@@ -193,7 +195,66 @@ function Invoke-KubeSnapIt {
193195

194196
# Helm backup function call
195197
try {
196-
Save-HelmBackup -Namespace $Namespace -AllNamespaces:$AllNamespaces -AllNonSystemNamespaces:$AllNonSystemNamespaces -OutputPath $OutputPath -DryRun:$DryRun -Verbose:$Verbose
198+
Save-HelmBackup -Namespace $Namespace -AllNamespaces:$AllNamespaces -AllNonSystemNamespaces:$AllNonSystemNamespaces -OutputPath $OutputPath -DryRun:$DryRun -Verbose:$Verbose -SnapshotHelm
199+
}
200+
catch {
201+
Write-Host "Error during Helm backup: $_" -ForegroundColor Red
202+
}
203+
return
204+
}
205+
206+
# Only Helm used values snapshot
207+
{ $SnapshotHelmUsedValues } {
208+
if (-not ($Namespace -or $AllNamespaces -or $AllNonSystemNamespaces)) {
209+
Write-Host "Error: -Namespace, -AllNamespaces, or -AllNonSystemNamespaces is required with -SnapshotHelmUsedValues." -ForegroundColor Red
210+
return
211+
}
212+
if (-not (Test-Path -Path $OutputPath)) {
213+
New-Item -Path $OutputPath -ItemType Directory -Force | Out-Null
214+
Write-Verbose "Output directory created: $OutputPath"
215+
}
216+
Write-Verbose "Starting Helm used values backup..."
217+
if ($DryRun) { Write-Host "Dry run enabled. No files will be saved." -ForegroundColor Yellow }
218+
219+
try {
220+
Save-HelmBackup `
221+
-Namespace $Namespace `
222+
-AllNamespaces:$AllNamespaces `
223+
-AllNonSystemNamespaces:$AllNonSystemNamespaces `
224+
-OutputPath $OutputPath `
225+
-DryRun:$DryRun `
226+
-Verbose:$Verbose `
227+
-SnapshotHelmUsedValues
228+
}
229+
catch {
230+
Write-Host "Error during Helm used values backup: $_" -ForegroundColor Red
231+
}
232+
return
233+
}
234+
235+
# Only Helm used values snapshot
236+
{ $SnapshotHelmUsedValues -and $SnapshotHelm } {
237+
if (-not ($Namespace -or $AllNamespaces -or $AllNonSystemNamespaces)) {
238+
Write-Host "Error: -Namespace, -AllNamespaces, or -AllNonSystemNamespaces is required with -SnapshotHelm and -SnapshotHelmUsedValues." -ForegroundColor Red
239+
return
240+
}
241+
if (-not (Test-Path -Path $OutputPath)) {
242+
New-Item -Path $OutputPath -ItemType Directory -Force | Out-Null
243+
Write-Verbose "Output directory created: $OutputPath"
244+
}
245+
Write-Verbose "Starting Helm backup..."
246+
if ($DryRun) { Write-Host "Dry run enabled. No files will be saved." -ForegroundColor Yellow }
247+
248+
try {
249+
Save-HelmBackup `
250+
-Namespace $Namespace `
251+
-AllNamespaces:$AllNamespaces `
252+
-AllNonSystemNamespaces:$AllNonSystemNamespaces `
253+
-OutputPath $OutputPath `
254+
-DryRun:$DryRun `
255+
-Verbose:$Verbose `
256+
-SnapshotHelm `
257+
-SnapshotHelmUsedValues
197258
}
198259
catch {
199260
Write-Host "Error during Helm backup: $_" -ForegroundColor Red

Private/Save-HelmBackup.ps1

+86-68
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ function Save-HelmBackup {
55
[switch]$DryRun,
66
[switch]$Verbose,
77
[switch]$AllNamespaces,
8-
[switch]$AllNonSystemNamespaces
8+
[switch]$AllNonSystemNamespaces,
9+
[switch]$SnapshotHelm, # Perform a full Helm snapshot
10+
[switch]$SnapshotHelmUsedValues # Perform only Helm used values snapshot
911
)
1012

1113
# Set the verbose preference based on the switch
@@ -29,10 +31,10 @@ function Save-HelmBackup {
2931
return
3032
}
3133

32-
# Function to run helm command and return output
34+
# Function to run Helm command and return output
3335
function Invoke-HelmCommand {
3436
param (
35-
[string]$command # helm command without the 'helm' part
37+
[string]$command # Helm command without the 'helm' part
3638
)
3739
$processInfo = New-Object System.Diagnostics.ProcessStartInfo
3840
$processInfo.FileName = "helm"
@@ -45,7 +47,7 @@ function Save-HelmBackup {
4547
$process = New-Object System.Diagnostics.Process
4648
$process.StartInfo = $processInfo
4749

48-
# Start the helm process
50+
# Start the Helm process
4951
$process.Start() | Out-Null
5052

5153
# Capture output and error
@@ -54,14 +56,33 @@ function Save-HelmBackup {
5456
$process.WaitForExit()
5557

5658
if ($stderr) {
57-
Write-Host "Error running helm command: $stderr" -ForegroundColor Red
59+
Write-Host "Error running Helm command: $stderr" -ForegroundColor Red
5860
}
5961

6062
return $output
6163
}
6264

65+
# Function to fetch used values
66+
function Get-HelmUsedValues {
67+
param (
68+
[string]$ReleaseName,
69+
[string]$NamespaceOption
70+
)
71+
72+
$helmUsedValuesCmd = "get values $ReleaseName $NamespaceOption --all -o yaml"
73+
Write-Verbose "Running command: helm $helmUsedValuesCmd"
74+
$usedValuesOutput = Invoke-HelmCommand $helmUsedValuesCmd
75+
76+
if ($usedValuesOutput) {
77+
return $usedValuesOutput
78+
} else {
79+
Write-Host "No used values found for release '$ReleaseName'." -ForegroundColor Yellow
80+
return $null
81+
}
82+
}
83+
6384
# Function to process a namespace
64-
function Process-Namespace {
85+
function Backup-HelmNamespace {
6586
param (
6687
[string]$Namespace
6788
)
@@ -91,49 +112,49 @@ function Save-HelmBackup {
91112
if ($DryRun) {
92113
Write-Host "Dry run: Found Helm release '$releaseName' in namespace '$releaseNamespace'."
93114
} else {
94-
# Fetch values for each release
95-
$helmGetValuesCmd = "get values $releaseName $namespaceOption -o yaml"
96-
Write-Verbose "Running command: helm $helmGetValuesCmd"
97-
$valuesOutput = Invoke-HelmCommand $helmGetValuesCmd
98-
99-
if ($valuesOutput) {
100-
# Sanitize the release name by replacing invalid characters with underscores
101-
$safeReleaseName = $releaseName -replace "[:\\/]", "_"
102-
103-
# Generate a filename based on the release name and timestamp
104-
$valuesFile = Join-Path -Path $OutputPath -ChildPath "${safeReleaseName}_values_$timestamp.yaml"
105-
106-
Write-Verbose "Saving Helm release '$releaseName' values to: $valuesFile"
107-
$valuesOutput | Out-File -FilePath $valuesFile -Force
108-
Write-Host "Helm release '$releaseName' values saved: $valuesFile" -ForegroundColor Green
109-
}
110-
111-
# Fetch manifest for each release
112-
$helmGetManifestCmd = "get manifest $releaseName $namespaceOption"
113-
Write-Verbose "Running command: helm $helmGetManifestCmd"
114-
$manifestOutput = Invoke-HelmCommand $helmGetManifestCmd
115-
116-
if ($manifestOutput) {
117-
# Generate a filename based on the release name and timestamp
118-
$manifestFile = Join-Path -Path $OutputPath -ChildPath "${safeReleaseName}_manifest_$timestamp.yaml"
119-
120-
Write-Verbose "Saving Helm release '$releaseName' manifest to: $manifestFile"
121-
$manifestOutput | Out-File -FilePath $manifestFile -Force
122-
Write-Host "Helm release '$releaseName' manifest saved: $manifestFile" -ForegroundColor Green
115+
# Full Helm snapshot (values, manifest, and history)
116+
if ($SnapshotHelm) {
117+
# Fetch values
118+
$helmGetValuesCmd = "get values $releaseName $namespaceOption -o yaml"
119+
Write-Verbose "Running command: helm $helmGetValuesCmd"
120+
$valuesOutput = Invoke-HelmCommand $helmGetValuesCmd
121+
if ($valuesOutput) {
122+
$safeReleaseName = $releaseName -replace "[:\\/]", "_"
123+
$valuesFile = Join-Path -Path $OutputPath -ChildPath "${safeReleaseName}_values_$timestamp.yaml"
124+
$valuesOutput | Out-File -FilePath $valuesFile -Force
125+
Write-Host "Helm release '$releaseName' values saved: $valuesFile" -ForegroundColor Green
126+
}
127+
128+
# Fetch manifest
129+
$helmGetManifestCmd = "get manifest $releaseName $namespaceOption"
130+
Write-Verbose "Running command: helm $helmGetManifestCmd"
131+
$manifestOutput = Invoke-HelmCommand $helmGetManifestCmd
132+
if ($manifestOutput) {
133+
$manifestFile = Join-Path -Path $OutputPath -ChildPath "${safeReleaseName}_manifest_$timestamp.yaml"
134+
$manifestOutput | Out-File -FilePath $manifestFile -Force
135+
Write-Host "Helm release '$releaseName' manifest saved: $manifestFile" -ForegroundColor Green
136+
}
137+
138+
# Fetch history
139+
$helmHistoryCmd = "history $releaseName $namespaceOption --output yaml"
140+
Write-Verbose "Running command: helm $helmHistoryCmd"
141+
$historyOutput = Invoke-HelmCommand $helmHistoryCmd
142+
if ($historyOutput) {
143+
$historyFile = Join-Path -Path $OutputPath -ChildPath "${safeReleaseName}_history_$timestamp.yaml"
144+
$historyOutput | Out-File -FilePath $historyFile -Force
145+
Write-Host "Helm release '$releaseName' history saved: $historyFile" -ForegroundColor Green
146+
}
123147
}
124148

125-
# Fetch release history for each release
126-
$helmHistoryCmd = "history $releaseName $namespaceOption --output yaml"
127-
Write-Verbose "Running command: helm $helmHistoryCmd"
128-
$historyOutput = Invoke-HelmCommand $helmHistoryCmd
129-
130-
if ($historyOutput) {
131-
# Generate a filename based on the release name and timestamp
132-
$historyFile = Join-Path -Path $OutputPath -ChildPath "${safeReleaseName}_history_$timestamp.yaml"
133-
134-
Write-Verbose "Saving Helm release '$releaseName' history to: $historyFile"
135-
$historyOutput | Out-File -FilePath $historyFile -Force
136-
Write-Host "Helm release '$releaseName' history saved: $historyFile" -ForegroundColor Green
149+
# Only Helm used values snapshot
150+
if ($SnapshotHelmUsedValues) {
151+
$usedValuesOutput = Get-HelmUsedValues -ReleaseName $releaseName -NamespaceOption $namespaceOption
152+
if ($usedValuesOutput) {
153+
$safeReleaseName = $releaseName -replace "[:\\/]", "_"
154+
$usedValuesFile = Join-Path -Path $OutputPath -ChildPath "${safeReleaseName}_used_values_$timestamp.yaml"
155+
$usedValuesOutput | Out-File -FilePath $usedValuesFile -Force
156+
Write-Host "Helm release '$releaseName' used values saved: $usedValuesFile" -ForegroundColor Green
157+
}
137158
}
138159
}
139160
}
@@ -143,28 +164,25 @@ function Save-HelmBackup {
143164
}
144165

145166
try {
146-
# Determine the namespace option
147-
if ($AllNamespaces) {
148-
# Get all namespaces
149-
$namespaces = kubectl get namespaces -o json | ConvertFrom-Json
150-
$allClusterNamespaces = $namespaces.items | ForEach-Object { $_.metadata.name }
151-
} elseif ($AllNonSystemNamespaces) {
152-
# Get all non-system namespaces
153-
$namespaces = kubectl get namespaces -o json | ConvertFrom-Json
154-
$allClusterNamespaces = $namespaces.items | Where-Object { $_.metadata.name -notmatch "^(kube-system|kube-public|kube-node-lease|default)$" } | ForEach-Object { $_.metadata.name }
155-
} elseif ($Namespace) {
156-
$allClusterNamespaces = @($Namespace)
157-
} else {
158-
Write-Host "No namespace specified." -ForegroundColor Red
159-
return
160-
}
161-
162-
# Process each namespace
163-
$allClusterNamespaces | ForEach-Object {
164-
Write-Host "Processing namespace: $_"
165-
Process-Namespace -Namespace $_
166-
}
167+
# Determine the namespace option
168+
if ($AllNamespaces) {
169+
$namespaces = kubectl get namespaces -o json | ConvertFrom-Json
170+
$allClusterNamespaces = $namespaces.items | ForEach-Object { $_.metadata.name }
171+
} elseif ($AllNonSystemNamespaces) {
172+
$namespaces = kubectl get namespaces -o json | ConvertFrom-Json
173+
$allClusterNamespaces = $namespaces.items | Where-Object { $_.metadata.name -notmatch "^(kube-system|kube-public|kube-node-lease|default)$" } | ForEach-Object { $_.metadata.name }
174+
} elseif ($Namespace) {
175+
$allClusterNamespaces = @($Namespace)
176+
} else {
177+
Write-Host "No namespace specified." -ForegroundColor Red
178+
return
179+
}
167180

181+
# Process each namespace
182+
$allClusterNamespaces | ForEach-Object {
183+
Write-Host "Processing namespace: $_"
184+
Backup-HelmNamespace -Namespace $_
185+
}
168186
}
169187
catch {
170188
Write-Host "Error occurred while backing up Helm releases: $_" -ForegroundColor Red

0 commit comments

Comments
 (0)