@@ -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"
@@ -54,15 +57,16 @@ func handleLegacyAnnotations(ctx context.Context, cl client.Client, namespace *c
5457 return nil
5558}
5659
57- func CreateNamespace (ctx context.Context , client client.Client , generateName , id , author string ) (* corev1.Namespace , error ) {
58- if ! strings .HasSuffix (generateName , "-" ) {
59- generateName += "-"
60+ func CreateNamespace (ctx context.Context , client client.Client , generateNamePrefix , identity , author string ) (* corev1.Namespace , error ) {
61+ if ! strings .HasSuffix (generateNamePrefix , "-" ) {
62+ generateNamePrefix += "-"
6063 }
64+ name := identityHash (identity )
6165 namespace := & corev1.Namespace {
6266 ObjectMeta : metav1.ObjectMeta {
63- GenerateName : generateName ,
67+ Name : fmt . Sprintf ( "%s-%s" , generateNamePrefix , name ) ,
6468 Annotations : map [string ]string {
65- IdentityAnnotationKey : id ,
69+ IdentityAnnotationKey : identity ,
6670 AuthorAnnotationKey : author ,
6771 },
6872 },
@@ -77,14 +81,19 @@ func CreateNamespace(ctx context.Context, client client.Client, generateName, id
7781 return nil , err
7882 }
7983
80- if namespace .Annotations [IdentityAnnotationKey ] != id && namespace .Annotations [legacyIdentityAnnotationKey ] != id {
84+ if namespace .Annotations [IdentityAnnotationKey ] != identity && namespace .Annotations [legacyIdentityAnnotationKey ] != identity {
8185 return nil , errors .NewAlreadyExists (corev1 .Resource ("namespace" ), namespace .Name )
8286 }
8387
84- if err := handleLegacyAnnotations (ctx , client , namespace , id ); err != nil {
88+ if err := handleLegacyAnnotations (ctx , client , namespace , identity ); err != nil {
8589 return nil , err
8690 }
8791 }
8892
8993 return namespace , nil
9094}
95+
96+ func identityHash (userName string ) string {
97+ hash := sha256 .Sum224 ([]byte (userName ))
98+ return strings .ToLower (base36 .EncodeBytes (hash [:8 ]))
99+ }
0 commit comments