Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Batch reloads runtime #2986

Merged
merged 5 commits into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/content/intro/how-nginx-ingress-controller-works.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ A reload involves multiple steps:

The Ingress Controller reloads NGINX every time the Control Loop processes a change that affects the generated NGINX configuration. In general, every time a resource is changed, the Ingress Controller will regenerate the configuration and reload NGINX. A resource could be of any type the Ingress Controller monitors -- see [The Ingress Controller is a Kubernetes Controller](#the-ingress-controller-is-a-kubernetes-controller) section.

There are two special cases:
There are three special cases:
- *Start*. When the Ingress Controller starts, it processes all resources in the cluster and only then reloads NGINX. This avoids creating a "reload storm" by reloading only once.
- *Batch updates*. When the Ingress Controller receives a number of concurrent requests from the Kubernetes API, it will pause NGINX reloads untill the task queue is empty. This reduces the number of reloads to minimize the impact of batch updates and reduce the risk of OOM errors.
ciarams87 marked this conversation as resolved.
Show resolved Hide resolved
- *NGINX Plus*. If the Ingress Controller uses NGINX Plus, it will not reload NGINX Plus for changes to the Endpoints resources. In this case, the Ingress Controller will use the NGINX Plus API to update the corresponding upstreams and skip reloading.
5 changes: 5 additions & 0 deletions internal/configs/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,11 @@ func (cnf *Configurator) EnableReloads() {
cnf.isReloadsEnabled = true
}

// DisableReloads disables NGINX reloads meaning that configuration changes will not be followed by a reload.
func (cnf *Configurator) DisableReloads() {
cnf.isReloadsEnabled = false
}

func (cnf *Configurator) reload(isEndpointsUpdate bool) error {
if !cnf.isReloadsEnabled {
return nil
Expand Down
20 changes: 20 additions & 0 deletions internal/k8s/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ type LoadBalancerController struct {
configMap *api_v1.ConfigMap
certManagerController *cm_controller.CmController
externalDNSController *ed_controller.ExtDNSController
batchSyncEnabled bool
}

var keyFunc = cache.DeletionHandlingMetaNamespaceKeyFunc
Expand Down Expand Up @@ -764,6 +765,11 @@ func (lbc *LoadBalancerController) syncConfigMap(task task) {
return
}

if lbc.batchSyncEnabled {
glog.V(3).Infof("Skipping ConfigMap update because batch sync is on")
return
}

lbc.updateAllConfigs()
}

Expand Down Expand Up @@ -836,6 +842,12 @@ func (lbc *LoadBalancerController) preSyncSecrets() {
}

func (lbc *LoadBalancerController) sync(task task) {
if lbc.isNginxReady && lbc.syncQueue.Len() > 1 && !lbc.batchSyncEnabled {
lbc.configurator.DisableReloads()
lbc.batchSyncEnabled = true

glog.V(3).Infof("Batch processing %v items", lbc.syncQueue.Len())
}
glog.V(3).Infof("Syncing %v", task.Key)
if lbc.spiffeCertFetcher != nil {
lbc.syncLock.Lock()
Expand Down Expand Up @@ -892,6 +904,14 @@ func (lbc *LoadBalancerController) sync(task task) {
lbc.isNginxReady = true
glog.V(3).Infof("NGINX is ready")
}

if lbc.batchSyncEnabled && lbc.syncQueue.Len() == 0 {
lbc.batchSyncEnabled = false
lbc.configurator.EnableReloads()
lbc.updateAllConfigs()

glog.V(3).Infof("Batch sync completed")
}
}

func (lbc *LoadBalancerController) syncIngressLink(task task) {
Expand Down
5 changes: 5 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,8 @@ create-kind-cluster: ## Create Kind cluster
.PHONY: delete-kind-cluster
delete-kind-cluster: ## Delete Kind cluster
kind delete cluster

.PHONY: test-lint
test-lint: ## Run Python linting tools
isort .
black .
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest
from kubernetes.config.kube_config import KUBE_CONFIG_DEFAULT_LOCATION
from settings import (
BATCH_RELOAD_NUMBER,
BATCH_RESOURCES,
BATCH_START,
DEFAULT_DEPLOYMENT_TYPE,
Expand Down Expand Up @@ -95,6 +96,12 @@ def pytest_addoption(parser) -> None:
default=BATCH_RESOURCES,
help="Number of VS/Ingress resources to deploy",
)
parser.addoption(
"--batch-reload-number",
action="store",
default=BATCH_RELOAD_NUMBER,
help="Number of reloads expected for batch reload test",
)
parser.addoption(
"--ns-count",
action="store",
Expand Down
4 changes: 3 additions & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"""Settings below are test specific"""
# Determines if batch reload tests will be ran or not
BATCH_START = "False"
# Number of Ingress/VS resources to deploy based on BATCH_START value, used in test_batch_startup_times.py
# Number of Ingress/VS resources to deploy based on BATCH_START value, used in test_batch_startup_times.py and test_batch_reloads.py
BATCH_RESOURCES = 1
# Threshold for batch reloads (reloads for batch requests should be at or below this number). Used in test_batch_reloads.py
BATCH_RELOAD_NUMBER = 2
# Number of namespaces to deploy to measure Pod performance, used in test_multiple_ns_perf.py
NS_COUNT = 0
66 changes: 36 additions & 30 deletions tests/suite/resources_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1209,21 +1209,24 @@ def create_items_from_yaml(kube_apis, yaml_manifest, namespace) -> {}:
print("Load yaml:")
with open(yaml_manifest) as f:
docs = yaml.safe_load_all(f)
for doc in docs:
if doc["kind"] == "Secret":
res["Secret"] = create_secret(kube_apis.v1, namespace, doc)
elif doc["kind"] == "ConfigMap":
res["ConfigMap"] = create_configmap(kube_apis.v1, namespace, doc)
elif doc["kind"] == "Ingress":
res["Ingress"] = create_ingress(kube_apis.networking_v1, namespace, doc)
elif doc["kind"] == "Service":
res["Service"] = create_service(kube_apis.v1, namespace, doc)
elif doc["kind"] == "Deployment":
res["Deployment"] = create_deployment(kube_apis.apps_v1_api, namespace, doc)
elif doc["kind"] == "DaemonSet":
res["DaemonSet"] = create_daemon_set(kube_apis.apps_v1_api, namespace, doc)
elif doc["kind"] == "Namespace":
res["Namespace"] = create_namespace(kube_apis.v1, doc)
try:
for doc in docs:
if doc["kind"] == "Secret":
res["Secret"] = create_secret(kube_apis.v1, namespace, doc)
elif doc["kind"] == "ConfigMap":
res["ConfigMap"] = create_configmap(kube_apis.v1, namespace, doc)
elif doc["kind"] == "Ingress":
res["Ingress"] = create_ingress(kube_apis.networking_v1, namespace, doc)
elif doc["kind"] == "Service":
res["Service"] = create_service(kube_apis.v1, namespace, doc)
elif doc["kind"] == "Deployment":
res["Deployment"] = create_deployment(kube_apis.apps_v1_api, namespace, doc)
elif doc["kind"] == "DaemonSet":
res["DaemonSet"] = create_daemon_set(kube_apis.apps_v1_api, namespace, doc)
elif doc["kind"] == "Namespace":
res["Namespace"] = create_namespace(kube_apis.v1, doc)
except Exception:
pass

return res

Expand Down Expand Up @@ -1322,21 +1325,24 @@ def delete_items_from_yaml(kube_apis, yaml_manifest, namespace) -> None:
print("Load yaml:")
with open(yaml_manifest) as f:
docs = yaml.safe_load_all(f)
for doc in docs:
if doc["kind"] == "Namespace":
delete_namespace(kube_apis.v1, doc["metadata"]["name"])
elif doc["kind"] == "Secret":
delete_secret(kube_apis.v1, doc["metadata"]["name"], namespace)
elif doc["kind"] == "Ingress":
delete_ingress(kube_apis.networking_v1, doc["metadata"]["name"], namespace)
elif doc["kind"] == "Service":
delete_service(kube_apis.v1, doc["metadata"]["name"], namespace)
elif doc["kind"] == "Deployment":
delete_deployment(kube_apis.apps_v1_api, doc["metadata"]["name"], namespace)
elif doc["kind"] == "DaemonSet":
delete_daemon_set(kube_apis.apps_v1_api, doc["metadata"]["name"], namespace)
elif doc["kind"] == "ConfigMap":
delete_configmap(kube_apis.v1, doc["metadata"]["name"], namespace)
try:
for doc in docs:
if doc["kind"] == "Namespace":
delete_namespace(kube_apis.v1, doc["metadata"]["name"])
elif doc["kind"] == "Secret":
delete_secret(kube_apis.v1, doc["metadata"]["name"], namespace)
elif doc["kind"] == "Ingress":
delete_ingress(kube_apis.networking_v1, doc["metadata"]["name"], namespace)
elif doc["kind"] == "Service":
delete_service(kube_apis.v1, doc["metadata"]["name"], namespace)
elif doc["kind"] == "Deployment":
delete_deployment(kube_apis.apps_v1_api, doc["metadata"]["name"], namespace)
elif doc["kind"] == "DaemonSet":
delete_daemon_set(kube_apis.apps_v1_api, doc["metadata"]["name"], namespace)
elif doc["kind"] == "ConfigMap":
delete_configmap(kube_apis.v1, doc["metadata"]["name"], namespace)
except Exception:
pass
ciarams87 marked this conversation as resolved.
Show resolved Hide resolved


def ensure_connection(request_url, expected_code=404, headers={}) -> None:
Expand Down
1 change: 1 addition & 0 deletions tests/suite/test_app_protect_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ def test_ap_enable_false_policy_correct(
ap_crd_info = read_ap_custom_resource(kube_apis.custom_objects, test_namespace, "appolicies", ap_policy)
assert_ap_crd_info(ap_crd_info, ap_policy)
ensure_response_from_backend(appprotect_setup.req_url, ingress_host, check404=True)
wait_before_test(5)

print("----------------------- Send request ----------------------")
response = requests.get(appprotect_setup.req_url + "/<script>", headers={"host": ingress_host}, verify=False)
Expand Down
Loading