174 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
			
		
		
	
	
			174 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
| import { CoreV1Api, Configuration, WatchApi, EVENT_DATA } from '@gitlab/cluster-client';
 | |
| 
 | |
| export const handleClusterError = async (err) => {
 | |
|   if (!err.response) {
 | |
|     throw err;
 | |
|   }
 | |
| 
 | |
|   const errorData = await err.response.json();
 | |
|   throw errorData;
 | |
| };
 | |
| 
 | |
| export const buildWatchPath = ({ resource, api = 'api/v1', namespace = '' }) => {
 | |
|   return namespace ? `/${api}/namespaces/${namespace}/${resource}` : `/${api}/${resource}`;
 | |
| };
 | |
| 
 | |
| export const mapWorkloadItem = (item) => {
 | |
|   if (item.metadata) {
 | |
|     const metadata = {
 | |
|       ...item.metadata,
 | |
|       annotations: item.metadata?.annotations || {},
 | |
|       labels: item.metadata?.labels || {},
 | |
|     };
 | |
|     return { status: item.status, spec: item.spec, metadata };
 | |
|   }
 | |
|   return { status: item.status, spec: item.spec };
 | |
| };
 | |
| 
 | |
| export const mapSetItem = (item) => {
 | |
|   const status = {
 | |
|     ...item.status,
 | |
|     readyReplicas: item.status?.readyReplicas || null,
 | |
|   };
 | |
| 
 | |
|   const metadata =
 | |
|     {
 | |
|       ...item.metadata,
 | |
|       annotations: item.metadata?.annotations || {},
 | |
|       labels: item.metadata?.labels || {},
 | |
|     } || null;
 | |
| 
 | |
|   const spec = item.spec || null;
 | |
| 
 | |
|   return { status, metadata, spec };
 | |
| };
 | |
| 
 | |
| export const mapJobItem = (item) => {
 | |
|   const metadata = {
 | |
|     ...item.metadata,
 | |
|     annotations: item.metadata?.annotations || {},
 | |
|     labels: item.metadata?.labels || {},
 | |
|   };
 | |
| 
 | |
|   const status = {
 | |
|     failed: item.status?.failed || 0,
 | |
|     succeeded: item.status?.succeeded || 0,
 | |
|   };
 | |
| 
 | |
|   return {
 | |
|     status,
 | |
|     metadata,
 | |
|     spec: item.spec,
 | |
|   };
 | |
| };
 | |
| 
 | |
| export const mapServicesItems = (item) => {
 | |
|   const { type, clusterIP, externalIP, ports } = item.spec;
 | |
| 
 | |
|   return {
 | |
|     metadata: {
 | |
|       ...item.metadata,
 | |
|       annotations: item.metadata?.annotations || {},
 | |
|       labels: item.metadata?.labels || {},
 | |
|     },
 | |
|     spec: {
 | |
|       type,
 | |
|       clusterIP: clusterIP || '-',
 | |
|       externalIP: externalIP || '-',
 | |
|       ports,
 | |
|     },
 | |
|   };
 | |
| };
 | |
| 
 | |
| export const mapCronJobItem = (item) => {
 | |
|   const metadata = {
 | |
|     ...item.metadata,
 | |
|     annotations: item.metadata?.annotations || {},
 | |
|     labels: item.metadata?.labels || {},
 | |
|   };
 | |
| 
 | |
|   const status = {
 | |
|     active: item.status?.active || 0,
 | |
|     lastScheduleTime: item.status?.lastScheduleTime || null,
 | |
|   };
 | |
| 
 | |
|   return {
 | |
|     status,
 | |
|     metadata,
 | |
|     spec: item.spec,
 | |
|   };
 | |
| };
 | |
| 
 | |
| export const watchWorkloadItems = ({
 | |
|   client,
 | |
|   query,
 | |
|   configuration,
 | |
|   namespace,
 | |
|   watchPath,
 | |
|   queryField,
 | |
|   mapFn = mapWorkloadItem,
 | |
| }) => {
 | |
|   const config = new Configuration(configuration);
 | |
|   const watcherApi = new WatchApi(config);
 | |
| 
 | |
|   watcherApi
 | |
|     .subscribeToStream(watchPath, { watch: true })
 | |
|     .then((watcher) => {
 | |
|       let result = [];
 | |
| 
 | |
|       watcher.on(EVENT_DATA, (data) => {
 | |
|         result = data.map(mapFn);
 | |
| 
 | |
|         client.writeQuery({
 | |
|           query,
 | |
|           variables: { configuration, namespace },
 | |
|           data: { [queryField]: result },
 | |
|         });
 | |
|       });
 | |
|     })
 | |
|     .catch((err) => {
 | |
|       handleClusterError(err);
 | |
|     });
 | |
| };
 | |
| 
 | |
| export const getK8sPods = ({
 | |
|   client,
 | |
|   query,
 | |
|   configuration,
 | |
|   namespace = '',
 | |
|   enableWatch = false,
 | |
|   mapFn = mapWorkloadItem,
 | |
| }) => {
 | |
|   const config = new Configuration(configuration);
 | |
| 
 | |
|   const coreV1Api = new CoreV1Api(config);
 | |
|   const podsApi = namespace
 | |
|     ? coreV1Api.listCoreV1NamespacedPod({ namespace })
 | |
|     : coreV1Api.listCoreV1PodForAllNamespaces();
 | |
| 
 | |
|   return podsApi
 | |
|     .then((res) => {
 | |
|       if (enableWatch) {
 | |
|         const watchPath = buildWatchPath({ resource: 'pods', namespace });
 | |
|         watchWorkloadItems({
 | |
|           client,
 | |
|           query,
 | |
|           configuration,
 | |
|           namespace,
 | |
|           watchPath,
 | |
|           queryField: 'k8sPods',
 | |
|         });
 | |
|       }
 | |
| 
 | |
|       const data = res?.items || [];
 | |
|       return data.map(mapFn);
 | |
|     })
 | |
|     .catch(async (err) => {
 | |
|       try {
 | |
|         await handleClusterError(err);
 | |
|       } catch (error) {
 | |
|         throw new Error(error.message);
 | |
|       }
 | |
|     });
 | |
| };
 |