Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KEP-2170: Strictly verify the CRD marker validation and defaulting in the integration testings #2304

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/proposals/2170-kubeflow-training-v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ type TrainJob struct {

type TrainJobSpec struct {
// Reference to the training runtime.
// The field is immutable.
// +kubebuilder:validation:XValidation:rule="self == oldSelf", message="runtimeRef is immutable"
RuntimeRef RuntimeRef `json:"runtimeRef"`

// Configuration of the desired trainer.
Expand Down
7 changes: 6 additions & 1 deletion manifests/v2/base/crds/kubeflow.org_trainjobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2747,7 +2747,9 @@ spec:
type: object
type: array
runtimeRef:
description: Reference to the training runtime.
description: |-
Reference to the training runtime.
The field is immutable.
properties:
apiGroup:
default: kubeflow.org
Expand All @@ -2770,6 +2772,9 @@ spec:
required:
- name
type: object
x-kubernetes-validations:
- message: runtimeRef is immutable
rule: self == oldSelf
suspend:
default: false
description: |-
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/kubeflow.org/v2alpha1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/apis/kubeflow.org/v2alpha1/trainjob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ type TrainJobList struct {
// TrainJobSpec represents specification of the desired TrainJob.
type TrainJobSpec struct {
// Reference to the training runtime.
// The field is immutable.
// +kubebuilder:validation:XValidation:rule="self == oldSelf", message="runtimeRef is immutable"
RuntimeRef RuntimeRef `json:"runtimeRef"`

// Configuration of the desired trainer.
Expand Down
82 changes: 82 additions & 0 deletions pkg/util.v2/testing/errormatcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
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

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 testing

import (
"fmt"

"github.com/onsi/gomega/format"
"github.com/onsi/gomega/types"
apierrors "k8s.io/apimachinery/pkg/api/errors"
)

func BeNotFoundError() types.GomegaMatcher {
return BeAPIError(NotFoundError)
}

func BeForbiddenError() types.GomegaMatcher {
return BeAPIError(ForbiddenError)
}
Comment on lines +31 to +33
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we could return this error ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will use this in webhook impl similar to

allErrs = append(allErrs, field.Forbidden(elasticNProcPerNodePath, fmt.Sprintf("must not be used with %s", nprocPerNodePath)))


func BeInvalidError() types.GomegaMatcher {
return BeAPIError(InvalidError)
}

type errorMatcher int

const (
NotFoundError errorMatcher = iota
ForbiddenError
InvalidError
)

func (em errorMatcher) String() string {
return []string{"NotFoundError", "ForbiddenError", "InvalidError"}[em]
}

type apiError func(error) bool

func (em errorMatcher) isAPIError(err error) bool {
return []apiError{apierrors.IsNotFound, apierrors.IsForbidden, apierrors.IsInvalid}[em](err)
}

type isErrorMatch struct {
name errorMatcher
}

func BeAPIError(name errorMatcher) types.GomegaMatcher {
return &isErrorMatch{
name: name,
}
}

func (matcher *isErrorMatch) Match(actual interface{}) (success bool, err error) {
err, ok := actual.(error)
if !ok {
return false, fmt.Errorf("%s expects an error", matcher.name.String())
}

return err != nil && matcher.name.isAPIError(err), nil
}

func (matcher *isErrorMatch) FailureMessage(actual interface{}) (message string) {
return format.Message(actual, "to be a %s", matcher.name.String())
}

func (matcher *isErrorMatch) NegatedFailureMessage(actual interface{}) (message string) {
return format.Message(actual, "not to be %s", matcher.name.String())
}
18 changes: 14 additions & 4 deletions pkg/util.v2/testing/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,16 @@ func (t *TrainJobWrapper) SpecAnnotation(key, value string) *TrainJobWrapper {
}

func (t *TrainJobWrapper) RuntimeRef(gvk schema.GroupVersionKind, name string) *TrainJobWrapper {
t.Spec.RuntimeRef = kubeflowv2.RuntimeRef{
APIGroup: &gvk.Group,
Kind: &gvk.Kind,
Name: name,
runtimeRef := kubeflowv2.RuntimeRef{
Name: name,
}
if gvk.Group != "" {
runtimeRef.APIGroup = &gvk.Group
}
if gvk.Kind != "" {
runtimeRef.Kind = &gvk.Kind
}
t.Spec.RuntimeRef = runtimeRef
return t
}

Expand All @@ -249,6 +254,11 @@ func (t *TrainJobWrapper) Trainer(trainer *kubeflowv2.Trainer) *TrainJobWrapper
return t
}

func (t *TrainJobWrapper) ManagedBy(m string) *TrainJobWrapper {
t.Spec.ManagedBy = &m
return t
}

func (t *TrainJobWrapper) Obj() *kubeflowv2.TrainJob {
return &t.TrainJob
}
Expand Down
Loading
Loading