-
Notifications
You must be signed in to change notification settings - Fork 796
Expand file tree
/
Copy pathsagemaker.ts
More file actions
509 lines (443 loc) · 19.4 KB
/
sagemaker.ts
File metadata and controls
509 lines (443 loc) · 19.4 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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*! * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import * as vscode from 'vscode'
import {
AppDetails,
AppType,
CreateAppCommand,
CreateAppCommandInput,
CreateAppCommandOutput,
DeleteAppCommand,
DeleteAppCommandInput,
DeleteAppCommandOutput,
DescribeAppCommand,
DescribeAppCommandInput,
DescribeAppCommandOutput,
DescribeDomainCommand,
DescribeDomainCommandInput,
DescribeDomainCommandOutput,
DescribeDomainResponse,
DescribeSpaceCommand,
DescribeSpaceCommandInput,
DescribeSpaceCommandOutput,
ListAppsCommandInput,
ListSpacesCommandInput,
ResourceSpec,
SageMakerClient,
SpaceDetails,
UpdateSpaceCommand,
UpdateSpaceCommandInput,
UpdateSpaceCommandOutput,
paginateListApps,
paginateListSpaces,
ListClustersCommandInput,
DescribeClusterCommand,
DescribeClusterCommandInput,
DescribeClusterCommandOutput,
ClusterSummary,
paginateListClusters,
} from '@amzn/sagemaker-client'
import { isEmpty } from 'lodash'
import { sleep } from '../utilities/timeoutUtils'
import { ClientWrapper } from './clientWrapper'
import { AsyncCollection } from '../utilities/asyncCollection'
import {
InstanceTypeError,
InstanceTypeMinimum,
InstanceTypeInsufficientMemory,
InstanceTypeInsufficientMemoryMessage,
InstanceTypeNotSelectedMessage,
RemoteAccess,
} from '../../awsService/sagemaker/constants'
import { getDomainSpaceKey } from '../../awsService/sagemaker/utils'
import { getLogger } from '../logger/logger'
import { ToolkitError } from '../errors'
import { continueText, cancel } from '../localizedText'
import { showConfirmationMessage } from '../utilities/messages'
import { AwsCredentialIdentity } from '@aws-sdk/types'
import globals from '../extensionGlobals'
import { HyperpodCluster } from './kubectlClient'
import { EKSClient } from '@aws-sdk/client-eks'
import { DevSettings } from '../settings'
const appTypeSettingsMap: Record<string, string> = {
[AppType.JupyterLab as string]: 'JupyterLabAppSettings',
[AppType.CodeEditor as string]: 'CodeEditorAppSettings',
} as const
export const waitForAppConfig = {
softTimeoutRetries: 12,
hardTimeoutRetries: 120,
intervalMs: 5000,
}
export interface SagemakerSpaceApp extends SpaceDetails {
App?: AppDetails
DomainSpaceKey: string
}
export class SagemakerClient extends ClientWrapper<SageMakerClient> {
public constructor(
public override readonly regionCode: string,
private readonly credentialsProvider?: () => Promise<AwsCredentialIdentity>
) {
super(regionCode, SageMakerClient)
}
protected override getClient(ignoreCache: boolean = false) {
if (!this.client || ignoreCache) {
const devSettings = DevSettings.instance
const customEndpoint = devSettings.get('endpoints', {})['sagemaker']
const endpoint = customEndpoint || `https://sagemaker.${this.regionCode}.amazonaws.com`
const args = {
serviceClient: SageMakerClient,
region: this.regionCode,
clientOptions: {
endpoint: endpoint,
region: this.regionCode,
...(this.credentialsProvider && { credentials: this.credentialsProvider }),
},
}
this.client = globals.sdkClientBuilderV3.createAwsService(args)
}
return this.client
}
public override dispose() {
getLogger().debug('SagemakerClient: Disposing client %O', this.client)
this.client?.destroy()
this.client = undefined
}
public listSpaces(request: ListSpacesCommandInput = {}): AsyncCollection<SpaceDetails[]> {
// @ts-ignore: Suppressing type mismatch on paginator return type
return this.makePaginatedRequest(paginateListSpaces, request, (page) => page.Spaces)
}
public listApps(request: ListAppsCommandInput = {}): AsyncCollection<AppDetails[]> {
// @ts-ignore: Suppressing type mismatch on paginator return type
return this.makePaginatedRequest(paginateListApps, request, (page) => page.Apps)
}
public describeApp(request: DescribeAppCommandInput): Promise<DescribeAppCommandOutput> {
return this.makeRequest(DescribeAppCommand, request)
}
public describeDomain(request: DescribeDomainCommandInput): Promise<DescribeDomainCommandOutput> {
return this.makeRequest(DescribeDomainCommand, request)
}
public describeSpace(request: DescribeSpaceCommandInput): Promise<DescribeSpaceCommandOutput> {
return this.makeRequest(DescribeSpaceCommand, request)
}
public updateSpace(request: UpdateSpaceCommandInput): Promise<UpdateSpaceCommandOutput> {
return this.makeRequest(UpdateSpaceCommand, request)
}
public createApp(request: CreateAppCommandInput): Promise<CreateAppCommandOutput> {
return this.makeRequest(CreateAppCommand, request)
}
public deleteApp(request: DeleteAppCommandInput): Promise<DeleteAppCommandOutput> {
return this.makeRequest(DeleteAppCommand, request)
}
public async listAppForSpace(domainId: string, spaceName: string): Promise<AppDetails | undefined> {
const appsList = await this.listApps({ DomainIdEquals: domainId, SpaceNameEquals: spaceName })
.flatten()
.promise()
return appsList[0] // At most one App for one SagemakerSpace
}
public async listAppsForDomain(domainId: string): Promise<AppDetails[]> {
return this.listApps({ DomainIdEquals: domainId }).flatten().promise()
}
/**
* Search for an app by space name from the domain's app list (case-insensitive).
* If space name is all lowercase, uses the more efficient SpaceNameEquals filter.
* Otherwise, fetches all apps in the domain and performs case-insensitive matching.
*/
public async listAppsForDomainMatchSpaceIgnoreCase(
domainId: string,
spaceName: string
): Promise<AppDetails | undefined> {
// If space name is all lowercase, use the efficient SpaceNameEquals filter
if (spaceName === spaceName.toLowerCase()) {
return this.listAppForSpace(domainId, spaceName)
}
// Otherwise, fetch all apps and do case-insensitive matching
const apps = await this.listAppsForDomain(domainId)
return apps.find((app) => app.SpaceName?.toLowerCase() === spaceName.toLowerCase())
}
public async startSpace(spaceName: string, domainId: string, skipInstanceTypePrompts: boolean = false) {
let spaceDetails: DescribeSpaceCommandOutput
// Get existing space details
try {
spaceDetails = await this.describeSpace({
DomainId: domainId,
SpaceName: spaceName,
})
} catch (err) {
throw this.handleStartSpaceError(err)
}
// Get app type
const appType = spaceDetails.SpaceSettings?.AppType
if (!appType || !(appType in appTypeSettingsMap)) {
throw new ToolkitError(`Unsupported AppType "${appType}" for space "${spaceName}"`)
}
// Get app resource spec
const requestedResourceSpec =
appType === AppType.JupyterLab
? spaceDetails.SpaceSettings?.JupyterLabAppSettings?.DefaultResourceSpec
: spaceDetails.SpaceSettings?.CodeEditorAppSettings?.DefaultResourceSpec
let instanceType = requestedResourceSpec?.InstanceType
// Is InstanceType defined and has enough memory?
if (instanceType && instanceType in InstanceTypeInsufficientMemory) {
if (skipInstanceTypePrompts) {
// User already consented, upgrade automatically
instanceType = InstanceTypeInsufficientMemory[instanceType]
} else {
// Prompt user to select one with sufficient memory (1 level up from their chosen one)
const confirmed = await showConfirmationMessage({
prompt: InstanceTypeInsufficientMemoryMessage(
spaceDetails.SpaceName || '',
instanceType,
InstanceTypeInsufficientMemory[instanceType]
),
confirm: 'Restart Space and Connect',
cancel: 'Cancel',
type: 'warning',
})
if (!confirmed) {
throw new ToolkitError('InstanceType has insufficient memory.', { code: InstanceTypeError })
}
instanceType = InstanceTypeInsufficientMemory[instanceType]
}
} else if (!instanceType) {
if (skipInstanceTypePrompts) {
// User already consented, use minimum
instanceType = InstanceTypeMinimum
} else {
// Prompt user to select the minimum supported instance type
const confirmed = await showConfirmationMessage({
prompt: InstanceTypeNotSelectedMessage(spaceDetails.SpaceName || ''),
confirm: continueText,
cancel: cancel,
type: 'warning',
})
if (!confirmed) {
throw new ToolkitError('InstanceType not defined.', { code: InstanceTypeError })
}
instanceType = InstanceTypeMinimum
}
}
// First, update the space if needed
const needsRemoteAccess =
!spaceDetails.SpaceSettings?.RemoteAccess ||
spaceDetails.SpaceSettings?.RemoteAccess === RemoteAccess.DISABLED
const instanceTypeChanged = requestedResourceSpec?.InstanceType !== instanceType
if (needsRemoteAccess || instanceTypeChanged) {
const updateSpaceRequest: UpdateSpaceCommandInput = {
DomainId: domainId,
SpaceName: spaceName,
SpaceSettings: {
...(needsRemoteAccess && { RemoteAccess: RemoteAccess.ENABLED }),
...(instanceTypeChanged && {
[appTypeSettingsMap[appType]]: {
DefaultResourceSpec: {
InstanceType: instanceType,
},
},
}),
},
}
try {
getLogger().debug('SagemakerClient: Updating space: domainId=%s, spaceName=%s', domainId, spaceName)
await this.updateSpace(updateSpaceRequest)
await this.waitForSpaceInService(spaceName, domainId)
} catch (err) {
throw this.handleStartSpaceError(err)
}
}
const resourceSpec: ResourceSpec = {
// Default values
SageMakerImageArn: 'arn:aws:sagemaker:us-west-2:542918446943:image/sagemaker-distribution-cpu',
SageMakerImageVersionAlias: '3.2.0',
// The existing resource spec
...requestedResourceSpec,
// The instance type user has chosen
InstanceType: instanceType,
}
const cleanedResourceSpec =
resourceSpec && 'EnvironmentArn' in resourceSpec
? { ...resourceSpec, EnvironmentArn: undefined, EnvironmentVersionArn: undefined }
: resourceSpec
// Second, create the App
const createAppRequest: CreateAppCommandInput = {
DomainId: domainId,
SpaceName: spaceName,
AppType: appType,
AppName: 'default',
ResourceSpec: cleanedResourceSpec,
}
try {
getLogger().debug('SagemakerClient: Creating app: domainId=%s, spaceName=%s', domainId, spaceName)
await this.createApp(createAppRequest)
} catch (err) {
throw this.handleStartSpaceError(err)
}
}
public async listSpaceApps(domainId?: string): Promise<Map<string, SagemakerSpaceApp>> {
// Create options object conditionally if domainId is provided
const options = domainId ? { DomainIdEquals: domainId } : undefined
const appMap: Map<string, AppDetails> = await this.listApps(options)
.flatten()
.filter((app) => !!app.DomainId && !!app.SpaceName)
.filter((app) => app.AppType === AppType.JupyterLab || app.AppType === AppType.CodeEditor)
.toMap((app) => getDomainSpaceKey(app.DomainId || '', app.SpaceName || ''))
const spaceApps: Map<string, SagemakerSpaceApp> = await this.listSpaces(options)
.flatten()
.filter((space) => !!space.DomainId && !!space.SpaceName)
.map((space) => {
const key = getDomainSpaceKey(space.DomainId || '', space.SpaceName || '')
return { ...space, App: appMap.get(key), DomainSpaceKey: key }
})
.toMap((space) => getDomainSpaceKey(space.DomainId || '', space.SpaceName || ''))
return spaceApps
}
public async fetchSpaceAppsAndDomains(
domainId?: string,
filterSmusDomains: boolean = true
): Promise<[Map<string, SagemakerSpaceApp>, Map<string, DescribeDomainResponse>]> {
try {
const spaceApps = await this.listSpaceApps(domainId)
// Get de-duped list of domain IDs for all of the spaces
const domainIds: string[] = domainId
? [domainId]
: [...new Set([...spaceApps].map(([_, spaceApp]) => spaceApp.DomainId || ''))]
// Get details for each domain
const domains: [string, DescribeDomainResponse][] = await Promise.all(
domainIds.map(async (domainId, index) => {
await sleep(index * 100)
try {
const response = await this.describeDomain({ DomainId: domainId })
return [domainId, response]
} catch {
// Return empty response for domain IDs that error'ed out
const emptyResponse: DescribeDomainResponse = {} as DescribeDomainResponse
return [domainId, emptyResponse]
}
})
)
const domainsMap = new Map<string, DescribeDomainResponse>(domains)
const filteredSpaceApps = new Map(
[...spaceApps]
// Filter out SageMaker Unified Studio domains only if filterSmusDomains is true
.filter(
([_, spaceApp]) =>
!filterSmusDomains ||
isEmpty(domainsMap.get(spaceApp.DomainId || '')?.DomainSettings?.UnifiedStudioSettings)
)
)
return [filteredSpaceApps, domainsMap]
} catch (err) {
const error = err as Error
getLogger().error('Failed to fetch space apps: %s', err)
if (error.name === 'AccessDeniedException') {
void vscode.window.showErrorMessage(
'AccessDeniedException: You do not have permission to view spaces. Please contact your administrator',
{ modal: false, detail: 'AWS Toolkit' }
)
}
throw err
}
}
private async waitForSpaceInService(
spaceName: string,
domainId: string,
maxRetries = 30,
intervalMs = 5000
): Promise<void> {
for (let attempt = 0; attempt < maxRetries; attempt++) {
const result = await this.describeSpace({ SpaceName: spaceName, DomainId: domainId })
if (result.Status === 'InService') {
return
}
await sleep(intervalMs)
}
throw new ToolkitError(
`Timed out waiting for space "${spaceName}" in domain "${domainId}" to reach "InService" status.`
)
}
public async waitForAppInService(
domainId: string,
spaceName: string,
appType: string,
progress?: vscode.Progress<{ message?: string; increment?: number }>
): Promise<void> {
for (let attempt = 0; attempt < waitForAppConfig.hardTimeoutRetries; attempt++) {
const { Status } = await this.describeApp({
DomainId: domainId,
SpaceName: spaceName,
AppType: appType as any,
AppName: 'default',
})
if (Status === 'InService') {
return
}
if (['Failed', 'DeleteFailed'].includes(Status ?? '')) {
throw new ToolkitError(`App failed to start. Status: ${Status}`)
}
if (attempt === waitForAppConfig.softTimeoutRetries) {
progress?.report({
message: `Starting the space is taking longer than usual. The space will connect when ready`,
})
}
await sleep(waitForAppConfig.intervalMs)
}
throw new ToolkitError(`Timed out waiting for app "${spaceName}" to reach "InService" status.`)
}
private handleStartSpaceError(err: unknown) {
const error = err as Error
if (error.name === 'AccessDeniedException') {
throw new ToolkitError('You do not have permission to start spaces. Please contact your administrator', {
cause: error,
})
} else {
throw err
}
}
public listClusters(request: ListClustersCommandInput = {}): AsyncCollection<ClusterSummary[]> {
// @ts-ignore: Suppressing type mismatch on paginator return type
return this.makePaginatedRequest(paginateListClusters, request, (page) => page.ClusterSummaries)
}
public describeCluster(request: DescribeClusterCommandInput): Promise<DescribeClusterCommandOutput> {
return this.makeRequest(DescribeClusterCommand, request)
}
public async listHyperpodClusters(): Promise<HyperpodCluster[]> {
const clusterSummaries = await this.listClusters().flatten().promise()
const clusters: HyperpodCluster[] = []
for (const summary of clusterSummaries) {
clusters.push(await this.getHyperpodCluster(summary.ClusterName!))
}
return clusters
}
async getHyperpodCluster(clusterName: string): Promise<HyperpodCluster> {
const response = await this.describeCluster({ ClusterName: clusterName })
if (!response.ClusterArn) {
throw new Error(`Cluster ${clusterName} not found`)
}
const orchestrator = response.Orchestrator
let eksClusterName: string | undefined
let eksClusterArn: string | undefined
if (orchestrator?.Eks) {
eksClusterName = orchestrator.Eks.ClusterArn?.split('/').pop()
eksClusterArn = orchestrator.Eks.ClusterArn
}
return {
clusterName: response.ClusterName!,
clusterArn: response.ClusterArn,
status: response.ClusterStatus!,
eksClusterName,
eksClusterArn,
regionCode: this.regionCode,
}
}
public getEKSClient(ignoreCache: boolean = false) {
const args = {
serviceClient: EKSClient as any,
region: this.regionCode,
clientOptions: {
region: this.regionCode,
...(this.credentialsProvider && { credentials: this.credentialsProvider }),
},
}
return globals.sdkClientBuilderV3.createAwsService(args) as EKSClient
}
}