Просмотр исходного кода

Merge pull request #2553 from porter-dev/belanger/add-pod-query-for-logging

Handle blue-green deployments from `LogSection` and fallback to live pods when `pod_values` fails
abelanger5 3 лет назад
Родитель
Сommit
2a1edbe2d8

+ 56 - 16
dashboard/src/main/home/cluster-dashboard/expanded-chart/logs-section/LogsSection.tsx

@@ -138,32 +138,72 @@ const LogsSection: React.FC<Props> = ({
     selectedDate
   );
 
-  useEffect(() => {
+  const refreshPodLogsValues = async () => {
     if (overridingPodName) {
       return;
     }
 
-    api
-      .getLogPodValues(
+    const filters = {
+      namespace: currentChart.namespace,
+      revision: initData.revision ?? currentChart.version.toString(),
+      match_prefix: currentChart.name,
+    };
+
+    // if the current chart is set to a blue-green deployment, we don't set a revision, but instead
+    // we set the match prefix to the current chart and the active image tag.
+    if (currentChart.config.bluegreen?.enabled) {
+      filters.revision = null;
+
+      if (currentChart?.name.includes("web")) {
+        filters.match_prefix = `${currentChart.name}-${currentChart.config.bluegreen?.activeImageTag}`;
+      } else {
+        filters.match_prefix = `${currentChart.name}-web-${currentChart.config.bluegreen?.activeImageTag}`;
+      }
+    }
+
+    const logPodValuesResp = await api.getLogPodValues("<TOKEN>", filters, {
+      project_id: currentProject.id,
+      cluster_id: currentCluster.id,
+    });
+
+    if (logPodValuesResp.data?.length != 0) {
+      setPodFilterOpts(_.uniq(logPodValuesResp.data ?? []));
+
+      // only set pod filter if the current pod is not found in the resulting data
+      if (!logPodValuesResp.data?.includes(podFilter)) {
+        setPodFilter(logPodValuesResp.data[0]);
+      }
+
+      return;
+    }
+
+    // if we're on the latest revision and no pod values were returned, query for all release pods
+    if (currentChart.info.status == "deployed") {
+      const allReleasePodsResp = await api.getAllReleasePods(
         "<TOKEN>",
+        {},
         {
-          namespace: currentChart?.namespace,
-          revision: initData.revision ?? currentChart.version.toString(),
-          match_prefix: currentChart.name,
-        },
-        {
-          project_id: currentProject.id,
+          id: currentProject.id,
+          name: currentChart.name,
+          namespace: currentChart.namespace,
           cluster_id: currentCluster.id,
         }
-      )
-      .then((res: any) => {
-        setPodFilterOpts(_.uniq(res.data ?? []));
+      );
 
-        // only set pod filter if the current pod is not found in the resulting data
-        if (!res.data?.includes(podFilter)) {
-          setPodFilter(res.data[0]);
-        }
+      let podList = allReleasePodsResp.data.map((pod: any) => {
+        return pod.metadata.name;
       });
+
+      setPodFilterOpts(podList);
+
+      if (!podFilter || !podList.includes(podFilter)) {
+        setPodFilter(podList[0]);
+      }
+    }
+  };
+
+  useEffect(() => {
+    refreshPodLogsValues();
   }, [initData]);
 
   useEffect(() => {

+ 15 - 0
dashboard/src/shared/api.tsx

@@ -1069,6 +1069,20 @@ const getMatchingPods = baseApi<
   return `/api/projects/${pathParams.id}/clusters/${pathParams.cluster_id}/pods`;
 });
 
+const getAllReleasePods = baseApi<
+  {},
+  {
+    id: number;
+    name: string;
+    namespace: string;
+    cluster_id: number;
+  }
+>("GET", (pathParams) => {
+  const { id, name, cluster_id, namespace } = pathParams;
+
+  return `/api/projects/${id}/clusters/${cluster_id}/namespaces/${namespace}/releases/${name}/0/pods/all`;
+});
+
 const getMetrics = baseApi<
   {
     metric: string;
@@ -2376,6 +2390,7 @@ export default {
   getJobPods,
   getPodByName,
   getMatchingPods,
+  getAllReleasePods,
   getMetrics,
   getNamespaces,
   getNGINXIngresses,