Skip to content

Commit

Permalink
feat: add logs link
Browse files Browse the repository at this point in the history
  • Loading branch information
achauve committed Dec 13, 2023
1 parent 2117fdd commit 9bbdbca
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 23 deletions.
14 changes: 12 additions & 2 deletions src/components/kube/cronjob.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import dayjs from "dayjs"
import _ from "lodash"

import { Cronjob, Job } from "@/lib/kube/types"
import { Cronjob, Job, getLogsUrl } from "@/lib/kube/types"
import PodWidget from "@/components/kube/pod"
import Tooltip from "@/components/ui/tooltip"
import JsonWidget from "@/components/ui/json"
Expand All @@ -23,12 +23,22 @@ export default function CronjobWidget({ cronjob }: { cronjob: Cronjob }) {
}

function Meta({ cronjob }: { cronjob: Cronjob }) {
const logsUrl = getLogsUrl(cronjob)
return (
<div className="flex flex-row justify-between">
<span className="font-bold">{cronjob.name}</span>
<span className="text-xs text-gray-500 text-right">
<div className="font-bold capitalize">CRON</div>
<div>{dayjs(cronjob.raw?.metadata.creationTimestamp).fromNow()}</div>
<div>
{dayjs(cronjob.raw?.metadata.creationTimestamp).fromNow()}
</div>{" "}
{logsUrl && (
<div>
<a href={logsUrl} target="_blank">
Logs
</a>
</div>
)}
</span>
</div>
)
Expand Down
10 changes: 9 additions & 1 deletion src/components/kube/deployment.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dayjs from "dayjs"

import { Deployment, getDeploymentStatus } from "@/lib/kube/types"
import { Deployment, getDeploymentStatus, getLogsUrl } from "@/lib/kube/types"
import PodWidget from "@/components/kube/pod"

export default function DeploymentWidget({
Expand All @@ -24,12 +24,20 @@ export default function DeploymentWidget({
}

function Meta({ deployment }: { deployment: Deployment }) {
const logsUrl = getLogsUrl(deployment)
return (
<div className="flex flex-row justify-between">
<span className="font-bold">{deployment.name}</span>
<span className="text-xs text-gray-500 text-right">
<div className="font-bold capitalize">DEPLOY</div>
<div>{dayjs(deployment.raw.metadata.creationTimestamp).fromNow()}</div>
{logsUrl && (
<div>
<a href={logsUrl} target="_blank">
Logs
</a>
</div>
)}
</span>
</div>
)
Expand Down
40 changes: 20 additions & 20 deletions src/lib/kube/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import "server-only"

import cron from "node-cron"
import _ from "lodash"
import { $ } from "execa"
import { z, ZodTypeAny } from "zod"
import _ from "lodash"
import cron from "node-cron"
import pMap from "p-map"
import { z, ZodTypeAny } from "zod"

import {
Namespace,
clusterSchema,
RawCluster,
CachedData,
Cluster,
eventSchema,
DumpFile,
podSchema,
replicasetSchema,
clusterSchema,
Cronjob,
cronjobSchema,
Deployment,
deploymentSchema,
DumpFile,
eventSchema,
jobSchema,
cronjobSchema,
RawPod,
RawReplicaSet,
RawDeployment,
RawJob,
KubeData,
makeCachedData,
Namespace,
podSchema,
RawCluster,
RawCronjob,
RawDeployment,
RawEvent,
RawJob,
RawNamespace,
KubeData,
CachedData,
Deployment,
Cronjob,
makeCachedData,
RawPod,
RawReplicaSet,
replicasetSchema,
} from "@/lib/kube/types"
import { grepS3BucketFiles } from "@/lib/s3"

Expand Down
29 changes: 29 additions & 0 deletions src/lib/kube/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ export const deploymentSchema = z.object({
name: z.string(),
namespace: z.string(),
creationTimestamp: z.coerce.date(),
labels: z.optional(
z.object({
application: z.optional(z.string()),
app: z.optional(z.string()),
component: z.optional(z.string()),
})
),
}),
status: z.object({
availableReplicas: z.optional(z.number()),
Expand Down Expand Up @@ -247,6 +254,11 @@ export const cronjobSchema = z.object({
name: z.string(),
namespace: z.string(),
creationTimestamp: z.coerce.date(),
labels: z.object({
application: z.optional(z.string()),
app: z.optional(z.string()),
component: z.optional(z.string()),
}),
}),
status: z.object({
lastScheduleTime: z.optional(z.coerce.date()),
Expand Down Expand Up @@ -415,3 +427,20 @@ export function getNamespaceStatus(namespace: Namespace): Status {

return "ok"
}

export function getAppLabel(workload: Deployment | Cronjob): string {
const meta = workload.raw.metadata
const appLabel =
meta.labels?.component ||
meta.labels?.application ||
meta.labels?.app ||
meta.name
return appLabel
}

export function getLogsUrl(workload: Deployment | Cronjob): string | undefined {
return process.env.NEXT_PUBLIC_GRAFANA_URL_LOGS_DEPLOYMENT?.replace(
"{{namespace}}",
workload.raw.metadata.namespace
).replace("{{app}}", getAppLabel(workload))
}

0 comments on commit 9bbdbca

Please sign in to comment.