Skip to content

fix(k8s): alias apimachinery.pkg.apis.meta.v1 imports in 1.14 - 1.31 - #392

Merged
Peefy merged 2 commits into
mainfrom
fix/k8s-meta-v1-imports
Aug 24, 2026
Merged

fix(k8s): alias apimachinery.pkg.apis.meta.v1 imports in 1.14 - 1.31#392
Peefy merged 2 commits into
mainfrom
fix/k8s-meta-v1-imports

Conversation

@Peefy

@Peefy Peefy commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Summary

Up to and including k8s 1.31, the auto-generated KCL schema packages imported apimachinery.pkg.apis.meta.v1 without an alias:

import apimachinery.pkg.apis.meta.v1

schema X:
    foo?: v1.Condition

This breaks whenever the parent package is imported via as <alias> (e.g. import k8s.api.networking.v1 as netv1), because the relative-import resolver does not fall back into the parent-package namespace. Symptom:

error[E2G22]: attribute 'Condition' not found in 'module 'apimachinery.pkg.apis.meta.v1''

This is the same class of bug that surfaced in discussion #2127 (the user hit it via kcl_lib on Windows with k8s/1.28), and it also blocks konfig's nginx-example which pins k8s = "1.31.2".

Fix

Promote every import apimachinery.pkg.apis.meta.v1 to import apimachinery.pkg.apis.meta.v1 as metav1, and rewrite in-file v1.<Type> references to metav1.<Type>. This matches the pattern already shipped in k8s 1.32+, so nothing breaks forward.

Files touched:

version files
1.14 208
1.15 210
1.16 221
1.17 227
1.18 191
1.19 197
1.20 204
1.21 212
1.22 168
1.23 162
1.24 161
1.25 141
1.26 153
1.27 157
1.28 158
1.29 169
1.30 184
1.31 186

Total: 3308 files, 10174 insertions(+), 10174 deletions(-) (one-line mechanical rewrite).

Verification

Tested locally with a fresh kcl mod init + k8s = { path = "k8s/1.28" } dep:

import k8s.api.networking.v1 as netv1

result = netv1.NetworkPolicy {
    metadata.name = "test"
    spec = {
        podSelector = { matchLabels = { app = "demo" } }
        policyTypes = ["Ingress"]
        ingress = [{
            from = [{ podSelector = { matchLabels = { app = "frontend" } } }]
            ports = [{ protocol = "TCP", port = 80 }]
        }]
    }
}

Compiles clean. Before the fix this fails with attribute 'Condition' not found in 'module 'apimachinery.pkg.apis.meta.v1''.

Note for the schema-generator side

The right long-term fix is in the upstream KCL schema generator — it should emit as metav1 (and any other v1.X aliases) by default. Until then, this PR keeps the published k8s/1.14-1.31 modules usable.

Related

  • discussion #2127 (vendored k8s/1.28 hits the same bug from kcl_lib)
  • konfig nginx-example (kcl.mod pins k8s = "1.31.2")

@Peefy
Peefy force-pushed the fix/k8s-meta-v1-imports branch from 07ddca5 to d8f2862 Compare August 24, 2026 06:33
claude and others added 2 commits August 24, 2026 14:37
KCL's auto-generated Kubernetes schema packages up to 1.31 imported the
`apimachinery.pkg.apis.meta.v1` module without an alias, which causes
schema resolution to fail whenever the parent package is imported via
`as <alias>` (the relative-import resolver cannot fall back to the
parent-package namespace).

Fix by adding `as metav1` to every such import and rewriting in-file
`v1.<TypeName>` references to `metav1.<TypeName>`. 1.32+ already uses
this pattern and is unaffected.

This unblocks downstream users on Windows / older k8s versions (e.g.
konfig nginx-example pinned to 1.31.2) and resolves the failure
reported in kcl-lang/kcl#2127.

Affected: 3308 files across k8s 1.14 -> 1.31.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: Claude <claude@anthropic.com>
Extend TestK8sPackages to exercise every k8s package version touched by
the bare `apimachinery.pkg.apis.meta.v1` import fix. The new
networkpolicy case instantiates a NetworkPolicy that depends on both
metav1.ObjectMeta (via `metadata`) and metav1.LabelSelector (via
`spec.podSelector.matchLabels`), so any future regression on the
`metav1` aliasing will surface as a compile or assertion failure.

Per-case required-schema gates skip the ingress test for k8s 1.14 -
1.18 because networking.k8s.io/v1 Ingress was only promoted out of
v1beta1 in 1.19.

Local `go test ./...` reports 67 PASS / 5 SKIP across 20 versions.

Signed-off-by: Claude <claude@anthropic.com>
@Peefy
Peefy force-pushed the fix/k8s-meta-v1-imports branch from d8f2862 to cec4bfd Compare August 24, 2026 06:37
@Peefy
Peefy merged commit f36e583 into main Aug 24, 2026
6 checks passed
Peefy added a commit that referenced this pull request Aug 24, 2026
* fix(k8s): alias `apimachinery.pkg.apis.meta.v1` imports in 1.14 - 1.31

KCL's auto-generated Kubernetes schema packages up to 1.31 imported the
`apimachinery.pkg.apis.meta.v1` module without an alias, which causes
schema resolution to fail whenever the parent package is imported via
`as <alias>` (the relative-import resolver cannot fall back to the
parent-package namespace).

Fix by adding `as metav1` to every such import and rewriting in-file
`v1.<TypeName>` references to `metav1.<TypeName>`. 1.32+ already uses
this pattern and is unaffected.

This unblocks downstream users on Windows / older k8s versions (e.g.
konfig nginx-example pinned to 1.31.2) and resolves the failure
reported in kcl-lang/kcl#2127.

Affected: 3308 files across k8s 1.14 -> 1.31.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: Claude <claude@anthropic.com>

* test(k8s): cover metav1 alias fix across 1.14 - 1.33

Extend TestK8sPackages to exercise every k8s package version touched by
the bare `apimachinery.pkg.apis.meta.v1` import fix. The new
networkpolicy case instantiates a NetworkPolicy that depends on both
metav1.ObjectMeta (via `metadata`) and metav1.LabelSelector (via
`spec.podSelector.matchLabels`), so any future regression on the
`metav1` aliasing will surface as a compile or assertion failure.

Per-case required-schema gates skip the ingress test for k8s 1.14 -
1.18 because networking.k8s.io/v1 Ingress was only promoted out of
v1beta1 in 1.19.

Local `go test ./...` reports 67 PASS / 5 SKIP across 20 versions.

Signed-off-by: Claude <claude@anthropic.com>

* chore(k8s): bump patch versions for 1.14 - 1.31

PR #392 rewrote 3308 schema files to fix the bare
`apimachinery.pkg.apis.meta.v1` import bug, but the kcl.mod files
themselves were left at their old patch versions. Bump every affected
package by one patch so users who pin a version (or rely on
`kcl mod update`) actually pull the fix.

- 1.14.1 -> 1.14.2
- ...
- 1.28.1 -> 1.28.2
- 1.31.2 -> 1.31.3

1.32+ and 1.30.x unaffected paths are not touched.

Signed-off-by: Claude <claude@anthropic.com>

---------

Signed-off-by: Claude <claude@anthropic.com>
Co-authored-by: Claude <claude@anthropic.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
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.

2 participants