Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
901575a
add shared KRM-validity check and non-KRM pre-scan
Catalin-Stratulat-Ericsson Oct 22, 2026
6850eb1
fn-render-cmd pre-scan for non-KRM files, skip via annotation, record…
Catalin-Stratulat-Ericsson Oct 22, 2026
47513ef
fn-eval: reuse fn render's non-KRM pre-scan
Catalin-Stratulat-Ericsson Oct 22, 2026
fd960be
fn-source: add non-KRM pre-scan (previously had none)
Catalin-Stratulat-Ericsson Oct 22, 2026
fec1368
pkg-update: classify non-KRM files by content, not filename
Catalin-Stratulat-Ericsson Oct 22, 2026
57876e7
live apply/status/destroy/plan: shared non-KRM pre-scan in pkg/live.Load
Catalin-Stratulat-Ericsson Oct 22, 2026
cfc7376
live-init: pre-scan for non-KRM files before namespace detection
Catalin-Stratulat-Ericsson Oct 22, 2026
4ca3f18
live-migrate: unconditional non-KRM pre-scan, annotation not honored
Catalin-Stratulat-Ericsson Oct 22, 2026
462a5ae
docs: document non-KRM pre-scan and auto-ignore-non-krm annotation
Catalin-Stratulat-Ericsson Oct 22, 2026
5896630
Potential fix for pull request finding 'Exclude all documents from fi…
Catalin-Stratulat-Ericsson Oct 22, 2026
4769d54
addressing copilot comment regarding (Restrict root fallback reads to…
Catalin-Stratulat-Ericsson Oct 22, 2026
186d1e8
addressing copilot comment regarding (Avoid writing failure status du…
Catalin-Stratulat-Ericsson Oct 22, 2026
a3da490
addressing copilot comment regarding (Preserve filename exceptions wh…
Catalin-Stratulat-Ericsson Oct 22, 2026
13c6488
addressing copilot comment regarding (Keep whole-file classification …
Catalin-Stratulat-Ericsson Oct 22, 2026
3674242
addressing copilot comment regarding (Apply opt-in per owning package…
Catalin-Stratulat-Ericsson Oct 22, 2026
3b5debc
addressing copilot comment regarding (Retain scan skips for namespace…
Catalin-Stratulat-Ericsson Oct 22, 2026
042865b
addressing copilot comment regarding (Preserve excluded non-KRM files…
Catalin-Stratulat-Ericsson Oct 22, 2026
468ea67
addressing copilot comment regarding (Use shared validator for functi…
Catalin-Stratulat-Ericsson Oct 22, 2026
627b3de
fixing go linting job failure due to reused variables in test
Catalin-Stratulat-Ericsson Oct 22, 2026
eaab59e
trying to patch build failures cli response change
Catalin-Stratulat-Ericsson Oct 22, 2026
0681b34
rebasing with main to resolve merge conflict
Catalin-Stratulat-Ericsson Oct 27, 2026
0ef46d9
adding unit tests for prescan.go + fixing some import pathing from th…
Catalin-Stratulat-Ericsson Oct 27, 2026
d143122
trying to de-duplicate some small bits
Catalin-Stratulat-Ericsson Oct 27, 2026
5620275
removing unnecessary identical input variables from tests
Catalin-Stratulat-Ericsson Oct 28, 2026
0c08ea3
fixing isKrmFile classifying a non-KRM List file as KRM
Catalin-Stratulat-Ericsson Oct 28, 2026
64ceee6
fixing live pre-scan flagging files its real reader never touches
Catalin-Stratulat-Ericsson Oct 29, 2026
e638291
widening IsKRM's name requirement for generateName and local-config
Catalin-Stratulat-Ericsson Oct 29, 2026
700ea78
fixing ambiguous file path in non-KRM pre-scan error messages by usin…
Catalin-Stratulat-Ericsson Oct 29, 2026
9be39ab
fixing attribution writing to files the pre-scan already excused
Catalin-Stratulat-Ericsson Oct 29, 2026
63dda6c
docs: clarify auto-ignore-non-krm doesn't cover malformed YAML
Catalin-Stratulat-Ericsson Oct 29, 2026
b605cad
fixing live init's namespace fallback diverging from .krmignore's
Catalin-Stratulat-Ericsson Oct 29, 2026
83e2688
fixing the live pre-scan's .krmignore scoping to match the real flat …
Catalin-Stratulat-Ericsson Oct 30, 2026
bc07917
merging with main
Catalin-Stratulat-Ericsson Oct 30, 2026
b8627e3
fixing fn eval and fn source walking into ignored subpackages
Catalin-Stratulat-Ericsson Nov 3, 2026
99d20f6
clarifying docs regarding annotation functionality compared to defaul…
Catalin-Stratulat-Ericsson Nov 3, 2026
6b57abc
added helper + removed autoignore.go wrapper to reduce duplication
Catalin-Stratulat-Ericsson Nov 3, 2026
6cfdff7
deduplicating isKustomization by reusing the api module's copy
Catalin-Stratulat-Ericsson Nov 4, 2026
da807dd
fix(krmvalidate): skip DirectSubpackages on .krmignore-excluded dirs
SurbhiAgarwal1 Nov 17, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions api/kptfile/v1/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,11 @@ func AreKRM(nodes []*yaml.RNode) error {
// IsKRM validates if given resource is a valid KRM resource by ensuring
// that resource has a valid apiVersion, kind and metadata.name field.
// It excludes kustomization resource from KRM check.
// It validates function configs, which always need a name. The CLI's
// pipeline-input classifier (pkg/lib/krmvalidate.IsKRM) is deliberately
// more lenient about names and must not be merged with this one.
func IsKRM(n *yaml.RNode) error {
if isKustomization(n) {
if IsKustomization(n) {
// exclude kustomization files from KRM check
// https://github.com/kptdev/kpt/issues/2388
return nil
Expand All @@ -435,8 +438,8 @@ func IsKRM(n *yaml.RNode) error {
return nil
}

// isKustomization determines if given YAML is a kustomization file or resource.
func isKustomization(n *yaml.RNode) bool {
// IsKustomization determines if given YAML is a kustomization file or resource.
func IsKustomization(n *yaml.RNode) bool {
resourcePath, _, err := kioutil.GetFileAnnotations(n)
if err == nil {
// perform the check only if we are able to reliably
Expand Down
2 changes: 1 addition & 1 deletion api/kptfile/v1/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ kind: Kustomization
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
got := isKustomization(yaml.MustParse(tc.input))
got := IsKustomization(yaml.MustParse(tc.input))
if got != tc.exp {
t.Fatalf("got %v expected %v", got, tc.exp)
}
Expand Down
2 changes: 1 addition & 1 deletion cli/alpha/live/plan/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (r *Runner) RunE(c *cobra.Command, args []string) error {

// Load the resources from disk or stdin and extract the
// inventory information.
objs, inv, err := live.Load(r.factory, path, c.InOrStdin())
objs, inv, err := live.Load(r.ctx, r.factory, path, c.InOrStdin())
if err != nil {
return err
}
Expand Down
10 changes: 10 additions & 0 deletions cli/fn/cmdeval/runfn.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
"sigs.k8s.io/kustomize/kyaml/yaml"

"github.com/kptdev/kpt/pkg/lib/krmvalidate"
"github.com/kptdev/kpt/pkg/lib/pkg"
)

Expand Down Expand Up @@ -144,13 +145,22 @@ func (r RunFns) getNodesAndFilters() (
var outputPkg *kio.LocalPackageReadWriter

if r.Path != "" {
// Non-KRM pre-scan, same as `kpt fn render`'s, except it skips
// subpackages a parent .krmignore excludes, like this reader does.
pr := printer.FromContextOrDie(r.Ctx)
scanResult, err := krmvalidate.ScanHonoringKrmignore(filesys.FileSystemOrOnDisk{}, string(r.uniquePath), pr.Printf)
if err != nil {
return nil, nil, nil, err
}

outputPkg = &kio.LocalPackageReadWriter{
PackagePath: string(r.uniquePath),
MatchFilesGlob: pkg.MatchAllKRM,
PreserveSeqIndent: true,
PackageFileName: kptfilev1.KptFileName,
IncludeSubpackages: true,
WrapBareSeqNode: true,
FileSkipFunc: scanResult.SkipFunc(string(r.uniquePath)),
}
}

Expand Down
12 changes: 10 additions & 2 deletions cli/fn/cmdeval/runfn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,17 @@ func TestCmd_Execute_notIncludeMetaResources(t *testing.T) {
dir := setupTest(t)
defer os.RemoveAll(dir)

// write a test filter to the directory of configuration
// Named (unlike the shared ValueReplacerYAMLData constant) so it's
// valid KRM and the non-KRM pre-scan doesn't block this test.
namedValueReplacerYAMLData := `apiVersion: v1
kind: ValueReplacer
metadata:
name: filter
stringMatch: Deployment
replace: StatefulSet
`
if !assert.NoError(t, os.WriteFile(
filepath.Join(dir, "filter.yaml"), []byte(ValueReplacerYAMLData), 0600)) {
filepath.Join(dir, "filter.yaml"), []byte(namedValueReplacerYAMLData), 0600)) {
return
}

Expand Down
2 changes: 1 addition & 1 deletion cli/live/apply/cmdapply.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (r *Runner) runE(c *cobra.Command, args []string) error {
}
}

objs, inv, err := live.Load(r.factory, path, c.InOrStdin())
objs, inv, err := live.Load(r.ctx, r.factory, path, c.InOrStdin())
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cli/live/destroy/cmddestroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (r *Runner) runE(c *cobra.Command, args []string) error {
}
}

_, inv, err := live.Load(r.factory, path, c.InOrStdin())
_, inv, err := live.Load(r.ctx, r.factory, path, c.InOrStdin())
if err != nil {
return err
}
Expand Down
100 changes: 96 additions & 4 deletions cli/live/init/cmdliveinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/kptdev/kpt/internal/docs/generated/livedocs"
"github.com/kptdev/kpt/pkg/kptfile/kptfileutil"
"github.com/kptdev/kpt/pkg/lib/errors"
"github.com/kptdev/kpt/pkg/lib/krmvalidate"
"github.com/kptdev/kpt/pkg/lib/pkg"
"github.com/kptdev/kpt/pkg/lib/util/attribution"
pathutil "github.com/kptdev/kpt/pkg/lib/util/path"
Expand All @@ -38,6 +39,9 @@ import (
k8scmdutil "k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/cli-utils/pkg/config"
"sigs.k8s.io/kustomize/kyaml/filesys"
"sigs.k8s.io/kustomize/kyaml/kio/filters"
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
"sigs.k8s.io/kustomize/kyaml/openapi"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

Expand Down Expand Up @@ -153,10 +157,42 @@ func (c *ConfigureInventoryInfo) Run(ctx context.Context) error {
const op errors.Op = "cmdliveinit.Run"
pr := printer.FromContextOrDie(ctx)

namespace, err := config.FindNamespace(c.Factory.ToRawKubeConfigLoader(), c.Pkg.UniquePath.String())
// Pre-scan for non-KRM files before config.FindNamespace() reads them.
scanResult, err := krmvalidate.ScanFlatAndDecide(filesys.FileSystemOrOnDisk{}, c.Pkg.UniquePath.String(), pr.Printf)
if err != nil {
return errors.E(op, c.Pkg.UniquePath,
fmt.Errorf("failed to determine inventory namespace from package resources: %w; please provide the namespace using the --namespace flag", err))
return errors.E(op, c.Pkg.UniquePath, err)
}

var namespace string
if len(scanResult.SkippedFiles()) > 0 {
// FindNamespace re-reads the package with no skip hook, so it
// would hit the excused files. Replicate its priority order
// instead: explicit --namespace wins, else check agreement.
loaderNamespace, enforceNamespace, nsErr := c.Factory.ToRawKubeConfigLoader().Namespace()
if nsErr != nil {
return errors.E(op, c.Pkg.UniquePath,
fmt.Errorf("failed to determine inventory namespace: %w; please provide the namespace using the --namespace flag", nsErr))
}
namespace = loaderNamespace
if !enforceNamespace {
filtered, ferr := filterSkipped(scanResult.AllResources, scanResult.SkipFunc(c.Pkg.UniquePath.String()))
if ferr != nil {
return errors.E(op, c.Pkg.UniquePath, ferr)
}
ns, ok, aerr := allInSameNamespace(filtered)
if aerr != nil {
return errors.E(op, c.Pkg.UniquePath, aerr)
}
if ok {
namespace = ns
}
}
} else {
namespace, err = config.FindNamespace(c.Factory.ToRawKubeConfigLoader(), c.Pkg.UniquePath.String())
if err != nil {
return errors.E(op, c.Pkg.UniquePath,
fmt.Errorf("failed to determine inventory namespace from package resources: %w; please provide the namespace using the --namespace flag", err))
}
}
namespace = strings.TrimSpace(namespace)
if !c.Quiet {
Expand Down Expand Up @@ -214,11 +250,67 @@ func (c *ConfigureInventoryInfo) Run(ctx context.Context) error {
}
// add metrics annotation to package resources to track the usage as the resources
// will be applied using kpt live group
at := attribution.Attributor{PackagePaths: []string{c.Pkg.UniquePath.String()}, CmdGroup: "live"}
at := attribution.Attributor{
PackagePaths: []string{c.Pkg.UniquePath.String()},
CmdGroup: "live",
FileSkipFunc: scanResult.SkipFunc(c.Pkg.UniquePath.String()),
}
at.Process()
return nil
}

// allInSameNamespace is kpt's own copy of cli-utils's unexported
// config.allInSameNamespace, operating on an already-filtered
// resource list instead of re-reading the directory itself.
func allInSameNamespace(nodes []*yaml.RNode) (string, bool, error) {
nodes, err := (&filters.IsLocalConfig{}).Filter(nodes)
if err != nil {
return "", false, err
}

var ns string
for _, node := range nodes {
rm, err := node.GetMeta()
if err != nil {
return "", false, err
}
if namespaced, found := openapi.IsNamespaceScoped(rm.TypeMeta); found && !namespaced {
continue
}
if rm.Namespace == "" {
return "", false, nil
}
if ns == "" {
ns = rm.Namespace
} else if rm.Namespace != ns {
return "", false, nil
}
}
if ns == "" {
return "", false, nil
}
return ns, true, nil
}

// filterSkipped drops any node skip reports true for, by its
// kioutil.PathAnnotation. A nil skip leaves nodes unchanged.
func filterSkipped(nodes []*yaml.RNode, skip func(relPath string) bool) ([]*yaml.RNode, error) {
if skip == nil {
return nodes, nil
}
var out []*yaml.RNode
for _, n := range nodes {
path, _, err := kioutil.GetFileAnnotations(n)
if err != nil {
return nil, err
}
if !skip(path) {
out = append(out, n)
}
}
return out, nil
}

// createRGFile fills in the inventory object values into the resourcegroup object and writes to file storage.
func createRGFile(p *pkg.Pkg, inv *kptfilev1.Inventory, filename string, force bool) error {
const op errors.Op = "cmdliveinit.createRGFile"
Expand Down
Loading
Loading