-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge-KubeConfig.ps1
More file actions
70 lines (58 loc) · 1.96 KB
/
Merge-KubeConfig.ps1
File metadata and controls
70 lines (58 loc) · 1.96 KB
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
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]
$Source,
[Parameter(Mandatory = $true)]
[string]
$Target
)
Import-Module -Name powershell-yaml
$sourceContent = Get-Content -Path $Source -Raw
$targetContent = Get-Content -Path $Target -Raw
$sourceYaml = ConvertFrom-Yaml -Yaml $sourceContent -Ordered
$targetYaml = ConvertFrom-Yaml -Yaml $targetContent -Ordered
if ( $sourceYaml['kind'] -ne 'Config') {
throw 'Source is not kubectl config file'
}
if ( $targetYaml['kind'] -ne 'Config') {
throw 'Target is not kubectl config file'
}
if ($sourceYaml['apiVersion'] -ne $targetYaml['apiVersion']) {
throw 'API Version is not the same'
}
foreach ($sourceUser in $sourceYaml['users']) {
if ($sourceUser['name'] -ne 'docker-desktop') {
$targetUser = $targetYaml['users'] | Where-Object { $_['name'] -eq $sourceUser['name'] }
if ($null -eq $targetUser) {
$targetYaml['users'] += $sourceUser
}
else {
$targetUser['user'] = $sourceUser['user']
}
}
}
foreach ($sourceCluster in $sourceYaml['clusters']) {
if ($sourceCluster['name'] -ne 'docker-desktop') {
$targetCluster = $targetYaml['clusters'] | Where-Object { $_['name'] -eq $sourceCluster['name'] }
if ($null -eq $targetCluster) {
$targetYaml['clusters'] += $sourceCluster
}
else {
$targetCluster['cluster'] = $sourceCluster['cluster']
}
}
}
foreach ($sourceContext in $sourceYaml['contexts']) {
if ($sourceContext['name'] -ne 'docker-desktop') {
$targetContext = $targetYaml['contexts'] | Where-Object { $_['name'] -eq $sourceContext['name'] }
if ($null -eq $targetContext) {
$targetYaml['contexts'] += $sourceContext
}
else {
$targetContext['context'] = $sourceContext['context']
}
}
}
$targetContent = ConvertTo-Yaml -Data $targetYaml
Set-Content -Path $Target -Value $targetContent