@@ -18,6 +18,7 @@ import (
1818 "fmt"
1919 "math"
2020 "strconv"
21+ "strings"
2122
2223 apivalidation "k8s.io/apimachinery/pkg/api/validation"
2324 "k8s.io/apimachinery/pkg/runtime"
@@ -36,6 +37,24 @@ import (
3637 corev1 "k8s.io/api/core/v1"
3738)
3839
40+ const (
41+ // This is the error message returned by IsDNS1035Label when the given input
42+ // is longer than 63 characters.
43+ dns1035MaxLengthExceededErrorMsg = "must be no more than 63 characters"
44+
45+ // Error message returned by JobSet validation if the generated child jobs
46+ // will be longer than 63 characters.
47+ jobNameTooLongErrorMsg = "JobSet name is too long, job names generated for this JobSet will exceed 63 characters"
48+
49+ // Error message returned by JobSet validation if the generated pod names
50+ // will be longer than 63 characters.
51+ podNameTooLongErrorMsg = "JobSet name is too long, pod names generated for this JobSet will exceed 63 characters"
52+
53+ // Error message returned by JobSet validation if the network subdomain
54+ // will be longer than 63 characters.
55+ subdomainTooLongErrMsg = ".spec.network.subdomain is too long, must be less than 63 characters"
56+ )
57+
3958func (js * JobSet ) SetupWebhookWithManager (mgr ctrl.Manager ) error {
4059 return ctrl .NewWebhookManagedBy (mgr ).
4160 For (js ).
@@ -92,6 +111,9 @@ func (js *JobSet) ValidateCreate() (admission.Warnings, error) {
92111
93112 // Since subdomain name is also used as service name, it must adhere to RFC 1035 as well.
94113 for _ , errMessage := range validation .IsDNS1035Label (js .Spec .Network .Subdomain ) {
114+ if strings .Contains (errMessage , dns1035MaxLengthExceededErrorMsg ) {
115+ errMessage = subdomainTooLongErrMsg
116+ }
95117 allErrs = append (allErrs , fmt .Errorf (errMessage ))
96118 }
97119 }
@@ -108,6 +130,9 @@ func (js *JobSet) ValidateCreate() (admission.Warnings, error) {
108130 // Use the largest job index as it will have the longest name.
109131 longestJobName := placement .GenJobName (js .Name , rjob .Name , int (rjob .Replicas - 1 ))
110132 for _ , errMessage := range validation .IsDNS1035Label (longestJobName ) {
133+ if strings .Contains (errMessage , dns1035MaxLengthExceededErrorMsg ) {
134+ errMessage = jobNameTooLongErrorMsg
135+ }
111136 allErrs = append (allErrs , fmt .Errorf (errMessage ))
112137 }
113138 // Check that the generated pod names for the replicated job is DNS 1035 compliant.
@@ -118,6 +143,9 @@ func (js *JobSet) ValidateCreate() (admission.Warnings, error) {
118143 // Add 5 char suffix to the deterministic part of the pod name to validate the full pod name is compliant.
119144 longestPodName := placement .GenPodName (js .Name , rjob .Name , maxJobIndex , maxPodIndex ) + "-abcde"
120145 for _ , errMessage := range validation .IsDNS1035Label (longestPodName ) {
146+ if strings .Contains (errMessage , dns1035MaxLengthExceededErrorMsg ) {
147+ errMessage = podNameTooLongErrorMsg
148+ }
121149 allErrs = append (allErrs , fmt .Errorf (errMessage ))
122150 }
123151 }
0 commit comments