Skip to content

fix: fromCRD handles structural schema rule 1 exceptions#49

Merged
arnarg merged 1 commit into
arnarg:mainfrom
rfuwa14:rfuwa/pzwotqpyvvsv
Aug 17, 2025
Merged

fix: fromCRD handles structural schema rule 1 exceptions#49
arnarg merged 1 commit into
arnarg:mainfrom
rfuwa14:rfuwa/pzwotqpyvvsv

Conversation

@rfuwa14

@rfuwa14 rfuwa14 commented Aug 15, 2025

Copy link
Copy Markdown
Contributor

Fixes #48

TEST

  • Test CRD manifest
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: bars.foo.baz
spec:
  scope: Namespaced
  group: foo.baz
  names:
    kind: Bar
    plural: bars
    shortNames:
      - bar
    singular: bar
  versions:
    - name: v1beta3
      schema:
        openAPIV3Schema:
          properties:
            spec:
              properties:
                node:
                  description: a test node
                  x-kubernetes-int-or-string: true
              required:
                - node
              type: object
          required:
            - spec
          type: object
      served: true
      storage: true

Failure replication

$nix build --show-trace .#gen.bar
# ...
error: attribute 'type' missing
at /nix/store/67bvmrzr4iin6fcvnz9qavr2fya9hnwm-source/pkgs/generators/crd/jsonschema.nix:154:29:
  153|                     # if property has an array type
  154|                     else if property.type == "array"
     |                             ^
  155|                     then
# ...
  • flake.lock
    "nixidy": {
      "inputs": {
        "flake-utils": "flake-utils",
        "nix-kube-generators": "nix-kube-generators",
        "nixpkgs": "nixpkgs_2"
      },
      "locked": {
        "lastModified": 1753281262,
        "narHash": "sha256-XZO8SNbjifLzUenfTClpzwBwiOi6OkViZ1DrECqUMHM=",
        "owner": "arnarg",
        "repo": "nixidy",
        "rev": "e7e7685825c6bd62a300bbe3eac09d7352629e92",
        "type": "github"
      },
      "original": {
        "owner": "arnarg",
        "repo": "nixidy",
        "type": "github"
      }
    },
...

Run against this change

$nix build .#gen.bar

@rfuwa14
rfuwa14 force-pushed the rfuwa/pzwotqpyvvsv branch 4 times, most recently from 1f5cf8a to 9f3df48 Compare August 15, 2025 16:58

@arnarg arnarg left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks for your pull request!

To not overcomplicate the if statements in jsonschema.nix we've been handling these kind of edge cases in crd2jsonschema.py instead. Could you move this logic there?

See:

"type" not in definition

@rfuwa14
rfuwa14 force-pushed the rfuwa/pzwotqpyvvsv branch from 9f3df48 to dffefef Compare August 17, 2025 03:06
@rfuwa14

rfuwa14 commented Aug 17, 2025

Copy link
Copy Markdown
Contributor Author

done and added an unit test.

TEST

$nix run .#crd2jsonschemaTest
......
----------------------------------------------------------------------
Ran 6 tests in 0.003s

OK

@arnarg arnarg left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Out of curiousity, are you using an LLM to write this?

Please don't creep in the type annotations and while unit tests are useful I want to first make this function more testable in the future.

To fix the issue at hand just edit the if statement I linked to like so:

elif "type" not in definition:
  if definition.get("x-kubernetes-preserve-unknown-fields", False)  == True:
    # ... 
  elif definition.get("x-kubernetes-int-or-string", False) == True:
    # ... 

fixes arnarg#48.

There are a few exceptions that allow a node to not have "type" field
[^1]. This PR addresses these exceptions in `fromCRD` function.

### TEST

- Test CRD manifest

```yaml
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: bars.foo.baz
spec:
  scope: Namespaced
  group: foo.baz
  names:
    kind: Bar
    plural: bars
    shortNames:
      - bar
    singular: bar
  versions:
    - name: v1beta3
      schema:
        openAPIV3Schema:
          properties:
            spec:
              properties:
                node:
                  description: a test node
                  x-kubernetes-int-or-string: true
              required:
                - node
              type: object
          required:
            - spec
          type: object
      served: true
      storage: true
```

#### Failure replication

```bash
$nix build --show-trace .#gen.bar
# ...
error: attribute 'type' missing
at /nix/store/67bvmrzr4iin6fcvnz9qavr2fya9hnwm-source/pkgs/generators/crd/jsonschema.nix:154:29:
  153|                     # if property has an array type
  154|                     else if property.type == "array"
     |                             ^
  155|                     then
# ...
```

- flake.lock
```
    "nixidy": {
      "inputs": {
        "flake-utils": "flake-utils",
        "nix-kube-generators": "nix-kube-generators",
        "nixpkgs": "nixpkgs_2"
      },
      "locked": {
        "lastModified": 1753281262,
        "narHash": "sha256-XZO8SNbjifLzUenfTClpzwBwiOi6OkViZ1DrECqUMHM=",
        "owner": "arnarg",
        "repo": "nixidy",
        "rev": "e7e7685825c6bd62a300bbe3eac09d7352629e92",
        "type": "github"
      },
      "original": {
        "owner": "arnarg",
        "repo": "nixidy",
        "type": "github"
      }
    },
...

#### Run against this change

```bash
$nix build --show-trace .#gen.bar

```


[^1]: https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#specifying-a-structural-schema
@rfuwa14
rfuwa14 force-pushed the rfuwa/pzwotqpyvvsv branch from b72cec8 to 6d321a2 Compare August 17, 2025 17:38
@rfuwa14

rfuwa14 commented Aug 17, 2025

Copy link
Copy Markdown
Contributor Author

I'm not using an LLM. I added types and a bit of refactoring according to my linter because there isn't a python linter setup in this repo afaik. I removed tests for now, but it is hard to contirubte relatively bigger changes (e.g. #51). Let me know how you'd like to handle this in the future.

@rfuwa14
rfuwa14 requested a review from arnarg August 17, 2025 17:43
@arnarg

arnarg commented Aug 17, 2025

Copy link
Copy Markdown
Owner

I didn't mean any offense with that question btw.

Yeah this file started as a simple script to transform a CRD into something the existing code generator already understands. Since then handling for more and more edge cases has been added so it should probably be cleaned up a bit and tests added.

@arnarg arnarg left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Looks good! Thank you for this contribution!

@arnarg
arnarg merged commit 4073585 into arnarg:main Aug 17, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: fromCRD handles structural schema rule 1 exceptions

2 participants