Browse Source

Make `regex` an alias of `regexp` tag filter type

The word `regex` seems to be a common alias of `regexp`, so people
often use this and are left with confusion when Flux does not adhere
to what they configured.

Make it an alias to better live up to people their expectations.
Hidde Beydals 5 months ago
parent
commit
dfc63b6bdf
3 changed files with 21 additions and 8 deletions
  1. 13
    7
      policy/pattern.go
  2. 7
    1
      policy/pattern_test.go
  3. 1
    0
      site/fluxctl.md

+ 13
- 7
policy/pattern.go View File

@@ -1,17 +1,19 @@
1 1
 package policy
2 2
 
3 3
 import (
4
+	"regexp"
5
+	"strings"
6
+
4 7
 	"github.com/Masterminds/semver"
5 8
 	"github.com/ryanuber/go-glob"
6 9
 	"github.com/weaveworks/flux/image"
7
-	"strings"
8
-	"regexp"
9 10
 )
10 11
 
11 12
 const (
12
-	globPrefix   = "glob:"
13
-	semverPrefix = "semver:"
14
-	regexpPrefix = "regexp:"
13
+	globPrefix      = "glob:"
14
+	semverPrefix    = "semver:"
15
+	regexpPrefix    = "regexp:"
16
+	regexpAltPrefix = "regex:"
15 17
 )
16 18
 
17 19
 var (
@@ -43,8 +45,8 @@ type SemverPattern struct {
43 45
 
44 46
 // RegexpPattern matches by regular expression.
45 47
 type RegexpPattern struct {
46
-	pattern	string // pattern without prefix
47
-	regexp	*regexp.Regexp
48
+	pattern string // pattern without prefix
49
+	regexp  *regexp.Regexp
48 50
 }
49 51
 
50 52
 // NewPattern instantiates a Pattern according to the prefix
@@ -60,6 +62,10 @@ func NewPattern(pattern string) Pattern {
60 62
 		pattern = strings.TrimPrefix(pattern, regexpPrefix)
61 63
 		r, _ := regexp.Compile(pattern)
62 64
 		return RegexpPattern{pattern, r}
65
+	case strings.HasPrefix(pattern, regexpAltPrefix):
66
+		pattern = strings.TrimPrefix(pattern, regexpAltPrefix)
67
+		r, _ := regexp.Compile(pattern)
68
+		return RegexpPattern{pattern, r}
63 69
 	default:
64 70
 		return GlobPattern(strings.TrimPrefix(pattern, globPrefix))
65 71
 	}

+ 7
- 1
policy/pattern_test.go View File

@@ -1,9 +1,9 @@
1 1
 package policy
2 2
 
3 3
 import (
4
+	"fmt"
4 5
 	"testing"
5 6
 
6
-	"fmt"
7 7
 	"github.com/stretchr/testify/assert"
8 8
 )
9 9
 
@@ -106,6 +106,12 @@ func TestRegexpPattern_Matches(t *testing.T) {
106 106
 			true:    []string{"foo", "BAR", "fooBAR"},
107 107
 			false:   []string{"1", "foo-1"},
108 108
 		},
109
+		{
110
+			name:    "regex",
111
+			pattern: `regex:^\w{7}(?:\w)?$`,
112
+			true:    []string{"af14eb2", "bb73ed94", "946427ff"},
113
+			false:   []string{"1", "foo", "946427ff-foo"},
114
+		},
109 115
 	} {
110 116
 		pattern := NewPattern(tt.pattern)
111 117
 		assert.IsType(t, RegexpPattern{}, pattern)

+ 1
- 0
site/fluxctl.md View File

@@ -536,6 +536,7 @@ If your images have complex tags you can filter by regular expression:
536 536
 fluxctl policy --workload=default:deployment/helloworld --tag-all='regexp:^([a-zA-Z]+)$'
537 537
 ```
538 538
 
539
+Instead of `regexp` it is also possible to use its alias `regex`.
539 540
 Please bear in mind that if you want to match the whole tag,
540 541
 you must bookend your pattern with `^` and `$`.
541 542
 

Loading…
Cancel
Save