From d02399232428db861c2ecbfd717694c4ae3f8d12 Mon Sep 17 00:00:00 2001 From: Yauheni Kaliuta Date: Thu, 29 Aug 2024 11:27:53 +0300 Subject: [PATCH] deploy: ApplyParams: do not write params.env if there are no updates (#1186) Optimize file operations, do not rewrite params.env on every reconcile if no updates for it. In practice updates happen only on the first run. Since updates happen in 2 loops, move actual update to own function which returns "number of updates" (actually 0 or 1, so acts as boolean) and use bitwise operations to accumulate updates for every field. It could be possible to use global boolean flag, but since it requires to be initiated before the loops and set when updates happen, the function should be guarded with mutex. Otherwise "true" value from one invocation can be reset to "false" by the next invocation". Signed-off-by: Yauheni Kaliuta (cherry picked from commit 450e19fd1afa76b32d663938da4376431b4bbf27) --- pkg/deploy/envParams.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/pkg/deploy/envParams.go b/pkg/deploy/envParams.go index a3eda173c1f..96ce3c7c577 100644 --- a/pkg/deploy/envParams.go +++ b/pkg/deploy/envParams.go @@ -53,6 +53,17 @@ func writeParamsToTmp(params map[string]string, tmpDir string) (string, error) { return tmp.Name(), nil } +// updateMap returns the number of updates made (it operates on 1 field, so 0 or 1 only). +func updateMap(m *map[string]string, key, val string) int { + old := (*m)[key] + if old == val { + return 0 + } + + (*m)[key] = val + return 1 +} + /* overwrite values in components' manifests params.env file This is useful for air gapped cluster @@ -75,22 +86,30 @@ func ApplyParams(componentPath string, imageParamsMap map[string]string, extraPa return err } + // will be used as a boolean (0 or non-0) and accumulate result of updates of every field + // Could use sum, but safe from hypothetically integer overflow + updated := 0 + // 1. Update images with env variables // e.g "odh-kuberay-operator-controller-image": "RELATED_IMAGE_ODH_KUBERAY_OPERATOR_CONTROLLER_IMAGE", for i := range paramsEnvMap { relatedImageValue := os.Getenv(imageParamsMap[i]) if relatedImageValue != "" { - paramsEnvMap[i] = relatedImageValue + updated |= updateMap(¶msEnvMap, i, relatedImageValue) } } // 2. Update other fileds with extraParamsMap which are not carried from component for _, extraParamsMap := range extraParamsMaps { for eKey, eValue := range extraParamsMap { - paramsEnvMap[eKey] = eValue + updated |= updateMap(¶msEnvMap, eKey, eValue) } } + if updated == 0 { + return nil + } + tmp, err := writeParamsToTmp(paramsEnvMap, componentPath) if err != nil { return err