debian-mirror-gitlab/doc/development/kubernetes.md

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

169 lines
6.8 KiB
Markdown
Raw Normal View History

2020-10-24 23:57:45 +05:30
---
2023-06-20 00:43:36 +05:30
stage: Deploy
group: Environments
2022-11-25 23:54:43 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/product/ux/technical-writing/#assignments
2020-10-24 23:57:45 +05:30
---
2023-05-27 22:25:52 +05:30
# Kubernetes integration development guidelines
2019-07-07 11:18:12 +05:30
2021-02-22 17:27:13 +05:30
This document provides various guidelines when developing for the GitLab
2021-11-18 22:05:49 +05:30
[Kubernetes integration](../user/infrastructure/clusters/index.md).
2019-07-07 11:18:12 +05:30
## Development
### Architecture
Some Kubernetes operations, such as creating restricted project
namespaces are performed on the GitLab Rails application. These
2021-01-03 14:25:43 +05:30
operations are performed using a [client library](#client-library),
and carry an element of risk. The operations are
run as the same user running the GitLab Rails application. For more information,
read the [security](#security) section below.
2019-07-07 11:18:12 +05:30
Some Kubernetes operations, such as installing cluster applications are
performed on one-off pods on the Kubernetes cluster itself. These
2021-01-03 14:25:43 +05:30
installation pods are named `install-<application_name>` and
2019-07-07 11:18:12 +05:30
are created within the `gitlab-managed-apps` namespace.
In terms of code organization, we generally add objects that represent
Kubernetes resources in
2019-12-04 20:38:33 +05:30
[`lib/gitlab/kubernetes`](https://gitlab.com/gitlab-org/gitlab-foss/tree/master/lib/gitlab/kubernetes).
2019-07-07 11:18:12 +05:30
### Client library
We use the [`kubeclient`](https://rubygems.org/gems/kubeclient) gem to
perform Kubernetes API calls. As the `kubeclient` gem does not support
2021-01-03 14:25:43 +05:30
different API Groups (such as `apis/rbac.authorization.k8s.io`) from a
2019-07-07 11:18:12 +05:30
single client, we have created a wrapper class,
2021-09-04 01:27:46 +05:30
[`Gitlab::Kubernetes::KubeClient`](https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/kubernetes/kube_client.rb)
2021-01-03 14:25:43 +05:30
that enable you to achieve this.
2019-07-07 11:18:12 +05:30
2021-01-03 14:25:43 +05:30
Selected Kubernetes API groups are supported. Do add support
2019-07-07 11:18:12 +05:30
for new API groups or methods to
2021-09-04 01:27:46 +05:30
[`Gitlab::Kubernetes::KubeClient`](https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/kubernetes/kube_client.rb)
2019-07-07 11:18:12 +05:30
if you need to use them. New API groups or API group versions can be
2021-01-03 14:25:43 +05:30
added to `SUPPORTED_API_GROUPS` - internally, this creates an
2019-07-07 11:18:12 +05:30
internal client for that group. New methods can be added as a delegation
to the relevant internal client.
### Performance considerations
2021-01-03 14:25:43 +05:30
All calls to the Kubernetes API must be in a background process. Don't
perform Kubernetes API calls within a web request. This blocks
2021-09-04 01:27:46 +05:30
webserver, and can lead to a denial-of-service (DoS) attack in GitLab as
2019-07-07 11:18:12 +05:30
the Kubernetes cluster response times are outside of our control.
The easiest way to ensure your calls happen a background process is to
2022-07-16 23:28:13 +05:30
delegate any such work to happen in a [Sidekiq worker](sidekiq/index.md).
2019-07-07 11:18:12 +05:30
2021-01-03 14:25:43 +05:30
You may want to make calls to Kubernetes and return the response, but a background
worker isn't a good fit. Consider using
2021-09-04 01:27:46 +05:30
[reactive caching](https://gitlab.com/gitlab-org/gitlab/-/blob/master/app/models/concerns/reactive_caching.rb).
2019-07-07 11:18:12 +05:30
For example:
```ruby
def calculate_reactive_cache!
{ pods: cluster.platform_kubernetes.kubeclient.get_pods }
end
def pods
with_reactive_cache do |data|
data[:pods]
end
end
```
### Testing
2021-01-03 14:25:43 +05:30
We have some WebMock stubs in
2021-09-04 01:27:46 +05:30
[`KubernetesHelpers`](https://gitlab.com/gitlab-org/gitlab/-/blob/master/spec/support/helpers/kubernetes_helpers.rb)
2019-07-07 11:18:12 +05:30
which can help with mocking out calls to Kubernetes API in your tests.
2019-12-26 22:10:19 +05:30
### Amazon EKS integration
This section outlines the process for allowing a GitLab instance to create EKS clusters.
The following prerequisites are required:
2021-01-03 14:25:43 +05:30
A `Customer` AWS account. The EKS cluster is created in this account. The following
resources must be present:
2019-12-26 22:10:19 +05:30
- A provisioning role that has permissions to create the cluster
and associated resources. It must list the `GitLab` AWS account
as a trusted entity.
- A VPC, management role, security group, and subnets for use by the cluster.
A `GitLab` AWS account. This is the account which performs
the provisioning actions. The following resources must be present:
- A service account with permissions to assume the provisioning
role in the `Customer` account above.
- Credentials for this service account configured in GitLab via
the `kubernetes` section of `gitlab.yml`.
The process for creating a cluster is as follows:
1. Using the `:provision_role_external_id`, GitLab assumes the role provided
by `:provision_role_arn` and stores a set of temporary credentials on the
provider record. By default these credentials are valid for one hour.
1. A CloudFormation stack is created, based on the
2021-09-04 01:27:46 +05:30
[`AWS CloudFormation EKS template`](https://gitlab.com/gitlab-org/gitlab/-/blob/master/vendor/aws/cloudformation/eks_cluster.yaml).
2019-12-26 22:10:19 +05:30
This triggers creation of all resources required for an EKS cluster.
1. GitLab polls the status of the stack until all resources are ready,
which takes somewhere between 10 and 15 minutes in most cases.
1. When the stack is ready, GitLab stores the cluster details and generates
another set of temporary credentials, this time to allow connecting to
2021-01-03 14:25:43 +05:30
the cluster via `kubeclient`. These credentials are valid for one minute.
2019-12-26 22:10:19 +05:30
1. GitLab configures the worker nodes so that they are able to authenticate
to the cluster, and creates a service account for itself for future operations.
1. Credentials that are no longer required are removed. This deletes the following
attributes:
- `access_key_id`
- `secret_access_key`
- `session_token`
2019-07-07 11:18:12 +05:30
## Security
2021-01-03 14:25:43 +05:30
### Server Side Request Forgery (SSRF) attacks
2019-07-07 11:18:12 +05:30
As URLs for Kubernetes clusters are user controlled it is easily
susceptible to Server Side Request Forgery (SSRF) attacks. You should
understand the mitigation strategies if you are adding more API calls to
a cluster.
Mitigation strategies include:
1. Not allowing redirects to attacker controller resources:
2021-09-04 01:27:46 +05:30
[`Kubeclient::KubeClient`](https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/kubernetes/kube_client.rb#)
2022-05-07 20:08:51 +05:30
can be configured to prevent any redirects by passing in
2019-07-07 11:18:12 +05:30
`http_max_redirects: 0` as an option.
1. Not exposing error messages: by doing so, we
prevent attackers from triggering errors to expose results from
attacker controlled requests. For example, we do not expose (or store)
raw error messages:
```ruby
rescue Kubernetes::HttpError => e
# bad
# app.make_errored!("Kubernetes error: #{e.message}")
# good
app.make_errored!("Kubernetes error: #{e.error_code}")
```
2021-01-03 14:25:43 +05:30
## Debugging Kubernetes integrations
2019-07-07 11:18:12 +05:30
Logs related to the Kubernetes integration can be found in
2023-01-13 00:05:48 +05:30
[`kubernetes.log`](../administration/logs/index.md#kuberneteslog-deprecated). On a local
2021-01-03 14:25:43 +05:30
GDK install, these logs are present in `log/kubernetes.log`.
2019-07-07 11:18:12 +05:30
2020-04-08 14:13:33 +05:30
You can also follow the installation logs to debug issues related to
2019-07-07 11:18:12 +05:30
installation. Once the installation/upgrade is underway, wait for the
pod to be created. Then run the following to obtain the pods logs as
they are written:
2020-03-13 15:44:24 +05:30
```shell
2019-07-07 11:18:12 +05:30
kubectl logs <pod_name> --follow -n gitlab-managed-apps
```