-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet-DeploymentResource.ps1
216 lines (182 loc) · 7.55 KB
/
Set-DeploymentResource.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env pwsh
#Requires -Version 6 -Module powershell-yaml
#========================================
# NAME : Set-DeploymentResource.ps1
# LANGUAGE : Microsoft PowerShell Core
# AUTHOR : Bryan Dady
# UPDATED : 2020-07-02
# COMMENT : Calculate and set/update values in Kubernetes values*.yaml files
#========================================
[CmdletBinding()]
param (
[parameter(Mandatory,
Position = 0,
HelpMessage='Provide the path to the root of the Kubernetes project repository.'
)]
[ValidateScript({Test-Path -Path $PSItem})]
[Alias('Project','Repository')]
[String]$Path
)
Set-StrictMode -Version latest
# Uncomment the following 2 lines for testing profile scripts with Verbose output
#'$VerbosePreference = ''Continue'''
#$VerbosePreference = 'Continue'
# Only call (and use results from Get-IsVerbose function, if it was loaded from ./Bootstrap.ps1)
if (Test-Path -Path Function:\Get-IsVerbose) {
Get-IsVerbose
}
# Only call (and use results from Get-MyScriptInfo function, if it was loaded from ./Bootstrap.ps1)
if (Test-Path -Path Function:\Get-MyScriptInfo) {
$MyScriptInfo = Get-MyScriptInfo($MyInvocation) -Verbose
if ($IsVerbose) { $MyScriptInfo }
}
if ($MyScriptInfo) {
Write-Output -InputObject (' # Start of {0} #' -f $MyScriptInfo.CommandName)
} else {
Write-Output -InputObject (' # Start of {0} #' -f $MyInvocation.MyCommand.Name)
}
Write-Verbose -Message 'Declaring Function Get-YAMLfromFile'
Function Get-YAMLfromFile {
[Cmdletbinding(SupportsShouldProcess)]
param (
[parameter(Mandatory,
Position = 0,
HelpMessage='Provide the path to the file from which to read YAML.',
ValueFromPipeline
)]
[ValidateScript({Test-Path -Path $PSItem})]
[Alias('File','FilePath')]
[String]$Path
)
$YAML = Get-Content -Path $Path | ConvertFrom-Yaml
# Do we need to validate the YAML before returning?
# $YAML.GetType().Name = Hashtable
# $YAML.Count -gt 1
# $YAML.Keys should include 'deployment'
if ($YAML.Count -gt 1) {
Return $YAML
} else {
Write-Warning -Message 'Invalid YAML'
Return $null
}
}
Write-Verbose -Message 'Declaring Function Set-YAMLtoFile'
Function Set-YAMLtoFile {
[Cmdletbinding(SupportsShouldProcess)]
param (
[parameter(Mandatory,
Position = 0,
HelpMessage='Provide the YAML content to be written.',
ValueFromPipeline
)]
[ValidateNotNullOrEmpty()]
[Alias('Object')]
[String]$Content,
[parameter(Mandatory,
Position = 1,
HelpMessage='Provide the path to the file to write $Content.',
ValueFromPipeline
)]
[ValidateScript({Test-Path -Path $PSItem})]
[Alias('File','FilePath')]
[String]$Path
)
$Content | ConvertTo-Yaml | Set-Content -Path $Path
# Do we need to validate the YAML was written before returning?
# $YAML.GetType().Name = Hashtable
# $YAML.Count -gt 1
# $YAML.Keys should include 'deployment'
if ($YAML.Count -gt 1) {
Return $YAML
} else {
Write-Warning -Message 'Invalid YAML'
Return $null
}
}
# Load current values from existing files into variables
# Confirm the expected file/folder structure in the project repository
Write-Verbose -Message ('Opening project {0}' -f $Path)
$ProjectPath = Get-Item -Path $Path
# Derive the path to the values files
$ValuesFile = Join-Path -Path $ProjectPath -ChildPath '/chart/values.yaml'
$DevFile = Join-Path -Path $ProjectPath -ChildPath '/chart/values-dev.yaml'
$StageFile = Join-Path -Path $ProjectPath -ChildPath '/chart/values-stage.yaml'
$ProdFile = Join-Path -Path $ProjectPath -ChildPath '/chart/values-prod.yaml'
# Use function to read yaml from file
$Shared = Get-YAMLfromFile -Path $ValuesFile
$Dev = Get-YAMLfromFile -Path $DevFile
$Stage = Get-YAMLfromFile -Path $StageFile
$Prod = Get-YAMLfromFile -Path $ProdFile
# Get the root / 'shared' autoscaling and deployment values
$Shared.autoscaling.enable # should always be true
Write-Verbose -Message ('$Shared.autoscaling.enable: {0}' -f $Shared.autoscaling.enable)
$s_rep_min = $Shared.autoscaling.minReplicas
$s_rep_max = $Shared.autoscaling.maxReplicas
$s_rep_qty = $Shared.deployment.replicaCount
$s_cpu_lim = $Shared.deployment.resources.limits.cpu
$s_mem_lim = $Shared.deployment.resources.limits.memory
$s_cpu_req = $Shared.deployment.resources.requests.cpu
$s_mem_req = $Shared.deployment.resources.requests.memory
# Calculate ratio of prod for Stage
# - for now it's in a variable
# Calculate 1/3 of prod for Dev
$d_rep_min = # $Dev.autoscaling.minReplicas
$d_rep_max = # $Dev.autoscaling.maxReplicas
$d_rep_qty = # $Dev.deployment.replicaCount
$d_cpu_lim = # $Dev.deployment.resources.limits.cpu
$d_mem_lim = # $Dev.deployment.resources.limits.memory
$d_cpu_req = # $Dev.deployment.resources.requests.cpu
$d_mem_req = # $Dev.deployment.resources.requests.memory
# Now we can setup some changes
Write-Verbose -Message ('$Dev.deployment.replicaCount was {0}, setting to {1}' -f $s_rep_qty, $d_rep_qty)
$Dev.deployment.replicaCount = $d_rep_qty
Write-Verbose -Message ('$Dev.autoscaling.minReplicas was {0}, setting to {1}' -f $s_rep_min, $d_rep_min)
$Dev.autoscaling.minReplicas = $d_rep_min
Write-Verbose -Message ('$Dev.autoscaling.maxReplicas was {0}, setting to {1}' -f $s_rep_max, $d_rep_max)
$Dev.autoscaling.maxReplicas = $d_rep_max
Write-Verbose -Message ('$Dev.resources.limits.cpu was {0}, setting to {1}' -f $s_cpu_lim, $d_cpu_lim)
$Dev.deployment.resources.limits.cpu = $d_cpu_lim
Write-Verbose -Message ('$Dev.resources.requests.cpu was {0}, setting to {1}' -f $s_cpu_req, $d_cpu_req)
$Dev.deployment.resources.requests.cpu = $d_cpu_req
Write-Verbose -Message ('$Dev.resources.limits.memory was {0}, setting to {1}' -f $s_mem_lim, $d_mem_lim)
$Dev.deployment.resources.limits.memory = $d_mem_lim
Write-Verbose -Message ('$Dev.resources.requests.memory was {0}, setting to {1}' -f $s_mem_req, $d_mem_req)
$Dev.deployment.resources.requests.memory = $d_mem_req
# And finally ... commit the changes to file
$DevTestFile = Join-Path -Path $ProjectPath -ChildPath '/chart/values-dev-test.yaml'
$StageTestFile = Join-Path -Path $ProjectPath -ChildPath '/chart/values-stage-test.yaml'
<#
.SYNOPSIS
Edit the System PATH statement globally in Windows Powershell with 4 new Advanced functions. Add-EnvPath, Set-EnvPath, Remove-EnvPath, Get-EnvPath - SUPPORTS -whatif parameter
.DESCRIPTION
Adds four new Advanced Functions to allow the ability to edit and Manipulate the System PATH ($Env:Path) from Windows Powershell - Must be run as a Local Administrator
.EXAMPLE
PS C:\> Get-EnvPathFromRegistry
Get Current Path
.EXAMPLE
PS C:\> Add-EnvPath C:\Foldername
Add Folder to Path
.EXAMPLE
PS C:\> Remove-EnvPath C:\Foldername
Remove C:\Foldername from the PATH
.EXAMPLE
PS C:\> Set-EnvPath C:\Foldernam;C:\AnotherFolder
Set the current PATH to the above. WARNING- ERASES ORIGINAL PATH
.NOTES
NAME : Set-EnvPath
VERSION : 1.0
LAST UPDATED: 2/20/2015
AUTHOR : Sean Kearney
# Added 'Test-LocalAdmin' function written by Boe Prox to validate is PowerShell prompt is running in Elevated mode
# Removed lines for correcting path in Add-EnvPath
# Switched Path search to an Array for "Exact Match" searching
# 2/20/2015
.LINK
https://gallery.technet.microsoft.com/3aa9d51a-44af-4d2a-aa44-6ea541a9f721
.LINK
Test-LocalAdmin
.INPUTS
None
.OUTPUTS
None
#>