Skip to content

Commit 6a93cc0

Browse files
knopers8teo
authored andcommitted
[core] hash processed config payloads from apricot during JIT update checks
This commit makes the JIT template generation support both consul-json and apricot backends of the O2 Configuration library. Also, instead of the LastIndex, we use the processed configuration payload in the hash sum which is used to detect any relevant changes in the DPL workflows.
1 parent 981cc04 commit 6a93cc0

1 file changed

Lines changed: 20 additions & 16 deletions

File tree

configuration/template/dplutil.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"os/exec"
3333
"path/filepath"
3434
"regexp"
35-
"strconv"
3635
"strings"
3736
texttemplate "text/template"
3837

@@ -45,29 +44,31 @@ import (
4544
// and returns the resolved dplWorkflow as a string
4645
func jitDplGenerate(confSvc ConfigurationService, varStack map[string]string, workflowRepo repos.IRepo, dplCommand string) (jitWorkflowName string, err error) {
4746
const nMaxExpectedQcPayloads = 2
48-
var metadata string
47+
var payloads []string
4948

5049
// Match any consul URL
51-
re := regexp.MustCompile(`consul-json://[^ |\n]*`)
50+
re := regexp.MustCompile(`(consul-json|apricot)://[^ |\n]*`)
5251
matches := re.FindAllStringSubmatch(dplCommand, nMaxExpectedQcPayloads)
52+
matches = append(matches)
5353

54-
// Concatenate the consul LastIndex for each payload in a single string
54+
// Gather all the processed configuration payloads from apricot
5555
for _, match := range matches {
5656
// Match any key under components
5757
keyRe := regexp.MustCompile(`components/[^']*`)
5858
consulKeyMatch := keyRe.FindAllStringSubmatch(match[0], 1)
5959
consulKey := strings.SplitAfter(consulKeyMatch[0][0], "components/")
6060

61-
// And query for Consul for its LastIndex
61+
// And query Apricot for the configuration payload
6262
newQ, err := componentcfg.NewQuery(consulKey[1])
6363
if err != nil {
6464
return "", fmt.Errorf("JIT could not create a query out of path '%s'. error: %w", consulKey[1], err)
6565
}
66-
_, lastIndex, err := confSvc.GetComponentConfigurationWithLastIndex(newQ)
66+
payload, err := confSvc.GetComponentConfiguration(newQ)
67+
6768
if err != nil {
68-
return "", fmt.Errorf("JIT failed trying to query qc consul payload '%s', error: %w", match, err)
69+
return "", fmt.Errorf("JIT failed trying to query QC payload '%s', error: %w", match, err)
6970
}
70-
metadata += strconv.FormatUint(lastIndex, 10)
71+
payloads = append(payloads, payload)
7172
}
7273

7374
// Get the O2 & QualityControl version
@@ -80,20 +81,23 @@ func jitDplGenerate(confSvc ConfigurationService, varStack map[string]string, wo
8081
// Get the env vars necessary for JIT
8182
jitEnvVars := varStack["jit_env_vars"]
8283

83-
// Generate a hash out of the concatenation of
84+
// Generate a hash out of
8485
// 1) The full DPL command
85-
// 2) The LastIndex of each payload
86-
// 3) The O2 + QualityControl package versions
87-
// 4) The JIT env vars
86+
// 2) The O2 + QualityControl package versions
87+
// 3) The JIT env vars
88+
// 4) The returned configuration payloads (as separate Write calls to avoid copies of large strings)
8889
hash := sha1.New()
89-
hash.Write([]byte(dplCommand + metadata + string(o2VersionOut) + jitEnvVars))
90+
hash.Write([]byte(dplCommand + string(o2VersionOut) + jitEnvVars))
91+
for _, payload := range payloads {
92+
hash.Write([]byte(payload))
93+
}
9094
jitWorkflowName = "jit-" + hex.EncodeToString(hash.Sum(nil))
9195

9296
// We now have a workflow name made out of a hash that should be unique with respect to
9397
// 1) DPL command and
94-
// 2) Consul payload versions
95-
// 3) O2 + QualityControl package versions
96-
// 4) JIT env vars
98+
// 2) O2 + QualityControl package versions
99+
// 3) JIT env vars
100+
// 4) Configuration payloads returned by Apricot
97101
// Only generate new tasks & workflows if the files don't exist
98102
// If they exist, hash comparison guarantees validity
99103
if _, err = os.Stat(filepath.Join(workflowRepo.GetCloneDir(), "workflows", jitWorkflowName+".yaml")); err == nil {

0 commit comments

Comments
 (0)