diff --git a/resource-definitions/template-driver/serviceaccount/README.md b/resource-definitions/template-driver/serviceaccount/README.md index cb09364..85e78c4 100644 --- a/resource-definitions/template-driver/serviceaccount/README.md +++ b/resource-definitions/template-driver/serviceaccount/README.md @@ -6,12 +6,42 @@ The [`workload` Resource Type](https://developer.humanitec.com/platform-orchestr 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. -A Resource Graph for a Workload using those Resource Definitions will look like this: +The examples demonstrates two alternative approaches: + +1. Providing a separate Kubernetes ServiceAccount for each Workload + + 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. + +2. Providing a single Kubernetes ServiceAccount for all Workloads in the same Application Environment + + This approach results in unified permissions for each Workload and less objects in the Resource Graph and on the cluster + +For option 1, a Resource Graph for Workloads using those Resource Definitions will look like this: ```mermaid flowchart LR - workloadVirtual[Workload "my-workload"] --> workload(id: modules.my-workload
type: workload
class: default) - workload --> serviceAccount(id: modules.my-workload
type: k8s-service-account
class: default) + workloadVirtual1[Workload "my-workload-1"
defined via Score] -.-> workload1(id: modules.my-workload-1
type: workload
class: default) + workload1 --> serviceAccount1(id: modules.my-workload-1
type: k8s-service-account
class: default) + workloadVirtual2[Workload "my-workload-2"
defined via Score] -.-> workload2(id: modules.my-workload-2
type: workload
class: default) + workload2 --> serviceAccount2(id: modules.my-workload-2
type: k8s-service-account
class: default) + + classDef dotted stroke-dasharray: 5 5; + class workloadVirtual1,workloadVirtual2 dotted ``` -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. \ No newline at end of file +For option 2, a Resource Graph for Workloads using those Resource Definitions will look like this: + +```mermaid +flowchart LR + workloadVirtual1[Workload "my-workload-1"
defined via Score] -.-> workload1(id: modules.my-workload-1
type: workload
class: default) + workload1 --> serviceAccount(id: ksa
type: k8s-service-account
class: default) + workloadVirtual2[Workload "my-workload-2"
defined via Score] -.-> workload2(id: modules.my-workload-2
type: workload
class: default) + workload2 --> serviceAccount + + classDef dotted stroke-dasharray: 5 5; + class workloadVirtual1,workloadVirtual2 dotted +``` + +Check the code in the Resource Definitions to activate the option you wish to use. + +In both cases, the resource `id` is used in the `k8s-service-account` Resource Definition to derive the name of the Kubernetes ServiceAccount. \ No newline at end of file diff --git a/resource-definitions/template-driver/serviceaccount/serviceaccount-k8ssa-def.yaml b/resource-definitions/template-driver/serviceaccount/serviceaccount-k8ssa-def.yaml index 3beb044..ef5a769 100644 --- a/resource-definitions/template-driver/serviceaccount/serviceaccount-k8ssa-def.yaml +++ b/resource-definitions/template-driver/serviceaccount/serviceaccount-k8ssa-def.yaml @@ -12,7 +12,9 @@ entity: res_id: ${context.res.id} templates: init: | - name: {{ index ( .driver.values.res_id | splitList "." ) 1 }} + res_id: {{ .driver.values.res_id }} + {{- $res_name := .driver.values.res_id | splitList "." | last }} + name: {{ $res_name | toRawJson }} outputs: | name: {{ .init.name }} manifests: | @@ -23,3 +25,5 @@ entity: kind: ServiceAccount metadata: name: {{ .init.name }} + annotations: + hum-res: {{ .init.res_id }} diff --git a/resource-definitions/template-driver/serviceaccount/serviceaccount-workload-def.yaml b/resource-definitions/template-driver/serviceaccount/serviceaccount-workload-def.yaml index 099dde4..d5e8667 100644 --- a/resource-definitions/template-driver/serviceaccount/serviceaccount-workload-def.yaml +++ b/resource-definitions/template-driver/serviceaccount/serviceaccount-workload-def.yaml @@ -1,4 +1,5 @@ # This Resource Definition adds a Kubernetes ServiceAccount to a Workload +# Note the inline comments on adjusting the setup apiVersion: entity.humanitec.io/v1b1 kind: Definition metadata: @@ -14,4 +15,7 @@ entity: update: - op: add path: /spec/serviceAccountName + # Option 1: separate ServiceAccount per workload. Using the current workload's ID by not specifying an ID value: ${resources.k8s-service-account.outputs.name} + # Option 2: single ServiceAccount for all workloads. Specifying a fixed ID, e.g. "ksa" + # value: ${resources.k8s-service-account#ksa.outputs.name}