|
| 1 | +# Metal3 Fake Kubernetes API server system (Metal3-FKAS) |
| 2 | + |
| 3 | +## FKAS |
| 4 | + |
| 5 | +Metal3-FKAS tool for testing CAPI-related projects. |
| 6 | +When being asked, it generates new fake kubernetes api endpoint, that responds |
| 7 | +to all typical requests that CAPI sends to a newly provisioned cluster. |
| 8 | + |
| 9 | +Despite being developed for Metal3 ecosystem, FKAS is provider-free. It can be adopted |
| 10 | +and used by any CAPI provider with intention to test the provider provisioning ability, |
| 11 | +without using real nodes. |
| 12 | + |
| 13 | +### Purpose |
| 14 | + |
| 15 | +After a CAPI Infrastructure provisions a new cluster, CAPI will send queries |
| 16 | +towards the newly launched cluster's API server to verify that the cluster is |
| 17 | +fully up and running. |
| 18 | + |
| 19 | +In a simulated provisioning process, there are no real nodes, hence we cannot |
| 20 | +have an actual API server running inside the node. Booting up a real kubelet |
| 21 | +and etcd servers elsewhere is possible, but these processes are likely to consume |
| 22 | +a lot of resources. |
| 23 | + |
| 24 | +FKAS is useful in this situation. When a request is sent towards `/register` endpoint, |
| 25 | +it will spawn a new simulated kubernetes API server with *unique* a host and |
| 26 | +port pair. |
| 27 | +User can, then, inject the address into cluster template consumed by |
| 28 | +CAPI with any infra provider. |
| 29 | + |
| 30 | +### How to use |
| 31 | + |
| 32 | +You can build the `metal3-fkas` image that is suitable for |
| 33 | +your local environment with |
| 34 | + |
| 35 | +```shell |
| 36 | +make build-fkas |
| 37 | +``` |
| 38 | + |
| 39 | +The result is an image with label `quay.io/metal3-io/metal3-fkas:<your-arch-name>` |
| 40 | + |
| 41 | +Alternatively, you can also build a custom image with |
| 42 | + |
| 43 | +```shell |
| 44 | +cd hack/fake-apiserver |
| 45 | +docker build -t <custom tag> . |
| 46 | +``` |
| 47 | + |
| 48 | +For local tests, it's normally needed to load the image into the cluster. |
| 49 | +For e.g. with `minikube` |
| 50 | + |
| 51 | +```shell |
| 52 | +minikube image load quay.io/metal3-io/metal3-fkas:latest |
| 53 | +``` |
| 54 | + |
| 55 | +Now you can deploy this container to the cluster, for e.g. with the deployment |
| 56 | +if k8s/metal3-fkas.yaml |
| 57 | + |
| 58 | +```shell |
| 59 | +kubectl apply -f metal3-fkas.yaml |
| 60 | +``` |
| 61 | + |
| 62 | +After building the container image and deploy it to the bootstrap kubernetes cluster, |
| 63 | +you need to create a tunnel to send request to it and get response, by using |
| 64 | +a LoadBalancer, or a simple port-forward |
| 65 | + |
| 66 | +```shell |
| 67 | +fkas_pod_name=$(kubectl get pods -n default -l app=metal3-fkas-system -o jsonpath='{.items[0].metadata.name}') |
| 68 | +kubectl port-forward pod/${fkas_pod_name} 3333:3333 2>/dev/null& |
| 69 | +``` |
| 70 | + |
| 71 | +Now, you can generate a fake API server endpoint by sending |
| 72 | +a POST request to the fake API server. |
| 73 | + |
| 74 | +```shell |
| 75 | +namespace=<cluster-namespace> |
| 76 | +cluster_name=<cluster-name> |
| 77 | + |
| 78 | +cluster_endpoint=$(curl -X POST "localhost:3333/register" \ |
| 79 | +-H "Content-Type: application/json" -d '{ |
| 80 | + "cluster": "'$cluster_name'", |
| 81 | + "namespace": "'$namespace'" |
| 82 | +}') |
| 83 | +``` |
| 84 | + |
| 85 | +The fake API server will return a response with the ip and port of the newly |
| 86 | +generated api server. For example: |
| 87 | + |
| 88 | +```json |
| 89 | +{ |
| 90 | + "Resource": "metal3/test1", |
| 91 | + "Host": "10.244.0.83", |
| 92 | + "Port": 20000 |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +A new cluster can be provisioned by injecting the host and port we |
| 97 | +got from FKAS to the cluster template provided by a CAPI infrastructure |
| 98 | +provider. For e.g., with CAPM3 that can be done as followed: |
| 99 | + |
| 100 | +```shell |
| 101 | +host=$(echo ${cluster_endpoints} | jq -r ".Host") |
| 102 | +port=$(echo ${cluster_endpoints} | jq -r ".Port") |
| 103 | + |
| 104 | +# Injecting the new api address into the cluster template by |
| 105 | +# exporting these env vars |
| 106 | +export CLUSTER_APIENDPOINT_HOST="${host}" |
| 107 | +export CLUSTER_APIENDPOINT_PORT="${port}" |
| 108 | + |
| 109 | +clusterctl generate cluster "${cluster}" \ |
| 110 | + --from "${CLUSTER_TEMPLATE}" \ |
| 111 | + --target-namespace "${namespace}" > /tmp/${cluster}-cluster.yaml |
| 112 | +kubectl apply -f /tmp/${cluster}-cluster.yaml |
| 113 | +``` |
| 114 | + |
| 115 | +After the cluster is created, CAPI will expect that information like node name |
| 116 | +and provider ID is registered in the API server. Since our API server doesn't |
| 117 | +live inside the node, we will need to feed the info to it, by sending a |
| 118 | +PUT request to `/updateNode` endpoint: |
| 119 | + |
| 120 | +```shell |
| 121 | +curl -X PUT "localhost:3333/updateNode" -H "Content-Type: application/json" -d '{ |
| 122 | + "cluster": "<cluster-name>", |
| 123 | + "namespace": "<namespace>", |
| 124 | + "nodeName": "<machine-object-name>", |
| 125 | + "providerID": "<provider-id>", |
| 126 | + "uuid": "<node-uuid>", |
| 127 | + "labels": "<node-labels>", |
| 128 | + "k8sversion": "<k8s-version-of-workload-cluster>" |
| 129 | +}' |
| 130 | +``` |
| 131 | + |
| 132 | +### Acknowledgements |
| 133 | + |
| 134 | +This was developed thanks to the implementation of |
| 135 | +[Cluster API Provider In Memory (CAPIM)](https://github.com/kubernetes-sigs/cluster-api/tree/main/test/infrastructure/inmemory). |
| 136 | + |
| 137 | +## Metal3 FKAS System |
| 138 | + |
| 139 | +### FKAS in Metal3 |
| 140 | + |
| 141 | +In metal3 ecosystem, currently we have two ways of simulating a workflow without |
| 142 | +using any baremetal or virtual machines: |
| 143 | + |
| 144 | +- [FakeIPA container](https://github.com/metal3-io/utility-images/tree/main/fake-ipa) |
| 145 | +- BMO simulation mode |
| 146 | + |
| 147 | +In both of these cases, the "nodes" are not able to boot up any kubernetes api server, |
| 148 | +hence the needs of having mock API servers on-demands. |
| 149 | + |
| 150 | +Similar to the general case, after having BMHs provisioned to `available` state, |
| 151 | +the user can send a request towards the Fake API server endpoint `/register`, |
| 152 | +which will spawn a new API server, with an unique `Host` and `Port` pair. |
| 153 | + |
| 154 | +User can, then, use this IP address to feed the cluster template, by exporting |
| 155 | +`CLUSTER_APIENDPOINT_HOST` and `CLUSTER_APIENDPOINT_PORT` variables. |
| 156 | + |
| 157 | +There is no need of manually check and send node info to `/updateNode`, as we have |
| 158 | +another tool to automate that part. |
| 159 | + |
| 160 | +### Metal3-FKAS-Reconciler |
| 161 | + |
| 162 | +This tool runs as a side-car container alongside FKAS, and works specifically |
| 163 | +for Metal3. It eliminates the needs of user to manually fetch the nodes information |
| 164 | +and send to `/updateNode` (as described earlier), by constantly watch the changes |
| 165 | +in BMH objects, notice if a BMH is being provisioned to a kubernetes node, and |
| 166 | +send request to `/updateNode` with appropriate information. |
| 167 | + |
| 168 | +If you want to use *Metal3-FKAS* with another CAPI provider, you can also implement |
| 169 | +your own reconciler, based on implementation of *metal3-fkas-reconciler*. |
| 170 | + |
| 171 | +### Deployment |
| 172 | + |
| 173 | +The `metal3-fkas-system` deployment (including `metal3-fkas` and `metal3-fkas-reconciler`) |
| 174 | +can be deployed with the `k8s/metal3-fkas-system.yaml` file. |
| 175 | + |
| 176 | +```shell |
| 177 | +kubectl apply -f k8s/metal3-fkas-system.yaml |
| 178 | +``` |
| 179 | + |
| 180 | +## Disclaimer |
| 181 | + |
| 182 | +This is intended for development environments only. |
| 183 | +Do **NOT** use it in production. |
0 commit comments