kastenhq / kubestr

Apache License 2.0
349 stars 47 forks source link

GenerateName like helper function for naming ephemeral resources #295

Open shlokc9 opened 1 month ago

shlokc9 commented 1 month ago

Description

In kubestr we create a number of ephemeral resources like Pods, PVCs, etc. that get cleaned up after the command execution. However, while naming these resources in code, developers generally append names of an existing resource to a hardcoded string. To associate the ephemeral resource with the existing resource.

Example

While writing a new command one might have to define a temporary PVC using an existing PVC named application-persistent-volume-claim. And if the hardcoded string defined here is ephemeral-persistent-volume-claim the resultant temporary PVC could be named ephemeral-persistent-volume-claim-for-application-persistent-volume-claim.

Problem

This temporary PVC name is 73 characters long and it is not considered a good practice to have string names with more than 64 characters.

Proposed Changes

This could be resolved with a helper function that hashes names longer than 64 characters and then replaces the last 5 characters of the 64 character name with the first 5 characters (base 64) of the hash. Developers can then call this helper function to shorten the names of the ephemeral resources they create.

As part of this issue, please also make sure to update the names of ephemeral resources for all existing commands. By calling the new helper function instead of passing a string name.