From fd072b4bc7a5da0475b5f1cfe37004f30d5e32ea Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Tue, 19 Nov 2024 11:49:19 -0700 Subject: [PATCH] chore: allow aliases that aren't simple alpha-numeric --- pkg/alias/get.go | 5 +++-- pkg/controller/handlers/alias/alias.go | 6 ++++++ pkg/system/ids.go | 1 + 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/pkg/alias/get.go b/pkg/alias/get.go index 0b39419d..a9404e1f 100644 --- a/pkg/alias/get.go +++ b/pkg/alias/get.go @@ -4,11 +4,12 @@ import ( "context" "errors" "fmt" - "strings" "github.com/otto8-ai/nah/pkg/name" "github.com/otto8-ai/nah/pkg/router" + "github.com/otto8-ai/otto8/pkg/hash" v1 "github.com/otto8-ai/otto8/pkg/storage/apis/otto.otto8.ai/v1" + "github.com/otto8-ai/otto8/pkg/system" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" @@ -53,7 +54,7 @@ func Get(ctx context.Context, c kclient.Client, obj v1.Aliasable, namespace stri } func KeyFromScopeID(scope, id string) string { - return strings.ToLower(name.SafeHashConcatName(id, scope)) + return system.AliasPrefix + hash.String(name.SafeHashConcatName(id, scope))[:8] } func GetScope(gvk schema.GroupVersionKind, obj v1.Aliasable) string { diff --git a/pkg/controller/handlers/alias/alias.go b/pkg/controller/handlers/alias/alias.go index 79946ba8..66687f31 100644 --- a/pkg/controller/handlers/alias/alias.go +++ b/pkg/controller/handlers/alias/alias.go @@ -108,6 +108,12 @@ func UnassignAlias(req router.Request, _ router.Response) error { return err } + // Check if alias name algorithm has changed + if src.Name != alias.KeyFromScopeID(alias.GetScope(gvk, aliasable), src.Spec.Name) { + log.Infof("Alias name algorithm has changed, deleting alias %s", src.Name) + return req.Delete(src) + } + aliasName, err := alias.Name(req.Client, aliasable) if err != nil { return err diff --git a/pkg/system/ids.go b/pkg/system/ids.go index bc533446..0ce338b4 100644 --- a/pkg/system/ids.go +++ b/pkg/system/ids.go @@ -18,6 +18,7 @@ const ( OAuthAppLoginPrefix = "oal1" EmailReceiverPrefix = "er1" ModelPrefix = "m1" + AliasPrefix = "al1" ) func IsThreadID(id string) bool {