ShortURL: Avoid teris-io/shortid (#110456)
This commit is contained in:
@@ -5,8 +5,6 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/teris-io/shortid"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
@@ -212,13 +210,7 @@ func (sk8s *shortURLK8sHandler) createKubernetesShortURLsHandler(c *contextmodel
|
||||
|
||||
c.Logger.Debug("Creating short URL", "path", cmd.Path)
|
||||
obj := shorturl.LegacyCreateCommandToUnstructured(cmd)
|
||||
|
||||
uid, err := shortid.Generate()
|
||||
if err != nil {
|
||||
c.JsonApiErr(http.StatusInternalServerError, "failed to generate uid", err)
|
||||
return
|
||||
}
|
||||
obj.SetGenerateName(uid)
|
||||
obj.SetGenerateName("u") // becomes a prefix
|
||||
|
||||
out, err := client.Create(c.Req.Context(), &obj, v1.CreateOptions{})
|
||||
if err != nil {
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
|
||||
shorturl "github.com/grafana/grafana/apps/shorturl/pkg/apis/shorturl/v1alpha1"
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
@@ -25,7 +24,6 @@ func convertToK8sResource(v *shorturls.ShortUrl, namespacer request.NamespaceMap
|
||||
p := &shorturl.ShortURL{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: v.Uid,
|
||||
UID: types.UID(v.Uid),
|
||||
ResourceVersion: fmt.Sprintf("%d", v.LastSeenAt),
|
||||
CreationTimestamp: metav1.NewTime(time.UnixMilli(v.CreatedAt)),
|
||||
Namespace: namespacer(v.OrgId),
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"github.com/grafana/grafana-app-sdk/app"
|
||||
appsdkapiserver "github.com/grafana/grafana-app-sdk/k8s/apiserver"
|
||||
"github.com/grafana/grafana-app-sdk/simple"
|
||||
|
||||
"github.com/grafana/grafana/apps/shorturl/pkg/apis"
|
||||
shorturl "github.com/grafana/grafana/apps/shorturl/pkg/apis/shorturl/v1alpha1"
|
||||
shorturlapp "github.com/grafana/grafana/apps/shorturl/pkg/app"
|
||||
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/shorturls"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
"github.com/teris-io/shortid"
|
||||
)
|
||||
|
||||
var getTime = time.Now
|
||||
@@ -53,11 +52,7 @@ func (s ShortURLService) CreateShortURL(ctx context.Context, user *user.SignedIn
|
||||
|
||||
uid := cmd.UID
|
||||
if uid == "" {
|
||||
var err error
|
||||
uid, err = shortid.Generate()
|
||||
if err != nil {
|
||||
return nil, shorturls.ErrShortURLInternal.Errorf("failed to generate uid: %w", err)
|
||||
}
|
||||
uid = util.GenerateShortUID()
|
||||
} else {
|
||||
// Ensure the UID is valid
|
||||
if !util.IsValidShortUID(uid) {
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/teris-io/shortid"
|
||||
"k8s.io/apimachinery/pkg/util/validation"
|
||||
)
|
||||
|
||||
@@ -33,11 +32,8 @@ func TestRandomUIDs(t *testing.T) {
|
||||
|
||||
func TestCaseInsensitiveCollisionsUIDs(t *testing.T) {
|
||||
history := make(map[string]bool, 0)
|
||||
for i := 0; i < 100000; i++ {
|
||||
for i := range 100000 {
|
||||
v := GenerateShortUID()
|
||||
if false {
|
||||
v, _ = shortid.Generate() // collides in less then 500 iterations
|
||||
}
|
||||
|
||||
lower := strings.ToLower(v)
|
||||
_, exists := history[lower]
|
||||
|
||||
Reference in New Issue
Block a user