Skip to content

Commit

Permalink
deploy: ApplyParams: do not write params.env if there are no updates (o…
Browse files Browse the repository at this point in the history
…pendatahub-io#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 <[email protected]>
(cherry picked from commit 450e19f)
  • Loading branch information
ykaliuta authored and zdtsw committed Aug 29, 2024
1 parent 66f3980 commit 2afbe50
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions pkg/deploy/envParams.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(&paramsEnvMap, 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(&paramsEnvMap, eKey, eValue)
}
}

if updated == 0 {
return nil
}

tmp, err := writeParamsToTmp(paramsEnvMap, componentPath)
if err != nil {
return err
Expand Down

0 comments on commit 2afbe50

Please sign in to comment.