-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ServerURL must begin with http or https Signed-off-by: lubronzhan <lzhan@vmware.com>
- Loading branch information
1 parent
9e31638
commit e4f467a
Showing
8 changed files
with
288 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,56 @@ | ||
package rule | ||
|
||
import "strings" | ||
import ( | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
type Rule struct { | ||
RegistryRegex string | ||
Project string | ||
ServerURL string | ||
} | ||
|
||
// assume rule.Rules are concatentated by ','. | ||
func StringToRules(raw []string, server string) []Rule { | ||
// StringToRules parse rule and create Rule object | ||
// assume rule.Rules are concatentated by '=>'. | ||
func StringToRules(raw []string, server string) ([]Rule, error) { | ||
res := make([]Rule, 0) | ||
|
||
// remove https/http from the serverURL | ||
u, err := url.Parse(server) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, r := range raw { | ||
registryRegex := r[:strings.LastIndex(r, ",")] | ||
project := r[strings.LastIndex(r, ",")+1:] | ||
// format read from configMap could be like '- docker.io=>value' | ||
if r == "" || !strings.Contains(r, "=>") { | ||
return nil, errors.Errorf("rule '%s' is invalid", r) | ||
} | ||
|
||
if len(r) >= 2 && r[0:2] == "- " { | ||
r = r[2:] | ||
} | ||
|
||
lastIndex := strings.LastIndex(r, "=") | ||
registryRegex := r[:lastIndex] | ||
project := r[lastIndex+2:] | ||
|
||
res = append(res, Rule{ | ||
RegistryRegex: registryRegex, | ||
Project: project, | ||
ServerURL: server, | ||
ServerURL: u.Host, | ||
}) | ||
} | ||
|
||
return res | ||
return res, nil | ||
} | ||
|
||
// append l after h, so l will be checked first. | ||
// there could be cases that regex in h is `gcr.io`, while in l is `gcr.io*`. | ||
func MergeRules(l, h []Rule) []Rule { | ||
// MergeRules appends rule l after h, so h will be checked first. | ||
// we append instead of merge since rules are regex, hard to merge, | ||
// for example 'google.com' and '$google.com^' are the same. | ||
func MergeRules(h, l []Rule) []Rule { | ||
return append(h, l...) | ||
} |
Oops, something went wrong.