-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add s3 storage artifact route and ui integration of it
- Loading branch information
1 parent
b33db83
commit 8fd6e8f
Showing
24 changed files
with
685 additions
and
36 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { FastifyInstance } from 'fastify'; | ||
import { createMinioClient, getObjectStream } from './storageUtils'; | ||
import { getDashboardConfig } from '../../../utils/resourceUtils'; | ||
|
||
export default async (fastify: FastifyInstance): Promise<void> => { | ||
const dashConfig = getDashboardConfig(); | ||
if (dashConfig?.spec?.dashboardConfig && !dashConfig.spec.dashboardConfig.disableS3Endpoint) { | ||
fastify.get('/:namespace/:bucket', async (request, reply) => { | ||
try { | ||
const { namespace, bucket } = request.params as { | ||
namespace: string; | ||
bucket: string; | ||
}; | ||
const query = request.query as { [key: string]: string }; | ||
const key = query.key; | ||
|
||
const stream = await getObjectStream({ | ||
bucket, | ||
client: await createMinioClient(fastify, namespace), | ||
key, | ||
}); | ||
|
||
reply.type('text/plain'); | ||
|
||
await new Promise<void>((resolve, reject) => { | ||
stream.on('data', (chunk) => { | ||
reply.raw.write(chunk); | ||
}); | ||
|
||
stream.on('end', () => { | ||
reply.raw.end(); | ||
resolve(); | ||
}); | ||
|
||
stream.on('error', (err) => { | ||
fastify.log.error('Stream error:', err); | ||
reply.raw.statusCode = 500; | ||
reply.raw.end(err.message); | ||
reject(err); | ||
}); | ||
}); | ||
|
||
return; | ||
} catch (err) { | ||
reply.code(500).send(err.message); | ||
return reply; | ||
} | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { Client as MinioClient } from 'minio'; | ||
import { DSPipelineKind, KubeFastifyInstance } from '../../../types'; | ||
import { Transform, PassThrough } from 'stream'; | ||
|
||
/** | ||
* Create minio client with aws instance profile credentials if needed. | ||
* @param config minio client options where `accessKey` and `secretKey` are optional. | ||
*/ | ||
export async function createMinioClient( | ||
fastify: KubeFastifyInstance, | ||
namespace: string, | ||
): Promise<MinioClient> { | ||
try { | ||
const dspaResponse = await fastify.kube.customObjectsApi | ||
.listNamespacedCustomObject( | ||
'datasciencepipelinesapplications.opendatahub.io', | ||
'v1alpha1', | ||
namespace, | ||
'datasciencepipelinesapplications', | ||
) | ||
.catch((e) => { | ||
throw `A ${ | ||
e.statusCode | ||
} error occurred when trying to fetch dspa aws storage credentials: ${ | ||
e.response?.body?.message || e?.response?.statusMessage | ||
}`; | ||
}); | ||
|
||
const dspas = ( | ||
dspaResponse?.body as { | ||
items: DSPipelineKind[]; | ||
} | ||
)?.items; | ||
|
||
if (!dspas || !dspas.length) { | ||
throw 'No Data Science Pipeline Application found'; | ||
} | ||
|
||
// check if data connection is available | ||
if ( | ||
dspas[0].status.conditions.find((condition) => condition.type === 'ObjectStoreAvailable') | ||
.status !== 'True' | ||
) { | ||
throw 'Object store is not available'; | ||
} | ||
|
||
// always get the first one | ||
const externalStorage = dspas[0].spec.objectStorage.externalStorage; | ||
|
||
if (externalStorage) { | ||
const { region, host: endPoint, s3CredentialsSecret } = externalStorage; | ||
|
||
// get secret | ||
const secret = await fastify.kube.coreV1Api.readNamespacedSecret( | ||
s3CredentialsSecret.secretName, | ||
namespace, | ||
); | ||
const accessKey = atob(secret.body.data[s3CredentialsSecret.accessKey]); | ||
const secretKey = atob(secret.body.data[s3CredentialsSecret.secretKey]); | ||
|
||
if (!accessKey || !secretKey) { | ||
throw 'Access key or secret key is empty'; | ||
} | ||
|
||
// sessionToken | ||
return new MinioClient({ accessKey, secretKey, endPoint, region }); | ||
} | ||
} catch (err) { | ||
console.error('Unable to create minio client: ', err); | ||
throw new Error('Unable to create minio client: ' + err); | ||
} | ||
} | ||
|
||
/** MinioRequestConfig describes the info required to retrieve an artifact. */ | ||
export interface MinioRequestConfig { | ||
bucket: string; | ||
key: string; | ||
client: MinioClient; | ||
} | ||
|
||
/** | ||
* Returns a stream from an object in a s3 compatible object store (e.g. minio). | ||
* | ||
* @param param.bucket Bucket name to retrieve the object from. | ||
* @param param.key Key of the object to retrieve. | ||
* @param param.client Minio client. | ||
* | ||
*/ | ||
export async function getObjectStream({ | ||
bucket, | ||
key, | ||
client, | ||
}: MinioRequestConfig): Promise<Transform> { | ||
const stream = await client.getObject(bucket, key); | ||
return stream.pipe(new PassThrough()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
frontend/src/concepts/pipelines/content/artifacts/ArtifactUriLink.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React from 'react'; | ||
import { Link } from 'react-router-dom'; | ||
import { usePipelinesAPI } from '~/concepts/pipelines/context'; | ||
import { useIsAreaAvailable, SupportedArea } from '~/concepts/areas'; | ||
import { extractS3UriComponents } from './utils'; | ||
|
||
interface ArtifactUriLinkProps { | ||
uri?: string; | ||
} | ||
|
||
export const ArtifactUriLink: React.FC<ArtifactUriLinkProps> = ({ uri }) => { | ||
const { namespace } = usePipelinesAPI(); | ||
const isS3EndpointAvailable = useIsAreaAvailable(SupportedArea.S3_ENDPOINT).status; | ||
|
||
const url = React.useMemo(() => { | ||
if (!uri || !isS3EndpointAvailable) { | ||
return; | ||
} | ||
|
||
// Check if the uri starts with http or https return it as is | ||
if (uri.startsWith('http://') || uri.startsWith('https://')) { | ||
return uri; | ||
} | ||
|
||
// Otherwise check if the uri is s3 | ||
// If it is not s3, return undefined as we only support fetching from s3 buckets | ||
const uriComponents = extractS3UriComponents(uri); | ||
if (!uriComponents) { | ||
return; | ||
} | ||
|
||
const { bucket, path } = uriComponents; | ||
|
||
// /api/storage/${namespace}/${bucket}?key=${path} | ||
return `/api/storage/${namespace}/${bucket}?key=${encodeURIComponent(path)}`; | ||
}, [isS3EndpointAvailable, namespace, uri]); | ||
|
||
if (!url) { | ||
return uri; | ||
} | ||
|
||
return ( | ||
<Link target="_blank" rel="noopener noreferrer" to={url}> | ||
{uri} | ||
</Link> | ||
); | ||
}; |
13 changes: 13 additions & 0 deletions
13
frontend/src/concepts/pipelines/content/artifacts/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export function extractS3UriComponents(uri: string): { bucket: string; path: string } | undefined { | ||
const s3Prefix = 's3://'; | ||
if (!uri.startsWith(s3Prefix)) { | ||
return; | ||
} | ||
|
||
const s3UrlWithoutPrefix = uri.slice(s3Prefix.length); | ||
const firstSlashIndex = s3UrlWithoutPrefix.indexOf('/'); | ||
const bucket = s3UrlWithoutPrefix.substring(0, firstSlashIndex); | ||
const path = s3UrlWithoutPrefix.substring(firstSlashIndex + 1); | ||
|
||
return { bucket, path }; | ||
} |
Oops, something went wrong.