Skip to content

Commit 6082bbc

Browse files
committed
[feat] after review fix. Added spark-ui svc validation
1 parent ef62a45 commit 6082bbc

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

internal/webhook/sparkapplication_validator.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
corev1 "k8s.io/api/core/v1"
2424
"k8s.io/apimachinery/pkg/api/equality"
2525
"k8s.io/apimachinery/pkg/runtime"
26+
"k8s.io/apimachinery/pkg/util/validation"
2627
"sigs.k8s.io/controller-runtime/pkg/client"
2728
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2829

@@ -100,10 +101,6 @@ func (v *SparkApplicationValidator) ValidateUpdate(ctx context.Context, oldObj r
100101
return nil, nil
101102
}
102103

103-
if err := v.validateMetadata(ctx, newApp); err != nil {
104-
return nil, err
105-
}
106-
107104
if err := v.validateSpec(ctx, newApp); err != nil {
108105
return nil, err
109106
}
@@ -202,9 +199,14 @@ func (v *SparkApplicationValidator) validateResourceUsage(ctx context.Context, a
202199

203200
func (v *SparkApplicationValidator) validateMetadata(ctx context.Context, app *v1beta2.SparkApplication) error {
204201
meta := app.ObjectMeta
205-
err := validateNameLength(ctx, meta)
206-
if err != nil {
202+
if err := validateNameLength(ctx, meta); err != nil {
207203
return err
208204
}
205+
206+
svcName := util.GetDefaultUIServiceName(app)
207+
if errs := validation.IsDNS1035Label(svcName); len(errs) != 0 {
208+
return fmt.Errorf("spark UI service name %q is not a DNS 1035 label. Try to make the application name shorter", svcName)
209+
}
210+
209211
return nil
210212
}

internal/webhook/webhook.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ package webhook
1919
import (
2020
"context"
2121
"fmt"
22+
2223
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
"k8s.io/apimachinery/pkg/util/validation"
2325
"k8s.io/apimachinery/pkg/util/validation/field"
2426
ctrl "sigs.k8s.io/controller-runtime"
2527
)
@@ -46,9 +48,9 @@ type Options struct {
4648
// and early error message.
4749
func validateNameLength(_ context.Context, appMeta metav1.ObjectMeta) error {
4850
var allErrs field.ErrorList
49-
if len(appMeta.Name) > maxAppNameLength {
51+
if len(appMeta.Name) > validation.LabelValueMaxLength {
5052
allErrs = append(allErrs, field.Invalid(field.NewPath("metadata", "name"), appMeta.Name,
51-
fmt.Sprintf("name must be no more than %d characters to allow for resource suffixes", maxAppNameLength)))
53+
fmt.Sprintf("name length must not exceed %d characters to allow for resource suffixes", maxAppNameLength)))
5254
}
5355
if allErrs != nil {
5456
return allErrs.ToAggregate()

test/e2e/bad_examples/long-name-validation.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
apiVersion: sparkoperator.k8s.io/v1beta2
22
kind: SparkApplication
33
metadata:
4-
name: this-is-a-very-very-very-long-application-name-that-exceeds-the-limit
5-
namespace: default
4+
name: this-is-a-very-very-very-long-application-name-that-words
65
spec:
76
type: Scala
87
mode: cluster
@@ -24,4 +23,4 @@ spec:
2423
version: 4.0.0
2524
instances: 1
2625
cores: 1
27-
memory: 512m
26+
memory: 512m

0 commit comments

Comments
 (0)