Welcome to mirror list, hosted at ThFree Co, Russian Federation.

resolver_helpers.js « helpers « graphql « kubernetes_dashboard « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b9c195d83d0e83277258319509652b090b7a6536 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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, metadata };
  }
  return { status: item.status };
};

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,
}) => {
  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(mapWorkloadItem);
    })
    .catch(async (err) => {
      try {
        await handleClusterError(err);
      } catch (error) {
        throw new Error(error.message);
      }
    });
};