Skip to content

Commit b025fb0

Browse files
committed
k8s naming convention validation
1 parent c754300 commit b025fb0

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

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

Lines changed: 39 additions & 1 deletion
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'
138138
session = `${workspaceName}_${namespace}_${clusterName}_${region}_${accountId}`
139+
if (!isValidK8sLabel(session)) {
140+
session = createValidK8sSession(workspaceName, namespace, clusterName, region, accountId)
141+
}
139142
}
140143
const remoteEnv = await prepareDevEnvConnection(
141144
connectionIdentifier,
@@ -192,6 +195,41 @@ 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 {
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+
return session
224+
}
225+
226+
/**
227+
* Validates if a string meets K8s DNS label requirements
228+
*/
229+
function isValidK8sLabel(label: string): boolean {
230+
return /^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$/.test(label)
231+
}
232+
195233
export async function stopSpace(
196234
node: SagemakerSpaceNode | SagemakerUnifiedStudioSpaceNode,
197235
ctx: vscode.ExtensionContext,

0 commit comments

Comments
 (0)