use only 1 repo and tag selector to match

Signed-off-by: wang yan <wangyan@vmware.com>
This commit is contained in:
wang yan 2019-10-15 16:44:14 +08:00
parent a3546478eb
commit ae4c698ab4
2 changed files with 92 additions and 26 deletions

View File

@ -28,39 +28,44 @@ func (rm *Matcher) Match(c art.Candidate) (bool, error) {
// match repositories according to the repository selectors // match repositories according to the repository selectors
var repositoryCandidates []*art.Candidate var repositoryCandidates []*art.Candidate
for _, repositorySelector := range r.ScopeSelectors["repository"] { repositorySelectors := r.ScopeSelectors["repository"]
selector, err := index.Get(repositorySelector.Kind, repositorySelector.Decoration, if len(repositorySelectors) < 1 {
repositorySelector.Pattern) continue
if err != nil { }
return false, err repositorySelector := repositorySelectors[0]
} selector, err := index.Get(repositorySelector.Kind, repositorySelector.Decoration,
repositoryCandidates, err = selector.Select(cands) repositorySelector.Pattern)
if err != nil { if err != nil {
return false, err return false, err
} }
repositoryCandidates, err = selector.Select(cands)
if err != nil {
return false, err
} }
if len(repositoryCandidates) == 0 { if len(repositoryCandidates) == 0 {
continue continue
} }
// match tag according to the tag selectors // match tag according to the tag selectors
var tagCandidates []*art.Candidate var tagCandidates []*art.Candidate
for _, tagSelector := range r.TagSelectors { tagSelectors := r.TagSelectors
selector, err := index.Get(tagSelector.Kind, tagSelector.Decoration, if len(tagSelectors) < 0 {
tagSelector.Pattern) continue
if err != nil { }
return false, err tagSelector := r.TagSelectors[0]
} selector, err = index.Get(tagSelector.Kind, tagSelector.Decoration,
tagCandidates, err = selector.Select(cands) tagSelector.Pattern)
if err != nil { if err != nil {
return false, err return false, err
} }
tagCandidates, err = selector.Select(cands)
if err != nil {
return false, err
} }
if len(tagCandidates) == 0 { if len(tagCandidates) == 0 {
continue continue
} }
return true, nil return true, nil
} }
return false, nil return false, nil

View File

@ -20,6 +20,7 @@ type MatchTestSuite struct {
require *require.Assertions require *require.Assertions
ctr immutabletag.APIController ctr immutabletag.APIController
ruleID int64 ruleID int64
ruleID2 int64
} }
// SetupSuite ... // SetupSuite ...
@ -50,7 +51,31 @@ func (s *MatchTestSuite) TestImmuMatch() {
{ {
Kind: "doublestar", Kind: "doublestar",
Decoration: "matches", Decoration: "matches",
Pattern: "**", Pattern: "redis",
},
},
},
}
rule2 := &model.Metadata{
ID: 1,
ProjectID: 2,
Priority: 1,
Template: "latestPushedK",
Action: "immuablity",
TagSelectors: []*model.Selector{
{
Kind: "doublestar",
Decoration: "matches",
Pattern: "**",
},
},
ScopeSelectors: map[string][]*model.Selector{
"repository": {
{
Kind: "doublestar",
Decoration: "matches",
Pattern: "mysql",
}, },
}, },
}, },
@ -60,6 +85,12 @@ func (s *MatchTestSuite) TestImmuMatch() {
s.ruleID = id s.ruleID = id
s.require.NotNil(err) s.require.NotNil(err)
id, err = s.ctr.CreateImmutableRule(rule2)
s.ruleID2 = id
s.require.NotNil(err)
match := NewRuleMatcher(2)
c1 := art.Candidate{ c1 := art.Candidate{
NamespaceID: 2, NamespaceID: 2,
Namespace: "immutable", Namespace: "immutable",
@ -71,8 +102,6 @@ func (s *MatchTestSuite) TestImmuMatch() {
CreationTime: time.Now().Unix() - 7200, CreationTime: time.Now().Unix() - 7200,
Labels: []string{"label1", "label4", "label5"}, Labels: []string{"label1", "label4", "label5"},
} }
match := NewRuleMatcher(2)
isMatch, err := match.Match(c1) isMatch, err := match.Match(c1)
s.require.Equal(isMatch, true) s.require.Equal(isMatch, true)
s.require.Nil(err) s.require.Nil(err)
@ -88,14 +117,46 @@ func (s *MatchTestSuite) TestImmuMatch() {
CreationTime: time.Now().Unix() - 7200, CreationTime: time.Now().Unix() - 7200,
Labels: []string{"label1", "label4", "label5"}, Labels: []string{"label1", "label4", "label5"},
} }
isMatch, err = match.Match(c2) isMatch, err = match.Match(c2)
s.require.Equal(isMatch, false) s.require.Equal(isMatch, false)
s.require.Nil(err) s.require.Nil(err)
c3 := art.Candidate{
NamespaceID: 2,
Namespace: "immutable",
Repository: "mysql",
Tag: "9.4.8",
Kind: art.Image,
PushedTime: time.Now().Unix() - 3600,
PulledTime: time.Now().Unix(),
CreationTime: time.Now().Unix() - 7200,
Labels: []string{"label1"},
}
isMatch, err = match.Match(c3)
s.require.Equal(isMatch, true)
s.require.Nil(err)
c4 := art.Candidate{
NamespaceID: 2,
Namespace: "immutable",
Repository: "hello",
Tag: "world",
Kind: art.Image,
PushedTime: time.Now().Unix() - 3600,
PulledTime: time.Now().Unix(),
CreationTime: time.Now().Unix() - 7200,
Labels: []string{"label1"},
}
isMatch, err = match.Match(c4)
s.require.Equal(isMatch, false)
s.require.Nil(err)
} }
// TearDownSuite clears env for test suite // TearDownSuite clears env for test suite
func (s *MatchTestSuite) TearDownSuite() { func (s *MatchTestSuite) TearDownSuite() {
err := s.ctr.DeleteImmutableRule(s.ruleID) err := s.ctr.DeleteImmutableRule(s.ruleID)
require.NoError(s.T(), err, "delete immutable") require.NoError(s.T(), err, "delete immutable")
err = s.ctr.DeleteImmutableRule(s.ruleID2)
require.NoError(s.T(), err, "delete immutable")
} }