Skip to content

Commit c2cc929

Browse files
committed
Shorten test pod names
Openshift limits the length of a job name to 63 characters. To prevent the failure of jobs with long names, this patch introduces shortening of fixed infixes and suffixes. Additionally, users will get an error if the combined lenght of the pod name exceeds the limit.
1 parent 65c78a1 commit c2cc929

File tree

5 files changed

+93
-13
lines changed

5 files changed

+93
-13
lines changed

api/v1beta1/ansibletest_webhook.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ package v1beta1
2525
import (
2626
"fmt"
2727

28+
apierrors "k8s.io/apimachinery/pkg/api/errors"
2829
"k8s.io/apimachinery/pkg/runtime"
30+
"k8s.io/apimachinery/pkg/runtime/schema"
31+
"k8s.io/apimachinery/pkg/util/validation"
32+
"k8s.io/apimachinery/pkg/util/validation/field"
2933
ctrl "sigs.k8s.io/controller-runtime"
3034
logf "sigs.k8s.io/controller-runtime/pkg/log"
3135
"sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -61,8 +65,31 @@ var _ webhook.Validator = &AnsibleTest{}
6165
func (r *AnsibleTest) ValidateCreate() (admission.Warnings, error) {
6266
ansibletestlog.Info("validate create", "name", r.Name)
6367

68+
var allErrs field.ErrorList
6469
var allWarnings admission.Warnings
6570

71+
if len(r.Name) >= validation.DNS1123LabelMaxLength {
72+
allErrs = append(allErrs, &field.Error{
73+
Type: field.ErrorTypeInvalid,
74+
BadValue: len(r.Name),
75+
Detail: fmt.Sprintf(ErrNameTooLong, "AnsibleTest", validation.DNS1123LabelMaxLength),
76+
},
77+
)
78+
}
79+
80+
for _, workflowStep := range r.Spec.Workflow {
81+
podNameLength := len(r.Name) + len(workflowStep.StepName) + len("-sXX-")
82+
83+
if podNameLength >= validation.DNS1123LabelMaxLength {
84+
allErrs = append(allErrs, &field.Error{
85+
Type: field.ErrorTypeInvalid,
86+
BadValue: podNameLength,
87+
Detail: fmt.Sprintf(ErrNameTooLong, "AnsibleTest", validation.DNS1123LabelMaxLength),
88+
},
89+
)
90+
}
91+
}
92+
6693
if r.Spec.Privileged {
6794
allWarnings = append(allWarnings, fmt.Sprintf(WarnPrivilegedModeOn, "AnsibleTest"))
6895
}
@@ -71,6 +98,14 @@ func (r *AnsibleTest) ValidateCreate() (admission.Warnings, error) {
7198
allWarnings = append(allWarnings, fmt.Sprintf(WarnSELinuxLevel, r.Kind))
7299
}
73100

101+
if len(allErrs) > 0 {
102+
return allWarnings, apierrors.NewInvalid(
103+
schema.GroupKind{
104+
Group: GroupVersion.WithKind("AnsibleTest").Group,
105+
Kind: GroupVersion.WithKind("AnsibleTest").Kind,
106+
}, r.GetName(), allErrs)
107+
}
108+
74109
return allWarnings, nil
75110
}
76111

api/v1beta1/common_webhook.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ const (
77

88
// ErrDebug
99
ErrDebug = "%s.Spec.Workflow parameter must be empty to run debug mode"
10+
11+
// ErrNameTooLong
12+
ErrNameTooLong = "The combined length of %s pod name exceeds the maximum of %d " +
13+
"characters. Shorten the CR name or workflow step name to proceed."
1014
)
1115

1216
const (

api/v1beta1/tempest_webhook.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
apierrors "k8s.io/apimachinery/pkg/api/errors"
3131
"k8s.io/apimachinery/pkg/runtime"
3232
"k8s.io/apimachinery/pkg/runtime/schema"
33+
"k8s.io/apimachinery/pkg/util/validation"
3334
"k8s.io/apimachinery/pkg/util/validation/field"
3435
ctrl "sigs.k8s.io/controller-runtime"
3536
logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -108,6 +109,28 @@ func (r *Tempest) ValidateCreate() (admission.Warnings, error) {
108109
)
109110
}
110111

112+
if len(r.Name) >= validation.DNS1123LabelMaxLength {
113+
allErrs = append(allErrs, &field.Error{
114+
Type: field.ErrorTypeInvalid,
115+
BadValue: len(r.Name),
116+
Detail: fmt.Sprintf(ErrNameTooLong, "Tempest", validation.DNS1123LabelMaxLength),
117+
},
118+
)
119+
}
120+
121+
for _, workflowStep := range r.Spec.Workflow {
122+
podNameLength := len(r.Name) + len(workflowStep.StepName) + len("-sXX-")
123+
124+
if podNameLength >= validation.DNS1123LabelMaxLength {
125+
allErrs = append(allErrs, &field.Error{
126+
Type: field.ErrorTypeInvalid,
127+
BadValue: podNameLength,
128+
Detail: fmt.Sprintf(ErrNameTooLong, "Tempest", validation.DNS1123LabelMaxLength),
129+
},
130+
)
131+
}
132+
}
133+
111134
if r.Spec.Privileged {
112135
allWarnings = append(allWarnings, fmt.Sprintf(WarnPrivilegedModeOn, "Tempest"))
113136
}

api/v1beta1/tobiko_webhook.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
apierrors "k8s.io/apimachinery/pkg/api/errors"
2929
"k8s.io/apimachinery/pkg/runtime"
3030
"k8s.io/apimachinery/pkg/runtime/schema"
31+
"k8s.io/apimachinery/pkg/util/validation"
3132
"k8s.io/apimachinery/pkg/util/validation/field"
3233
ctrl "sigs.k8s.io/controller-runtime"
3334
logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -75,12 +76,38 @@ func (r *Tobiko) ValidateCreate() (admission.Warnings, error) {
7576
})
7677
}
7778

