mirror of
https://github.com/hanzo-ml/spark-operator.git
synced 2026-10-30 20:39:07 +00:00
* Bump k8s api to 0.35.4, and controller runtime to v0.23.3 Signed-off-by: Shingo Omura <[email protected]> * Migrated defaulter/validator to be Generic ones Signed-off-by: Shingo Omura <[email protected]> * Fixed Endpoints deprecation lint errors (replaced with EndpointSlice) Signed-off-by: Shingo Omura <[email protected]> * Deleted unused internal/controller/sparkapplication/validator.go Signed-off-by: Shingo Omura <[email protected]> * Add SA1019 exclusion to golangci-lint config so to preserve Requeue behavior of controllers Signed-off-by: Shingo Omura <[email protected]> * Fixed util.WriteObjectToFile test. Null creationTimestamp is not marshalled anymore. Signed-off-by: Shingo Omura <[email protected]> * Updated codegen and API definitions Signed-off-by: Shingo Omura <[email protected]> * Fixed mgr.GetEventRecorderFor() deprecation (replace with mgr.GetEventRecorder()) Signed-off-by: Shingo Omura <[email protected]> --------- Signed-off-by: Shingo Omura <[email protected]>
105 lines
4.0 KiB
Go
105 lines
4.0 KiB
Go
/*
|
|
Copyright 2024 The Kubeflow authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package webhook
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"k8s.io/apimachinery/pkg/util/validation"
|
|
"sigs.k8s.io/controller-runtime/pkg/log"
|
|
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
|
|
|
|
"github.com/kubeflow/spark-operator/v2/api/v1beta2"
|
|
)
|
|
|
|
// NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here.
|
|
// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook.
|
|
// +kubebuilder:webhook:admissionReviewVersions=v1,failurePolicy=fail,groups=sparkoperator.k8s.io,matchPolicy=Exact,mutating=false,name=validate-scheduledsparkapplication.sparkoperator.k8s.io,path=/validate-sparkoperator-k8s-io-v1beta2-scheduledsparkapplication,reinvocationPolicy=Never,resources=scheduledsparkapplications,sideEffects=None,verbs=create;update,versions=v1beta2,webhookVersions=v1
|
|
|
|
type ScheduledSparkApplicationValidator struct{}
|
|
|
|
// NewScheduledSparkApplicationValidator creates a new ScheduledSparkApplicationValidator instance.
|
|
func NewScheduledSparkApplicationValidator() *ScheduledSparkApplicationValidator {
|
|
return &ScheduledSparkApplicationValidator{}
|
|
}
|
|
|
|
var _ admission.Validator[*v1beta2.ScheduledSparkApplication] = &ScheduledSparkApplicationValidator{}
|
|
|
|
// ValidateCreate implements admission.Validator.
|
|
func (v *ScheduledSparkApplicationValidator) ValidateCreate(ctx context.Context, app *v1beta2.ScheduledSparkApplication) (warnings admission.Warnings, err error) {
|
|
if app == nil {
|
|
return nil, nil
|
|
}
|
|
|
|
logger := log.FromContext(ctx)
|
|
logger.Info("Validating ScheduledSparkApplication create")
|
|
// Validate metadata.name early to prevent downstream Service creation failures
|
|
if err := v.validateName(app.Name); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := v.validate(app); err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
// ValidateUpdate implements admission.Validator.
|
|
func (v *ScheduledSparkApplicationValidator) ValidateUpdate(ctx context.Context, oldApp *v1beta2.ScheduledSparkApplication, newApp *v1beta2.ScheduledSparkApplication) (warnings admission.Warnings, err error) {
|
|
if oldApp == nil || newApp == nil {
|
|
return nil, nil
|
|
}
|
|
|
|
logger := log.FromContext(ctx)
|
|
logger.Info("Validating ScheduledSparkApplication update")
|
|
// Name is immutable in Kubernetes, but validate anyway for safety in case of admission reconcilers
|
|
if err := v.validateName(newApp.Name); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := v.validate(newApp); err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
// ValidateDelete implements admission.Validator.
|
|
func (v *ScheduledSparkApplicationValidator) ValidateDelete(ctx context.Context, app *v1beta2.ScheduledSparkApplication) (warnings admission.Warnings, err error) {
|
|
if app == nil {
|
|
return nil, nil
|
|
}
|
|
|
|
logger := log.FromContext(ctx)
|
|
logger.Info("Validating ScheduledSparkApplication delete")
|
|
return nil, nil
|
|
}
|
|
|
|
func (v *ScheduledSparkApplicationValidator) validate(_ *v1beta2.ScheduledSparkApplication) error {
|
|
// TODO: implement validate logic
|
|
return nil
|
|
}
|
|
|
|
// validateName ensures the ScheduledSparkApplication metadata.name, when combined with suffixes,
|
|
// results in a valid DNS-1035 label for Kubernetes Service names. This prevents failures later
|
|
// when creating SparkApplication resources that require DNS-1035 compliant names.
|
|
func (v *ScheduledSparkApplicationValidator) validateName(name string) error {
|
|
if errs := validation.IsDNS1035Label(name); len(errs) > 0 {
|
|
return fmt.Errorf("invalid ScheduledSparkApplication name %q: %s", name, strings.Join(errs, ", "))
|
|
}
|
|
return nil
|
|
}
|