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

flux.js « resolvers « graphql « environments « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d39b1bed7b66c41b45281a7647950ced0ecc339b (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
import axios from '~/lib/utils/axios_utils';
import {
  HELM_RELEASES_RESOURCE_TYPE,
  KUSTOMIZATIONS_RESOURCE_TYPE,
} from '~/environments/constants';

const helmReleasesApiVersion = 'helm.toolkit.fluxcd.io/v2beta1';
const kustomizationsApiVersion = 'kustomize.toolkit.fluxcd.io/v1beta1';

const handleClusterError = (err) => {
  const error = err?.response?.data?.message ? new Error(err.response.data.message) : err;
  throw error;
};

const buildFluxResourceUrl = ({
  basePath,
  namespace,
  apiVersion,
  resourceType,
  environmentName = '',
}) => {
  return `${basePath}/apis/${apiVersion}/namespaces/${namespace}/${resourceType}/${environmentName}`;
};

const getFluxResourceStatus = (configuration, url) => {
  const { headers } = configuration;
  const withCredentials = true;

  return axios
    .get(url, { withCredentials, headers })
    .then((res) => {
      return res?.data?.status?.conditions || [];
    })
    .catch((err) => {
      handleClusterError(err);
    });
};

const getFluxResources = (configuration, url) => {
  const { headers } = configuration;
  const withCredentials = true;

  return axios
    .get(url, { withCredentials, headers })
    .then((res) => {
      const items = res?.data?.items || [];
      const result = items.map((item) => {
        return {
          apiVersion: item.apiVersion,
          metadata: {
            name: item.metadata?.name,
            namespace: item.metadata?.namespace,
          },
        };
      });
      return result || [];
    })
    .catch((err) => {
      const error = err?.response?.data?.reason || err;
      throw new Error(error);
    });
};

export default {
  fluxKustomizationStatus(_, { configuration, namespace, environmentName, fluxResourcePath = '' }) {
    let url;

    if (fluxResourcePath) {
      url = `${configuration.basePath}/apis/${fluxResourcePath}`;
    } else {
      url = buildFluxResourceUrl({
        basePath: configuration.basePath,
        resourceType: KUSTOMIZATIONS_RESOURCE_TYPE,
        apiVersion: kustomizationsApiVersion,
        namespace,
        environmentName,
      });
    }
    return getFluxResourceStatus(configuration, url);
  },
  fluxHelmReleaseStatus(_, { configuration, namespace, environmentName, fluxResourcePath }) {
    let url;

    if (fluxResourcePath) {
      url = `${configuration.basePath}/apis/${fluxResourcePath}`;
    } else {
      url = buildFluxResourceUrl({
        basePath: configuration.basePath,
        resourceType: HELM_RELEASES_RESOURCE_TYPE,
        apiVersion: helmReleasesApiVersion,
        namespace,
        environmentName,
      });
    }
    return getFluxResourceStatus(configuration, url);
  },
  fluxKustomizations(_, { configuration, namespace }) {
    const url = buildFluxResourceUrl({
      basePath: configuration.basePath,
      resourceType: KUSTOMIZATIONS_RESOURCE_TYPE,
      apiVersion: kustomizationsApiVersion,
      namespace,
    });
    return getFluxResources(configuration, url);
  },
  fluxHelmReleases(_, { configuration, namespace }) {
    const url = buildFluxResourceUrl({
      basePath: configuration.basePath,
      resourceType: HELM_RELEASES_RESOURCE_TYPE,
      apiVersion: helmReleasesApiVersion,
      namespace,
    });
    return getFluxResources(configuration, url);
  },
};