Skip to content

Commit fb987af

Browse files
author
NETIZEN-11
committed
Fix GitHub Issue kptdev#4333: Improve handling of package context in subpackages
- Add isRootKptfile() helper function for robust root detection - Enhance pkgContextResource() to generate package-context.yaml only for root packages - Subpackages are now correctly suppressed to prevent duplicate ConfigMap creation - Fix path normalization to handle relative paths like './Kptfile' - Ensure only one ConfigMap 'kptfile.kpt.dev' exists in mutation pipeline This resolves the 'ConfigMap already exists' error when rendering packages with subpackages.
1 parent 45cf0a1 commit fb987af

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

internal/builtins/pkg_context.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ var (
3434
kptfileGVK = resid.NewGvk(kptfilev1.KptFileGVK().Group, kptfilev1.KptFileGVK().Version, kptfilev1.KptFileGVK().Kind)
3535
)
3636

37+
// isRootKptfile checks if the given path represents a root Kptfile
38+
func isRootKptfile(kptfilePath string) bool {
39+
cleanPath := path.Clean(kptfilePath)
40+
base := path.Base(cleanPath)
41+
return base == kptfilev1.KptFileGVK().Kind
42+
}
43+
3744
// PackageContextGenerator is a built-in KRM function that generates
3845
// a KRM object that contains package context information that can be
3946
// used by functions such as `set-namespace` to customize package with
@@ -115,13 +122,23 @@ func pkgContextResource(kptfile *yaml.RNode, packageConfig *builtintypes.Package
115122
return nil, err
116123
}
117124

118-
// We only want one "package-context.yaml" in each kpt package
119-
if kptfilePath != kptfilev1.KptFileGVK().Kind {
125+
// We only want one "package-context.yaml" in the root kpt package
126+
// Root package has Kptfile at the root path (no subdirectories), while subpackages
127+
// will have paths like "subpkg/Kptfile"
128+
// Normalize the path first to handle relative paths like "./Kptfile"
129+
cleanPath := path.Clean(kptfilePath)
130+
if !isRootKptfile(cleanPath) {
131+
return nil, nil
132+
}
133+
134+
// Check if this is the root package by verifying the Kptfile is at the root level
135+
if path.Dir(cleanPath) != "." {
136+
// This is a subpackage, don't generate package context
120137
return nil, nil
121138
}
122139

123140
annotations := map[string]string{
124-
kioutil.PathAnnotation: path.Join(path.Dir(kptfilePath), builtintypes.PkgContextFile),
141+
kioutil.PathAnnotation: path.Join(path.Dir(cleanPath), builtintypes.PkgContextFile),
125142
}
126143

127144
for k, v := range annotations {

0 commit comments

Comments
 (0)