Skip to content

Commit 85efeac

Browse files
authored
Use deterministic hashed names (#2773)
Signed-off-by: jose.vazquez <[email protected]>
1 parent 1093618 commit 85efeac

File tree

4 files changed

+100
-5
lines changed

4 files changed

+100
-5
lines changed

internal/v3/translate/names.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package translate
16+
17+
import (
18+
"fmt"
19+
"hash/fnv"
20+
21+
"k8s.io/apimachinery/pkg/util/rand"
22+
)
23+
24+
func HashNames(name string, args ...string) string {
25+
hasher := fnv.New64a()
26+
hasher.Write([]byte(name))
27+
for _, arg := range args {
28+
hasher.Write([]byte(arg))
29+
}
30+
rawHash := hasher.Sum64()
31+
32+
return rand.SafeEncodeString(fmt.Sprint(rawHash))
33+
}
34+
35+
func PrefixedName(prefix string, name string, args ...string) string {
36+
return fmt.Sprintf("%s-%s", prefix, HashNames(name, args...))
37+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package translate_test
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/assert"
21+
22+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/v3/translate"
23+
)
24+
25+
func TestPrefixedName(t *testing.T) {
26+
for _, tc := range []struct {
27+
title string
28+
prefix string
29+
name string
30+
args []string
31+
want string
32+
}{
33+
{
34+
title: "just A",
35+
prefix: "just",
36+
name: "A",
37+
want: "just-56b7d6667d8f6cc88c8d",
38+
},
39+
{
40+
title: "a very long name",
41+
prefix: "a",
42+
name: "very logn name with several parts",
43+
want: "a-54fd46599bbf74b8db44",
44+
},
45+
{
46+
title: "names",
47+
prefix: "several",
48+
name: "names",
49+
args: []string{"name0", "name1"},
50+
want: "several-75db99b58df57d54bd6",
51+
},
52+
} {
53+
t.Run(tc.title, func(t *testing.T) {
54+
got := translate.PrefixedName(tc.prefix, tc.name, tc.args...)
55+
assert.Equal(t, tc.want, got)
56+
})
57+
}
58+
}

internal/v3/translate/ref.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func (ref *namedRef) Name(prefix string, path []string) string {
145145
if path[0] == "entry" {
146146
path = path[1:]
147147
}
148-
return strings.ToLower(strings.Join(append([]string{prefix}, path...), "-"))
148+
return PrefixedName(prefix, path[0], path[1:]...)
149149
}
150150

151151
func (ref *namedRef) Collapse(deps DependencyRepo, path []string, obj map[string]any) error {

internal/v3/translate/translator_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func TestFromAPI(t *testing.T) {
284284
{
285285
DatadogApiKeySecretRef: &v1.ApiTokenSecretRef{
286286
Key: pointer.MakePtr("datadogApiKey"),
287-
Name: pointer.MakePtr("groupalertscfg-notifications-datadogapikey"),
287+
Name: pointer.MakePtr("groupalertscfg-5b8c46f7fd54484b88b4"),
288288
},
289289
DatadogRegion: pointer.MakePtr("US"),
290290
DelayMin: pointer.MakePtr(42),
@@ -315,7 +315,7 @@ func TestFromAPI(t *testing.T) {
315315
},
316316
&corev1.Secret{
317317
ObjectMeta: metav1.ObjectMeta{
318-
Name: "groupalertscfg-notifications-datadogapikey",
318+
Name: "groupalertscfg-5b8c46f7fd54484b88b4",
319319
Namespace: "ns",
320320
},
321321
Data: map[string][]byte{
@@ -366,7 +366,7 @@ func TestFromAPI(t *testing.T) {
366366
Entry: &v1.ThirdPartyIntegrationSpecV20250312Entry{
367367
Type: pointer.MakePtr("SLACK"),
368368
ApiTokenSecretRef: &v1.ApiTokenSecretRef{
369-
Name: pointer.MakePtr("3rdparty-slack-apitoken"),
369+
Name: pointer.MakePtr("3rdparty-slack-5f444b9b4cc55b5b8cb"),
370370
Key: pointer.MakePtr("apiToken"),
371371
},
372372
ChannelName: pointer.MakePtr("alert-channel"),
@@ -387,7 +387,7 @@ func TestFromAPI(t *testing.T) {
387387
},
388388
&corev1.Secret{
389389
ObjectMeta: metav1.ObjectMeta{
390-
Name: "3rdparty-slack-apitoken",
390+
Name: "3rdparty-slack-5f444b9b4cc55b5b8cb",
391391
Namespace: "ns",
392392
},
393393
Data: map[string][]byte{

0 commit comments

Comments
 (0)