@@ -28,6 +28,7 @@ import (
2828 "github.com/kptdev/kpt/internal/docs/generated/livedocs"
2929 "github.com/kptdev/kpt/pkg/kptfile/kptfileutil"
3030 "github.com/kptdev/kpt/pkg/lib/errors"
31+ "github.com/kptdev/kpt/pkg/lib/krmvalidate"
3132 "github.com/kptdev/kpt/pkg/lib/pkg"
3233 "github.com/kptdev/kpt/pkg/lib/util/attribution"
3334 pathutil "github.com/kptdev/kpt/pkg/lib/util/path"
@@ -38,6 +39,9 @@ import (
3839 k8scmdutil "k8s.io/kubectl/pkg/cmd/util"
3940 "sigs.k8s.io/cli-utils/pkg/config"
4041 "sigs.k8s.io/kustomize/kyaml/filesys"
42+ "sigs.k8s.io/kustomize/kyaml/kio/filters"
43+ "sigs.k8s.io/kustomize/kyaml/kio/kioutil"
44+ "sigs.k8s.io/kustomize/kyaml/openapi"
4145 "sigs.k8s.io/kustomize/kyaml/yaml"
4246)
4347
@@ -153,10 +157,42 @@ func (c *ConfigureInventoryInfo) Run(ctx context.Context) error {
153157 const op errors.Op = "cmdliveinit.Run"
154158 pr := printer .FromContextOrDie (ctx )
155159
156- namespace , err := config .FindNamespace (c .Factory .ToRawKubeConfigLoader (), c .Pkg .UniquePath .String ())
160+ // Pre-scan for non-KRM files before config.FindNamespace() reads them.
161+ scanResult , err := krmvalidate .ScanFlatAndDecide (filesys.FileSystemOrOnDisk {}, c .Pkg .UniquePath .String (), pr .Printf )
157162 if err != nil {
158- return errors .E (op , c .Pkg .UniquePath ,
159- fmt .Errorf ("failed to determine inventory namespace from package resources: %w; please provide the namespace using the --namespace flag" , err ))
163+ return errors .E (op , c .Pkg .UniquePath , err )
164+ }
165+
166+ var namespace string
167+ if len (scanResult .SkippedFiles ()) > 0 {
168+ // FindNamespace re-reads the package with no skip hook, so it
169+ // would hit the excused files. Replicate its priority order
170+ // instead: explicit --namespace wins, else check agreement.
171+ loaderNamespace , enforceNamespace , nsErr := c .Factory .ToRawKubeConfigLoader ().Namespace ()
172+ if nsErr != nil {
173+ return errors .E (op , c .Pkg .UniquePath ,
174+ fmt .Errorf ("failed to determine inventory namespace: %w; please provide the namespace using the --namespace flag" , nsErr ))
175+ }
176+ namespace = loaderNamespace
177+ if ! enforceNamespace {
178+ filtered , ferr := filterSkipped (scanResult .AllResources , scanResult .SkipFunc (c .Pkg .UniquePath .String ()))
179+ if ferr != nil {
180+ return errors .E (op , c .Pkg .UniquePath , ferr )
181+ }
182+ ns , ok , aerr := allInSameNamespace (filtered )
183+ if aerr != nil {
184+ return errors .E (op , c .Pkg .UniquePath , aerr )
185+ }
186+ if ok {
187+ namespace = ns
188+ }
189+ }
190+ } else {
191+ namespace , err = config .FindNamespace (c .Factory .ToRawKubeConfigLoader (), c .Pkg .UniquePath .String ())
192+ if err != nil {
193+ return errors .E (op , c .Pkg .UniquePath ,
194+ fmt .Errorf ("failed to determine inventory namespace from package resources: %w; please provide the namespace using the --namespace flag" , err ))
195+ }
160196 }
161197 namespace = strings .TrimSpace (namespace )
162198 if ! c .Quiet {
@@ -214,11 +250,67 @@ func (c *ConfigureInventoryInfo) Run(ctx context.Context) error {
214250 }
215251 // add metrics annotation to package resources to track the usage as the resources
216252 // will be applied using kpt live group
217- at := attribution.Attributor {PackagePaths : []string {c .Pkg .UniquePath .String ()}, CmdGroup : "live" }
253+ at := attribution.Attributor {
254+ PackagePaths : []string {c .Pkg .UniquePath .String ()},
255+ CmdGroup : "live" ,
256+ FileSkipFunc : scanResult .SkipFunc (c .Pkg .UniquePath .String ()),
257+ }
218258 at .Process ()
219259 return nil
220260}
221261
262+ // allInSameNamespace is kpt's own copy of cli-utils's unexported
263+ // config.allInSameNamespace, operating on an already-filtered
264+ // resource list instead of re-reading the directory itself.
265+ func allInSameNamespace (nodes []* yaml.RNode ) (string , bool , error ) {
266+ nodes , err := (& filters.IsLocalConfig {}).Filter (nodes )
267+ if err != nil {
268+ return "" , false , err
269+ }
270+
271+ var ns string
272+ for _ , node := range nodes {
273+ rm , err := node .GetMeta ()
274+ if err != nil {
275+ return "" , false , err
276+ }
277+ if namespaced , found := openapi .IsNamespaceScoped (rm .TypeMeta ); found && ! namespaced {
278+ continue
279+ }
280+ if rm .Namespace == "" {
281+ return "" , false , nil
282+ }
283+ if ns == "" {
284+ ns = rm .Namespace
285+ } else if rm .Namespace != ns {
286+ return "" , false , nil
287+ }
288+ }
289+ if ns == "" {
290+ return "" , false , nil
291+ }
292+ return ns , true , nil
293+ }
294+
295+ // filterSkipped drops any node skip reports true for, by its
296+ // kioutil.PathAnnotation. A nil skip leaves nodes unchanged.
297+ func filterSkipped (nodes []* yaml.RNode , skip func (relPath string ) bool ) ([]* yaml.RNode , error ) {
298+ if skip == nil {
299+ return nodes , nil
300+ }
301+ var out []* yaml.RNode
302+ for _ , n := range nodes {
303+ path , _ , err := kioutil .GetFileAnnotations (n )
304+ if err != nil {
305+ return nil , err
306+ }
307+ if ! skip (path ) {
308+ out = append (out , n )
309+ }
310+ }
311+ return out , nil
312+ }
313+
222314// createRGFile fills in the inventory object values into the resourcegroup object and writes to file storage.
223315func createRGFile (p * pkg.Pkg , inv * kptfilev1.Inventory , filename string , force bool ) error {
224316 const op errors.Op = "cmdliveinit.createRGFile"
0 commit comments