From bbb85eb2e7e3c23c162a47aab4f2d915c324b66e Mon Sep 17 00:00:00 2001 From: Takamichi Omori Date: Tue, 15 Feb 2022 17:43:59 +0900 Subject: [PATCH 1/4] go get "github.com/wantedly/resource-name-sanitizer" --- go.mod | 1 + go.sum | 2 ++ 2 files changed, 3 insertions(+) diff --git a/go.mod b/go.mod index a134d42..8a8a2e5 100644 --- a/go.mod +++ b/go.mod @@ -27,6 +27,7 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/wantedly/resource-name-sanitizer v0.0.0-20211210090254-2558977bd7ef // indirect golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 // indirect diff --git a/go.sum b/go.sum index a7a1e47..413cb6f 100644 --- a/go.sum +++ b/go.sum @@ -239,6 +239,8 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stuart-warren/yamlfmt v0.1.2 h1:ojguhYdHpNWy62fLkrQtLFGAzrqFxVaU8f5Z0U8mkMI= github.com/stuart-warren/yamlfmt v0.1.2/go.mod h1:X5TuPH+hf4O0U1KBvNqygvHbvAnoi9Wyl9BbtPv8SZk= +github.com/wantedly/resource-name-sanitizer v0.0.0-20211210090254-2558977bd7ef h1:CY3oLZo+4TqA06Z5V1ORJS9pWG+tUGKomwwjVQnO9b4= +github.com/wantedly/resource-name-sanitizer v0.0.0-20211210090254-2558977bd7ef/go.mod h1:489R51g2vdMpWugfLm6ncruK4AL71J87anxjH7AimOs= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= From 97eea676e46f47f493ff1e4ed659b63516f9ebe7 Mon Sep 17 00:00:00 2001 From: Takamichi Omori Date: Tue, 15 Feb 2022 17:44:11 +0900 Subject: [PATCH 2/4] make testcase lower --- refresh_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/refresh_test.go b/refresh_test.go index 88dd6eb..ed4c91d 100644 --- a/refresh_test.go +++ b/refresh_test.go @@ -119,8 +119,8 @@ func TestRefresh(t *testing.T) { initialState: nil, objectList: refresh.ObjectList{ Items: []client.Object{ - ut.GenDeployment("random-70-character-name-cmFuZG9tLTcwLWNoYXJhY3Rlci1uYW1lCci1uYW1lC==", map[string]string{labelKey: "1"}), - ut.GenDeployment("", map[string]string{labelKey: "random-70-character-objKey-cmFuZG9tLTcwLWNoYXJhY3Rlci1uYW1lCci1uYW1lC"}), + ut.GenDeployment("random-70-character-name-cmfuzg9tltcwlwnoyxjhy3rlci1uyw1lcci1uyw1lc==", map[string]string{labelKey: "1"}), + ut.GenDeployment("", map[string]string{labelKey: "random-70-character-objkey-cmfuzg9tltcwlwnoyxjhy3rlci1uyw1lcci1uyw1lc"}), }, GroupVersionKind: gvk, Identity: labelGetter, From 1b04642bcbc219b25dcb11df642f4cc7b643c6a5 Mon Sep 17 00:00:00 2001 From: Takamichi Omori Date: Tue, 15 Feb 2022 17:44:19 +0900 Subject: [PATCH 3/4] use resource-name-sanitizer --- refresh.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/refresh.go b/refresh.go index cb8a463..a90eb2c 100644 --- a/refresh.go +++ b/refresh.go @@ -12,6 +12,8 @@ import ( "k8s.io/apimachinery/pkg/util/sets" "sigs.k8s.io/controller-runtime/pkg/client" util "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + + "github.com/wantedly/resource-name-sanitizer" ) // Builder is responsible for creating on Application @@ -79,14 +81,12 @@ func (r refresher) Refresh(ctx context.Context, parent client.Object, list Objec if existing, ok := existingObjs[objKey]; ok { emptyObj.SetName(existing.GetName()) } else { + s := sanitizer.NewSubdomainLabelSafe() if name := obj.GetName(); name != "" { - if 63 < len(name) { - name = name[:63] - } - emptyObj.SetName(name) + emptyObj.SetName(s.Sanitize(name)) } else { name := createResourceName("%s-%s", objKey, parent.GetName()) - emptyObj.SetName(name) + emptyObj.SetName(s.Sanitize(name)) } } if _, err := util.CreateOrUpdate(ctx, r.client, emptyObj, func() error { From 0704744df568cb126b8dfcf5b9ad2f29504a0b6c Mon Sep 17 00:00:00 2001 From: Takamichi Omori Date: Tue, 15 Feb 2022 17:44:26 +0900 Subject: [PATCH 4/4] update snapshots --- .snapshots/TestRefresh-if_object_has_long_name.yaml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.snapshots/TestRefresh-if_object_has_long_name.yaml b/.snapshots/TestRefresh-if_object_has_long_name.yaml index c86e2bb..725fff2 100644 --- a/.snapshots/TestRefresh-if_object_has_long_name.yaml +++ b/.snapshots/TestRefresh-if_object_has_long_name.yaml @@ -4,8 +4,8 @@ items: - metadata: creationTimestamp: null labels: - some-label-key: random-70-character-objKey-cmFuZG9tLTcwLWNoYXJhY3Rlci1uYW1lCci1uYW1lC - name: random-70-character-objKey-cmFuZG9tLTcwLWNoYXJhY3Rlci-some-s + some-label-key: random-70-character-objkey-cmfuzg9tltcwlwnoyxjhy3rlci1uyw1lcci1uyw1lc + name: random-70-character-objkey-cmfuzg9tltcwlwnoyxjhy3rlci-some-s namespace: some-namespace ownerReferences: - apiVersion: v1 @@ -17,13 +17,13 @@ items: spec: selector: matchLabels: - some-label-key: random-70-character-objKey-cmFuZG9tLTcwLWNoYXJhY3Rlci1uYW1lCci1uYW1lC + some-label-key: random-70-character-objkey-cmfuzg9tltcwlwnoyxjhy3rlci1uyw1lcci1uyw1lc strategy: {} template: metadata: creationTimestamp: null labels: - some-label-key: random-70-character-objKey-cmFuZG9tLTcwLWNoYXJhY3Rlci1uYW1lCci1uYW1lC + some-label-key: random-70-character-objkey-cmfuzg9tltcwlwnoyxjhy3rlci1uyw1lcci1uyw1lc spec: containers: - image: some-deployment:some-commit-sha @@ -34,7 +34,7 @@ items: creationTimestamp: null labels: some-label-key: "1" - name: random-70-character-name-cmFuZG9tLTcwLWNoYXJhY3Rlci1uYW1lCci1uY + name: random-70-character-name-cmfuzg9tltcwlwnoyxjhy3rlci1uy-f8d9db6b namespace: some-namespace ownerReferences: - apiVersion: v1