Skip to content

Commit 1f72257

Browse files
authored
Populate package-path metadata into package context (#3596)
We construct a path based on the hierarchy of parent packages. This is useful for packages/package functions that want to construct values meaningful beyond just their package - for example domain names, GCP project IDs, bucket names, descriptions etc.
1 parent 1db5254 commit 1f72257

File tree

8 files changed

+201
-46
lines changed

8 files changed

+201
-46
lines changed

internal/builtins/pkg_context.go

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ import (
3030

3131
const (
3232
PkgContextFile = "package-context.yaml"
33-
pkgContextName = "kptfile.kpt.dev"
33+
PkgContextName = "kptfile.kpt.dev"
34+
35+
ConfigKeyPackagePath = "package-path"
3436
)
3537

3638
var (
@@ -42,7 +44,17 @@ var (
4244
// a KRM object that contains package context information that can be
4345
// used by functions such as `set-namespace` to customize package with
4446
// 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+
}
4658

4759
// Run function reads the function input `resourceList` from a given reader `r`
4860
// and writes the function output to the provided writer `w`.
@@ -66,14 +78,14 @@ func (pc *PackageContextGenerator) Process(resourceList *framework.ResourceList)
6678
// - Generates a package context resource for each kpt package (i.e Kptfile)
6779
for _, resource := range resourceList.Items {
6880
gvk := resid.GvkFromNode(resource)
69-
if gvk.Equals(configMapGVK) && resource.GetName() == pkgContextName {
81+
if gvk.Equals(configMapGVK) && resource.GetName() == PkgContextName {
7082
// drop existing package context resources
7183
continue
7284
}
7385
updatedResources = append(updatedResources, resource)
7486
if gvk.Equals(kptfileGVK) {
7587
// it's a Kptfile, generate a corresponding package context
76-
pkgContext, err := pkgContextResource(resource)
88+
pkgContext, err := pkgContextResource(resource, pc.PackageConfig)
7789
if err != nil {
7890
resourceList.Results = framework.Results{
7991
&framework.Result{
@@ -102,10 +114,10 @@ func (pc *PackageContextGenerator) Process(resourceList *framework.ResourceList)
102114

103115
// pkgContextResource generates package context resource from a given
104116
// 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) {
106118
cm := yaml.MustParse(AbstractPkgContext())
107119

108-
kptfilePath, _, err := kioutil.GetFileAnnotations(kf)
120+
kptfilePath, _, err := kioutil.GetFileAnnotations(kptfile)
109121
if err != nil {
110122
return nil, err
111123
}
@@ -118,9 +130,16 @@ func pkgContextResource(kf *yaml.RNode) (*yaml.RNode, error) {
118130
return nil, err
119131
}
120132
}
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)
124143
return cm, nil
125144
}
126145

@@ -136,5 +155,5 @@ metadata:
136155
config.kubernetes.io/local-config: "true"
137156
data:
138157
name: example
139-
`, pkgContextName)
158+
`, PkgContextName)
140159
}

porch/pkg/engine/builtin.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,13 @@ type builtinEvalMutation struct {
3333
runner fn.FunctionRunner
3434
}
3535

36-
func newBuiltinFunctionMutation(function string) (mutation, error) {
37-
var runner fn.FunctionRunner
38-
switch function {
39-
case fnruntime.FuncGenPkgContext:
40-
runner = &builtins.PackageContextGenerator{}
41-
default:
42-
return nil, fmt.Errorf("unrecognized built-in function %q", function)
36+
func newPackageContextGeneratorMutation(packageConfig *builtins.PackageConfig) (mutation, error) {
37+
runner := &builtins.PackageContextGenerator{
38+
PackageConfig: packageConfig,
4339
}
4440

4541
return &builtinEvalMutation{
46-
function: function,
42+
function: fnruntime.FuncGenPkgContext,
4743
runner: runner,
4844
}, nil
4945
}

porch/pkg/engine/builtin_test.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,14 @@ import (
2020
"path/filepath"
2121
"testing"
2222

23-
"github.com/GoogleContainerTools/kpt/internal/fnruntime"
23+
"github.com/GoogleContainerTools/kpt/internal/builtins"
2424
"github.com/google/go-cmp/cmp"
2525
)
2626

2727
const (
2828
updateGoldenFiles = "UPDATE_GOLDEN_FILES"
2929
)
3030

31-
func TestUnknownBuiltinFunctionMutation(t *testing.T) {
32-
const doesNotExist = "function-does-not-exist"
33-
m, err := newBuiltinFunctionMutation(doesNotExist)
34-
if err == nil || m != nil {
35-
t.Errorf("creating builtin function runner for an unknown function %q unexpectedly succeeded", doesNotExist)
36-
}
37-
}
38-
3931
func TestPackageContext(t *testing.T) {
4032
testdata, err := filepath.Abs(filepath.Join(".", "testdata", "context"))
4133
if err != nil {
@@ -44,7 +36,10 @@ func TestPackageContext(t *testing.T) {
4436

4537
input := readPackage(t, filepath.Join(testdata, "input"))
4638

47-
m, err := newBuiltinFunctionMutation(fnruntime.FuncGenPkgContext)
39+
packageConfig := &builtins.PackageConfig{
40+
PackagePath: "parent1/parent1.2/parent1.2.3/me",
41+
}
42+
m, err := newPackageContextGeneratorMutation(packageConfig)
4843
if err != nil {
4944
t.Fatalf("Failed to get builtin function mutation: %v", err)
5045
}

porch/pkg/engine/clone.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"os"
2222
"strings"
2323

24-
"github.com/GoogleContainerTools/kpt/internal/fnruntime"
24+
"github.com/GoogleContainerTools/kpt/internal/builtins"
2525
v1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
2626
api "github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1"
2727
configapi "github.com/GoogleContainerTools/kpt/porch/api/porchconfig/v1alpha1"
@@ -44,6 +44,9 @@ type clonePackageMutation struct {
4444
repoOpener RepositoryOpener
4545
credentialResolver repository.CredentialResolver
4646
referenceResolver ReferenceResolver
47+
48+
// packageConfig contains the package configuration.
49+
packageConfig *builtins.PackageConfig
4750
}
4851

4952
func (m *clonePackageMutation) Apply(ctx context.Context, resources repository.PackageResources) (repository.PackageResources, *api.Task, error) {
@@ -77,13 +80,13 @@ func (m *clonePackageMutation) Apply(ctx context.Context, resources repository.P
7780
if m.isDeployment {
7881
// TODO(droot): executing this as mutation is not really needed, but can be
7982
// refactored once we finalize the task/mutation/commit model.
80-
genPkgContextMutation, err := newBuiltinFunctionMutation(fnruntime.FuncGenPkgContext)
83+
genPkgContextMutation, err := newPackageContextGeneratorMutation(m.packageConfig)
8184
if err != nil {
8285
return repository.PackageResources{}, nil, err
8386
}
8487
cloned, _, err = genPkgContextMutation.Apply(ctx, cloned)
8588
if err != nil {
86-
return repository.PackageResources{}, nil, fmt.Errorf("failed to generate deployment context %w", err)
89+
return repository.PackageResources{}, nil, fmt.Errorf("failed to generate deployment context: %w", err)
8790
}
8891
}
8992

0 commit comments

Comments
 (0)