Skip to content

Commit

Permalink
improvement(k8s): add more metadata env vars to container Pod specs
Browse files Browse the repository at this point in the history
Specifically adding POD_HOST_IP, POD_UID and POD_NODE_NAME. This is
much simpler than adding `valueFrom` to the container module spec, and
I think it's fine to add any standard/common fields like this in our
code.

Fixes #1703 (partially at least)
  • Loading branch information
edvald committed Mar 29, 2020
1 parent ec0c99d commit e1d4bf4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
19 changes: 17 additions & 2 deletions garden-service/src/plugins/kubernetes/container/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,16 @@ export async function createWorkloadManifest({
const env = prepareEnvVars({ ...runtimeContext.envVars, ...service.spec.env })

// expose some metadata to the container
env.push({
name: "POD_HOST_IP",
valueFrom: { fieldRef: { fieldPath: "status.hostIP" } },
})

env.push({
name: "POD_IP",
valueFrom: { fieldRef: { fieldPath: "status.podIP" } },
})

env.push({
name: "POD_NAME",
valueFrom: { fieldRef: { fieldPath: "metadata.name" } },
Expand All @@ -283,15 +293,20 @@ export async function createWorkloadManifest({
})

env.push({
name: "POD_IP",
valueFrom: { fieldRef: { fieldPath: "status.podIP" } },
name: "POD_NODE_NAME",
valueFrom: { fieldRef: { fieldPath: "spec.nodeName" } },
})

env.push({
name: "POD_SERVICE_ACCOUNT",
valueFrom: { fieldRef: { fieldPath: "spec.serviceAccountName" } },
})

env.push({
name: "POD_UID",
valueFrom: { fieldRef: { fieldPath: "metadata.uid" } },
})

const registryConfig = provider.config.deploymentRegistry
const imageId = await containerHelpers.getDeploymentImageId(service.module, registryConfig)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,13 @@ describe("kubernetes container deployment handlers", () => {
image: "simple-service:" + version,
command: ["sh", "-c", "echo Server running... && nc -l -p 8080"],
env: [
{ name: "POD_HOST_IP", valueFrom: { fieldRef: { fieldPath: "status.hostIP" } } },
{ name: "POD_IP", valueFrom: { fieldRef: { fieldPath: "status.podIP" } } },
{ name: "POD_NAME", valueFrom: { fieldRef: { fieldPath: "metadata.name" } } },
{ name: "POD_NAMESPACE", valueFrom: { fieldRef: { fieldPath: "metadata.namespace" } } },
{ name: "POD_IP", valueFrom: { fieldRef: { fieldPath: "status.podIP" } } },
{ name: "POD_NODE_NAME", valueFrom: { fieldRef: { fieldPath: "spec.nodeName" } } },
{ name: "POD_SERVICE_ACCOUNT", valueFrom: { fieldRef: { fieldPath: "spec.serviceAccountName" } } },
{ name: "POD_UID", valueFrom: { fieldRef: { fieldPath: "metadata.uid" } } },
],
ports: [{ name: "http", protocol: "TCP", containerPort: 8080 }],
resources: { requests: { cpu: "10m", memory: "64Mi" }, limits: { cpu: "1", memory: "1Gi" } },
Expand Down

0 comments on commit e1d4bf4

Please sign in to comment.