From d734dacb24a88f069fe37e05cf6b7859504db66f Mon Sep 17 00:00:00 2001 From: JGAntunes Date: Thu, 10 Oct 2024 12:46:16 +0100 Subject: [PATCH] fix(upgrade-service): allow no preflights and no config --- .../upgrade_service/PreflightChecks.tsx | 30 +++--- .../upgrade_service/UpgradeService.tsx | 99 +++++++++++++++---- .../hooks/getPreflightResult.tsx | 7 +- .../components/upgrade_service/hooks/index.ts | 4 +- .../hooks/postPreflightRun.tsx | 14 +-- 5 files changed, 107 insertions(+), 47 deletions(-) diff --git a/web/src/components/upgrade_service/PreflightChecks.tsx b/web/src/components/upgrade_service/PreflightChecks.tsx index 4376381401..8a4e0f0ac0 100644 --- a/web/src/components/upgrade_service/PreflightChecks.tsx +++ b/web/src/components/upgrade_service/PreflightChecks.tsx @@ -8,7 +8,7 @@ import SkipPreflightsModal from "@components/shared/modals/SkipPreflightsModal"; import PreflightsProgress from "@components/troubleshoot/PreflightsProgress"; import "../../scss/components/PreflightCheckPage.scss"; -import { useGetPrelightResults, useRerunPreflights } from "./hooks/index"; +import { useGetPrelightResults, useRunPreflights } from "./hooks/index"; import { KotsParams } from "@types"; import { useUpgradeServiceContext } from "./UpgradeServiceContext"; @@ -35,10 +35,13 @@ const PreflightCheck = ({ const { sequence = "0", slug } = useParams() as KotsParams; + const { + mutate: runPreflights, + error: runPreflightsError, + isSuccess: runPreflightsSuccess, + } = useRunPreflights({ slug, sequence }); const { data: preflightCheck, error: getPreflightResultsError } = - useGetPrelightResults({ slug, sequence }); - const { mutate: rerunPreflights, error: rerunPreflightsError } = - useRerunPreflights({ slug, sequence }); + useGetPrelightResults({ slug, sequence, enabled: runPreflightsSuccess }); if (!preflightCheck?.showPreflightCheckPending) { if (showConfirmIgnorePreflightsModal) { @@ -48,12 +51,17 @@ const PreflightCheck = ({ useEffect(() => { setCurrentStep(1); - + // Config changed so we'll re-run the preflights + if (!isEqual(prevConfig, config)) { + runPreflights(); + return; + } + // No preflight results means we haven't run them yet, let's do that if ( - !isEqual(prevConfig, config) && - preflightCheck?.preflightResults.length > 0 + !preflightCheck?.preflightResults || + preflightCheck.preflightResults.length === 0 ) { - rerunPreflights(); + runPreflights(); } }, []); @@ -82,12 +90,12 @@ const PreflightCheck = ({ )} - {rerunPreflightsError?.message && ( + {runPreflightsError?.message && (

Encountered an error

-

{rerunPreflightsError.message}

+

{runPreflightsError.message}

)} @@ -126,7 +134,7 @@ const PreflightCheck = ({ diff --git a/web/src/components/upgrade_service/UpgradeService.tsx b/web/src/components/upgrade_service/UpgradeService.tsx index fcaf705729..7029efaf2c 100644 --- a/web/src/components/upgrade_service/UpgradeService.tsx +++ b/web/src/components/upgrade_service/UpgradeService.tsx @@ -1,4 +1,4 @@ -import { Route, Routes, Navigate } from "react-router-dom"; +import { Route, Routes, Navigate, useMatch } from "react-router-dom"; import { Helmet } from "react-helmet"; import NotFound from "@components/static/NotFound"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; @@ -8,12 +8,13 @@ import AppConfig from "@components/upgrade_service/AppConfig"; // types import { ToastProvider } from "@src/context/ToastContext"; import StepIndicator from "./StepIndicator"; -import { useState } from "react"; +import { useEffect, useState } from "react"; import PreflightChecks from "./PreflightChecks"; import ConfirmAndDeploy from "./ConfirmAndDeploy"; import { KotsPageTitle } from "@components/Head"; import { UpgradeServiceProvider } from "./UpgradeServiceContext"; +import Loader from "@components/shared/Loader"; // react-query client const queryClient = new QueryClient(); @@ -22,6 +23,39 @@ const UpgradeService = () => { throw new Error("Crashz!"); }; const [currentStep, setCurrentStep] = useState(0); + const [isConfigurable, setIsConfigurable] = useState(false); + const [hasPreflight, setHasPreflight] = useState(false); + const [isLoading, setIsLoading] = useState(true); + + const { params } = useMatch("/upgrade-service/app/:slug/*"); + + useEffect(() => { + fetch(`${process.env.API_ENDPOINT}/upgrade-service/app/${params.slug}`, { + method: "GET", + headers: { + "Content-Type": "application/json", + }, + credentials: "include", + }) + .then(async (response) => { + if (!response.ok) { + const res = await response.json(); + throw new Error(res.error); + } + const data = (await response.json()) as { + isConfigurable: boolean; + hasPreflight: boolean; + }; + console.log("Configuration check"); + console.log(data); + setHasPreflight(data.hasPreflight); + setIsConfigurable(data.isConfigurable); + setIsLoading(false); + }) + .catch((err) => { + // TODO handle error + }); + }, []); return ( @@ -42,25 +76,48 @@ const UpgradeService = () => { value={currentStep} className="tw-my-8" /> - - } />{" "} - - } /> - } - /> - } - /> - } - /> - - } /> - + {isLoading ? ( +
+ + Checking required steps... + + +
+ ) : ( + + } /> + + } /> + + ) : ( + + ) + } + /> + + ) : ( + + ) + } + /> + + } + /> + + } /> + + )}
diff --git a/web/src/components/upgrade_service/hooks/getPreflightResult.tsx b/web/src/components/upgrade_service/hooks/getPreflightResult.tsx index 4bc412f55f..e44314c87c 100644 --- a/web/src/components/upgrade_service/hooks/getPreflightResult.tsx +++ b/web/src/components/upgrade_service/hooks/getPreflightResult.tsx @@ -8,9 +8,7 @@ import { useState } from "react"; async function getPreflightResult({ slug, }: { - apiEndpoint?: string; slug: string; - sequence?: string; }): Promise { const jsonResponse = await fetch( `${process.env.API_ENDPOINT}/upgrade-service/app/${slug}/preflight/result`, @@ -170,9 +168,11 @@ function makeRefetchInterval(preflightCheck: PreflightCheck): number | false { function useGetPrelightResults({ slug, sequence, + enabled = true, }: { slug: string; sequence?: string; + enabled?: boolean; }) { // this is for the progress bar const [refetchCount, setRefetchCount] = useState(0); @@ -181,8 +181,9 @@ function useGetPrelightResults({ queryFn: () => { setRefetchCount(refetchCount + 1); - return getPreflightResult({ slug, sequence }); + return getPreflightResult({ slug }); }, + enabled, queryKey: ["preflight-results", sequence, slug], onError: (err: Error) => { console.log(err); diff --git a/web/src/components/upgrade_service/hooks/index.ts b/web/src/components/upgrade_service/hooks/index.ts index 0380064401..453d5768c4 100644 --- a/web/src/components/upgrade_service/hooks/index.ts +++ b/web/src/components/upgrade_service/hooks/index.ts @@ -1,5 +1,5 @@ import { useGetPrelightResults } from "./getPreflightResult"; -import { useRerunPreflights } from "./postPreflightRun"; +import { useRunPreflights } from "./postPreflightRun"; import { useDeployAppVersion } from "./postDeployAppVersion"; -export { useGetPrelightResults, useRerunPreflights, useDeployAppVersion }; +export { useGetPrelightResults, useRunPreflights, useDeployAppVersion }; diff --git a/web/src/components/upgrade_service/hooks/postPreflightRun.tsx b/web/src/components/upgrade_service/hooks/postPreflightRun.tsx index 6905c607ee..260ba19168 100644 --- a/web/src/components/upgrade_service/hooks/postPreflightRun.tsx +++ b/web/src/components/upgrade_service/hooks/postPreflightRun.tsx @@ -1,12 +1,6 @@ import { useMutation, useQueryClient } from "@tanstack/react-query"; -async function postPreflightRun({ - slug, -}: { - apiEndpoint?: string; - slug: string; - sequence: string; -}) { +async function postPreflightRun({ slug }: { slug: string }) { const response = await fetch( `${process.env.API_ENDPOINT}/upgrade-service/app/${slug}/preflight/run`, { @@ -26,7 +20,7 @@ async function postPreflightRun({ } } -function useRerunPreflights({ +function useRunPreflights({ slug, sequence, }: { @@ -36,7 +30,7 @@ function useRerunPreflights({ const queryClient = useQueryClient(); return useMutation({ - mutationFn: () => postPreflightRun({ slug, sequence }), + mutationFn: () => postPreflightRun({ slug }), onError: (err: Error) => { console.log(err); throw new Error(err.message || "Error running preflight checks"); @@ -49,4 +43,4 @@ function useRerunPreflights({ }); } -export { useRerunPreflights }; +export { useRunPreflights };