@@ -18,8 +18,11 @@ package resources
1818
1919import (
2020 "context"
21+ "crypto/sha256"
22+ "fmt"
2123 "strings"
2224
25+ "github.com/martinlindhe/base36"
2326 corev1 "k8s.io/api/core/v1"
2427 "k8s.io/apimachinery/pkg/api/errors"
2528 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -29,6 +32,7 @@ import (
2932
3033const (
3134 IdentityAnnotationKey = "backend.kube-bind.io/identity"
35+ AuthorAnnotationKey = "backend.kube-bind.io/author"
3236 legacyIdentityAnnotationKey = "example-backend.kube-bind.io/identity"
3337)
3438
@@ -53,15 +57,14 @@ func handleLegacyAnnotations(ctx context.Context, cl client.Client, namespace *c
5357 return nil
5458}
5559
56- func CreateNamespace (ctx context.Context , client client.Client , generateName , id string ) (* corev1.Namespace , error ) {
57- if ! strings .HasSuffix (generateName , "-" ) {
58- generateName += "-"
59- }
60+ func CreateNamespace (ctx context.Context , client client.Client , generateNamePrefix , identity , author string ) (* corev1.Namespace , error ) {
61+ name := identityHash (identity )
6062 namespace := & corev1.Namespace {
6163 ObjectMeta : metav1.ObjectMeta {
62- GenerateName : generateName ,
64+ Name : fmt . Sprintf ( "%s-%s" , generateNamePrefix , name ) ,
6365 Annotations : map [string ]string {
64- IdentityAnnotationKey : id ,
66+ IdentityAnnotationKey : identity ,
67+ AuthorAnnotationKey : author ,
6568 },
6669 },
6770 }
@@ -75,14 +78,19 @@ func CreateNamespace(ctx context.Context, client client.Client, generateName, id
7578 return nil , err
7679 }
7780
78- if namespace .Annotations [IdentityAnnotationKey ] != id && namespace .Annotations [legacyIdentityAnnotationKey ] != id {
81+ if namespace .Annotations [IdentityAnnotationKey ] != identity && namespace .Annotations [legacyIdentityAnnotationKey ] != identity {
7982 return nil , errors .NewAlreadyExists (corev1 .Resource ("namespace" ), namespace .Name )
8083 }
8184
82- if err := handleLegacyAnnotations (ctx , client , namespace , id ); err != nil {
85+ if err := handleLegacyAnnotations (ctx , client , namespace , identity ); err != nil {
8386 return nil , err
8487 }
8588 }
8689
8790 return namespace , nil
8891}
92+
93+ func identityHash (userName string ) string {
94+ hash := sha256 .Sum224 ([]byte (userName ))
95+ return strings .ToLower (base36 .EncodeBytes (hash [:8 ]))
96+ }
0 commit comments