diff options
Diffstat (limited to 'app/assets/javascripts/environments/graphql/resolvers/kubernetes.js')
-rw-r--r-- | app/assets/javascripts/environments/graphql/resolvers/kubernetes.js | 61 |
1 files changed, 48 insertions, 13 deletions
diff --git a/app/assets/javascripts/environments/graphql/resolvers/kubernetes.js b/app/assets/javascripts/environments/graphql/resolvers/kubernetes.js index 06a55158152..9111dc9c86c 100644 --- a/app/assets/javascripts/environments/graphql/resolvers/kubernetes.js +++ b/app/assets/javascripts/environments/graphql/resolvers/kubernetes.js @@ -14,6 +14,7 @@ import { import { humanizeClusterErrors } from '../../helpers/k8s_integration_helper'; import k8sPodsQuery from '../queries/k8s_pods.query.graphql'; import k8sWorkloadsQuery from '../queries/k8s_workloads.query.graphql'; +import k8sServicesQuery from '../queries/k8s_services.query.graphql'; const mapWorkloadItems = (items, kind) => { return items.map((item) => { @@ -96,13 +97,53 @@ const watchWorkloadItems = ({ kind, apiVersion, configuration, namespace, client }); }; +const mapServicesItems = (items) => { + return items.map((item) => { + const { type, clusterIP, externalIP, ports } = item.spec; + return { + metadata: item.metadata, + spec: { + type, + clusterIP: clusterIP || '-', + externalIP: externalIP || '-', + ports, + }, + }; + }); +}; + +const watchServices = ({ configuration, namespace, client }) => { + const path = namespace ? `/api/v1/namespaces/${namespace}/services` : '/api/v1/services'; + const config = new Configuration(configuration); + const watcherApi = new WatchApi(config); + + watcherApi + .subscribeToStream(path, { watch: true }) + .then((watcher) => { + let result = []; + + watcher.on(EVENT_DATA, (data) => { + result = mapServicesItems(data); + + client.writeQuery({ + query: k8sServicesQuery, + variables: { configuration, namespace }, + data: { k8sServices: result }, + }); + }); + }) + .catch((err) => { + handleClusterError(err); + }); +}; + export default { k8sPods(_, { configuration, namespace }, { client }) { const query = k8sPodsQuery; const enableWatch = gon.features?.k8sWatchApi; return getK8sPods({ client, query, configuration, namespace, enableWatch }); }, - k8sServices(_, { configuration, namespace }) { + k8sServices(_, { configuration, namespace }, { client }) { const coreV1Api = new CoreV1Api(new Configuration(configuration)); const servicesApi = namespace ? coreV1Api.listCoreV1NamespacedService({ namespace }) @@ -111,18 +152,12 @@ export default { return servicesApi .then((res) => { const items = res?.items || []; - return items.map((item) => { - const { type, clusterIP, externalIP, ports } = item.spec; - return { - metadata: item.metadata, - spec: { - type, - clusterIP: clusterIP || '-', - externalIP: externalIP || '-', - ports, - }, - }; - }); + + if (gon.features?.k8sWatchApi) { + watchServices({ configuration, namespace, client }); + } + + return mapServicesItems(items); }) .catch(async (err) => { try { |