Skip to content

Commit 64504f4

Browse files
authored
Adding options on separate vs. single ServiceAccount (#158)
2 parents 6b36d1e + fe54acd commit 64504f4

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

resource-definitions/template-driver/serviceaccount/README.md

+34-4
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,42 @@ The [`workload` Resource Type](https://developer.humanitec.com/platform-orchestr
66

77
This `workload` Resource Definition adds the `serviceAccountName` item to the Pod spec and references a [`k8s-service-account` type Resource](https://developer.humanitec.com/platform-orchestrator/reference/resource-types/#k8s-service-account), causing it to be provisioned. The `k8s-service-account` Resource Definition generates the Kubernetes manifest for the actual ServiceAccount.
88

9-
A Resource Graph for a Workload using those Resource Definitions will look like this:
9+
The examples demonstrates two alternative approaches:
10+
11+
1. Providing a separate Kubernetes ServiceAccount for each Workload
12+
13+
This approach lets you fine tune the permissions obtained via the ServiceAccount for each Workload, but create more objects in the Resource Graph and on the cluster.
14+
15+
2. Providing a single Kubernetes ServiceAccount for all Workloads in the same Application Environment
16+
17+
This approach results in unified permissions for each Workload and less objects in the Resource Graph and on the cluster
18+
19+
For option 1, a Resource Graph for Workloads using those Resource Definitions will look like this:
1020

1121
```mermaid
1222
flowchart LR
13-
workloadVirtual[Workload &quot;my-workload&quot;] --> workload(id: modules.my-workload<br/>type: workload<br/>class: default)
14-
workload --> serviceAccount(id: modules.my-workload<br/>type: k8s-service-account<br/>class: default)
23+
workloadVirtual1[Workload &quot;my-workload-1&quot;<br/>defined via Score] -.-> workload1(id: modules.my-workload-1<br/>type: workload<br/>class: default)
24+
workload1 --> serviceAccount1(id: modules.my-workload-1<br/>type: k8s-service-account<br/>class: default)
25+
workloadVirtual2[Workload &quot;my-workload-2&quot;<br/>defined via Score] -.-> workload2(id: modules.my-workload-2<br/>type: workload<br/>class: default)
26+
workload2 --> serviceAccount2(id: modules.my-workload-2<br/>type: k8s-service-account<br/>class: default)
27+
28+
classDef dotted stroke-dasharray: 5 5;
29+
class workloadVirtual1,workloadVirtual2 dotted
1530
```
1631

17-
Note that the resource `id` is used in the `k8s-service-account` Resource Definition to derive the name of the actual Kubernetes ServiceAccount. Check the code for details.
32+
For option 2, a Resource Graph for Workloads using those Resource Definitions will look like this:
33+
34+
```mermaid
35+
flowchart LR
36+
workloadVirtual1[Workload &quot;my-workload-1&quot;<br/>defined via Score] -.-> workload1(id: modules.my-workload-1<br/>type: workload<br/>class: default)
37+
workload1 --> serviceAccount(id: ksa<br/>type: k8s-service-account<br/>class: default)
38+
workloadVirtual2[Workload &quot;my-workload-2&quot;<br/>defined via Score] -.-> workload2(id: modules.my-workload-2<br/>type: workload<br/>class: default)
39+
workload2 --> serviceAccount
40+
41+
classDef dotted stroke-dasharray: 5 5;
42+
class workloadVirtual1,workloadVirtual2 dotted
43+
```
44+
45+
Check the code in the Resource Definitions to activate the option you wish to use.
46+
47+
In both cases, the resource `id` is used in the `k8s-service-account` Resource Definition to derive the name of the Kubernetes ServiceAccount.

resource-definitions/template-driver/serviceaccount/serviceaccount-k8ssa-def.yaml

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ entity:
1212
res_id: ${context.res.id}
1313
templates:
1414
init: |
15-
name: {{ index ( .driver.values.res_id | splitList "." ) 1 }}
15+
res_id: {{ .driver.values.res_id }}
16+
{{- $res_name := .driver.values.res_id | splitList "." | last }}
17+
name: {{ $res_name | toRawJson }}
1618
outputs: |
1719
name: {{ .init.name }}
1820
manifests: |
@@ -23,3 +25,5 @@ entity:
2325
kind: ServiceAccount
2426
metadata:
2527
name: {{ .init.name }}
28+
annotations:
29+
hum-res: {{ .init.res_id }}

resource-definitions/template-driver/serviceaccount/serviceaccount-workload-def.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# This Resource Definition adds a Kubernetes ServiceAccount to a Workload
2+
# Note the inline comments on adjusting the setup
23
apiVersion: entity.humanitec.io/v1b1
34
kind: Definition
45
metadata:
@@ -14,4 +15,7 @@ entity:
1415
update:
1516
- op: add
1617
path: /spec/serviceAccountName
18+
# Option 1: separate ServiceAccount per workload. Using the current workload's ID by not specifying an ID
1719
value: ${resources.k8s-service-account.outputs.name}
20+
# Option 2: single ServiceAccount for all workloads. Specifying a fixed ID, e.g. "ksa"
21+
# value: ${resources.k8s-service-account#ksa.outputs.name}

0 commit comments

Comments
 (0)