Skip to content

Commit

Permalink
feat(k8s): apply container service annotations to Pod templates as well
Browse files Browse the repository at this point in the history
When using the Kubernetes provider, the annotations set on
`services[].annotations` in container modules are now applied to both
Service and Pod resources. You can generally specify the annotations
intended for both Pods or Services here, and the ones that don't apply
on either side will be ignored (i.e. if you put a Service annotation
here, it'll also appear on Pod specs but will be safely ignored there,
and vice versa).
  • Loading branch information
edvald committed Mar 20, 2021
1 parent d6f1373 commit f5abdd4
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
6 changes: 5 additions & 1 deletion core/src/plugins/container/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,11 @@ export function getContainerVolumesSchema(targetType: string) {
const containerServiceSchema = () =>
baseServiceSpecSchema().keys({
annotations: annotationsSchema().description(
"Annotations to attach to the service (Note: May not be applicable to all providers)."
dedent`
Annotations to attach to the service _(note: May not be applicable to all providers)_.
When using the Kubernetes provider, these annotations are applied to both Service and Pod resources. You can generally specify the annotations intended for both Pods or Services here, and the ones that don't apply on either side will be ignored (i.e. if you put a Service annotation here, it'll also appear on Pod specs but will be safely ignored there, and vice versa).
`
),
command: joi
.array()
Expand Down
3 changes: 3 additions & 0 deletions core/src/plugins/kubernetes/container/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,9 @@ function workloadConfig({
selector,
template: {
metadata: {
// Note: We only have the one set of annotations for both Service and Pod resources. One intended for the
// other will just be ignored since they don't overlap in any cases I could find with commonly used tools.
annotations: service.spec.annotations,
labels,
},
spec: {
Expand Down
22 changes: 22 additions & 0 deletions core/test/integ/src/plugins/kubernetes/container/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ describe("kubernetes container deployment handlers", () => {
selector: { matchLabels: { service: "simple-service" } },
template: {
metadata: {
annotations: {},
labels: { module: "simple-service", service: "simple-service" },
},
spec: {
Expand Down Expand Up @@ -115,6 +116,27 @@ describe("kubernetes container deployment handlers", () => {
})
})

it("should attach service annotations to Pod template", async () => {
const service = graph.getService("simple-service")
const namespace = provider.config.namespace!.name!

service.spec.annotations = { "annotation.key": "someValue" }

const resource = await createWorkloadManifest({
api,
provider,
service,
runtimeContext: emptyRuntimeContext,
namespace,
enableHotReload: false,
log: garden.log,
production: false,
blueGreen: false,
})

expect(resource.spec.template?.metadata?.annotations).to.eql(service.spec.annotations)
})

it("should name the Deployment with a version suffix and set a version label if blueGreen=true", async () => {
const service = graph.getService("simple-service")
const namespace = provider.config.namespace!.name!
Expand Down
11 changes: 9 additions & 2 deletions docs/reference/module-types/container.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,12 @@ services:
# them, using conditional expressions.
disabled: false

# Annotations to attach to the service (Note: May not be applicable to all providers).
# Annotations to attach to the service _(note: May not be applicable to all providers)_.
#
# When using the Kubernetes provider, these annotations are applied to both Service and Pod resources. You can
# generally specify the annotations intended for both Pods or Services here, and the ones that don't apply on
# either side will be ignored (i.e. if you put a Service annotation here, it'll also appear on Pod specs but will
# be safely ignored there, and vice versa).
annotations: {}

# The command/entrypoint to run the container with when starting the service.
Expand Down Expand Up @@ -922,7 +927,9 @@ Note however that template strings referencing the service's outputs (i.e. runti

[services](#services) > annotations

Annotations to attach to the service (Note: May not be applicable to all providers).
Annotations to attach to the service _(note: May not be applicable to all providers)_.

When using the Kubernetes provider, these annotations are applied to both Service and Pod resources. You can generally specify the annotations intended for both Pods or Services here, and the ones that don't apply on either side will be ignored (i.e. if you put a Service annotation here, it'll also appear on Pod specs but will be safely ignored there, and vice versa).

| Type | Default | Required |
| -------- | ------- | -------- |
Expand Down
11 changes: 9 additions & 2 deletions docs/reference/module-types/maven-container.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,12 @@ services:
# them, using conditional expressions.
disabled: false

# Annotations to attach to the service (Note: May not be applicable to all providers).
# Annotations to attach to the service _(note: May not be applicable to all providers)_.
#
# When using the Kubernetes provider, these annotations are applied to both Service and Pod resources. You can
# generally specify the annotations intended for both Pods or Services here, and the ones that don't apply on
# either side will be ignored (i.e. if you put a Service annotation here, it'll also appear on Pod specs but will
# be safely ignored there, and vice versa).
annotations: {}

# The command/entrypoint to run the container with when starting the service.
Expand Down Expand Up @@ -930,7 +935,9 @@ Note however that template strings referencing the service's outputs (i.e. runti

[services](#services) > annotations

Annotations to attach to the service (Note: May not be applicable to all providers).
Annotations to attach to the service _(note: May not be applicable to all providers)_.

When using the Kubernetes provider, these annotations are applied to both Service and Pod resources. You can generally specify the annotations intended for both Pods or Services here, and the ones that don't apply on either side will be ignored (i.e. if you put a Service annotation here, it'll also appear on Pod specs but will be safely ignored there, and vice versa).

| Type | Default | Required |
| -------- | ------- | -------- |
Expand Down

0 comments on commit f5abdd4

Please sign in to comment.