-
Notifications
You must be signed in to change notification settings - Fork 39
Add jobrequest technical docs #5659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e0350ae
Update the version of mermaid to 11.17.2 to get the person shape in f…
jfharden a3a7a54
Add JobRequest system technical and architecture docs
jfharden bf1b398
Rewrite list to use present tense
jfharden 8b56a9b
Update diagrams for legibility and accuracy
jfharden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
275 changes: 272 additions & 3 deletions
275
source/kubernetes/job-requests/architecture/index.html.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,278 @@ | ||
| --- | ||
| title: Job Request Architecture | ||
| title: JobRequest Architecture | ||
| weight: 10 | ||
| layout: multipage_layout | ||
| --- | ||
|
|
||
| # Job Request Architecture | ||
| # JobRequest Architecture | ||
|
|
||
| Coming soon... | ||
| ## System Components | ||
|
|
||
| * [govuk-job-request-operator](/repos/govuk-job-request-operator.html): | ||
| This includes: | ||
| * [Custom Resource Defitinitions | ||
| (CRDs)](https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/) | ||
| for JobRequest (short name `jr`) and JobRequestReview (short name `jrr`) | ||
| resources | ||
| * [Kubernetes | ||
| Operator](https://kubernetes.io/docs/concepts/extend-kubernetes/operator/) | ||
| which is responsible for reconcilling the JobRequest and JobRequestReview | ||
| resources | ||
| * [Kubernetes Mutating Admission | ||
| Policy](https://kubernetes.io/docs/reference/access-authn-authz/mutating-admission-policy/) | ||
| which is responsible for validating, and adding the AWS ARN of the | ||
| requesting user into annotations on the JobRequest and JobRequestReview | ||
| during admission | ||
| * [Garbage | ||
| collection](https://kubernetes.io/docs/concepts/architecture/garbage-collection/) | ||
| to clean up JobRequest and JobRequestReview resoureces more than 30 days old | ||
| * [OPA | ||
| Gatekeeper](https://kubernetes.io/blog/2019/08/06/opa-gatekeeper-policy-and-governance-for-kubernetes/) | ||
| [policies](https://github.com/alphagov/govuk-infrastructure/tree/main/terraform/deployments/cluster-services/modules/gatekeeper) | ||
| responsible for validating API requests to create/update JobRequest and | ||
| JobRequestReview resources, and providing useful error messages to the end user | ||
| if the request is denied. | ||
| * [govuk-cli](/repos/govuk-job-request-operator.html): A command line interface | ||
| with a jobrequest subcommand which is the primary interface developers are | ||
| expected to use to create and review JobRequests. This also does validation | ||
| prior to making create requests to the Kubernetes API to give the most friendly | ||
| error messages possible. | ||
|
|
||
| ## Expected usage | ||
|
|
||
| The following describes the intended happy path from creation of a JobRequest to Job completion. | ||
|
|
||
| 1. User A uses `govuk-cli` to create a JobRequest, it prints out the CLI command which can be used to review the JobRequest, and waits to show the logs from the Job that will be created later. | ||
| 2. User A sends User B the printed out CLI command to run in order to review the request. | ||
| 3. User B runs the CLI command, reads and reviews the command intended to run, chooses to approve or reject it, then `govuk-cli` creates a JobRequestReview. | ||
| 4. Review outcome: | ||
| a. If user B rejects the JobRequest, assuming user A is still following the logs, user A sees a message telling them the request was rejected. This flow ends here. | ||
| b. If user B approves the JobRequest then continue this process | ||
| 5. The JobRequest operator changes the Status of the JobRequest to approved. | ||
| 6. The JobRequest operator reads the pod spec it needs to create from the target resource specified in the JobRequest | ||
| 7. The JobRequest operator creates a Kubernetes Job from the pod spec retrieved in the previous step, overriding the command to be the one approved in the JobRequest | ||
| 8. The JobRequest operator updates the Status of the JobRequest to include the name of the Job that was created | ||
| 9. The govuk-cli command that was run in step 1. sees the Job has been created, informs user A, and starts printing out the logs of the Job as they are produced. | ||
| 9. The JobRequest operator watches the status of the Job and updates the JobRequest status to include the current state of the Job. | ||
| 10. When the status of the Job reaches a terminal state, the govuk-cli stops following the logs and informs User A the Job has completed. | ||
|
|
||
| ### Sequence Diagram for an Approved JobRequest | ||
|
|
||
| <pre lang="mermaid"> | ||
|
|
||
| <code> | ||
| sequenceDiagram | ||
| actor Requester | ||
| actor Reviewer | ||
| participant k8sAPI as Kubernetes API | ||
| participant Operator | ||
|
|
||
| Requester->>k8sAPI: `govuk-cli create jobrequest... --follow` | ||
| k8sAPI->>Operator: Job Request Created | ||
| Requester->>Reviewer: Please review my request | ||
| Reviewer->>k8sAPI: `govuk-cli jobrequest review...` Approved | ||
| k8sAPI->>Operator: JobRequestReview Created | ||
| Operator->>k8sAPI: Create Job | ||
| k8sAPI->>Requester: Logs | ||
| k8sAPI->>Requester: More Logs | ||
| k8sAPI->>Requester: Job Complete | ||
| </code> | ||
| </pre> | ||
|
|
||
| ### Sequence Diagram for a Rejected JobRequest | ||
|
|
||
| <pre lang="mermaid"> | ||
|
|
||
| <code> | ||
| sequenceDiagram | ||
| actor Requester | ||
| actor Reviewer | ||
| participant k8sAPI as Kubernetes API | ||
| participant Operator | ||
|
|
||
| Requester->>k8sAPI: `govuk-cli create jobrequest... --follow` | ||
| k8sAPI->>Operator: JobRequest Created | ||
| Requester->>Reviewer: Please review my request | ||
| Reviewer->>k8sAPI: `govuk-cli jobrequest review...` Rejected | ||
| k8sAPI->>Operator: Rejected JobRequestReview Created | ||
| Operator->>k8sAPI: Update JobRequest to Rejected | ||
| k8sAPI->>Requester: JobRequest Rejected | ||
| </code> | ||
| </pre> | ||
|
|
||
| ## Garbage Collection | ||
|
|
||
| Any time a JobRequest or JobRequestReview resource is presented for | ||
| reconcilliation, if it has lived longer than the TTL duration (which is set | ||
| for 720 hours (30 days)), it will be deleted. | ||
|
|
||
| The [Kubernetes Controller Runtime | ||
| configuration](https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/cache#Config) | ||
| includes a SyncPeriod, any resources managed by the controller runtime will be | ||
| presented to the operator every time the `SyncPeriod` has elapsed. | ||
|
|
||
| The SyncPeriod in the [is configurable in the | ||
| govuk-job-request-operator](https://github.com/alphagov/govuk-job-request-operator#configuration) | ||
| by setting the `--resource-ttl` flag. | ||
|
|
||
| ## Architecture Diagrams | ||
|
|
||
| ### JobRequest state diagram | ||
|
|
||
| <pre lang="mermaid"> | ||
|
|
||
| <code> | ||
| stateDiagram | ||
| state "''" as NoState | ||
|
|
||
| [*] --> NoState | ||
| NoState --> Malformed | ||
| NoState --> Pending | ||
| Pending --> Rejected | ||
| Pending --> Approved | ||
| Approved --> Started | ||
| Approved --> Malformed | ||
| Started --> Complete | ||
| Started --> Failed | ||
| Started --> Malformed | ||
| Malformed --> [*] | ||
| Complete --> [*] | ||
| Failed --> [*] | ||
| </code> | ||
| </pre> | ||
|
|
||
| ### JobRequestReview state diagram | ||
|
|
||
| <pre lang="mermaid"> | ||
|
|
||
| <code> | ||
| stateDiagram | ||
| [*] --> Approved | ||
| [*] --> Rejected | ||
|
|
||
| Approved --> JobRequestNotFound | ||
| Approved --> JobRequestMalformed | ||
| Approved --> Conflict | ||
|
|
||
| Rejected --> JobRequestNotFound | ||
| Rejected --> JobRequestMalformed | ||
| Rejected --> Conflict | ||
|
|
||
| Approved --> [*] | ||
| Rejected --> [*] | ||
| JobRequestNotFound --> [*] | ||
| JobRequestMalformed --> [*] | ||
| Conflict --> [*] | ||
| </code> | ||
| </pre> | ||
|
|
||
| ### Creating a JobRequest Flowchart | ||
|
|
||
| <pre lang="mermaid"> | ||
|
|
||
| <code> | ||
| flowchart TD | ||
| RequesterA@{ shape: person } | ||
| RequesterB@{ shape: person } | ||
| govuk-cli | ||
|
|
||
| RequesterA--govuk-cli jobrequest create ...-->govuk-cli | ||
| govuk-cli--Create JobRequest-->k8sAPI | ||
|
|
||
| RequesterB--Create JobRequest-->k8sAPI | ||
|
|
||
| subgraph k8s[Kubernetes] | ||
| direction TD | ||
|
|
||
| k8sAPI[Kubernetes API] | ||
| MutatingAdmissionPolicy | ||
| gatekeeper[OPA Gatekeeper] | ||
| govuk-job-request-operator[Operator JobRequest controller] | ||
| etcd[(Etcd)] | ||
|
|
||
| k8sAPI-- Create JobRequest -->MutatingAdmissionPolicy | ||
| MutatingAdmissionPolicy--Create JobRequest-->gatekeeper | ||
| gatekeeper--Create JobRequest-->etcd | ||
|
|
||
| etcd--Created JobRequest-->govuk-job-request-operator | ||
|
|
||
| govuk-job-request-operator-->validateJobRequest{Validate} | ||
| validateJobRequest--valid\n\nSet JobRequest Pending-->k8sAPI | ||
| validateJobRequest--invalid\n\nSet JobRequest Malformed-->k8sAPI | ||
| end | ||
| </code> | ||
| </pre> | ||
|
|
||
| ### Reviewing a JobRequest Flowchart | ||
|
|
||
| <pre lang="mermaid"> | ||
|
|
||
| <code> | ||
| flowchart TD | ||
| RequesterA@{ shape: person } | ||
| RequesterB@{ shape: person } | ||
| govuk-cli | ||
|
|
||
| RequesterA--govuk-cli jobrequest review ...-->govuk-cli | ||
| govuk-cli--Create JobRequestReview-->k8sAPI | ||
|
|
||
| RequesterB--Create JobRequestReview-->k8sAPI | ||
|
|
||
| subgraph k8s[Kubernetes] | ||
| direction TD | ||
|
|
||
| k8sAPI[Kubernetes API] | ||
| MutatingAdmissionPolicy | ||
| gatekeeper[OPA Gatekeeper] | ||
| govuk-job-request-operator[Operator JobRequestReview controller] | ||
| etcd[(Etcd)] | ||
|
|
||
| k8sAPI-- Create JobRequestReview -->MutatingAdmissionPolicy | ||
| MutatingAdmissionPolicy-- Create JobRequestReview -->gatekeeper | ||
| gatekeeper-- Create JobRequestReview -->etcd | ||
|
|
||
| etcd-- Created JobRequestReview -->govuk-job-request-operator | ||
|
|
||
| govuk-job-request-operator-->validateJobRequestReview{Validate} | ||
| validateJobRequestReview-- invalid\n\nSet JobRequestReview Malformed -->k8sAPI | ||
|
|
||
| validateJobRequestReview-- valid -->jobRequestFound{JobRequest Exists?} | ||
| jobRequestFound-- found\n\nSet state of JobRequestReview to Approved/Rejected\n\nSet state of JobRequest to Approved/Rejected -->k8sAPI | ||
| jobRequestFound-- not-found\n\nSet state of JobRequestReview toJobRequestNotFound -->k8sAPI | ||
| end | ||
| </code> | ||
| </pre> | ||
|
|
||
| ### After a JobRequest has been Approved Flowchart | ||
|
|
||
| <pre lang="mermaid"> | ||
|
|
||
| <code> | ||
| flowchart TD | ||
| subgraph k8s[Kubernetes] | ||
| direction TD | ||
|
|
||
| k8sAPI[Kubernetes API] | ||
| MutatingAdmissionPolicy | ||
| gatekeeper[OPA Gatekeeper] | ||
| govuk-job-request-operator[Operator JobRequest controller] | ||
| etcd[(Etcd)] | ||
|
|
||
| etcd-- 1. JobRequest Updated -->govuk-job-request-operator | ||
| govuk-job-request-operator<-- 2. Get Pod/Deployment -->k8sAPI | ||
|
|
||
| govuk-job-request-operator-- 3. Create Job -->k8sAPI | ||
| k8sAPI-- 3. Create Job -->etcd | ||
|
|
||
| govuk-job-request-operator-- 4. Watch Job -->k8sAPI | ||
|
|
||
| etcd-- 5. Job State Change --> govuk-job-request-operator | ||
| govuk-job-request-operator-- 5. Update JobRequest with Job State -->k8sAPI | ||
| k8sAPI-- 5. Update JobRequest -->MutatingAdmissionPolicy | ||
| MutatingAdmissionPolicy-- 5. Update JobRequest -->gatekeeper | ||
| gatekeeper-- 5. Update JobRequest -->etcd | ||
|
|
||
| end | ||
|
|
||
| </code> | ||
| </pre> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.