-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[WIP] Validate external types with CRD machinery #1690
Open
cartermckinnon
wants to merge
1
commit into
main
Choose a base branch
from
validation-with-crd-spec
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package bridge | ||
|
||
import ( | ||
_ "embed" | ||
"errors" | ||
"fmt" | ||
"log" | ||
|
||
"k8s.io/apiextensions-apiserver/pkg/apihelpers" | ||
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" | ||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
apiservervalidation "k8s.io/apiextensions-apiserver/pkg/apiserver/validation" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/runtime/serializer" | ||
) | ||
|
||
//go:embed crds/node.eks.aws_nodeconfigs.yaml | ||
var customResourceDefinitionYAML []byte | ||
|
||
var customResourceDefinition *apiextensionsv1.CustomResourceDefinition | ||
|
||
func init() { | ||
scheme := runtime.NewScheme() | ||
if err := apiextensions.AddToScheme(scheme); err != nil { | ||
panic("Failed to register apiextensions on validation scheme") | ||
} | ||
if err := apiextensionsv1.AddToScheme(scheme); err != nil { | ||
panic("Failed to register apiextensionsv1 on validation scheme") | ||
} | ||
codecs := serializer.NewCodecFactory(scheme) | ||
obj, gvk, err := codecs.UniversalDeserializer().Decode(customResourceDefinitionYAML, nil, nil) | ||
if err != nil { | ||
log.Fatalf("failed to decode CRD: %v", err) | ||
} | ||
if crd, ok := obj.(*apiextensionsv1.CustomResourceDefinition); !ok { | ||
log.Fatalf("CRD YAML is not a valid apiextensionsv1.CustomResourceDefinition: %v", gvk) | ||
} else { | ||
customResourceDefinition = crd | ||
} | ||
} | ||
|
||
func ValidateExternalType(obj runtime.Object, gvk schema.GroupVersionKind) error { | ||
validationSchema, err := apihelpers.GetSchemaForVersion(customResourceDefinition, gvk.Version) | ||
if err != nil { | ||
return err | ||
} | ||
var internalSchemaProps *apiextensions.JSONSchemaProps | ||
var internalValidationSchema *apiextensions.CustomResourceValidation | ||
if validationSchema != nil { | ||
internalValidationSchema = &apiextensions.CustomResourceValidation{} | ||
if err := apiextensionsv1.Convert_v1_CustomResourceValidation_To_apiextensions_CustomResourceValidation(validationSchema, internalValidationSchema, nil); err != nil { | ||
return fmt.Errorf("failed to convert CRD validation to internal version: %v", err) | ||
} | ||
internalSchemaProps = internalValidationSchema.OpenAPIV3Schema | ||
} | ||
validator, _, err := apiservervalidation.NewSchemaValidator(internalSchemaProps) | ||
if err != nil { | ||
return err | ||
} | ||
res := validator.Validate(obj) | ||
if len(res.Errors) > 0 { | ||
return errors.Join(res.Errors...) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
nodeadm/vendor/github.com/antlr/antlr4/runtime/Go/antlr/v4/LICENSE
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
68 changes: 68 additions & 0 deletions
68
nodeadm/vendor/github.com/antlr/antlr4/runtime/Go/antlr/v4/antlrdoc.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
based on tests, this is breaking how we merge partial configs 😞
will we have to do the merging in json/yaml like kubelet? https://github.com/kubernetes/kubernetes/blob/master/cmd/kubelet/app/server.go#L314-L345