Skip to content

Commit

Permalink
install repo chart is now like reading a story :)
Browse files Browse the repository at this point in the history
  • Loading branch information
nir2002 committed Aug 2, 2023
1 parent fa8980c commit a1430b9
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 244 deletions.
165 changes: 89 additions & 76 deletions dashboard/src/API/apiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,138 +5,151 @@ import {
ReleaseHealthStatus,
ReleaseRevision,
Repository,
} from "../data/types"
import { QueryFunctionContext } from "@tanstack/react-query"
} from "../data/types";
import { QueryFunctionContext } from "@tanstack/react-query";
interface ClustersResponse {
AuthInfo: string
Cluster: string
IsCurrent: boolean
Name: string
Namespace: string
AuthInfo: string;
Cluster: string;
IsCurrent: boolean;
Name: string;
Namespace: string;
}
class ApiService {
currentCluster = ""
currentCluster = "";
constructor(protected readonly isMockMode: boolean = false) {}

setCluster = (cluster: string) => {
this.currentCluster = cluster
}
this.currentCluster = cluster;
};

public async fetchWithDefaults<T>(
url: string,
options?: RequestInit
): Promise<T> {
let response;

public fetchWithDefaults = async (url: string, options?: RequestInit) => {
if (this.currentCluster) {
const headers = new Headers(options?.headers)
const headers = new Headers(options?.headers);
if (!headers.has("X-Kubecontext")) {
headers.set("X-Kubecontext", this.currentCluster)
headers.set("X-Kubecontext", this.currentCluster);
}
return fetch(url, { ...options, headers })
response = await fetch(url, { ...options, headers });
} else {
response = await fetch(url, options);
}
return fetch(url, options)

if (!response.ok) {
const error = await response.text();
throw new Error(error);
}

let data;
if (!response.headers.get("Content-Type")) {
return {} as T;
} else if (response.headers.get("Content-Type")?.includes("text/plain")) {
data = await response.text();
} else {
data = await response.json();
}
return data;
}

getToolVersion = async () => {
const response = await fetch(`/status`)
const data = await response.json()
return data
}
const response = await fetch(`/status`);
const data = await response.json();
return data;
};

getRepositoryLatestVersion = async (repositoryName: string) => {
const response = await this.fetchWithDefaults(
const data = await this.fetchWithDefaults(
`/api/helm/repositories/latestver?name=${repositoryName}`
)
const data = await response.json()
return data
}
);
return data;
};

getInstalledReleases = async () => {
const response = await this.fetchWithDefaults(`/api/helm/releases`)
const data = await response.json()
return data
}
const data = await this.fetchWithDefaults(`/api/helm/releases`);
return data;
};

getClusters = async () => {
const response = await fetch(`/api/k8s/contexts`)
const data = (await response.json()) as ClustersResponse[]
return data
}
const response = await fetch(`/api/k8s/contexts`);
const data = (await response.json()) as ClustersResponse[];
return data;
};

getNamespaces = async () => {
const response = await this.fetchWithDefaults(`/api/k8s/namespaces/list`)
const data = await response.json()
return data
}
const data = await this.fetchWithDefaults(`/api/k8s/namespaces/list`);
return data;
};

getRepositories = async () => {
const response = await this.fetchWithDefaults(`/api/helm/repositories`)
const data = await response.json()
return data
}
const data = await this.fetchWithDefaults(`/api/helm/repositories`);
return data;
};

getRepositoryCharts = async ({
queryKey,
}: QueryFunctionContext<Chart[], Repository>) => {
const [, repository] = queryKey
const response = await this.fetchWithDefaults(
const [, repository] = queryKey;
const data = await this.fetchWithDefaults(
`/api/helm/repositories/${repository}`
)
const data = await response.json()
return data
}
);
return data;
};

getChartVersions = async ({
queryKey,
}: QueryFunctionContext<ChartVersion[], Chart>) => {
const [, chart] = queryKey
const [, chart] = queryKey;

const response = await this.fetchWithDefaults(
const data = await this.fetchWithDefaults(
`/api/helm/repositories/versions?name=${chart.name}`
)
const data = await response.json()
return data
}
);
return data;
};

getResourceStatus = async ({
release,
}: {
release: Release
release: Release;
}): Promise<ReleaseHealthStatus[] | null> => {
if (!release) return null
if (!release) return null;

const response = await this.fetchWithDefaults(
const data = await this.fetchWithDefaults(
`/api/helm/releases/${release.namespace}/${release.name}/resources?health=true`
)
const data = await response.json()
return data
}
);
return data;
};

getReleasesHistory = async ({
queryKey,
}: QueryFunctionContext<Release[], Release>): Promise<ReleaseRevision[]> => {
const [, params] = queryKey
const [, params] = queryKey;

if (!params.namespace || !params.chart) return []
if (!params.namespace || !params.chart) return [];

const response = await this.fetchWithDefaults(
const data = await this.fetchWithDefaults(
`/api/helm/releases/${params.namespace}/${params.chart}/history`
)
const data = await response.json()
);

return data
}
return data;
};

getValues = async ({ queryKey }: any) => {
const [, params] = queryKey
const { namespace, chart, version } = params
const [, params] = queryKey;
const { namespace, chart, version } = params;

if (!namespace || !chart || !chart.name || version === undefined)
return Promise.reject(new Error("missing parameters"))
return Promise.reject(new Error("missing parameters"));

const url = `/api/helm/repositories/values?chart=${namespace}/${chart.name}&version=${version}`
const response = await this.fetchWithDefaults(url)
const data = await response.text()
const url = `/api/helm/repositories/values?chart=${namespace}/${chart.name}&version=${version}`;
const data = await this.fetchWithDefaults(url);

return data
}
return data;
};
}

const apiService = new ApiService()
const apiService = new ApiService();

export default apiService
export default apiService;
49 changes: 25 additions & 24 deletions dashboard/src/API/releases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,31 +231,44 @@ export function useChartReleaseValues({
export const useVersionData = ({
version,
userValues,
chart,
chartAddress,
releaseValues,
namespace,
releaseName,
isInstallRepoChart = false,
}: {
version: string;
userValues: string;
chart: string;
chartAddress: string;
releaseValues: string;
namespace: string;
releaseName: string;
isInstallRepoChart?: boolean;
}) => {
return useQuery(
[version, userValues, chart, releaseValues, namespace, releaseName],
[
version,
userValues,
chartAddress,
releaseValues,
namespace,
releaseName,
isInstallRepoChart,
],
async () => {
const formData = getVersionManifestFormData({
version,
userValues,
chart,
chart: chartAddress,
releaseValues,
releaseName,
});

const fetchUrl = `/api/helm/releases/${
namespace ? namespace : "[empty]"
}${`/${releaseName}`}`;
const fetchUrl = isInstallRepoChart
? `/api/helm/releases/${namespace || "default"}`
: `/api/helm/releases/${
namespace ? namespace : "[empty]"
}${`/${releaseName}`}`;

const data = await callApi(fetchUrl, {
method: "post",
Expand All @@ -266,11 +279,10 @@ export const useVersionData = ({
},
{
enabled:
Boolean(chart) &&
Boolean(chartAddress) &&
Boolean(version) &&
Boolean(releaseName) &&
Boolean(namespace) &&
Boolean(releaseValues),
releaseValues !== undefined,
}
);
};
Expand Down Expand Up @@ -315,24 +327,13 @@ export interface Condition {
message: string;
}

// TODO: there is no need at this anymore, we can use apiService.fetchWithDefaults directly
// so this function can be removed
export async function callApi<T>(
url: string,
options?: RequestInit
): Promise<T> {
const response = await apiService.fetchWithDefaults(url, options);
const data = await apiService.fetchWithDefaults(url, options);

if (!response.ok) {
const error = await response.text();
throw new Error(error);
}

let data;
if (!response.headers.get("Content-Type")) {
return {} as T;
} else if (response.headers.get("Content-Type")?.includes("text/plain")) {
data = await response.text();
} else {
data = await response.json();
}
return data;
}
12 changes: 8 additions & 4 deletions dashboard/src/API/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ export const getVersionManifestFormData = ({
userValues,
chart,
releaseValues,
releaseName,
}: {
version: string;
userValues?: string;
chart: string;
releaseValues?: string;
releaseName?: string;
}) => {
const formData = new FormData();
// preview needs to come first, for some reason it has a meaning at the backend
Expand All @@ -21,6 +23,10 @@ export const getVersionManifestFormData = ({
"values",
userValues ? userValues : releaseValues ? releaseValues : ""
);
if (releaseName) {
formData.append("name", releaseName);
}

return formData;
};

Expand All @@ -44,21 +50,19 @@ export const useDiffData = ({
formData.append("a", currentVerManifest);
formData.append("b", (selectedVerData as any).manifest);

const response = await apiService.fetchWithDefaults("/diff", {
const diff = await apiService.fetchWithDefaults("/diff", {
method: "post",
body: formData,
});

const diff = await response.text();

return diff;
},
{
enabled:
Boolean(selectedRepo) &&
!versionsError &&
Boolean(chart) &&
Boolean(currentVerManifest) &&
currentVerManifest !== undefined &&
Boolean(selectedVerData),
}
);
Expand Down
Loading

0 comments on commit a1430b9

Please sign in to comment.