Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic when regex is not valid #24

Merged
merged 2 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Notes:
Below is an example of a VersionTracker resource:

```yaml
apiVersion: vt.skillz.com/v1alpha1
apiVersion: opvic.skillz.com/v1alpha1
kind: VersionTracker
metadata:
name: myApp # name of the subject
Expand Down
7 changes: 6 additions & 1 deletion agent/extract_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ func (r *VersionTrackerReconciler) ExtractSubjectVersion(v v1alpha1.VersionTrack
continue
}
fieldValue := valueStrings[0]
version = GetResultsFromRegex(lv.Extraction.Regex.Pattern, lv.Extraction.Regex.Result, fieldValue)
version, err = utils.GetResultsFromRegex(lv.Extraction.Regex.Pattern, lv.Extraction.Regex.Result, fieldValue)
if err != nil {
log.Error(fmt.Errorf("failed to extract version from: %s", fieldValue), "invalid regex", "regex", lv.Extraction.Regex.Pattern, "result template", lv.Extraction.Regex.Result)
reconciliationErrorsTotal.Inc()
continue
}
if version == "" {
log.Error(fmt.Errorf("failed to extract version from: %s", fieldValue), "extraction failed", "regex", lv.Extraction.Regex.Pattern, "result template", lv.Extraction.Regex.Result)
reconciliationErrorsTotal.Inc()
Expand Down
8 changes: 0 additions & 8 deletions agent/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package agent

import (
"fmt"
"regexp"

"k8s.io/client-go/util/jsonpath"
"k8s.io/kubectl/pkg/cmd/get"
Expand All @@ -29,10 +28,3 @@ func getFeilds(jsonPath string, resource interface{}) ([]string, error) {
}
return valueStrings, nil
}

func GetResultsFromRegex(pattern, tmpl, content string) string {
regex := regexp.MustCompile(pattern)
matches := regex.FindStringSubmatchIndex(content)
result := regex.ExpandString([]byte{}, tmpl, content, matches)
return string(result)
}
10 changes: 8 additions & 2 deletions controlplane/providers/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ func (p *Provider) getVersionsFromReleases(conf v1alpha1.RemoteVersion) ([]strin
if release.GetTagName() == "" {
continue
}
matched, v := utils.MatchPattern(conf.Extraction.Regex.Pattern, conf.Extraction.Regex.Result, release.GetName())
matched, v, err := utils.MatchPattern(conf.Extraction.Regex.Pattern, conf.Extraction.Regex.Result, release.GetName())
if err != nil {
return nil, err
}
if matched {
matchedVersions = append(matchedVersions, v)
}
Expand Down Expand Up @@ -217,7 +220,10 @@ func (p *Provider) getVersionsFromTags(conf v1alpha1.RemoteVersion) ([]string, e
if tag.GetName() == "" {
continue
}
matched, v := utils.MatchPattern(conf.Extraction.Regex.Pattern, conf.Extraction.Regex.Result, tag.GetName())
matched, v, err := utils.MatchPattern(conf.Extraction.Regex.Pattern, conf.Extraction.Regex.Result, tag.GetName())
if err != nil {
return nil, err
}
if matched {
matchedVersions = append(matchedVersions, v)
}
Expand Down
5 changes: 4 additions & 1 deletion controlplane/providers/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ func (p *Provider) GetVersions(conf v1alpha1.RemoteVersion) ([]string, error) {
} else if conf.Strategy == v1alpha1.HelmStrategyAppVersion {
version = chartVersion.AppVersion
}
matched, v := utils.MatchPattern(conf.Extraction.Regex.Pattern, conf.Extraction.Regex.Result, version)
matched, v, err := utils.MatchPattern(conf.Extraction.Regex.Pattern, conf.Extraction.Regex.Result, version)
if err != nil {
return nil, err
}
if matched {
matchedVersions = append(matchedVersions, v)
}
Expand Down
20 changes: 13 additions & 7 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@ import (
"github.com/hashicorp/go-version"
)

func GetResultsFromRegex(pattern, tmpl, content string) string {
func GetResultsFromRegex(pattern, tmpl, content string) (string, error) {
if pattern == "" || tmpl == "" {
return content
return content, nil
}
regex, err := regexp.Compile(pattern)
if err != nil {
return "", err
}
regex := regexp.MustCompile(pattern)
matches := regex.FindStringSubmatchIndex(content)
result := regex.ExpandString([]byte{}, tmpl, content, matches)
return string(result)
return string(result), nil
}

func MatchPattern(pattern, tmpl, version string) (bool, string) {
result := GetResultsFromRegex(pattern, tmpl, version)
return result != "", result
func MatchPattern(pattern, tmpl, version string) (bool, string, error) {
result, err := GetResultsFromRegex(pattern, tmpl, version)
if err != nil {
return false, "", err
}
return result != "", result, nil
}

func MeetConstraint(constraint, ver string) (bool, error) {
Expand Down