Skip to content

Commit 689f455

Browse files
committed
fix(shim_controller): ensure trimmed K8s resource name ends in an alphanumeric char
Signed-off-by: Vaughn Dice <vdice@akamai.com>
1 parent 3205ada commit 689f455

2 files changed

Lines changed: 49 additions & 8 deletions

File tree

internal/controller/shim_controller.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"fmt"
2424
"math"
2525
"os"
26+
"regexp"
2627
"strconv"
2728
"strings"
2829

@@ -481,24 +482,23 @@ func (sr *ShimReconciler) createJobManifest(shim *rcmv1.Shim, node *corev1.Node,
481482
}
482483
sr.setOperationConfiguration(shim, &opConfig, artifact)
483484

484-
name := node.Name + "-" + shim.Name + "-" + operation
485-
nameMax := int(math.Min(float64(len(name)), K8sNameMaxLength))
485+
name := trimK8sName(node.Name + "-" + shim.Name + "-" + operation)
486486

487487
job := &batchv1.Job{
488488
TypeMeta: metav1.TypeMeta{
489489
APIVersion: "batch/v1",
490490
Kind: "Job",
491491
},
492492
ObjectMeta: metav1.ObjectMeta{
493-
Name: name[:nameMax],
493+
Name: name,
494494
Namespace: os.Getenv("CONTROLLER_NAMESPACE"),
495495
Annotations: map[string]string{
496496
"spinkube.dev/nodeName": node.Name,
497497
"spinkube.dev/shimName": shim.Name,
498498
"spinkube.dev/operation": operation,
499499
},
500500
Labels: map[string]string{
501-
name[:nameMax]: "true",
501+
name: "true",
502502
"spinkube.dev/shimName": shim.Name,
503503
"spinkube.dev/operation": operation,
504504
"spinkube.dev/job": "true",
@@ -612,8 +612,7 @@ func (sr *ShimReconciler) handleDeployRuntimeClass(ctx context.Context, shim *rc
612612

613613
// createRuntimeClassManifest creates a RuntimeClass manifest for a Shim.
614614
func (sr *ShimReconciler) createRuntimeClassManifest(shim *rcmv1.Shim) (*nodev1.RuntimeClass, error) {
615-
name := shim.Spec.RuntimeClass.Name
616-
nameMax := int(math.Min(float64(len(name)), K8sNameMaxLength))
615+
name := trimK8sName(shim.Spec.RuntimeClass.Name)
617616

618617
nodeSelector := shim.Spec.NodeSelector
619618
if nodeSelector == nil {
@@ -626,8 +625,8 @@ func (sr *ShimReconciler) createRuntimeClassManifest(shim *rcmv1.Shim) (*nodev1.
626625
Kind: "RuntimeClass",
627626
},
628627
ObjectMeta: metav1.ObjectMeta{
629-
Name: name[:nameMax],
630-
Labels: map[string]string{name[:nameMax]: "true"},
628+
Name: name,
629+
Labels: map[string]string{name: "true"},
631630
},
632631
Handler: shim.Spec.RuntimeClass.Handler,
633632
Scheduling: &nodev1.Scheduling{
@@ -730,3 +729,12 @@ func (sr *ShimReconciler) ensureFinalizerForShim(ctx context.Context, shim *rcmv
730729
func ptr[T any](v T) *T {
731730
return &v
732731
}
732+
733+
// trims the K8s name per the K8sNameMaxLength default and ensures it ends with an alphanumeric character
734+
func trimK8sName(name string) string {
735+
var trailingNonAlphaNumRegex = regexp.MustCompile(`[^a-zA-Z0-9]+$`)
736+
737+
nameMax := int(math.Min(float64(len(name)), K8sNameMaxLength))
738+
739+
return trailingNonAlphaNumRegex.ReplaceAllString(name[:nameMax], "")
740+
}

internal/controller/shim_controller_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,36 @@ func TestNormalizeArch(t *testing.T) {
261261
})
262262
}
263263
}
264+
265+
func TestTrimK8sName(t *testing.T) {
266+
tests := []struct {
267+
input string
268+
want string
269+
}{
270+
{
271+
"k8s-name",
272+
"k8s-name",
273+
},
274+
{
275+
"k8s-name!",
276+
"k8s-name",
277+
},
278+
{
279+
"verrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrylong-k8s-nameeeeeeeeeeeeeeeee",
280+
"verrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrylong-k8s-nameeeeeeeeeeee",
281+
},
282+
{
283+
"verrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrylong-k8s-name----------------",
284+
"verrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrylong-k8s-name",
285+
},
286+
}
287+
288+
for _, tt := range tests {
289+
t.Run(tt.input, func(t *testing.T) {
290+
got := trimK8sName(tt.input)
291+
if got != tt.want {
292+
t.Errorf("trimK8sName(%q) = %q, want %q", tt.input, got, tt.want)
293+
}
294+
})
295+
}
296+
}

0 commit comments

Comments
 (0)