Skip to content

Commit 9ec8341

Browse files
committed
k8s naming convention validation
1 parent c754300 commit 9ec8341

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

packages/core/src/awsService/sagemaker/commands.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,13 @@ export async function deeplinkConnect(
132132

133133
try {
134134
let connectionType = 'sm_dl'
135-
if (domain === '' && eksClusterArn) {
135+
if (!domain && eksClusterArn && workspaceName && namespace) {
136136
const { accountId, region, clusterName } = parseEKSClusterArn(eksClusterArn)
137137
connectionType = 'sm_hp'
138-
session = `${workspaceName}_${namespace}_${clusterName}_${region}_${accountId}`
138+
const validSession = createValidK8sSession(workspaceName, namespace, clusterName, region, accountId)
139+
if (validSession) {
140+
session = validSession
141+
}
139142
}
140143
const remoteEnv = await prepareDevEnvConnection(
141144
connectionIdentifier,
@@ -192,6 +195,44 @@ function parseEKSClusterArn(eksClusterArn: string): { accountId: string; region:
192195
return { accountId, region, clusterName }
193196
}
194197

198+
/**
199+
* Validates and sanitizes session names for Kubernetes DNS label compliance
200+
*/
201+
function createValidK8sSession(
202+
workspaceName: string,
203+
namespace: string,
204+
clusterName: string,
205+
region: string,
206+
accountId: string
207+
): string | undefined {
208+
const sanitize = (str: string): string =>
209+
str
210+
.toLowerCase()
211+
.replace(/[^a-z0-9-]/g, '-')
212+
.replace(/^-+|-+$/g, '')
213+
.substring(0, 15)
214+
215+
const components = [workspaceName, namespace, clusterName, region, accountId]
216+
.map(sanitize)
217+
.filter((c) => c.length > 0)
218+
219+
const session = components
220+
.join('_')
221+
.substring(0, 63)
222+
.replace(/^-+|-+$/g, '')
223+
if (isValidK8sLabel(session)) {
224+
return session
225+
}
226+
return undefined
227+
}
228+
229+
/**
230+
* Validates if a string meets K8s DNS label requirements
231+
*/
232+
function isValidK8sLabel(label: string): boolean {
233+
return /^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$/.test(label)
234+
}
235+
195236
export async function stopSpace(
196237
node: SagemakerSpaceNode | SagemakerUnifiedStudioSpaceNode,
197238
ctx: vscode.ExtensionContext,

0 commit comments

Comments
 (0)