Skip to content

Commit

Permalink
improvement(logger): various tweaks to log lines (#5452)
Browse files Browse the repository at this point in the history
Namely:

- Show provider name in the section for `ephemeral-kubernetes`
- Add origin to K8s logs
- Add origin to build util logs

Note that adding `origin` to a given log context means that it'll be
printed with the log line.

So instead of:

`deploy.api => Deployment/web: Started container web`

We print:

`deploy.api => [kubernetes] Deployment/web: Started container web`
  • Loading branch information
eysi09 committed Nov 21, 2023
1 parent 4f95f26 commit 11f7614
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
23 changes: 12 additions & 11 deletions core/src/plugins/kubernetes/container/build/common.ts
Expand Up @@ -346,11 +346,11 @@ export async function ensureUtilDeployment({
})

return deployLock.acquire(namespace, async () => {
const deployLog = log.createLog()
const buildUtilLog = log.createLog({ origin: "build-util" })

const { authSecret, updated: secretUpdated } = await ensureBuilderSecret({
provider,
log,
log: buildUtilLog,
api,
namespace,
})
Expand All @@ -364,13 +364,13 @@ export async function ensureUtilDeployment({
api,
namespace,
manifests: [deployment, service],
log: deployLog,
log: buildUtilLog,
})

// if the service account changed, all pods part of the deployment must be restarted
// so that they receive new credentials (e.g. for IRSA)
if (status.remoteResources.length > 0 && serviceAccountChanged) {
await cycleDeployment({ ctx, provider, deployment, api, namespace, deployLog })
await cycleDeployment({ ctx, provider, deployment, api, namespace, deployLog: buildUtilLog })
}

if (status.state === "ready") {
Expand All @@ -382,24 +382,24 @@ export async function ensureUtilDeployment({
}

// Deploy the service
deployLog.info(
styles.primary(`-> Deploying ${utilDeploymentName} service in ${namespace} namespace (was ${status.state})`)
buildUtilLog.info(
`Deploying ${utilDeploymentName} service in ${styles.highlight(namespace)} namespace (was ${status.state})`
)

await api.upsert({ kind: "Deployment", namespace, log: deployLog, obj: deployment })
await api.upsert({ kind: "Service", namespace, log: deployLog, obj: service })
await api.upsert({ kind: "Deployment", namespace, log: buildUtilLog, obj: deployment })
await api.upsert({ kind: "Service", namespace, log: buildUtilLog, obj: service })

await waitForResources({
namespace,
ctx,
provider,
actionName: "garden-util",
resources: [deployment, service],
log: deployLog,
log: buildUtilLog,
timeoutSec: 600,
})

deployLog.info("Done!")
buildUtilLog.success("Done")

return { authSecret, updated: true }
})
Expand Down Expand Up @@ -490,7 +490,8 @@ export async function ensureBuilderSecret({
const existingSecret = await api.readBySpecOrNull({ log, namespace, manifest: authSecret })

if (!existingSecret || authSecret.data?.[dockerAuthSecretKey] !== existingSecret.data?.[dockerAuthSecretKey]) {
log.info(styles.primary(`-> Updating Docker auth secret in namespace ${namespace}`))
const reason = !existingSecret ? "was missing" : "has changed"
log.info(`Updating Docker auth secret in namespace ${styles.highlight(namespace)} (${reason})`)
await api.upsert({ kind: "Secret", namespace, log, obj: authSecret })
updated = true
}
Expand Down
24 changes: 12 additions & 12 deletions core/src/plugins/kubernetes/ephemeral/config.ts
Expand Up @@ -45,9 +45,10 @@ export const configSchema = () =>
.description(`The provider configuration for the ${EPHEMERAL_KUBERNETES_PROVIDER_NAME} plugin.`)

export async function configureProvider(params: ConfigureProviderParams<KubernetesConfig>) {
const { base, log, projectName, ctx, config: baseConfig } = params
const { base, log, ctx, config: baseConfig } = params

const providerLog = log.createLog({ name: ctx.provider.name }).info("Configuring provider...")

log.info(`Configuring ${EPHEMERAL_KUBERNETES_PROVIDER_NAME} provider for project ${projectName}`)
if (!ctx.cloudApi) {
throw new ConfigurationError({
message: `You are not logged in. You must be logged into Garden Cloud in order to use ${EPHEMERAL_KUBERNETES_PROVIDER_NAME} provider.`,
Expand All @@ -63,32 +64,31 @@ export async function configureProvider(params: ConfigureProviderParams<Kubernet
const ephemeralClusterDirPath = join(ctx.gardenDirPath, "ephemeral-kubernetes")
await mkdirp(ephemeralClusterDirPath)

log.info("Retrieving ephemeral Kubernetes cluster")
providerLog.info("Getting cluster info...")
const createEphemeralClusterResponse = await ctx.cloudApi.createEphemeralCluster()
const clusterId = createEphemeralClusterResponse.instanceMetadata.instanceId
log.info(`Ephemeral Kubernetes cluster retrieved successfully`)

const deadlineDateTime = moment(createEphemeralClusterResponse.instanceMetadata.deadline)
const diffInNowAndDeadline = moment.duration(deadlineDateTime.diff(moment())).asMinutes().toFixed(1)
log.info(
styles.accent(
`Ephemeral cluster will be destroyed in ${diffInNowAndDeadline} minutes, at ${deadlineDateTime.format(
"YYYY-MM-DD HH:mm:ss"
)}`
)
providerLog.info(
`Cluster will be destroyed in ${styles.highlight(diffInNowAndDeadline)} minutes (at ${deadlineDateTime.format(
"YYYY-MM-DD HH:mm:ss"
)})`
)

log.info("Fetching kubeconfig for the ephemeral cluster")
providerLog.info("Getting kubeconfig...")
const kubeConfig = await ctx.cloudApi.getKubeConfigForCluster(clusterId)
const kubeconfigFileName = `${clusterId}-kubeconfig.yaml`
const kubeConfigPath = join(ctx.gardenDirPath, "ephemeral-kubernetes", kubeconfigFileName)
await writeFile(kubeConfigPath, kubeConfig)
log.info(`Kubeconfig for ephemeral cluster saved at path: ${styles.underline(kubeConfigPath)}`)
providerLog.info(`Kubeconfig saved at local path: ${styles.highlight(kubeConfigPath)}`)

const parsedKubeConfig: any = load(kubeConfig)
baseConfig.context = parsedKubeConfig["current-context"]
baseConfig.kubeconfig = kubeConfigPath

providerLog.success("Done")

// set deployment registry
baseConfig.deploymentRegistry = {
hostname: createEphemeralClusterResponse.registry.endpointAddress,
Expand Down
1 change: 1 addition & 0 deletions core/src/plugins/kubernetes/status/status.ts
Expand Up @@ -289,6 +289,7 @@ export async function waitForResources({
.createLog({
// TODO: Avoid setting fallback, the action name should be known
name: actionName || "<kubernetes>",
origin: "kubernetes",
})
.info(waitingMsg)
emitLog(waitingMsg)
Expand Down

0 comments on commit 11f7614

Please sign in to comment.