Skip to content

Commit d87708b

Browse files
committed
add set-default-name function
1 parent 5378158 commit d87708b

File tree

10 files changed

+1157
-0
lines changed

10 files changed

+1157
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM golang:1.17-alpine3.13
2+
ENV CGO_ENABLED=0
3+
WORKDIR /go/src/
4+
5+
COPY go.mod go.sum ./
6+
RUN go mod download
7+
8+
COPY . .
9+
RUN go build -o /usr/local/bin/function ./
10+
11+
#############################################
12+
13+
FROM alpine:3.13
14+
COPY --from=0 /usr/local/bin/function /usr/local/bin/function
15+
ENTRYPOINT ["function"]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# set-default-name
2+
3+
### Feature
4+
`set-default-name` does a simple task: cruelly updates the resource metadata name or name references if `metadata.name`
5+
match the fieldSpec list in `fieldspec/customName.go`.
6+
7+
### Warning
8+
!!! Be careful when using this function.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package builtin
2+
3+
import (
4+
"strings"
5+
6+
"sigs.k8s.io/kustomize/api/types"
7+
"sigs.k8s.io/kustomize/kyaml/resid"
8+
)
9+
10+
// NameBackReferences is an association between a gvk.GVK (a ReferralTarget)
11+
// and a list of Referrers that could refer to it.
12+
//
13+
// It is used to handle name changes, and can be thought of as a
14+
// a contact list. If you change your own contact info (name,
15+
// phone number, etc.), you must tell your contacts or they won't
16+
// know about the change.
17+
//
18+
// For example, ConfigMaps can be used by Pods and everything that
19+
// contains a Pod; Deployment, Job, StatefulSet, etc.
20+
// The ConfigMap is the ReferralTarget, the others are Referrers.
21+
//
22+
// If the the name of a ConfigMap instance changed from 'alice' to 'bob',
23+
// one must
24+
// - visit all objects that could refer to the ConfigMap (the Referrers)
25+
// - see if they mention 'alice',
26+
// - if so, change the Referrer's name reference to 'bob'.
27+
//
28+
// The NameBackReferences instance to aid in this could look like
29+
// {
30+
// kind: ConfigMap
31+
// version: v1
32+
// fieldSpecs:
33+
// - kind: Pod
34+
// version: v1
35+
// path: spec/volumes/configMap/name
36+
// - kind: Deployment
37+
// path: spec/template/spec/volumes/configMap/name
38+
// - kind: Job
39+
// path: spec/template/spec/volumes/configMap/name
40+
// (etc.)
41+
// }
42+
type NameBackReferences struct {
43+
resid.Gvk `json:",inline,omitempty" yaml:",inline,omitempty"`
44+
Referrers types.FsSlice `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
45+
}
46+
47+
func (n NameBackReferences) String() string {
48+
var r []string
49+
for _, f := range n.Referrers {
50+
r = append(r, f.String())
51+
}
52+
return n.Gvk.String() + ": (\n" +
53+
strings.Join(r, "\n") + "\n)"
54+
}
55+
56+
type NbrSlice []NameBackReferences
57+
58+
func (s NbrSlice) Len() int { return len(s) }
59+
func (s NbrSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
60+
func (s NbrSlice) Less(i, j int) bool {
61+
return s[i].Gvk.IsLessThan(s[j].Gvk)
62+
}
63+
64+
func (s NbrSlice) mergeAll(o NbrSlice) (result NbrSlice, err error) {
65+
result = s
66+
for _, r := range o {
67+
result, err = result.mergeOne(r)
68+
if err != nil {
69+
return nil, err
70+
}
71+
}
72+
return result, nil
73+
}
74+
75+
func (s NbrSlice) mergeOne(other NameBackReferences) (NbrSlice, error) {
76+
var result NbrSlice
77+
var err error
78+
found := false
79+
for _, c := range s {
80+
if c.Gvk.Equals(other.Gvk) {
81+
c.Referrers, err = c.Referrers.MergeAll(other.Referrers)
82+
if err != nil {
83+
return nil, err
84+
}
85+
found = true
86+
}
87+
result = append(result, c)
88+
}
89+
90+
if !found {
91+
result = append(result, other)
92+
}
93+
return result, nil
94+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package builtin
2+
3+
import (
4+
"log"
5+
6+
"sigs.k8s.io/kustomize/api/filters/nameref"
7+
"sigs.k8s.io/kustomize/api/resmap"
8+
"sigs.k8s.io/kustomize/api/resource"
9+
"sigs.k8s.io/kustomize/kyaml/resid"
10+
)
11+
12+
type nameReferenceTransformer struct {
13+
backRefs []NameBackReferences
14+
}
15+
16+
const doDebug = false
17+
18+
var _ resmap.Transformer = &nameReferenceTransformer{}
19+
20+
type filterMap map[*resource.Resource][]nameref.Filter
21+
22+
func NewNameReferenceTransformer(
23+
br []NameBackReferences) resmap.Transformer {
24+
if br == nil {
25+
log.Fatal("backrefs not expected to be nil")
26+
}
27+
return &nameReferenceTransformer{backRefs: br}
28+
}
29+
30+
func (t *nameReferenceTransformer) Transform(m resmap.ResMap) error {
31+
fMap := t.determineFilters(m.Resources())
32+
for r, fList := range fMap {
33+
c := m.SubsetThatCouldBeReferencedByResource(r)
34+
for _, f := range fList {
35+
f.Referrer = r
36+
f.ReferralCandidates = c
37+
if err := f.Referrer.ApplyFilter(f); err != nil {
38+
return err
39+
}
40+
}
41+
}
42+
43+
return nil
44+
}
45+
46+
func (t *nameReferenceTransformer) determineFilters(
47+
resources []*resource.Resource) (fMap filterMap) {
48+
// We cache the resource OrgId values because they don't change and otherwise are very visible in a memory pprof
49+
resourceOrgIds := make([]resid.ResId, len(resources))
50+
for i, resource := range resources {
51+
resourceOrgIds[i] = resource.OrgId()
52+
}
53+
fMap = make(filterMap)
54+
for _, backReference := range t.backRefs {
55+
for _, referrerSpec := range backReference.Referrers {
56+
for i, res := range resources {
57+
if resourceOrgIds[i].IsSelected(&referrerSpec.Gvk) {
58+
fMap[res] = append(fMap[res], nameref.Filter{
59+
NameFieldToUpdate: referrerSpec,
60+
ReferralTarget: backReference.Gvk,
61+
})
62+
}
63+
}
64+
}
65+
}
66+
return fMap
67+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package fieldspec
2+
3+
/* CustomNameFieldSpecs list should be adjusted actively before we get a variant constructor solution.
4+
*/
5+
6+
const (
7+
CustomNameFieldSpecs = `
8+
customMetaName:
9+
- path: metadata/name
10+
group: storage.cnrm.cloud.google.com
11+
12+
- path: metadata/name
13+
group: serviceusage.cnrm.cloud.google.com
14+
15+
- path: metadata/name
16+
group: redis.cnrm.cloud.google.com
17+
18+
- path: metadata/name
19+
group: spanner.cnrm.cloud.google.com
20+
21+
# A fieldSpec object under customMetaName.
22+
# - path: <fieldspec>
23+
# group: <API Group Name> if ignored, matches all
24+
# version: <API Version> if ignored, matches all
25+
# kind: <Kind> if ignored, matches all
26+
# create: [true|false] default to false, if set to true, create the field path in resource.
27+
`
28+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package fieldspec
2+
3+
const (
4+
NameReferenceFieldSpecs = `
5+
nameReference:
6+
- kind: RedisInstance
7+
group: redis.cnrm.cloud.google.com/v1beta1
8+
fieldSpecs:
9+
- path: spec/displayName
10+
kind: RedisInstance
11+
group: redis.cnrm.cloud.google.com/v1beta1
12+
`
13+
)

functions/go/set-default-name/go.mod

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/set-project-id
2+
3+
go 1.17
4+
5+
require (
6+
sigs.k8s.io/kustomize/api v0.10.0
7+
sigs.k8s.io/kustomize/kyaml v0.13.1-0.20211202184144-fe551be87b8d
8+
9+
// sigs.k8s.io/kustomize/api v0.8.11-0.20210614195535-7e8ba62e9fd9
10+
// sigs.k8s.io/kustomize/kyaml v0.10.21
11+
)
12+
13+
require (
14+
github.com/PuerkitoBio/purell v1.1.1 // indirect
15+
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
16+
github.com/davecgh/go-spew v1.1.1 // indirect
17+
github.com/go-errors/errors v1.0.1 // indirect
18+
github.com/go-openapi/jsonpointer v0.19.3 // indirect
19+
github.com/go-openapi/jsonreference v0.19.3 // indirect
20+
github.com/go-openapi/swag v0.19.5 // indirect
21+
github.com/inconshreveable/mousetrap v1.0.0 // indirect
22+
github.com/mailru/easyjson v0.7.0 // indirect
23+
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
24+
github.com/pkg/errors v0.9.1 // indirect
25+
github.com/pmezard/go-difflib v1.0.0 // indirect
26+
github.com/spf13/cobra v1.2.1 // indirect
27+
github.com/spf13/pflag v1.0.5 // indirect
28+
github.com/stretchr/testify v1.7.0 // indirect
29+
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca // indirect
30+
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
31+
golang.org/x/text v0.3.5 // indirect
32+
gopkg.in/yaml.v2 v2.4.0 // indirect
33+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
34+
k8s.io/kube-openapi v0.0.0-20210421082810-95288971da7e // indirect
35+
sigs.k8s.io/yaml v1.2.0 // indirect
36+
)

0 commit comments

Comments
 (0)