Skip to content

Commit 30682e3

Browse files
committed
Support wildecard match; refactor code in attempt to build an authoring pattern.
1 parent af72c63 commit 30682e3

File tree

327 files changed

+58362
-280
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

327 files changed

+58362
-280
lines changed

functions/go/gcp-set-project-id/fieldspec/annotation.go renamed to functions/go/gcp-set-project-id/consts/annotation.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package fieldspec
1+
package consts
22

33
// annotationFieldSpecs update the KRM resources' annotations whose field spec matches the path.
44
// This requires the annotation to already exist in the KRM resource, which is different from kustomize's
55
// commonAnnotations that creates the annotation if not exist.
6-
const annotationFieldSpecs = `
7-
commonAnnotations:
6+
const AnnotationFieldSpecs = `
7+
annotations:
88
- path: metadata/annotations
99
1010
- path: spec/template/metadata/annotations

functions/go/gcp-set-project-id/fieldspec/project.go renamed to functions/go/gcp-set-project-id/consts/project.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
1-
package fieldspec
1+
package consts
22

3-
/* projectIDFieldSpecs contains the fieldSpec paths of Google Cloud ProjectID shown in
4-
blueprints spanner, bucket and redis.
5-
*/
3+
// ProjectFieldSpecs contains the fieldSpec paths of Google Cloud ProjectID
4+
const ProjectFieldSpecs = `
5+
projectFieldSpec:
6+
# Blueprint redis-bucket
7+
- path: spec/authorizedNetworkRef/external
8+
regexPattern: (?P<prefix>projects\/)(?P<projectID>\S+)(?P<suffix>\/global\/networks\/default)
9+
group: redis.cnrm.cloud.google.com
10+
version: v1beta1
11+
kind: RedisInstance
612
7-
const (
8-
ProjectIDFieldSpecs = `
9-
projectID:
10-
# Cluster blueprint
11-
- path: spec/resourceRef/external
13+
# Blueprint cluster
14+
- path: spec/resourceRef/external
1215
group: iam.cnrm.cloud.google.com
1316
version: v1beta1
1417
kind: IAMPolicyMember
1518
16-
# Cluster blueprint
19+
# Blueprint cluster
1720
- path: spec/projectRef/external
1821
group: gkehub.cnrm.cloud.google.com
1922
version: v1beta1
2023
kind: GKEHubFeatureMembership
2124
`
22-
)
2325

2426
/*
2527
# Test-Only
@@ -28,7 +30,7 @@ version: v1
2830
group: apps
2931
kind: Deployment
3032
create: true
31-
*/
33+
*/
3234

3335
/* UNSUPPORTED Project FieldSpec due to substitution/partial-setter
3436
- path: metadata/annotations[cnrm.cloud.google.com/project-id]
@@ -79,4 +81,4 @@ create: true
7981
version: v1beta1
8082
kind: GKEHubFeatureMembership
8183
create: true
82-
*/
84+
*/
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package custom
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
7+
"sigs.k8s.io/kustomize/api/filters/fieldspec"
8+
"sigs.k8s.io/kustomize/api/filters/filtersutil"
9+
"sigs.k8s.io/kustomize/api/types"
10+
"sigs.k8s.io/kustomize/kyaml/kio"
11+
"sigs.k8s.io/kustomize/kyaml/yaml"
12+
)
13+
14+
var _ kio.Filter = Filter{}
15+
16+
type Filter struct {
17+
ProjectID string
18+
FsSlice []types.FieldSpec
19+
}
20+
21+
func (f Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
22+
_, err := kio.FilterAll(yaml.FilterFunc(
23+
func(node *yaml.RNode) (*yaml.RNode, error) {
24+
var fns []yaml.Filter
25+
for _, fs := range f.FsSlice {
26+
27+
fn := fieldspec.Filter{
28+
SetValue: f.updateProjectIDFn(fs.RegexPattern),
29+
FieldSpec: fs,
30+
}
31+
fns = append(fns, fn)
32+
}
33+
return node.Pipe(fns...)
34+
})).Filter(nodes)
35+
return nodes, err
36+
}
37+
38+
func (f Filter) updateProjectIDFn(regexPath string) filtersutil.SetFn {
39+
return func(node *yaml.RNode) (err error) {
40+
if regexPath == "" {
41+
return node.PipeE(updater{ProjectID: f.ProjectID})
42+
}
43+
defer func() {
44+
// recover from regex panic.
45+
if recover() != nil {
46+
err = fmt.Errorf("invalid regex pattern %v", regexPath)
47+
}
48+
}()
49+
re := regexp.MustCompile(regexPath)
50+
match := re.FindStringSubmatch(node.YNode().Value)
51+
namedGroup := make(map[string]string)
52+
for i, name := range re.SubexpNames() {
53+
if i != 0 && name != "" {
54+
namedGroup[name] = match[i]
55+
}
56+
}
57+
newProjectID := namedGroup["prefix"] + f.ProjectID + namedGroup["suffix"]
58+
return node.PipeE(updater{ProjectID: newProjectID})
59+
}
60+
}
61+
62+
type updater struct {
63+
ProjectID string
64+
}
65+
66+
func (u updater) Filter(rn *yaml.RNode) (*yaml.RNode, error) {
67+
return rn.Pipe(yaml.FieldSetter{StringValue: u.ProjectID})
68+
}

functions/go/gcp-set-project-id/go.mod

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
module github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/set-project-id
1+
module github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/gcp-set-project-id
22

33
go 1.17
44

55
require (
6-
sigs.k8s.io/kustomize/api v0.10.2-0.20211202184144-fe551be87b8d
7-
sigs.k8s.io/kustomize/kyaml v0.13.1-0.20211202184144-fe551be87b8d
8-
sigs.k8s.io/yaml v1.2.0
6+
sigs.k8s.io/kustomize/api v0.11.1
7+
sigs.k8s.io/kustomize/kyaml v0.13.3
8+
sigs.k8s.io/yaml v1.3.0
99
)
1010

1111
require (
@@ -31,3 +31,5 @@ require (
3131
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
3232
k8s.io/kube-openapi v0.0.0-20210421082810-95288971da7e // indirect
3333
)
34+
35+
replace sigs.k8s.io/kustomize/api => ./thirdparty/kustomize/api

functions/go/gcp-set-project-id/go.sum

Lines changed: 9 additions & 84 deletions
Large diffs are not rendered by default.

functions/go/gcp-set-project-id/plugin.go

Lines changed: 0 additions & 101 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package plugins
2+
3+
import (
4+
"sigs.k8s.io/kustomize/api/filters/annotations"
5+
"sigs.k8s.io/kustomize/api/resmap"
6+
"sigs.k8s.io/kustomize/api/types"
7+
"sigs.k8s.io/yaml"
8+
)
9+
10+
type AnnotationPlugin struct {
11+
Annotations map[string]string
12+
FieldSpecs []types.FieldSpec `json:"annotations,omitempty" yaml:"annotations,omitempty"`
13+
}
14+
15+
func (p *AnnotationPlugin) Config(
16+
_ *resmap.PluginHelpers, c []byte) (err error) {
17+
p.Annotations = nil
18+
p.FieldSpecs = nil
19+
return yaml.Unmarshal(c, p)
20+
}
21+
22+
func (p *AnnotationPlugin) Transform(m resmap.ResMap) error {
23+
return m.ApplyFilter(annotations.Filter{
24+
Annotations: p.Annotations,
25+
FsSlice: p.FieldSpecs,
26+
})
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package plugins
2+
3+
import (
4+
"github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/gcp-set-project-id/custom"
5+
"sigs.k8s.io/kustomize/api/resmap"
6+
"sigs.k8s.io/kustomize/api/types"
7+
"sigs.k8s.io/yaml"
8+
)
9+
10+
type CustomFieldSpecPlugin struct {
11+
ProjectID string
12+
FsSlice []types.FieldSpec `json:"projectFieldSpec,omitempty" yaml:"projectFieldSpec,omitempty"`
13+
}
14+
15+
func (f *CustomFieldSpecPlugin) Config(c []byte) error {
16+
f.FsSlice = nil
17+
return yaml.Unmarshal(c, f)
18+
}
19+
20+
func (f *CustomFieldSpecPlugin) Transform(m resmap.ResMap) error {
21+
return m.ApplyFilter(custom.Filter{
22+
ProjectID: f.ProjectID,
23+
FsSlice: f.FsSlice,
24+
})
25+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2021 The Kubernetes Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Deprecated: Package api/builtins will not be available in API v1.
5+
package builtins
6+
7+
import (
8+
internal "sigs.k8s.io/kustomize/api/internal/builtins"
9+
)
10+
11+
type (
12+
AnnotationsTransformerPlugin = internal.AnnotationsTransformerPlugin
13+
ConfigMapGeneratorPlugin = internal.ConfigMapGeneratorPlugin
14+
HashTransformerPlugin = internal.HashTransformerPlugin
15+
HelmChartInflationGeneratorPlugin = internal.HelmChartInflationGeneratorPlugin
16+
IAMPolicyGeneratorPlugin = internal.IAMPolicyGeneratorPlugin
17+
ImageTagTransformerPlugin = internal.ImageTagTransformerPlugin
18+
LabelTransformerPlugin = internal.LabelTransformerPlugin
19+
LegacyOrderTransformerPlugin = internal.LegacyOrderTransformerPlugin
20+
NamespaceTransformerPlugin = internal.NamespaceTransformerPlugin
21+
PatchJson6902TransformerPlugin = internal.PatchJson6902TransformerPlugin
22+
PatchStrategicMergeTransformerPlugin = internal.PatchStrategicMergeTransformerPlugin
23+
PatchTransformerPlugin = internal.PatchTransformerPlugin
24+
PrefixTransformerPlugin = internal.PrefixTransformerPlugin
25+
SuffixTransformerPlugin = internal.SuffixTransformerPlugin
26+
ReplacementTransformerPlugin = internal.ReplacementTransformerPlugin
27+
ReplicaCountTransformerPlugin = internal.ReplicaCountTransformerPlugin
28+
SecretGeneratorPlugin = internal.SecretGeneratorPlugin
29+
ValueAddTransformerPlugin = internal.ValueAddTransformerPlugin
30+
)
31+
32+
var (
33+
NewAnnotationsTransformerPlugin = internal.NewAnnotationsTransformerPlugin
34+
NewConfigMapGeneratorPlugin = internal.NewConfigMapGeneratorPlugin
35+
NewHashTransformerPlugin = internal.NewHashTransformerPlugin
36+
NewHelmChartInflationGeneratorPlugin = internal.NewHelmChartInflationGeneratorPlugin
37+
NewIAMPolicyGeneratorPlugin = internal.NewIAMPolicyGeneratorPlugin
38+
NewImageTagTransformerPlugin = internal.NewImageTagTransformerPlugin
39+
NewLabelTransformerPlugin = internal.NewLabelTransformerPlugin
40+
NewLegacyOrderTransformerPlugin = internal.NewLegacyOrderTransformerPlugin
41+
NewNamespaceTransformerPlugin = internal.NewNamespaceTransformerPlugin
42+
NewPatchJson6902TransformerPlugin = internal.NewPatchJson6902TransformerPlugin
43+
NewPatchStrategicMergeTransformerPlugin = internal.NewPatchStrategicMergeTransformerPlugin
44+
NewPatchTransformerPlugin = internal.NewPatchTransformerPlugin
45+
NewPrefixTransformerPlugin = internal.NewPrefixTransformerPlugin
46+
NewSuffixTransformerPlugin = internal.NewSuffixTransformerPlugin
47+
NewReplacementTransformerPlugin = internal.NewReplacementTransformerPlugin
48+
NewReplicaCountTransformerPlugin = internal.NewReplicaCountTransformerPlugin
49+
NewSecretGeneratorPlugin = internal.NewSecretGeneratorPlugin
50+
NewValueAddTransformerPlugin = internal.NewValueAddTransformerPlugin
51+
)

0 commit comments

Comments
 (0)