79+
if len(r.Name) >= validation.DNS1123LabelMaxLength {
80+
allErrs = append(allErrs, &field.Error{
81+
Type: field.ErrorTypeInvalid,
82+
BadValue: len(r.Name),
83+
Detail: fmt.Sprintf(ErrNameTooLong, "Tobiko", validation.DNS1123LabelMaxLength),
84+
},
85+
)
86+
}
87+
88+
for _, workflowStep := range r.Spec.Workflow {
89+
podNameLength := len(r.Name) + len(workflowStep.StepName) + len("-sXX-")
90+
91+
if podNameLength >= validation.DNS1123LabelMaxLength {
92+
allErrs = append(allErrs, &field.Error{
93+
Type: field.ErrorTypeInvalid,
94+
BadValue: podNameLength,
95+
Detail: fmt.Sprintf(ErrNameTooLong, "Tobiko", validation.DNS1123LabelMaxLength),
96+
},
97+
)
98+
}
99+
}
100+
78101
if r.Spec.Privileged {
79102
allWarnings = append(allWarnings, fmt.Sprintf(WarnPrivilegedModeOn, "Tobiko"))
80103
} else {
81104
allWarnings = append(allWarnings, fmt.Sprintf(WarnPrivilegedModeOff, "Tobiko"))
82105
}
83106

107+
if r.Spec.Privileged && len(r.Spec.Workflow) > 0 && len(r.Spec.SELinuxLevel) == 0 {
108+
allWarnings = append(allWarnings, fmt.Sprintf(WarnSELinuxLevel, r.Kind))
109+
}
110+
84111
if len(allErrs) > 0 {
85112
return allWarnings, apierrors.NewInvalid(
86113
schema.GroupKind{
@@ -89,10 +116,6 @@ func (r *Tobiko) ValidateCreate() (admission.Warnings, error) {
89116
}, r.GetName(), allErrs)
90117
}
91118

92-
if r.Spec.Privileged && len(r.Spec.Workflow) > 0 && len(r.Spec.SELinuxLevel) == 0 {
93-
allWarnings = append(allWarnings, fmt.Sprintf(WarnSELinuxLevel, r.Kind))
94-
}
95-
96119
return allWarnings, nil
97120
}
98121

controllers/common.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,11 @@ import (
3030
)
3131

3232
const (
33-
workflowNameSuffix = "-workflow-counter"
34-
podNameStepInfix = "-workflow-step-"
35-
envVarsConfigMapinfix = "-env-vars-step-"
36-
customDataConfigMapinfix = "-custom-data-step-"
33+
podNameStepInfix = "-s"
34+
envVarsConfigMapinfix = "-env-vars-s"
35+
customDataConfigMapinfix = "-custom-data-s"
3736
workflowStepNumInvalid = -1
38-
workflowStepNameInvalid = "no-step-name"
37+
workflowStepNameInvalid = "no-name"
3938
workflowStepLabel = "workflowStep"
4039
instanceNameLabel = "instanceName"
4140
operatorNameLabel = "operator"
@@ -389,10 +388,6 @@ func (r *Reconciler) GetPodName(instance interface{}, workflowStepNum int) strin
389388
return workflowStepNameInvalid
390389
}
391390

392-
func (r *Reconciler) GetWorkflowConfigMapName(instance client.Object) string {
393-
return instance.GetName() + workflowNameSuffix
394-
}
395-
396391
func (r *Reconciler) GetPVCLogsName(instance client.Object, workflowStepNum int) string {
397392
instanceName := instance.GetName()
398393
instanceCreationTimestamp := instance.GetCreationTimestamp().Format(time.UnixDate)

0 commit comments

Comments
 (0)