-
Notifications
You must be signed in to change notification settings - Fork 330
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
Merged
+353
−8
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
fa1f98c
feat: auto-detect Kubernetes schema
msvechla 9b341f6
fix: only auto-detect k8s crd for k8s files
qvalentin 03aed76
feat: check if GVK in main kubeSchema
qvalentin d10995f
fix: test
qvalentin e8524cb
fix: promises were made
qvalentin c12a525
fix: rename crd options and document them
qvalentin a346aae
fix eslint errors
rowahl 7794177
fix tests
rowahl a944378
fix: do not use CRDs-catalog for builtin k8s ressources
qvalentin 3720271
fix: clarify @ts-ignore
qvalentin 1988d81
fix: import type from ../jsonSchema
qvalentin 8c68029
Handle case where custom schema provider is present
datho7561 ba55b1a
Add logic to handle OpenShift CRDs
datho7561 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,92 @@ | ||
| import { SingleYAMLDocument } from '../parser/yamlParser07'; | ||
| import { JSONDocument } from '../parser/jsonParser07'; | ||
|
|
||
| import { ResolvedSchema } from 'vscode-json-languageservice/lib/umd/services/jsonSchemaService'; | ||
| import { JSONSchema } from '../jsonSchema'; | ||
|
|
||
| /** | ||
| * Retrieve schema by auto-detecting the Kubernetes GroupVersionKind (GVK) from the document. | ||
| * If there is no definition for the GVK in the main kubernetes schema, | ||
| * the schema is then retrieved from the CRD catalog. | ||
| * Public for testing purpose, not part of the API. | ||
| * @param doc | ||
| * @param crdCatalogURI The URL of the CRD catalog to retrieve the schema from | ||
| * @param kubernetesSchema The main kubernetes schema, if it includes a definition for the GVK it will be used | ||
| */ | ||
| export function autoDetectKubernetesSchemaFromDocument( | ||
| doc: SingleYAMLDocument | JSONDocument, | ||
| crdCatalogURI: string, | ||
| kubernetesSchema: ResolvedSchema | ||
| ): string | undefined { | ||
| const res = getGroupVersionKindFromDocument(doc); | ||
| if (!res) { | ||
| return undefined; | ||
| } | ||
| const { group, version, kind } = res; | ||
| if (!group || !version || !kind) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const k8sSchema: JSONSchema = kubernetesSchema.schema; | ||
| const kubernetesBuildIns: string[] = (k8sSchema.oneOf || []) | ||
| .map((s) => { | ||
| if (typeof s === 'boolean') { | ||
| return undefined; | ||
| } | ||
| return s._$ref || s.$ref; | ||
| }) | ||
| .filter((ref) => ref) | ||
| .map((ref) => ref.replace('_definitions.json#/definitions/', '').toLowerCase()); | ||
| const groupWithoutK8sIO = group.replace('.k8s.io', ''); | ||
| const k8sTypeName = `io.k8s.api.${groupWithoutK8sIO.toLowerCase()}.${version.toLowerCase()}.${kind.toLowerCase()}`; | ||
|
|
||
| if (kubernetesBuildIns.includes(k8sTypeName)) { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (k8sTypeName.includes('openshift.io')) { | ||
| return `${crdCatalogURI}/openshift/v4.15-strict/${kind.toLowerCase()}_${group.toLowerCase()}_${version.toLowerCase()}.json`; | ||
| } | ||
|
|
||
| const schemaURL = `${crdCatalogURI}/${group.toLowerCase()}/${kind.toLowerCase()}_${version.toLowerCase()}.json`; | ||
| return schemaURL; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieve the group, version and kind from the document. | ||
| * Public for testing purpose, not part of the API. | ||
| * @param doc | ||
| */ | ||
| export function getGroupVersionKindFromDocument( | ||
| doc: SingleYAMLDocument | JSONDocument | ||
| ): { group: string; version: string; kind: string } | undefined { | ||
| if (doc instanceof SingleYAMLDocument) { | ||
| try { | ||
| const rootJSON = doc.root.internalNode.toJSON(); | ||
| if (!rootJSON) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const groupVersion = rootJSON['apiVersion']; | ||
| if (!groupVersion) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const [group, version] = groupVersion.split('/'); | ||
| if (!group || !version) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const kind = rootJSON['kind']; | ||
| if (!kind) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return { group, version, kind }; | ||
| } catch (error) { | ||
| console.error('Error parsing YAML document:', error); | ||
| return undefined; | ||
| } | ||
| } | ||
| return undefined; | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
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.
Check warning
Code scanning / ESLint
Disallow the `any` type Warning