From 7c6d621c25f247ea8933a2f539a50472d62ddf19 Mon Sep 17 00:00:00 2001 From: Kamil Sambor Date: Mon, 25 Nov 2024 15:25:41 +0100 Subject: [PATCH] Added a parameter 'apiTimeout' to allow customization to the Apache timeout. The default is set to 60s which we do set for HAProxy timeouts currently. To be able to change the HAProxy value based on the apiTimeout with any update (and not just the first time) the code adds a custom annotation "api.placement.openstack.org/timeout" with the value that was initially set, this way flags it as being set by the placement-operator. There will be follow up patch in openstack-operator to utilize the method 'SetDefaultRouteAnnotations' to set these default route annotations in openstack-operator --- ...placement.openstack.org_placementapis.yaml | 5 ++++ api/v1beta1/placementapi_types.go | 6 +++++ api/v1beta1/placementapi_webhook.go | 23 +++++++++++++++++++ ...placement.openstack.org_placementapis.yaml | 5 ++++ controllers/placementapi_controller.go | 1 + templates/placementapi/config/httpd.conf | 1 + .../placementapi_controller_test.go | 3 +++ 7 files changed, 44 insertions(+) diff --git a/api/bases/placement.openstack.org_placementapis.yaml b/api/bases/placement.openstack.org_placementapis.yaml index 97a02555..263ef2da 100644 --- a/api/bases/placement.openstack.org_placementapis.yaml +++ b/api/bases/placement.openstack.org_placementapis.yaml @@ -48,6 +48,11 @@ spec: spec: description: PlacementAPISpec defines the desired state of PlacementAPI properties: + apiTimeout: + default: 60 + description: APITimeout for HAProxy, Apache + minimum: 10 + type: integer containerImage: description: PlacementAPI Container Image URL (will be set to environmental default if empty) diff --git a/api/v1beta1/placementapi_types.go b/api/v1beta1/placementapi_types.go index e0ac84fd..a072e83b 100644 --- a/api/v1beta1/placementapi_types.go +++ b/api/v1beta1/placementapi_types.go @@ -50,6 +50,12 @@ type PlacementAPISpec struct { // PlacementAPISpecCore - type PlacementAPISpecCore struct { + // +kubebuilder:validation:Optional + // +kubebuilder:default=60 + // +kubebuilder:validation:Minimum=10 + // APITimeout for HAProxy, Apache + APITimeout int `json:"apiTimeout"` + // +kubebuilder:validation:Optional // +kubebuilder:default=placement // ServiceUser - optional username used for this service to register in keystone diff --git a/api/v1beta1/placementapi_webhook.go b/api/v1beta1/placementapi_webhook.go index 7ddf75c8..841e10f8 100644 --- a/api/v1beta1/placementapi_webhook.go +++ b/api/v1beta1/placementapi_webhook.go @@ -175,3 +175,26 @@ func ValidateDefaultConfigOverwrite( } return errors } + +// SetDefaultRouteAnnotations sets HAProxy timeout values of the route +func (spec *PlacementAPISpecCore) SetDefaultRouteAnnotations(annotations map[string]string) { + const haProxyAnno = "haproxy.router.openshift.io/timeout" + // Use a custom annotation to flag when the operator has set the default HAProxy timeout + // With the annotation func determines when to overwrite existing HAProxy timeout with the APITimeout + const placementAnno = "api.placement.openstack.org/timeout" + valPlacementAPI, okPlacementAPI := annotations[placementAnno] + valHAProxy, okHAProxy := annotations[haProxyAnno] + // Human operator set the HAProxy timeout manually + if !okPlacementAPI && okHAProxy { + return + } + // Human operator modified the HAProxy timeout manually without removing the Placemen flag + if okPlacementAPI && okHAProxy && valPlacementAPI != valHAProxy { + delete(annotations, placementAnno) + placementapilog.Info("Human operator modified the HAProxy timeout manually without removing the Placement flag. Deleting the Placement flag to ensure proper configuration.") + return + } + timeout := fmt.Sprintf("%ds", spec.APITimeout) + annotations[placementAnno] = timeout + annotations[haProxyAnno] = timeout +} diff --git a/config/crd/bases/placement.openstack.org_placementapis.yaml b/config/crd/bases/placement.openstack.org_placementapis.yaml index 97a02555..263ef2da 100644 --- a/config/crd/bases/placement.openstack.org_placementapis.yaml +++ b/config/crd/bases/placement.openstack.org_placementapis.yaml @@ -48,6 +48,11 @@ spec: spec: description: PlacementAPISpec defines the desired state of PlacementAPI properties: + apiTimeout: + default: 60 + description: APITimeout for HAProxy, Apache + minimum: 10 + type: integer containerImage: description: PlacementAPI Container Image URL (will be set to environmental default if empty) diff --git a/controllers/placementapi_controller.go b/controllers/placementapi_controller.go index 310c648e..410ab41c 100644 --- a/controllers/placementapi_controller.go +++ b/controllers/placementapi_controller.go @@ -1265,6 +1265,7 @@ func (r *PlacementAPIReconciler) generateServiceConfigMaps( httpdVhostConfig[endpt.String()] = endptConfig } templateParameters["VHosts"] = httpdVhostConfig + templateParameters["TimeOut"] = instance.Spec.APITimeout extraTemplates := map[string]string{ "placement.conf": "placementapi/config/placement.conf", diff --git a/templates/placementapi/config/httpd.conf b/templates/placementapi/config/httpd.conf index f114b9bf..7c53ffd7 100644 --- a/templates/placementapi/config/httpd.conf +++ b/templates/placementapi/config/httpd.conf @@ -35,6 +35,7 @@ CustomLog /dev/stdout proxy env=forwarded ErrorLogFormat "%M" ServerName {{ $vhost.ServerName }} + TimeOut {{ $.TimeOut }} ## Vhost docroot ErrorLog /dev/stdout diff --git a/tests/functional/placementapi_controller_test.go b/tests/functional/placementapi_controller_test.go index 79a95e7d..a977eaa2 100644 --- a/tests/functional/placementapi_controller_test.go +++ b/tests/functional/placementapi_controller_test.go @@ -344,6 +344,9 @@ var _ = Describe("PlacementAPI controller", func() { myCnf := cm.Data["my.cnf"] Expect(myCnf).To( ContainSubstring("[client]\nssl=0")) + configData := cm.Data["httpd.conf"] + Expect(configData).Should( + ContainSubstring("TimeOut 60")) }) It("creates service account, role and rolebindig", func() {