github.com/uber/kraken@v0.1.4/lib/persistedretry/tagreplication/remotes.go (about) 1 // Copyright (c) 2016-2019 Uber Technologies, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package tagreplication 15 16 import ( 17 "fmt" 18 "regexp" 19 ) 20 21 // RemoteValidator validates remotes. 22 type RemoteValidator interface { 23 Valid(tag, addr string) bool 24 } 25 26 // Remote represents a remote build-index. 27 type Remote struct { 28 regexp *regexp.Regexp 29 addr string 30 } 31 32 // Remotes represents all namespaces and their configured remote build-indexes. 33 type Remotes []*Remote 34 35 // Match returns all matched remotes for a tag. 36 func (rs Remotes) Match(tag string) (addrs []string) { 37 for _, r := range rs { 38 if r.regexp.MatchString(tag) { 39 addrs = append(addrs, r.addr) 40 } 41 } 42 return addrs 43 } 44 45 // Valid returns true if tag matches to addr. 46 func (rs Remotes) Valid(tag, addr string) bool { 47 for _, a := range rs.Match(tag) { 48 if a == addr { 49 return true 50 } 51 } 52 return false 53 } 54 55 // RemotesConfig defines remote replication configuration which specifies which 56 // namespaces should be replicated to certain build-indexes. 57 // 58 // For example, given the configuration: 59 // 60 // build-index-zone1: 61 // - namespace_foo/.* 62 // 63 // build-index-zone2: 64 // - namespace_foo/.* 65 // 66 // Any builds matching the namespace_foo/.* namespace should be replicated to 67 // zone1 and zone2 build-indexes. 68 type RemotesConfig map[string][]string 69 70 // Build builds configuration into Remotes. 71 func (c RemotesConfig) Build() (Remotes, error) { 72 var remotes Remotes 73 for addr, namespaces := range c { 74 for _, ns := range namespaces { 75 re, err := regexp.Compile(ns) 76 if err != nil { 77 return nil, fmt.Errorf("regexp compile namespace %s: %s", ns, err) 78 } 79 remotes = append(remotes, &Remote{re, addr}) 80 } 81 } 82 return remotes, nil 83 }