dynamomodel.go 3.38 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0
 *
 * 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
 *
 * http://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 validation

import (
	"fmt"
	"strings"

24
	nvidiacomv1alpha1 "github.com/ai-dynamo/dynamo/deploy/operator/api/v1alpha1"
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
	"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// DynamoModelValidator validates DynamoModel resources.
// This validator can be used by both webhooks and controllers for consistent validation.
type DynamoModelValidator struct {
	model *nvidiacomv1alpha1.DynamoModel
}

// NewDynamoModelValidator creates a new validator for DynamoModel.
func NewDynamoModelValidator(model *nvidiacomv1alpha1.DynamoModel) *DynamoModelValidator {
	return &DynamoModelValidator{
		model: model,
	}
}

// Validate performs stateless validation on the DynamoModel.
// Returns warnings and error.
func (v *DynamoModelValidator) Validate() (admission.Warnings, error) {
	// Validate modelName is not empty
	if v.model.Spec.ModelName == "" {
		return nil, fmt.Errorf("spec.modelName is required")
	}

	// Validate baseModelName is not empty
	if v.model.Spec.BaseModelName == "" {
		return nil, fmt.Errorf("spec.baseModelName is required")
	}

	// Validate LoRA model requirements
	if v.model.Spec.ModelType == "lora" {
		if v.model.Spec.Source == nil {
			return nil, fmt.Errorf("spec.source is required when modelType is 'lora'")
		}

		if v.model.Spec.Source.URI == "" {
			return nil, fmt.Errorf("spec.source.uri must be specified when modelType is 'lora'")
		}

		// Validate URI format
		if err := v.validateSourceURI(v.model.Spec.Source.URI); err != nil {
			return nil, err
		}
	}

	return nil, nil
}

// ValidateUpdate performs stateful validation comparing old and new DynamoModel.
// Returns warnings and error.
func (v *DynamoModelValidator) ValidateUpdate(old *nvidiacomv1alpha1.DynamoModel) (admission.Warnings, error) {
	var warnings admission.Warnings

	// modelType is immutable
	if v.model.Spec.ModelType != old.Spec.ModelType {
		warnings = append(warnings, "Changing spec.modelType may cause unexpected behavior")
		return warnings, fmt.Errorf("spec.modelType is immutable and cannot be changed after creation")
	}

	// baseModelName is immutable
	if v.model.Spec.BaseModelName != old.Spec.BaseModelName {
		warnings = append(warnings, "Changing spec.baseModelName will break endpoint discovery")
		return warnings, fmt.Errorf("spec.baseModelName is immutable and cannot be changed after creation")
	}

	return nil, nil
}

// validateSourceURI validates the model source URI format.
func (v *DynamoModelValidator) validateSourceURI(uri string) error {
	if uri == "" {
		return fmt.Errorf("source URI cannot be empty")
	}

	// Check for supported schemes
	if !strings.HasPrefix(uri, "s3://") && !strings.HasPrefix(uri, "hf://") {
		return fmt.Errorf("source URI must start with 's3://' or 'hf://', got: %s", uri)
	}

	return nil
}