ingress-nginx-helm/docs/developer-guide/code-overview.md

164 lines
8.2 KiB
Markdown
Raw Normal View History

# Ingress NGINX - Code Overview
This document provides an overview of Ingress NGINX code.
## Core Golang code
This part of the code is responsible for the main logic of Ingress NGINX. It contains all the logics that parses [Ingress Objects](https://kubernetes.io/docs/concepts/services-networking/ingress/),
[annotations](https://kubernetes.io/docs/reference/glossary/?fundamental=true#term-annotation), watches Endpoints and turn them into usable nginx.conf configuration.
### Core Sync Logics:
Ingress-nginx has an internal model of the ingresses, secrets and endpoints in a given cluster. It maintains two copies of that:
2022-07-02 21:43:24 +00:00
1. One copy is the currently running configuration model
2. Second copy is the one generated in response to some changes in the cluster
The sync logic diffs the two models and if there's a change it tries to converge the running configuration to the new one.
There are static and dynamic configuration changes.
All endpoints and certificate changes are handled dynamically by posting the payload to an internal NGINX endpoint that is handled by Lua.
---
The following parts of the code can be found:
### Entrypoint
The `main` package is responsible for starting ingress-nginx program, which can be found in [cmd/nginx](https://github.com/kubernetes/ingress-nginx/tree/main/cmd/nginx) directory.
### Version
Is the package of the code responsible for adding `version` subcommand, and can be found in [version](https://github.com/kubernetes/ingress-nginx/tree/main/version) directory.
### Internal code
This part of the code contains the internal logics that compose Ingress NGINX Controller, and it's split into:
#### Admission Controller
Contains the code of [Kubernetes Admission Controller](https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/) which validates the syntax of ingress objects before accepting it.
This code can be found in [internal/admission/controller](https://github.com/kubernetes/ingress-nginx/tree/main/internal/admission/controller) directory.
#### File functions
Contains auxiliary codes that deal with files, such as generating the SHA1 checksum of a file, or creating required directories.
This code can be found in [internal/file](https://github.com/kubernetes/ingress-nginx/blob/main/internal/file) directory.
#### Ingress functions
Contains all the logics from NGINX Ingress Controller, with some examples being:
* Expected Golang structures that will be used in templates and other parts of the code - [internal/ingress/types.go](https://github.com/kubernetes/ingress-nginx/blob/main/internal/ingress/types.go).
* supported annotations and its parsing logics - [internal/ingress/annotations](https://github.com/kubernetes/ingress-nginx/tree/main/internal/ingress/annotations).
* reconciliation loops and logics - [internal/ingress/controller](https://github.com/kubernetes/ingress-nginx/tree/main/internal/ingress/controller)
* defaults - define the default struct - [internal/ingress/defaults](https://github.com/kubernetes/ingress-nginx/tree/main/internal/ingress/defaults).
* Error interface and types implementation - [internal/ingress/errors](https://github.com/kubernetes/ingress-nginx/tree/main/internal/ingress/errors)
* Metrics collectors for Prometheus exporting - [internal/ingress/metric](https://github.com/kubernetes/ingress-nginx/tree/main/internal/ingress/metric).
* Resolver - Extracts information from a controller - [internal/ingress/resolver](https://github.com/kubernetes/ingress-nginx/tree/main/internal/ingress/resolver).
* Ingress Object status publisher - [internal/ingress/status](https://github.com/kubernetes/ingress-nginx/tree/main/internal/ingress/status).
And other parts of the code that will be written in this document in a future.
#### K8s functions
Contains helper functions for parsing Kubernetes objects.
This part of the code can be found in [internal/k8s](https://github.com/kubernetes/ingress-nginx/tree/main/internal/k8s) directory.
#### Networking functions
Contains helper functions for networking, such as IPv4 and IPv6 parsing, SSL certificate parsing, etc.
This part of the code can be found in [internal/net](https://github.com/kubernetes/ingress-nginx/tree/main/internal/net) directory.
#### NGINX functions
Contains helper function to deal with NGINX, such as verify if it's running and reading it's configuration file parts.
This part of the code can be found in [internal/nginx](https://github.com/kubernetes/ingress-nginx/tree/main/internal/nginx) directory.
#### Tasks / Queue
Contains the functions responsible for the sync queue part of the controller.
This part of the code can be found in [internal/task](https://github.com/kubernetes/ingress-nginx/tree/main/internal/task) directory.
#### Other parts of internal
Other parts of internal code might not be covered here, like runtime and watch but they can be added in a future.
## E2E Test
The e2e tests code is in [test](https://github.com/kubernetes/ingress-nginx/tree/main/test) directory.
## Other programs
Describe here `kubectl plugin`, `dbg`, `waitshutdown` and cover the hack scripts.
### kubectl plugin
It containes kubectl plugin for inspecting your ingress-nginx deployments.
This part of code can be found in [cmd/plugin](https://github.com/kubernetes/ingress-nginx/tree/main/cmd/plugin) directory
Detail functions flow and available flow can be found in [kubectl-plugin](https://github.com/kubernetes/ingress-nginx/blob/main/docs/kubectl-plugin.md)
## Deploy files
This directory contains the `yaml` deploy files used as examples or references in the docs to deploy Ingress NGINX and other components.
Those files are in [deploy](https://github.com/kubernetes/ingress-nginx/tree/main/deploy) directory.
## Helm Chart
Used to generate the Helm chart published.
Code is in [charts/ingress-nginx](https://github.com/kubernetes/ingress-nginx/tree/main/charts/ingress-nginx).
## Documentation/Website
The documentation used to generate the website https://kubernetes.github.io/ingress-nginx/
This code is available in [docs](https://github.com/kubernetes/ingress-nginx/tree/main/docs) and it's main "language" is `Markdown`, used by [mkdocs](https://github.com/kubernetes/ingress-nginx/blob/main/mkdocs.yml) file to generate static pages.
## Container Images
Container images used to run ingress-nginx, or to build the final image.
### Base Images
Contains the `Dockerfiles` and scripts used to build base images that are used in other parts of the repo. They are present in [images](https://github.com/kubernetes/ingress-nginx/tree/main/images) repo. Some examples:
* [nginx](https://github.com/kubernetes/ingress-nginx/tree/main/images/nginx) - The base NGINX image ingress-nginx uses is not a vanilla NGINX. It bundles many libraries together and it is a job in itself to maintain that and keep things up-to-date.
* [custom-error-pages](https://github.com/kubernetes/ingress-nginx/tree/main/images/custom-error-pages) - Used on the custom error page examples.
There are other images inside this directory.
### Ingress Controller Image
The image used to build the final ingress controller, used in deploy scripts and Helm charts.
This is NGINX with some Lua enhancement. We do dynamic certificate, endpoints handling, canary traffic split, custom load balancing etc at this component. One can also add new functionalities using Lua plugin system.
The files are in [rootfs](https://github.com/kubernetes/ingress-nginx/tree/main/rootfs) directory and contains:
* The Dockerfile
Darwin arm64 (#8399) * Use sed instead of gnu find flags Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * Support building linux/amd64 on darin/arm64 Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * Upgrade awesome_bot to dkhamsing/awesome_bot:1.20.0 Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * Favor find -prune for vendor Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * Skip golang modcache folder Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * Favor find -prune for changelog Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * Ignore Changelogs of any case Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * Fix service-l7 link Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * Fix route53-mapper link Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * Update rootfs contents description The auxiliary scripts were removed after: https://github.com/kubernetes/ingress-nginx/tree/ab8349008a1db07205c4e6a9a80b16caafd272d4/rootfs/ingress-controller Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * Update paths for modsecurity Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * Update paths for modsecurity_snippet Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * Update toc for 20190815-zone-aware-routing.md Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * Use Internet Archive for datapath.io blog entry Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * Use Internet Archive for cloudflare.com help center entry Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * Use https for nginx.org Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> Co-authored-by: Josh Soref <jsoref@users.noreply.github.com>
2022-04-06 20:46:26 +00:00
* [nginx config](https://github.com/kubernetes/ingress-nginx/tree/main/rootfs/etc/nginx)
#### Ingress NGINX Lua Scripts
Ingress NGINX uses Lua Scripts to enable features like hot reloading, rate limiting and monitoring. Some are written using the [OpenResty](https://openresty.org/en/) helper.
The directory containing Lua scripts is [rootfs/etc/nginx/lua](https://github.com/kubernetes/ingress-nginx/tree/main/rootfs/etc/nginx/lua).
#### Nginx Go template file
One of the functions of Ingress NGINX is to turn [Ingress](https://kubernetes.io/docs/concepts/services-networking/ingress/) objects into nginx.conf file.
To do so, the final step is to apply those configurations in [nginx.tmpl](https://github.com/kubernetes/ingress-nginx/tree/main/rootfs/etc/nginx/template) turning it into a final nginx.conf file.