# Cluster Autoscaler on Ionos Cloud Managed Kubernetes

The cluster autoscaler for the Ionos Cloud scales worker nodes within Managed Kubernetes cluster
node pools. It can be deployed as `Deployment` in your cluster.

## Deployment

### Managed

Managed autoscaling can be enabled or disabled via Kubernetes Manager in the [DCD](https://dcd.ionos.com/latest/)
or [API](https://api.ionos.com/docs/cloud/v6/#tag/Kubernetes/operation/k8sNodepoolsPut).
In this case a `Deployment` is not needed, since it will be deployed in the managed Kubernetes controlplane.

### In-cluster

If you don't have a token, generate one:

```sh
curl -u '<username>:<password>' https://api.ionos.com/auth/v1/tokens/generate
```

Store the token in a secret:

```sh
kubectl create secret generic cloud-config --from-literal=token=MY_TOKEN
```

Edit [`example-values.yaml`](./example-values.yaml) and deploy using helm:

```console
helm install ionoscloud-cluster-autoscaler autoscaler/cluster-autoscaler -f example-values.yaml
```

### Configuration

The `IONOS_ADDITIONAL_HEADERS` environment variable can be used to configure the client to send additional headers on
each Ionos Cloud API request, such as `X-Contract-Number`. This can be useful for users with multiple contracts.
The format is a semicolon-separated list of key:value pairs, e.g. `IONOS_ADDITIONAL_HEADERS="X-Contract-Number:1234657890"`.

## Development

The unit tests use mocks generated by [mockery](https://github.com/vektra/mockery/v2). To update them run:

```sh
mockery --inpackage --testonly --case snake --boilerplate-file ../../../hack/boilerplate/boilerplate.generatego.txt --name APIClient
mockery --inpackage --testonly --case snake --boilerplate-file ../../../hack/boilerplate/boilerplate.generatego.txt --name IonosCloudManager
```

### Debugging

The global logging verbosity controlled by the `--v` flag is passed on to the Ionos Cloud SDK client logger.
Verbosity 5 maps to `Debug` and 7 to `Trace`. **Do not enable this in production, as it will print full request and response data.**

### Build an image

Build and push a docker image in the `cluster-autoscaler` directory:

```sh
make build-in-docker BUILD_TAGS=ionoscloud
make make-image BUILD_TAGS=ionoscloud TAG='<tag>' REGISTRY='<registry>'
make push-image BUILD_TAGS=ionoscloud TAG='<tag>' REGISTRY='<registry>'
```

If you're using [rootless podman](https://github.com/containers/podman/blob/main/docs/tutorials/rootless_tutorial.md), create a symlink to docker and build the image using:

```sh
make make-image BUILD_TAGS=ionoscloud TAG='<tag>' REGISTRY='<registry>' DOCKER_NETWORK=host
```
