forked from containers/podman-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KubePlayYAML.svelte
338 lines (305 loc) · 13 KB
/
KubePlayYAML.svelte
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<script lang="ts">
import { onMount, onDestroy } from 'svelte';
import type { Unsubscriber } from 'svelte/store';
import type { ProviderContainerConnectionInfo, ProviderInfo } from '../../../../main/src/plugin/api/provider-info';
let providerUnsubscribe: Unsubscriber;
import { providerInfos } from '../../stores/providers';
import MonacoEditor from '../editor/MonacoEditor.svelte';
import NoContainerEngineEmptyScreen from '../image/NoContainerEngineEmptyScreen.svelte';
import NavPage from '../ui/NavPage.svelte';
import KubePlayIcon from '../kube/KubePlayIcon.svelte';
import ErrorMessage from '../ui/ErrorMessage.svelte';
import WarningMessage from '../ui/WarningMessage.svelte';
import type { V1NamespaceList } from '@kubernetes/client-node/dist/api';
import { faCircleCheck } from '@fortawesome/free-solid-svg-icons';
import Fa from 'svelte-fa/src/fa.svelte';
let runStarted = false;
let runFinished = false;
let runError = '';
let runWarning = '';
let kubernetesYamlFilePath = undefined;
let hasInvalidFields = true;
let defaultContextName: string;
let currentNamespace: string;
let allNamespaces: V1NamespaceList;
let playKubeResultRaw;
let playKubeResultJSON;
let userChoice: 'podman' | 'kubernetes' = 'podman';
let providers: ProviderInfo[] = [];
$: providerConnections = providers
.map(provider => provider.containerConnections)
.flat()
// keep only podman providers as it is not supported by docker
.filter(providerContainerConnection => providerContainerConnection.type === 'podman')
.filter(providerContainerConnection => providerContainerConnection.status === 'started');
$: selectedProviderConnection = providerConnections.length > 0 ? providerConnections[0] : undefined;
let selectedProvider: ProviderContainerConnectionInfo = undefined;
$: selectedProvider = !selectedProvider && selectedProviderConnection ? selectedProviderConnection : selectedProvider;
function removeEmptyOrNull(obj: any) {
Object.keys(obj).forEach(
k =>
(obj[k] && typeof obj[k] === 'object' && removeEmptyOrNull(obj[k])) ||
(!obj[k] && obj[k] !== undefined && delete obj[k]),
);
return obj;
}
async function playKubeFile(): Promise<void> {
runStarted = true;
runFinished = false;
runError = '';
if (kubernetesYamlFilePath) {
// depending on the user choice, do podman or kubernetes
if (userChoice === 'podman') {
try {
const result = await window.playKube(kubernetesYamlFilePath, selectedProvider);
// remove the null values from the result
playKubeResultRaw = JSON.stringify(removeEmptyOrNull(result), null, 2);
playKubeResultJSON = JSON.parse(playKubeResultRaw);
// If there are container errors, that means that it was *able* to create the container
// but if failed to start. We will add this to the "warning" section as we were able to create the
// We add this with comma deliminated errors
if (playKubeResultJSON.Pods.length > 0) {
// Filter out the pods that have container errors, but check to see that container errors exists first
const containerErrors = playKubeResultJSON.Pods.filter(
pod => pod.ContainerErrors && pod.ContainerErrors.length > 0,
);
// For each Pod that has container errors, we will add the container errors to the warning message
if (containerErrors.length > 0) {
runWarning = `The following pods were created but failed to start: ${containerErrors
.map(pod => pod.ContainerErrors.join(', '))
.join(', ')}`;
}
}
runFinished = true;
} catch (error) {
runError = error;
console.error('error playing kube file', error);
}
} else if (userChoice === 'kubernetes') {
try {
await window.kubernetesCreateResourcesFromFile(defaultContextName, kubernetesYamlFilePath, currentNamespace);
runFinished = true;
} catch (error) {
runError = error;
console.error('error playing kube file', error);
}
}
}
runStarted = false;
}
onMount(async () => {
providerUnsubscribe = providerInfos.subscribe(value => {
providers = value;
});
// grab default context
defaultContextName = await window.kubernetesGetCurrentContextName();
// grab current namespace
currentNamespace = await window.kubernetesGetCurrentNamespace();
// check that the variable is set to a value, otherwise set to default namespace
if (!currentNamespace) {
currentNamespace = 'default';
}
// grab all the namespaces (will be useful to provide a drop-down to select the namespace)
try {
allNamespaces = await window.kubernetesListNamespaces();
} catch (error) {
console.debug('Not able to list all namespaces, probably a permission error', error);
}
});
onDestroy(() => {
if (providerUnsubscribe) {
providerUnsubscribe();
}
});
function goBackToHistory(): void {
window.history.go(-1);
}
async function getKubernetesfileLocation() {
const result = await window.openFileDialog('Select a .yaml file to play', {
name: 'YAML files',
extensions: ['yaml', 'yml'],
});
if (!result.canceled && result.filePaths.length === 1) {
kubernetesYamlFilePath = result.filePaths[0];
hasInvalidFields = false;
}
}
</script>
{#if providerConnections.length === 0}
<NoContainerEngineEmptyScreen />
{/if}
{#if providerConnections.length > 0}
<NavPage title="Play Pods or Containers from a Kubernetes YAML File" searchEnabled="{false}">
<div slot="content" class="bg-charcoal-700 p-5 min-w-full h-fit">
<div class="bg-charcoal-800 px-6 py-4 space-y-6 lg:px-8 sm:pb-6 xl:pb-8 rounded-lg">
<div class="text-xl font-medium">Select file:</div>
<div hidden="{runStarted}">
<label for="containerFilePath" class="block mb-2 text-sm font-bold text-gray-400">Kubernetes YAML file</label>
<input
on:click="{() => getKubernetesfileLocation()}"
name="containerFilePath"
id="containerFilePath"
bind:value="{kubernetesYamlFilePath}"
readonly
placeholder="Select a .yaml file to play"
class="w-full p-2 outline-none text-sm bg-charcoal-600 rounded-sm text-gray-700 placeholder-gray-700"
required />
</div>
<div>
<div class="text-sm font-bold text-gray-400 pb-2">Select Runtime:</div>
<div class="px-5">
<div class="flex flex-col space-y-3">
<div
hidden="{providerConnections.length === 0}"
class:border-2="{defaultContextName}"
class="rounded-md p-5 cursor-pointer {userChoice === 'podman'
? 'border-dustypurple-700'
: 'border-charcoal-600'}"
on:click="{() => {
userChoice = 'podman';
}}">
<div class="flex flex-row align-middle items-center">
<div class=" text-2xl {userChoice === 'podman' ? 'text-dustypurple-500' : 'text-charcoal-600'}">
<Fa icon="{faCircleCheck}" />
</div>
<div class="pl-2 text-gray-900">Using a Podman container engine</div>
</div>
<div hidden="{runStarted}">
{#if providerConnections.length > 1}
<label
for="providerConnectionName"
class="py-6 block mb-2 text-sm font-medium text-gray-400 dark:text-gray-400"
>Container Engine
<select
class="border text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 bg-gray-900 border-gray-900 placeholder-gray-700 text-white"
name="providerChoice"
bind:value="{selectedProvider}">
{#each providerConnections as providerConnection}
<option value="{providerConnection}">{providerConnection.name}</option>
{/each}
</select>
</label>
{/if}
{#if providerConnections.length === 1}
<input
type="hidden"
name="providerChoice"
readonly
bind:value="{selectedProviderConnection.name}" />
{/if}
</div>
</div>
<div
hidden="{!defaultContextName}"
class="border-2 rounded-md p-5 cursor-pointer {userChoice === 'kubernetes'
? 'border-dustypurple-700'
: 'border-charcoal-600'}"
on:click="{() => {
userChoice = 'kubernetes';
}}">
<div class="flex flex-row align-middle items-center">
<div class=" text-2xl {userChoice === 'kubernetes' ? 'text-dustypurple-500' : 'text-charcoal-600'}">
<Fa icon="{faCircleCheck}" />
</div>
<div class="pl-2 text-gray-900">Using a Kubernetes cluster</div>
</div>
{#if defaultContextName}
<div class="pt-2">
<label
for="contextToUse"
class="block mb-1 text-sm font-bold text-gray-400"
class:text-gray-900="{userChoice !== 'kubernetes'}">Kubernetes Context:</label>
<input
type="text"
disabled="{userChoice === 'podman'}"
bind:value="{defaultContextName}"
name="defaultContextName"
id="defaultContextName"
readonly
class="cursor-default w-full p-2 outline-none text-sm bg-charcoal-600 rounded-sm text-gray-700 placeholder-gray-700"
required />
</div>
{/if}
{#if allNamespaces}
<div class="pt-2">
<label
for="namespaceToUse"
class="block mb-1 text-sm font-medium text-gray-400"
class:text-gray-900="{userChoice !== 'kubernetes'}">Kubernetes namespace:</label>
<select
disabled="{userChoice === 'podman'}"
class="w-full p-2 outline-none text-sm bg-charcoal-900 rounded-sm text-gray-700 placeholder-gray-700"
name="namespaceChoice"
bind:value="{currentNamespace}">
{#each allNamespaces.items as namespace}
<option value="{namespace.metadata.name}">
{namespace.metadata.name}
</option>
{/each}
</select>
</div>
{/if}
</div>
</div>
</div>
</div>
{#if !runFinished}
<button
on:click="{() => playKubeFile()}"
disabled="{hasInvalidFields || runStarted}"
class="w-full pf-c-button pf-m-primary"
type="button">
<div class="flex flex-row align-text-top justify-center items-center">
{#if runStarted}
<div class="mr-4">
<i class="pf-c-button__progress">
<span class="pf-c-spinner pf-m-md" role="progressbar">
<span class="pf-c-spinner__clipper"></span>
<span class="pf-c-spinner__lead-ball"></span>
<span class="pf-c-spinner__tail-ball"></span>
</span>
</i>
</div>
{:else}
<KubePlayIcon />
{/if}
Play
</div>
</button>
{/if}
{#if runStarted}
<div class="text-gray-700 text-sm">
Please wait during the Play Kube and do not change screen. This process may take a few minutes to
complete...
</div>
{/if}
{#if runWarning}
<WarningMessage class="text-sm" error="{runWarning}" />
{/if}
{#if runError}
<ErrorMessage class="text-sm" error="{runError}" />
{/if}
{#if playKubeResultJSON}
<!-- Output area similar to DeployPodToKube.svelte -->
<div class="bg-charcoal-800 p-5 my-4">
<div class="flex flex-row items-center">
<div>
{#if playKubeResultJSON?.Pods.length > 1}
Created pods:
{:else}
Created pod:
{/if}
</div>
</div>
<div class="h-[100px] pt-2">
<MonacoEditor content="{playKubeResultRaw}" language="json" />
</div>
</div>
{/if}
{#if runFinished}
<button on:click="{() => goBackToHistory()}" class="pt-4 w-full pf-c-button pf-m-primary">Done</button>
{/if}
</div>
</div>
</NavPage>
{/if}