Skip to content

Commit b778f2b

Browse files
eskip: remove flowid dependency (#3065)
eskip package is generally useful without proxy or filters and therefore ideally should have minimal number of dependencies. This change deprecates GenerateIfNeeded which is only used by etcd dataclient and reimplements it to remove dependency on github.com/zalando/skipper/filters/flowid See 35ba6cc Signed-off-by: Alexander Yastrebov <[email protected]>
1 parent f97919e commit b778f2b

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

eskip/eskip.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ package eskip
55
import (
66
"errors"
77
"fmt"
8+
"math/rand"
89
"net/url"
910
"regexp"
1011
"strings"
1112
"sync"
1213

1314
log "github.com/sirupsen/logrus"
14-
"github.com/zalando/skipper/filters/flowid"
1515
)
1616

1717
const duplicateHeaderPredicateErrorFmt = "duplicate header predicate: %s"
@@ -712,29 +712,28 @@ func ParsePredicates(p string) ([]*Predicate, error) {
712712
return ps, nil
713713
}
714714

715-
const randomIdLength = 16
716-
717-
var routeIdRx = regexp.MustCompile(`\W`)
715+
const (
716+
randomIdLength = 16
717+
// does not contain underscore to produce compatible output with previously used flow id generator
718+
alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
719+
)
718720

719721
// generate weak random id for a route if
720722
// it doesn't have one.
723+
//
724+
// Deprecated: do not use, generate valid route id that matches [a-zA-Z_] yourself.
721725
func GenerateIfNeeded(existingId string) string {
722726
if existingId != "" {
723727
return existingId
724728
}
725729

726-
// using this to avoid adding a new dependency.
727-
g, err := flowid.NewStandardGenerator(randomIdLength)
728-
if err != nil {
729-
return existingId
730-
}
731-
id, err := g.Generate()
732-
if err != nil {
733-
return existingId
730+
var sb strings.Builder
731+
sb.WriteString("route")
732+
733+
for i := 0; i < randomIdLength; i++ {
734+
ai := rand.Intn(len(alphabet))
735+
sb.WriteByte(alphabet[ai])
734736
}
735737

736-
// replace characters that are not allowed
737-
// for eskip route ids.
738-
id = routeIdRx.ReplaceAllString(id, "x")
739-
return "route" + id
738+
return sb.String()
740739
}

etcd/etcd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ func (c *Client) Delete(id string) error {
493493

494494
func (c *Client) UpsertAll(routes []*eskip.Route) error {
495495
for _, r := range routes {
496+
//lint:ignore SA1019 due to backward compatibility
496497
r.Id = eskip.GenerateIfNeeded(r.Id)
497498
err := c.Upsert(r)
498499
if err != nil {

0 commit comments

Comments
 (0)