# Prepare the cluster

> What a Kubernetes cluster provides before you install Corfedo with the Helm chart, from cert-manager and a certificate issuer to the public entry, a bucket and PostgreSQL.

Source: https://docs.corfedo.ai/operate/kubernetes/prepare-the-cluster/

Provide these before you [install](https://docs.corfedo.ai/operate/kubernetes/).

## Kubernetes

- Kubernetes 1.30 or later.
- Helm 3.8 or later.
- A default storage class, or one named in `search.volume.storageClassName` and `indexer.volume.storageClassName`, for the [volume of each search node and each indexer](https://docs.corfedo.ai/operate/kubernetes/sizing-and-search-nodes/). Without one their volume claims stay `Pending`.

## cert-manager

Corfedo's workloads authenticate each other with certificates from cert-manager. Install cert-manager with automatic approval off, approver-policy with its defaults, and the cert-manager CSI driver with `app.driver.useTokenRequest: true`.

```sh
helm install cert-manager oci://quay.io/jetstack/charts/cert-manager \
  --namespace cert-manager --create-namespace \
  --set crds.enabled=true --set disableAutoApproval=true --wait
helm install cert-manager-approver-policy \
  oci://quay.io/jetstack/charts/cert-manager-approver-policy \
  --namespace cert-manager --wait
helm install cert-manager-csi-driver \
  oci://quay.io/jetstack/charts/cert-manager-csi-driver \
  --namespace cert-manager --set app.driver.useTokenRequest=true --wait
```

The chart is tested with cert-manager 1.21, approver-policy 0.29 and the CSI driver 0.16.

With automatic approval off, every certificate in the cluster needs an approval policy, existing ones too.

If cert-manager's controller uses another service account or namespace than `cert-manager`, set `workload.certManager.serviceAccount`.

## Issuer for workload certificates

Name an existing issuer in `workload.issuerRef`: one backed by your organization's PKI, or a CA issuer whose key lies outside Corfedo's namespace.

Use the issuer for this installation's workload certificates only, and let no other approval policy select it for Corfedo's namespace. Otherwise a role's first certificate can be denied, and its pods wait long for a new request.

The account that installs Corfedo needs the right to create cluster-wide `CertificateRequestPolicy` objects. For an issuer outside the group `cert-manager.io`, such as AWS Private CA, add its signer names to approver-policy's `app.approveSignerNames`.

### Create a CA issuer for a test cluster

Save these manifests as `workload-ca.yaml`. They create the issuer `corfedo-workload`.

```yaml
# Signs only the CA certificate below.
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
  name: corfedo-workload-ca-bootstrap
  namespace: cert-manager
spec:
  selfSigned: {}
---
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: corfedo-workload-ca
  namespace: cert-manager
spec:
  isCA: true
  commonName: corfedo-workload-ca
  secretName: corfedo-workload-ca
  duration: 8760h
  privateKey:
    algorithm: ECDSA
    size: 256
  issuerRef:
    group: cert-manager.io
    kind: Issuer
    name: corfedo-workload-ca-bootstrap
---
# The issuer of the workload certificates. A ClusterIssuer reads its CA Secret
# from cert-manager's namespace.
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
  name: corfedo-workload
spec:
  ca:
    secretName: corfedo-workload-ca
---
# With automatic approval off, the CA certificate needs a policy of its own.
# It allows exactly the request above and is bound to cert-manager's controller.
apiVersion: policy.cert-manager.io/v1alpha1
kind: CertificateRequestPolicy
metadata:
  name: corfedo-workload-ca-bootstrap
spec:
  allowed:
    isCA: true
    commonName:
      value: corfedo-workload-ca
      required: true
  selector:
    issuerRef:
      group: cert-manager.io
      kind: Issuer
      name: corfedo-workload-ca-bootstrap
    namespace:
      matchNames:
        - cert-manager
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: corfedo-workload-ca-bootstrap-policy
  namespace: cert-manager
rules:
  - apiGroups: ["policy.cert-manager.io"]
    resources: ["certificaterequestpolicies"]
    verbs: ["use"]
    resourceNames: ["corfedo-workload-ca-bootstrap"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: corfedo-workload-ca-bootstrap-policy
  namespace: cert-manager
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: corfedo-workload-ca-bootstrap-policy
subjects:
  - kind: ServiceAccount
    name: cert-manager
    namespace: cert-manager
```

Apply them and write the CA certificate to `ca.crt` for the trust bundle.

```sh
kubectl apply -f workload-ca.yaml
kubectl wait certificate/corfedo-workload-ca --namespace cert-manager \
  --for=condition=Ready --timeout=60s
kubectl get secret corfedo-workload-ca --namespace cert-manager \
  -o jsonpath='{.data.tls\.crt}' | base64 -d > ca.crt
```

## Public entry

- An ingress controller, or a load balancer that sends to the pods directly, which ends TLS and forwards plain HTTP with the client's address and scheme.
- DNS that points Corfedo's host name at the entry, and its certificate there, see [HTTPS certificate](https://docs.corfedo.ai/operate/kubernetes/routing/#https-certificate).
- The network the forwarded requests come from. Corfedo accepts a forwarded client address from that network only.

With cert-manager's ACME issuer, the certificate needs an [approval policy](#cert-manager) like this one for `letsencrypt`:

```yaml
apiVersion: policy.cert-manager.io/v1alpha1
kind: CertificateRequestPolicy
metadata:
  name: corfedo-public
spec:
  allowed:
    dnsNames:
      values: [corfedo.example.com]
      required: true
    usages: [digital signature, key encipherment]
  selector:
    issuerRef:
      group: cert-manager.io
      kind: ClusterIssuer
      name: letsencrypt
    namespace:
      matchNames:
        - corfedo
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: corfedo-public-policy
  namespace: corfedo
rules:
  - apiGroups: ["policy.cert-manager.io"]
    resources: ["certificaterequestpolicies"]
    verbs: ["use"]
    resourceNames: ["corfedo-public"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: corfedo-public-policy
  namespace: corfedo
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: corfedo-public-policy
subjects:
  - kind: ServiceAccount
    name: cert-manager
    namespace: cert-manager
```

## Object storage

- An S3-compatible bucket that never had versioning enabled, reached over HTTPS. The store must support conditional writes (`If-None-Match`), SHA-256 checksums, listing and batch deletion, or the setup Job fails.
- For each installation, an empty bucket or prefix (`storage.s3.prefix`), not inside another installation's.
- Access keys for it. Search nodes only read and indexers write without deleting, so each can have a narrower key. The platform's key needs these, here for the prefix `acme/corfedo`:

| Right | On |
| --- | --- |
| Get, Head, Put and Delete objects | Under `acme/corfedo/`, `acme/corfedo/.corfedo-probe/` included |
| List the bucket | Prefix `acme/corfedo/` |
| Read the versioning status | The bucket |
| Head the object, list the bucket with it as prefix | `.corfedo-root-id` of each parent prefix and the top level: `acme/.corfedo-root-id`, `.corfedo-root-id` |

## PostgreSQL

A PostgreSQL server with a database and six logins, none of them a superuser. The migration login owns the database, creates the `pg_trgm` and `btree_gist` extensions and grants the other logins their rights.

```sql
CREATE ROLE corfedo_migrator LOGIN PASSWORD '...';
CREATE ROLE corfedo_runtime LOGIN PASSWORD '...';
CREATE ROLE corfedo_authentication LOGIN PASSWORD '...';
CREATE ROLE corfedo_issuer LOGIN PASSWORD '...';
CREATE ROLE corfedo_validation LOGIN PASSWORD '...';
CREATE ROLE corfedo_webhooks LOGIN PASSWORD '...';
CREATE DATABASE corfedo OWNER corfedo_migrator;
```

By default Corfedo connects over TLS and checks the server's certificate and host name.

Set `database.maxConnections` to the server's `max_connections`, which must be at least 635 for the chart's default sizes. Each replica you add to a role opens up to that role's `roles.<role>.maxConnections` more, so raise the server's `max_connections` and `database.maxConnections` by that much before you add it. Count other users of the same server in `database.otherConnections`.

## Network access

- The pods reach PostgreSQL, the bucket, your code hosts, your identity provider and your AI provider. [Routing](https://docs.corfedo.ai/operate/kubernetes/routing/#outside-services) lists every outside service.
- Corfedo calls an identity provider only at public addresses. For one that resolves to a private address inside the cluster, set `issuerEgress.mode` to `AllowPrivateNetwork`.
- When one of these has a certificate from a private certificate authority, put the authority's certificates in a ConfigMap and name it in `certificateAuthorities`.
