Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource name shortner fixed #295 #296

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/block/block_mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ func NewBlockMountChecker(args BlockMountCheckerArgs) (BlockMountChecker, error)

b := &blockMountChecker{}
b.args = args
b.podName = fmt.Sprintf(blockMountCheckerPodNameFmt, b.args.StorageClass)
b.pvcName = fmt.Sprintf(blockMountCheckerPVCNameFmt, b.args.StorageClass)
b.podName = ResourceNameShortener(fmt.Sprintf(blockMountCheckerPodNameFmt, b.args.StorageClass))
b.pvcName = ResourceNameShortener(fmt.Sprintf(blockMountCheckerPVCNameFmt, b.args.StorageClass))
b.validator = csi.NewArgumentValidator(b.args.KubeCli, b.args.DynCli)
b.appCreator = csi.NewApplicationCreator(b.args.KubeCli, args.K8sObjectReadyTimeout)
b.cleaner = csi.NewCleaner(b.args.KubeCli, b.args.DynCli)
Expand Down
28 changes: 28 additions & 0 deletions pkg/block/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package block

import (
"crypto/sha256"
"encoding/base64"
)

// resource name maxs lenght
const maxResourceNameLength = 64

// ResourceNameShortener shortens the resource name if it exceeds maxResourceNameLength of 64.
// It replaces the last 5 characters with the first 5 characters of a hash (base64 encoded).
func ResourceNameShortener(name string) string {
if len(name) <= maxResourceNameLength {
return name
}

// Create a SHA-256 hash of the name
hash := sha256.Sum256([]byte(name))

// Convert the hash to a base64 string
hashBase64 := base64.RawURLEncoding.EncodeToString(hash[:])

// Truncate the name and replace the last 5 characters with the first 5 characters of the hash
truncatedName := name[:maxResourceNameLength-6] + "-" + hashBase64[:5]

return truncatedName
}
39 changes: 39 additions & 0 deletions pkg/block/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package block

import (
"testing"
)

// TestShortenResourceName tests different variations of the ephemeral resource name.
func TestResourceNameShortener(t *testing.T) {
tests := []struct {
name string
input string
expected int // We will check the length of the output
}{
{
name: "Short name (less than 64 characters)",
input: "ephemeral-persistent-volume-claim",
expected: len("ephemeral-persistent-volume-claim"),
},
{
name: "Exact 64 characters",
input: "ephemeral-persistent-volume-claim-for-application-persistent-volume-clai",
expected: 64,
},
{
name: "Name longer than 64 characters",
input: "ephemeral-persistent-volume-claim-for-application-persistent-volume-claim",
expected: 64,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ResourceNameShortener(tt.input)
if len(got) != tt.expected {
t.Errorf("ShortenResourceName() = %v (length %d), expected length %d", got, len(got), tt.expected)
}
})
}
}