Skip to content

Commit

Permalink
eskip: remove flowid dependency (#3065)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
AlexanderYastrebov authored May 8, 2024
1 parent f97919e commit b778f2b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
31 changes: 15 additions & 16 deletions eskip/eskip.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ package eskip
import (
"errors"
"fmt"
"math/rand"
"net/url"
"regexp"
"strings"
"sync"

log "github.com/sirupsen/logrus"
"github.com/zalando/skipper/filters/flowid"
)

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

const randomIdLength = 16

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

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

// using this to avoid adding a new dependency.
g, err := flowid.NewStandardGenerator(randomIdLength)
if err != nil {
return existingId
}
id, err := g.Generate()
if err != nil {
return existingId
var sb strings.Builder
sb.WriteString("route")

for i := 0; i < randomIdLength; i++ {
ai := rand.Intn(len(alphabet))
sb.WriteByte(alphabet[ai])
}

// replace characters that are not allowed
// for eskip route ids.
id = routeIdRx.ReplaceAllString(id, "x")
return "route" + id
return sb.String()
}
1 change: 1 addition & 0 deletions etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ func (c *Client) Delete(id string) error {

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

0 comments on commit b778f2b

Please sign in to comment.