Skip to content

Commit f2874d4

Browse files
committed
mane namespaces names hash based
1 parent 204e08a commit f2874d4

2 files changed

Lines changed: 46 additions & 37 deletions

File tree

backend/kubernetes/resources/namespace.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ package resources
1818

1919
import (
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,13 @@ 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-
}
60+
func CreateNamespace(ctx context.Context, client client.Client, generateNamePrefix, identity, author string) (*corev1.Namespace, error) {
61+
name := identityHash(identity)
6162
namespace := &corev1.Namespace{
6263
ObjectMeta: metav1.ObjectMeta{
63-
GenerateName: generateName,
64+
Name: fmt.Sprintf("%s-%s", generateNamePrefix, name),
6465
Annotations: map[string]string{
65-
IdentityAnnotationKey: id,
66+
IdentityAnnotationKey: identity,
6667
AuthorAnnotationKey: author,
6768
},
6869
},
@@ -77,14 +78,19 @@ func CreateNamespace(ctx context.Context, client client.Client, generateName, id
7778
return nil, err
7879
}
7980

80-
if namespace.Annotations[IdentityAnnotationKey] != id && namespace.Annotations[legacyIdentityAnnotationKey] != id {
81+
if namespace.Annotations[IdentityAnnotationKey] != identity && namespace.Annotations[legacyIdentityAnnotationKey] != identity {
8182
return nil, errors.NewAlreadyExists(corev1.Resource("namespace"), namespace.Name)
8283
}
8384

84-
if err := handleLegacyAnnotations(ctx, client, namespace, id); err != nil {
85+
if err := handleLegacyAnnotations(ctx, client, namespace, identity); err != nil {
8586
return nil, err
8687
}
8788
}
8889

8990
return namespace, nil
9091
}
92+
93+
func identityHash(userName string) string {
94+
hash := sha256.Sum224([]byte(userName))
95+
return strings.ToLower(base36.EncodeBytes(hash[:8]))
96+
}

backend/kubernetes/resources/namespace_test.go

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,47 +18,52 @@ package resources
1818

1919
import (
2020
"context"
21+
"crypto/sha256"
22+
"fmt"
2123
"strings"
2224
"testing"
2325

26+
"github.com/martinlindhe/base36"
2427
corev1 "k8s.io/api/core/v1"
2528
"k8s.io/apimachinery/pkg/runtime"
2629
"sigs.k8s.io/controller-runtime/pkg/client"
2730
"sigs.k8s.io/controller-runtime/pkg/client/fake"
2831
)
2932

33+
func expectedIdentityHash(identity string) string {
34+
hash := sha256.Sum224([]byte(identity))
35+
return strings.ToLower(base36.EncodeBytes(hash[:8]))
36+
}
37+
3038
func TestCreateNamespace(t *testing.T) {
3139
scheme := runtime.NewScheme()
3240
_ = corev1.AddToScheme(scheme)
3341

3442
tests := []struct {
35-
name string
36-
generateName string
37-
identity string
38-
author string
39-
wantErr bool
40-
wantNameGenerated bool
41-
wantAnnotations map[string]string
43+
name string
44+
generateName string
45+
identity string
46+
author string
47+
wantErr bool
48+
wantAnnotations map[string]string
4249
}{
4350
{
44-
name: "create new namespace with generateName",
45-
generateName: "test-ns",
46-
identity: "test-id-123",
47-
author: "bob",
48-
wantErr: false,
49-
wantNameGenerated: true,
51+
name: "create new namespace with generateName",
52+
generateName: "test-ns",
53+
identity: "test-id-123",
54+
author: "bob",
55+
wantErr: false,
5056
wantAnnotations: map[string]string{
5157
IdentityAnnotationKey: "test-id-123",
5258
AuthorAnnotationKey: "bob",
5359
},
5460
},
5561
{
56-
name: "create new namespace with generateName already ending with dash",
57-
generateName: "test-ns-",
58-
identity: "test-id-456",
59-
author: "alice",
60-
wantErr: false,
61-
wantNameGenerated: true,
62+
name: "create new namespace with generateName already ending with dash",
63+
generateName: "test-ns-",
64+
identity: "test-id-456",
65+
author: "alice",
66+
wantErr: false,
6267
wantAnnotations: map[string]string{
6368
IdentityAnnotationKey: "test-id-456",
6469
AuthorAnnotationKey: "alice",
@@ -91,18 +96,16 @@ func TestCreateNamespace(t *testing.T) {
9196
return
9297
}
9398

94-
expectedGenerateNamePrefix := tt.generateName
95-
if !strings.HasSuffix(expectedGenerateNamePrefix, "-") {
96-
expectedGenerateNamePrefix += "-"
99+
expectedPrefix := tt.generateName
100+
expectedHash := expectedIdentityHash(tt.identity)
101+
expectedName := fmt.Sprintf("%s-%s", expectedPrefix, expectedHash)
102+
103+
if result.Name != expectedName {
104+
t.Errorf("CreateNamespace() name = %v, expected %v", result.Name, expectedName)
97105
}
98106

99-
if tt.wantNameGenerated {
100-
if !strings.HasPrefix(result.Name, expectedGenerateNamePrefix) {
101-
t.Errorf("CreateNamespace() name = %v, expected to start with %v", result.Name, expectedGenerateNamePrefix)
102-
}
103-
if result.GenerateName != expectedGenerateNamePrefix {
104-
t.Errorf("CreateNamespace() generateName = %v, expected %v", result.GenerateName, expectedGenerateNamePrefix)
105-
}
107+
if result.GenerateName != "" {
108+
t.Errorf("CreateNamespace() generateName = %v, expected empty string", result.GenerateName)
106109
}
107110

108111
for key, expectedValue := range tt.wantAnnotations {

0 commit comments

Comments
 (0)