From e45385ca35d99faec85d708fd1dbe19ac4477358 Mon Sep 17 00:00:00 2001 From: Christopher Haar Date: Thu, 19 Nov 2026 20:32:20 +0100 Subject: [PATCH 1/2] Generate models for all Kubernetes API groups The k8s dependency source downloaded the core/v1 OpenAPI spec plus a fixed list of seven API groups, all at v1, so models for other groups and versions (autoscaling/v2, discovery, events, ...) were never generated and code importing them did not compile. Fetch the specs the way the dependency's schemas were originally sourced: a shallow sparse checkout of kubernetes/kubernetes at the requested version, limited to api/openapi-spec, which carries the complete spec set. The git-backed source already treats k8s dependencies' files as OpenAPI documents. Signed-off-by: Christopher Haar --- internal/schemas/manager/source.go | 114 ++++++----------------------- 1 file changed, 23 insertions(+), 91 deletions(-) diff --git a/internal/schemas/manager/source.go b/internal/schemas/manager/source.go index feed85a7c..280913fb1 100644 --- a/internal/schemas/manager/source.go +++ b/internal/schemas/manager/source.go @@ -434,98 +434,30 @@ func NewHTTPSource(dep v1alpha1.Dependency) Source { // k8sOpenAPISource is a source that generates OpenAPI schemas for Kubernetes // built-in APIs by downloading the swagger spec. -type k8sOpenAPISource struct { - k8sRef *v1alpha1.K8sDependency - client *http.Client - fs afero.Fs -} - -func (k *k8sOpenAPISource) ID() string { - return fmt.Sprintf("k8s://%s", k.k8sRef.Version) -} - -func (k *k8sOpenAPISource) Version(_ context.Context) (string, error) { - return k.k8sRef.Version, nil -} - -func (k *k8sOpenAPISource) Resources(ctx context.Context) (afero.Fs, error) { - if k.fs != nil { - return k.fs, nil - } - - // Download the OpenAPI v3 spec from the Kubernetes repo - specURL := fmt.Sprintf("https://raw.githubusercontent.com/kubernetes/kubernetes/v%s/api/openapi-spec/v3/api__v1_openapi.json", strings.TrimPrefix(k.k8sRef.Version, "v")) - req, err := http.NewRequestWithContext(ctx, http.MethodGet, specURL, nil) - if err != nil { - return nil, errors.Wrap(err, "failed to create request") - } - - resp, err := k.client.Do(req) - if err != nil { - return nil, errors.Wrap(err, "failed to fetch Kubernetes OpenAPI spec") - } - defer resp.Body.Close() //nolint:errcheck // nothing to do - - if resp.StatusCode != http.StatusOK { - return nil, errors.Errorf("failed to download Kubernetes OpenAPI spec: HTTP %d", resp.StatusCode) - } - - content, err := io.ReadAll(io.LimitReader(resp.Body, maxHTTPSize)) - if err != nil { - return nil, errors.Wrap(err, "failed to read Kubernetes OpenAPI spec") - } - - resultFS := afero.NewMemMapFs() - if err := afero.WriteFile(resultFS, "api__v1_openapi.json", content, 0o644); err != nil { - return nil, errors.Wrap(err, "failed to write OpenAPI spec") - } - - // Also try to download the apis specs - apisGroups := []string{ - "apis__apps__v1", - "apis__autoscaling__v1", - "apis__batch__v1", - "apis__networking.k8s.io__v1", - "apis__policy__v1", - "apis__rbac.authorization.k8s.io__v1", - "apis__storage.k8s.io__v1", - } - - for _, group := range apisGroups { - groupURL := fmt.Sprintf("https://raw.githubusercontent.com/kubernetes/kubernetes/v%s/api/openapi-spec/v3/%s_openapi.json", strings.TrimPrefix(k.k8sRef.Version, "v"), group) - groupReq, err := http.NewRequestWithContext(ctx, http.MethodGet, groupURL, nil) - if err != nil { - continue - } - - groupResp, err := k.client.Do(groupReq) - if err != nil { - continue - } - - if groupResp.StatusCode == http.StatusOK { - groupContent, err := io.ReadAll(io.LimitReader(groupResp.Body, maxHTTPSize)) - if err == nil { - _ = afero.WriteFile(resultFS, group+"_openapi.json", groupContent, 0o644) - } - } - _ = groupResp.Body.Close() - } - - k.fs = resultFS - return k.fs, nil -} - -func (k *k8sOpenAPISource) Type() SourceType { - return SourceTypeOpenAPI -} - -// NewK8sSource returns a source for Kubernetes built-in APIs. +// NewK8sSource returns a source for Kubernetes built-in APIs. The OpenAPI +// specs are taken from a sparse checkout of kubernetes/kubernetes at the +// requested version, which carries the complete spec set: every API group +// and version, not just a fixed list. The checkout is shallow and limited to +// api/openapi-spec. func NewK8sSource(dep v1alpha1.Dependency) Source { - return &k8sOpenAPISource{ - k8sRef: dep.K8s, - client: &http.Client{ - Timeout: defaultHTTPTimeout, + return NewGitSource(k8sAsGitDependency(dep), &git.DefaultCloner{}, &git.HTTPSAuthProvider{}) +} + +// k8sAsGitDependency converts a k8s dependency to the git dependency its +// specs are fetched through. The dependency keeps its k8s type, so the git +// source treats the files as OpenAPI documents. +func k8sAsGitDependency(dep v1alpha1.Dependency) v1alpha1.Dependency { + version := dep.K8s.Version + if !strings.HasPrefix(version, "v") { + version = "v" + version + } + return v1alpha1.Dependency{ + Type: v1alpha1.DependencyTypeK8s, + K8s: dep.K8s, + Git: &v1alpha1.GitDependency{ + Repository: "https://github.com/kubernetes/kubernetes", + Ref: version, + Path: "api/openapi-spec", }, } } From 1592420329026f8a72185c55e79272fcab31d756 Mon Sep 17 00:00:00 2001 From: Christopher Haar Date: Sat, 21 Nov 2026 11:49:06 +0100 Subject: [PATCH 2/2] Keep the k8s source's lock identity off the git fetch Address review feedback: NewK8sSource returning the git source directly made Version clone kubernetes/kubernetes before the schema lock comparison - network access and a format change (git:// entries) for a dependency whose schemas may already be up to date. Restore k8sOpenAPISource as the source the manager sees: ID stays k8s://, Version returns the configured version without network access, and only Resources delegates to the git-backed checkout. Also stop the git clone retry loop when the context is cancelled, so a dependency group that already failed doesn't keep fetching. Signed-off-by: Christopher Haar --- internal/schemas/manager/source.go | 49 +++++++++-- internal/schemas/manager/source_test.go | 106 ++++++++++++++++++++++++ 2 files changed, 146 insertions(+), 9 deletions(-) diff --git a/internal/schemas/manager/source.go b/internal/schemas/manager/source.go index 280913fb1..838d70e43 100644 --- a/internal/schemas/manager/source.go +++ b/internal/schemas/manager/source.go @@ -206,7 +206,7 @@ func (g *gitSource) Version(ctx context.Context) (string, error) { return g.commitSHA, nil } -func (g *gitSource) Resources(_ context.Context) (afero.Fs, error) { +func (g *gitSource) Resources(ctx context.Context) (afero.Fs, error) { if g.fs != nil { return g.fs, nil } @@ -217,6 +217,12 @@ func (g *gitSource) Resources(_ context.Context) (afero.Fs, error) { var lastErr error for attempt := 1; attempt <= maxCloneAttempts; attempt++ { + // When fetching several dependencies, another one failing cancels + // the group's context; don't keep retrying a doomed fetch. + if err := ctx.Err(); err != nil { + return nil, errors.Wrap(err, "clone cancelled") + } + memFS = memfs.New() headRef, err := g.cloner.CloneRepository( @@ -432,15 +438,40 @@ func NewHTTPSource(dep v1alpha1.Dependency) Source { } } -// k8sOpenAPISource is a source that generates OpenAPI schemas for Kubernetes -// built-in APIs by downloading the swagger spec. -// NewK8sSource returns a source for Kubernetes built-in APIs. The OpenAPI -// specs are taken from a sparse checkout of kubernetes/kubernetes at the -// requested version, which carries the complete spec set: every API group -// and version, not just a fixed list. The checkout is shallow and limited to -// api/openapi-spec. +// k8sOpenAPISource is a source for the OpenAPI specs of Kubernetes built-in +// APIs. The specs are taken from a shallow checkout of kubernetes/kubernetes +// at the requested version, limited to api/openapi-spec, which carries the +// complete spec set: every API group and version, not just a fixed list. The +// checkout is an implementation detail of Resources: the source keeps its +// k8s:// identity and its configured version, so schema lock entries don't +// change and comparing against the lock needs no network access. +type k8sOpenAPISource struct { + k8sRef *v1alpha1.K8sDependency + git Source +} + +func (k *k8sOpenAPISource) ID() string { + return fmt.Sprintf("k8s://%s", k.k8sRef.Version) +} + +func (k *k8sOpenAPISource) Version(_ context.Context) (string, error) { + return k.k8sRef.Version, nil +} + +func (k *k8sOpenAPISource) Resources(ctx context.Context) (afero.Fs, error) { + return k.git.Resources(ctx) +} + +func (k *k8sOpenAPISource) Type() SourceType { + return SourceTypeOpenAPI +} + +// NewK8sSource returns a source for Kubernetes built-in APIs. func NewK8sSource(dep v1alpha1.Dependency) Source { - return NewGitSource(k8sAsGitDependency(dep), &git.DefaultCloner{}, &git.HTTPSAuthProvider{}) + return &k8sOpenAPISource{ + k8sRef: dep.K8s, + git: NewGitSource(k8sAsGitDependency(dep), &git.DefaultCloner{}, &git.HTTPSAuthProvider{}), + } } // k8sAsGitDependency converts a k8s dependency to the git dependency its diff --git a/internal/schemas/manager/source_test.go b/internal/schemas/manager/source_test.go index 387cfe75a..956a39c8e 100644 --- a/internal/schemas/manager/source_test.go +++ b/internal/schemas/manager/source_test.go @@ -17,6 +17,9 @@ limitations under the License. package manager import ( + "context" + "errors" + "strings" "testing" "github.com/go-git/go-billy/v5" @@ -680,3 +683,106 @@ func TestCalculateFilesystemHashDifferentContent(t *testing.T) { }) } } + +// TestK8sSource pins the k8s source's contract: the git checkout backing +// Resources is an implementation detail, so the source's identity and +// version — what the schema lock records and what lock comparison reads — +// stay those of the k8s dependency and need no network access. +func TestK8sSource(t *testing.T) { + t.Parallel() + + dep := v1alpha1.Dependency{ + Type: v1alpha1.DependencyTypeK8s, + K8s: &v1alpha1.K8sDependency{Version: "1.34.0"}, + } + + t.Run("LockIdentityWithoutNetwork", func(t *testing.T) { + t.Parallel() + + // The cloner must stay untouched: Version runs before the lock + // comparison, so a clone here would fetch kubernetes/kubernetes + // even when the recorded revision already matches. + cloner := &mockCloner{} + src := &k8sOpenAPISource{ + k8sRef: dep.K8s, + git: NewGitSource(k8sAsGitDependency(dep), cloner, &mockAuthProvider{}), + } + + if got, want := src.ID(), "k8s://1.34.0"; got != want { + t.Errorf("ID() = %q, want %q", got, want) + } + version, err := src.Version(t.Context()) + if err != nil { + t.Fatalf("Version() failed: %v", err) + } + if got, want := version, "1.34.0"; got != want { + t.Errorf("Version() = %q, want %q", got, want) + } + if got := src.Type(); got != SourceTypeOpenAPI { + t.Errorf("Type() = %q, want %q", got, SourceTypeOpenAPI) + } + if cloner.cloneCalls != 0 { + t.Errorf("ID/Version/Type cloned %d times, want no clones", cloner.cloneCalls) + } + }) + + t.Run("ResourcesDelegateToGit", func(t *testing.T) { + t.Parallel() + + cloner := &mockCloner{ref: plumbing.NewHashReference("refs/tags/v1.34.0", plumbing.NewHash("abc123def456789012345678901234567890abcd"))} + src := &k8sOpenAPISource{ + k8sRef: dep.K8s, + git: NewGitSource(k8sAsGitDependency(dep), cloner, &mockAuthProvider{}), + } + + fs, err := src.Resources(t.Context()) + if err != nil { + t.Fatalf("Resources() failed: %v", err) + } + if fs == nil { + t.Fatal("Resources() returned no filesystem") + } + if cloner.cloneCalls != 1 { + t.Errorf("Resources() cloned %d times, want 1", cloner.cloneCalls) + } + }) + + t.Run("GitDependencyShape", func(t *testing.T) { + t.Parallel() + + gitDep := k8sAsGitDependency(dep) + if got, want := gitDep.Git.Repository, "https://github.com/kubernetes/kubernetes"; got != want { + t.Errorf("Repository = %q, want %q", got, want) + } + if got, want := gitDep.Git.Ref, "v1.34.0"; got != want { + t.Errorf("Ref = %q, want %q (the configured version always gains a v prefix)", got, want) + } + if got, want := gitDep.Git.Path, "api/openapi-spec"; got != want { + t.Errorf("Path = %q, want %q", got, want) + } + }) +} + +// TestGitSourceResourcesCancelled verifies a cancelled context stops the +// clone retry loop: when fetching several dependencies, another one failing +// cancels the group's context, and a doomed fetch must not keep retrying. +func TestGitSourceResourcesCancelled(t *testing.T) { + t.Parallel() + + cloner := &mockCloner{cloneErr: errors.New("network down")} + src := NewGitSource(v1alpha1.Dependency{ + Type: v1alpha1.DependencyTypeCRD, + Git: &v1alpha1.GitDependency{Repository: "https://example.org/repo", Ref: "main"}, + }, cloner, &mockAuthProvider{}) + + ctx, cancel := context.WithCancel(t.Context()) + cancel() + + _, err := src.Resources(ctx) + if err == nil || !strings.Contains(err.Error(), "clone cancelled") { + t.Fatalf("Resources() with cancelled context: want clone cancelled error, got %v", err) + } + if cloner.cloneCalls != 0 { + t.Errorf("Resources() cloned %d times after cancellation, want none", cloner.cloneCalls) + } +}