@@ -30,7 +30,9 @@ import (
30
30
31
31
const (
32
32
PkgContextFile = "package-context.yaml"
33
- pkgContextName = "kptfile.kpt.dev"
33
+ PkgContextName = "kptfile.kpt.dev"
34
+
35
+ ConfigKeyPackagePath = "package-path"
34
36
)
35
37
36
38
var (
42
44
// a KRM object that contains package context information that can be
43
45
// used by functions such as `set-namespace` to customize package with
44
46
// minimal configuration.
45
- type PackageContextGenerator struct {}
47
+ type PackageContextGenerator struct {
48
+ // PackageConfig contains the package configuration to set.
49
+ PackageConfig * PackageConfig
50
+ }
51
+
52
+ // PackageConfig holds package automatic configuration
53
+ type PackageConfig struct {
54
+ // PackagePath is the path to the package, as determined by the names of the parent packages.
55
+ // The path to a package is the parent package path joined with the package name.
56
+ PackagePath string
57
+ }
46
58
47
59
// Run function reads the function input `resourceList` from a given reader `r`
48
60
// and writes the function output to the provided writer `w`.
@@ -66,14 +78,14 @@ func (pc *PackageContextGenerator) Process(resourceList *framework.ResourceList)
66
78
// - Generates a package context resource for each kpt package (i.e Kptfile)
67
79
for _ , resource := range resourceList .Items {
68
80
gvk := resid .GvkFromNode (resource )
69
- if gvk .Equals (configMapGVK ) && resource .GetName () == pkgContextName {
81
+ if gvk .Equals (configMapGVK ) && resource .GetName () == PkgContextName {
70
82
// drop existing package context resources
71
83
continue
72
84
}
73
85
updatedResources = append (updatedResources , resource )
74
86
if gvk .Equals (kptfileGVK ) {
75
87
// it's a Kptfile, generate a corresponding package context
76
- pkgContext , err := pkgContextResource (resource )
88
+ pkgContext , err := pkgContextResource (resource , pc . PackageConfig )
77
89
if err != nil {
78
90
resourceList .Results = framework.Results {
79
91
& framework.Result {
@@ -102,10 +114,10 @@ func (pc *PackageContextGenerator) Process(resourceList *framework.ResourceList)
102
114
103
115
// pkgContextResource generates package context resource from a given
104
116
// Kptfile. The resource is generated adjacent to the Kptfile of the package.
105
- func pkgContextResource (kf * yaml.RNode ) (* yaml.RNode , error ) {
117
+ func pkgContextResource (kptfile * yaml.RNode , packageConfig * PackageConfig ) (* yaml.RNode , error ) {
106
118
cm := yaml .MustParse (AbstractPkgContext ())
107
119
108
- kptfilePath , _ , err := kioutil .GetFileAnnotations (kf )
120
+ kptfilePath , _ , err := kioutil .GetFileAnnotations (kptfile )
109
121
if err != nil {
110
122
return nil , err
111
123
}
@@ -118,9 +130,16 @@ func pkgContextResource(kf *yaml.RNode) (*yaml.RNode, error) {
118
130
return nil , err
119
131
}
120
132
}
121
- cm .SetDataMap (map [string ]string {
122
- "name" : kf .GetName (),
123
- })
133
+ data := map [string ]string {
134
+ "name" : kptfile .GetName (),
135
+ }
136
+ if packageConfig != nil {
137
+ if packageConfig .PackagePath != "" {
138
+ data [ConfigKeyPackagePath ] = packageConfig .PackagePath
139
+ }
140
+ }
141
+
142
+ cm .SetDataMap (data )
124
143
return cm , nil
125
144
}
126
145
@@ -136,5 +155,5 @@ metadata:
136
155
config.kubernetes.io/local-config: "true"
137
156
data:
138
157
name: example
139
- ` , pkgContextName )
158
+ ` , PkgContextName )
140
159
}
0 commit comments