Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1020 from weaveworks/add-regex-warning
Browse files Browse the repository at this point in the history
Add warning comments to all YAML-modifying regular expressions
  • Loading branch information
Sam Broughton authored Mar 22, 2018
2 parents f220230 + 797c67e commit 0e970a1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
6 changes: 4 additions & 2 deletions cluster/kubernetes/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func updateAnnotations(def []byte, tagAll string, f func(map[string]string) map[
fragment = strings.TrimSuffix(fragment, "\n")

// indent the fragment 2 spaces
// TODO: delete all regular expressions which are used to modify YAML.
// See #1019. Modifying this is not recommended.
fragment = regexp.MustCompile(`(.+)`).ReplaceAllString(fragment, " $1")

// Add a newline if it's not blank
Expand All @@ -74,8 +76,8 @@ func updateAnnotations(def []byte, tagAll string, f func(map[string]string) map[
}

// Find where to insert the fragment.
// TODO: This should handle potentially different indentation.
// TODO: There's probably a more elegant regex-ey way to do this in one pass.
// TODO: delete all regular expressions which are used to modify YAML.
// See #1019. Modifying this is not recommended.
replaced := false
annotationsRE := regexp.MustCompile(`(?m:\n annotations:\s*(?:#.*)*(?:\n .*|\n)*$)`)
newDef := annotationsRE.ReplaceAllStringFunc(string(def), func(found string) string {
Expand Down
14 changes: 12 additions & 2 deletions cluster/kubernetes/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,18 @@ func tryUpdate(def []byte, container string, newImage image.Ref, out io.Writer)
}

// Detect how indented the "containers" block is.
// TODO: delete all regular expressions which are used to modify YAML.
// See #1019. Modifying this is not recommended.
newDef := string(def)
matches := regexp.MustCompile(`( +)containers:.*`).FindStringSubmatch(newDef)
if len(matches) != 2 {
return fmt.Errorf("could not find container specs")
}
indent := matches[1]

optq :=`["']?` // An optional single or double quote
// TODO: delete all regular expressions which are used to modify YAML.
// See #1019. Modifying this is not recommended.
optq := `["']?` // An optional single or double quote
// Replace the container images
// Parse out all the container blocks
containersRE := regexp.MustCompile(`(?m:^` + indent + `containers:\s*(?:#.*)*$(?:\n(?:` + indent + `[-\s#].*)?)*)`)
Expand Down Expand Up @@ -158,6 +162,8 @@ func tryUpdate(def []byte, container string, newImage image.Ref, out io.Writer)
return fmt.Errorf("did not update expected containers: %s", strings.Join(missed, ", "))
}

// TODO: delete all regular expressions which are used to modify YAML.
// See #1019. Modifying this is not recommended.
// The name we want is that under `metadata:`, which will *probably* be the first one
replacedName := false
replaceRCNameRE := regexp.MustCompile(`(\s+` + optq + `name` + optq + `:\s*)` + optq + `(?:[\w\.\-/:]+\s*?)` + optq + `([\t\f #]+.*)`)
Expand All @@ -169,14 +175,16 @@ func tryUpdate(def []byte, container string, newImage image.Ref, out io.Writer)
return replaceRCNameRE.ReplaceAllString(found, fmt.Sprintf(`${1}%s${2}`, newDefName))
})

// TODO: delete all regular expressions which are used to modify YAML.
// See #1019. Modifying this is not recommended.
// Replacing labels: these are in two places, the container template and the selector
// TODO: This doesn't handle # comments
// TODO: This encodes an expectation of map keys being ordered (i.e. name *then* version)
// TODO: This assumes that these are indented by exactly 2 spaces (which may not be true)
replaceLabelsRE := multilineRE(
`((?: selector| labels):.*)`,
`((?: ){2,4}name:.*)`,
`((?: ){2,4}version:\s*) (?:` + optq + `[-\w]+` + optq + `)(\s.*)`,
`((?: ){2,4}version:\s*) (?:`+optq+`[-\w]+`+optq+`)(\s.*)`,
)
replaceLabels := fmt.Sprintf("$1\n$2\n$3 %s$4", maybeQuote(newImage.Tag))
newDef = replaceLabelsRE.ReplaceAllString(newDef, replaceLabels)
Expand All @@ -189,6 +197,8 @@ func multilineRE(lines ...string) *regexp.Regexp {
return regexp.MustCompile(`(?m:^` + strings.Join(lines, "\n") + `$)`)
}

// TODO: delete all regular expressions which are used to modify YAML.
// See #1019. Modifying this is not recommended.
var looksLikeNumber *regexp.Regexp = regexp.MustCompile("^(" + strings.Join([]string{
`(-?[1-9](\.[0-9]*[1-9])?(e[-+][1-9][0-9]*)?)`,
`(-?(0|[1-9][0-9]*))`,
Expand Down

0 comments on commit 0e970a1

Please sign in to comment.