-
Notifications
You must be signed in to change notification settings - Fork 319
feat: auto-detect Kubernetes crd schema #1050
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
Conversation
|
@qvalentin Thanks for the nice work. I actually started working on this as well: https://github.com/tricktron/yaml-language-server/tree/f-kubernetes-improvements However, I just hardcoded the core kubernetes groups but yours looks better in my opinion. I then actually got sidetracked by adding some more features to my https://github.com/tricktron/crd2jsonschema tool so that I could easily test end-to-end with the newest openshift and tekton crds. Maybe you can also take over some tests from my my branch, e.g. main...tricktron:yaml-language-server:f-kubernetes-improvements#diff-5d1a1b48130b3cb749d3ae36d72e52f8882d6fa5dc9eaedc5d6e606e411b176cR1194-R1219 And lastly, we should then probably add some documentation as well. This might be a breaking change for the helm language server where you hardcode the kubernetes schemas to |
|
@tricktron I took a look at the schemaValidation.test.ts but since in my logic the customSchemaProvider has higher priority then the CRD logic the tests don't work with my branch. But I might copy the config options format. |
|
Hi @msivasubramaniaan what are your thoughts on this? |
Add an option to overwrite the default Kubernetes schema URL with a custom one, by setting a `YAMLLS_KUBERNETES_SCHEMA_URL` environment variable. See - redhat-developer#998 - redhat-developer#824 - redhat-developer#841 - redhat-developer#962 - redhat-developer#1050 Main use case is to use https://github.com/ricoberger/kubernetes-json-schema, which contains the schema for Kubernetes and the CRDs I'm using.
|
I'll try to take a look at this sometime this afternoon, it seems really helpful. |
|
The tests are failing it seems, with a type error |
|
I'm trying out: apiVersion: route.openshift.io/v1
kind: Route
spec:It seems it was trying to use a custom schema provider instead of the crd schema autodetection, so I made this change: diff --git a/src/languageservice/services/yamlSchemaService.ts b/src/languageservice/services/yamlSchemaService.ts
index 432851f..9102233 100644
--- a/src/languageservice/services/yamlSchemaService.ts
+++ b/src/languageservice/services/yamlSchemaService.ts
@@ -430,6 +430,22 @@ export class YAMLSchemaService extends JSONSchemaService {
return resolveSchemaForResource([modelineSchema]);
}
+ if (this.yamlSettings?.kubernetesCRDStoreEnabled) {
+ for (const entry of this.filePatternAssociations) {
+ if (entry.uris && entry.uris[0] == KUBERNETES_SCHEMA_URL && entry.matchesPattern(resource)) {
+ return resolveSchemaForResource([KUBERNETES_SCHEMA_URL]).then((schema) => {
+ const kubeSchema = autoDetectKubernetesSchemaFromDocument(
+ doc,
+ this.yamlSettings.kubernetesCRDStoreUrl ?? CRD_CATALOG_URL,
+ schema
+ );
+ if (kubeSchema) {
+ return resolveSchemaForResource([kubeSchema]);
+ }
+ });
+ }
+ }
+ }
if (this.customSchemaProvider) {
return this.customSchemaProvider(resource)
.then((schemaUri) => {
@@ -473,22 +489,6 @@ export class YAMLSchemaService extends JSONSchemaService {
}
);
}
- if (this.yamlSettings?.kubernetesCRDStoreEnabled) {
- for (const entry of this.filePatternAssociations) {
- if (entry.uris && entry.uris[0] == KUBERNETES_SCHEMA_URL && entry.matchesPattern(resource)) {
- return resolveSchemaForResource([KUBERNETES_SCHEMA_URL]).then((schema) => {
- const kubeSchema = autoDetectKubernetesSchemaFromDocument(
- doc,
- this.yamlSettings.kubernetesCRDStoreUrl ?? CRD_CATALOG_URL,
- schema
- );
- if (kubeSchema) {
- return resolveSchemaForResource([kubeSchema]);
- }
- });
- }
- }
- }
return resolveSchema();
}After I do that and try to validate the YAML, I get "Unable to load schema..." as an error in the YAML, since the calculated URL doesn't line up with the location of the schema in the repo (the OpenShift CRDs are split across multiple group ids, whereas in the repo with the CRDs all the schemas are in the folder "openshift/v4.11-strict"). After this I decided to try out: apiVersion: mysql.oracle.com/v2
kind: InnoDBCluster
spec:And that appears to work after applying the above patch, but the patch appears to break many other tests. |
|
Hi @datho7561 I'm not sure what should have higher priority, a custom schema provider or the crd check. I would guess the custom schema provider? I'm just using the lsp with neovim where no custom schema provider is active. I wonder which provider is active in your setup. So the mysql example is working for me, but indeed the openshift one is not. Edit: I took a quick look at the catalog and it seems openshift is the only directory that is not using an API group name. I would say it's just a bad example and should be treated separately. |
I'm using vscode-yaml; when vscode-yaml is used, there's a custom schema provider active in yaml-language-server that sends a custom LSP request If you move the CRD resolving code into the
I have written a hack to get the correct URL for OpenShift CRDs, I'll also push this as a commit. |
Add an option to overwrite the default Kubernetes schema URL with a custom one, by setting a `YAMLLS_KUBERNETES_SCHEMA_URL` environment variable. See - redhat-developer#998 - redhat-developer#824 - redhat-developer#841 - redhat-developer#962 - redhat-developer#1050 Main use case is to use https://github.com/ricoberger/kubernetes-json-schema, which contains the schema for Kubernetes and the CRDs I'm using.
|
@qvalentin I think this feature is ready for merge after the changes I made. Let me know if you have objections to anything I changed or if it doesn't work for you. I'm planning on merging this Monday otherwise. |
automatically detect the Kubernetes schema based on the document's GroupVersionKind (GVK) and retrieve the matching schema from the CRD catalog.
The CRD schema resolving logic now runs even if a custom schema provider is present, allowing this to work properly in vscode-yaml. Signed-off-by: David Thompson <[email protected]>
The CRD schemas for OpenShift are layed out slightly different from everything else in the CRD schema repo in order to provide different sets of schemas for different versions of OpenShift. Signed-off-by: David Thompson <[email protected]>
See redhat-developer/yaml-language-server#1050 Settings to enable/disable the feature and change the repository used to find CRD schemas. Use https://raw.githubusercontent.com/nlamirault/crd-schema-store/refs/heads/main/schemas to test an alternate store of CRD schemas. Signed-off-by: David Thompson <[email protected]>
What does this PR do?
This PR adds an option to automatically detect the Kubernetes schema by parsing the contents of the document. In case a Kubernetes GroupVersionKind (GVK) is detected, the matching schema is retrieved from the CRD catalog.
What issues does this PR fix or reference?
#605
Is it tested? How?
Tests for parsing the Kubernetes Group Version Kind have been added
Updated version of #962 including the following changes:
TODO: