fetch from k8s proxy

This commit is contained in:
Manabu Mccloskey 2023-03-06 17:59:08 -08:00
parent 98489ad55d
commit f3c473ccf3

View file

@ -1,10 +1,16 @@
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import { Table, TableColumn, Progress } from '@backstage/core-components';
import {tfstate} from "./terraform"
import Alert from '@material-ui/lab/Alert';
import useAsync from 'react-use/lib/useAsync';
import {
DiscoveryApi,
discoveryApiRef,
useApi,
} from '@backstage/core-plugin-api';
// eslint-disable-next-line no-restricted-imports
import {gunzipSync} from "zlib";
type TFState = {
terraform_version: string
resources: {
@ -53,17 +59,27 @@ export const TFTable = (props: TFTableProps) => {
}
export const FetchTFState = () => {
const discoveryApi = useApi(discoveryApiRef);
const { value, loading, error } = useAsync((): Promise<TFState> => {
return getTFState("tfstate-default-helloworld", "flux-system", discoveryApi)
} )
if (loading) {
return <Progress />
} else if (error) {
return <Alert severity="error">{error}</Alert>;
}
const tfdata = tfstate as TFState
const resources = tfdata.resources.map(value => {
// const a = await getTFState("tfstate-default-helloworld", "flux-system", discoveryApi)
// const tfdata = tfstate as TFState
const resources = value!.resources.map(val => {
const out: Resource = {
name: value.name,
provider: value.provider,
type: value.type,
name: val.name,
provider: val.provider,
type: val.type,
}
if (value.instances.length > 0) {
out.arn = value.instances[0].attributes.arn
out.id = value.instances[0].attributes.id
if (val.instances.length > 0) {
out.arn = val.instances[0].attributes.arn
out.id = val.instances[0].attributes.id
}
return out
})
@ -84,3 +100,49 @@ export const FetchTFState = () => {
//
// return <DenseTable users={value || []} />;
};
type payload = {
kind: string
apiVersion: string
metadata: {
annotations: {
[key: string]: string
}
}
type: string
data: {
tfstate: string
}
}
async function getTFState(name: string, namespace: string, discoveryApi: DiscoveryApi): Promise<TFState> {
const kubernetesBaseUrl = await discoveryApi.getBaseUrl('kubernetes');
const kubernetesProxyEndpoint = `${kubernetesBaseUrl}/proxy`;
return new Promise(async (resolve, reject) => {
const resp = await fetch(`${kubernetesProxyEndpoint}/api/v1/namespaces/${namespace}/secrets/${name}`, {
method: 'GET',
headers: {
'X-Kubernetes-Cluster': "canoe-packaging",
Authorization: `Bearer TOKEN`,
},
});
if (resp.ok) {
const payload = await resp.json() as payload
const data = Buffer.from(payload.data.tfstate, 'base64')
let compression = "gzip"
if ( "encoding" in payload.metadata.annotations) {
compression = payload.metadata.annotations.encoding
}
if (compression === "gzip") {
const a = gunzipSync(data).toString("utf-8")
resolve(JSON.parse(a) as TFState)
}
reject(`unknown compression method specified: ${compression}`)
} else {
reject(`Failed to retrieve terraform information: ${resp.status}: ${resp.statusText} `)
}
})
}