Skip to content

Commit 0020a49

Browse files
authored
Merge branch 'kubernetes-sigs:master' into issue/5127
2 parents 9eddc3c + 5a3e920 commit 0020a49

39 files changed

+1738
-47
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,11 @@ test-unit-all: \
134134
# This target is used by our Github Actions CI to run unit tests for all non-plugin modules in multiple GOOS environments.
135135
.PHONY: test-unit-non-plugin
136136
test-unit-non-plugin:
137-
./hack/for-each-module.sh "make test" "./plugin/*" 15
137+
./hack/for-each-module.sh "make test" "./plugin/*" 16
138138

139139
.PHONY: build-non-plugin-all
140140
build-non-plugin-all:
141-
./hack/for-each-module.sh "make build" "./plugin/*" 15
141+
./hack/for-each-module.sh "make build" "./plugin/*" 16
142142

143143
.PHONY: test-unit-kustomize-plugins
144144
test-unit-kustomize-plugins:

OWNERS

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# See https://github.com/kubernetes/community/blob/master/community-membership.md
22
approvers:
33
- kustomize-approvers
4-
54
reviewers:
65
- kustomize-reviewers

OWNERS_ALIASES

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,12 @@ aliases:
1313
- yuwenma
1414
- annasong20
1515
- koba1t
16-
17-
kyaml-approvers:
18-
- mengqiy
19-
- mortent
20-
- phanimarupaka
21-
kyaml-reviewers:
22-
- mengqiy
23-
- mortent
24-
- phanimarupaka
25-
26-
emeritus-approvers:
27-
- liujingfang1
28-
- Shell32-Natsu
29-
- justinsb
30-
- monopole
31-
- pwittrock
16+
# emeritus:
17+
# - liujingfang1
18+
# - Shell32-Natsu
19+
# - justinsb
20+
# - monopole
21+
# - pwittrock
22+
# - mengqiy
23+
# - mortent
24+
# - phanimarupaka

api/filters/namespace/namespace.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,11 @@ func (ns Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
5656

5757
// Run runs the filter on a single node rather than a slice
5858
func (ns Filter) run(node *yaml.RNode) (*yaml.RNode, error) {
59-
// Special handling for metadata.namespace -- :(
59+
// Special handling for metadata.namespace and metadata.name -- :(
6060
// never let SetEntry handle metadata.namespace--it will incorrectly include cluster-scoped resources
61-
ns.FsSlice = ns.removeMetaNamespaceFieldSpecs(ns.FsSlice)
61+
// only update metadata.name if api version is expected one--so-as it leaves other resources of kind namespace alone
62+
apiVersion := node.GetApiVersion()
63+
ns.FsSlice = ns.removeUnneededMetaFieldSpecs(apiVersion, ns.FsSlice)
6264
gvk := resid.GvkFromNode(node)
6365
if err := ns.metaNamespaceHack(node, gvk); err != nil {
6466
return nil, err
@@ -186,12 +188,15 @@ func (ns Filter) removeRoleBindingSubjectFieldSpecs(fs types.FsSlice) types.FsSl
186188
return val
187189
}
188190

189-
func (ns Filter) removeMetaNamespaceFieldSpecs(fs types.FsSlice) types.FsSlice {
191+
func (ns Filter) removeUnneededMetaFieldSpecs(apiVersion string, fs types.FsSlice) types.FsSlice {
190192
var val types.FsSlice
191193
for i := range fs {
192194
if fs[i].Path == types.MetadataNamespacePath {
193195
continue
194196
}
197+
if apiVersion != types.MetadataNamespaceApiVersion && fs[i].Path == types.MetadataNamePath {
198+
continue
199+
}
195200
val = append(val, fs[i])
196201
}
197202
return val

api/internal/builtins/HelmChartInflationGenerator.go

Lines changed: 15 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/krusty/namespaces_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,3 +801,31 @@ metadata:
801801
namespace: iter8-monitoring
802802
`)
803803
}
804+
805+
// Demonstrates that metadata.name is only overridden for a kind: Namespace with apiVersion: v1
806+
// Test for issue #5072
807+
func TestNameNotOveriddenForNonCoreApiVersionOnANamespaceKind(t *testing.T) {
808+
th := kusttest_test.MakeHarness(t)
809+
810+
th.WriteF("azure-servicebus.yaml", `
811+
apiVersion: servicebus.azure.com/v1beta20210101preview
812+
kind: Namespace
813+
metadata:
814+
name: core-sb-99
815+
namespace: without-podinfo
816+
`)
817+
th.WriteK(".", `
818+
namespace: podinfo
819+
resources:
820+
- azure-servicebus.yaml
821+
`)
822+
823+
m := th.Run(".", th.MakeDefaultOptions())
824+
th.AssertActualEqualsExpected(m, `
825+
apiVersion: servicebus.azure.com/v1beta20210101preview
826+
kind: Namespace
827+
metadata:
828+
name: core-sb-99
829+
namespace: podinfo
830+
`)
831+
}

api/types/kustomization.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ import (
1414
)
1515

1616
const (
17-
KustomizationVersion = "kustomize.config.k8s.io/v1beta1"
18-
KustomizationKind = "Kustomization"
19-
ComponentVersion = "kustomize.config.k8s.io/v1alpha1"
20-
ComponentKind = "Component"
21-
MetadataNamespacePath = "metadata/namespace"
17+
KustomizationVersion = "kustomize.config.k8s.io/v1beta1"
18+
KustomizationKind = "Kustomization"
19+
ComponentVersion = "kustomize.config.k8s.io/v1alpha1"
20+
ComponentKind = "Component"
21+
MetadataNamespacePath = "metadata/namespace"
22+
MetadataNamespaceApiVersion = "v1"
23+
MetadataNamePath = "metadata/name"
2224

2325
OriginAnnotations = "originAnnotations"
2426
TransformerAnnotations = "transformerAnnotations"

cmd/config/OWNERS

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export KUSTOMIZE_ROOT ?= $(shell pwd | sed -E 's|(.*\/kustomize)/(.*)|\1|')
2+
include $(KUSTOMIZE_ROOT)/Makefile-modules.mk
3+
4+
CONTROLLER_GEN_VERSION=v0.11.3
5+
6+
generate: $(MYGOBIN)/controller-gen $(MYGOBIN)/embedmd
7+
go generate ./...
8+
embedmd -w README.md
9+
10+
build: generate
11+
go build -v -o $(MYGOBIN)/app-fn cmd/main.go
12+
13+
$(MYGOBIN)/controller-gen:
14+
go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_GEN_VERSION)
15+
16+
$(MYGOBIN)/embedmd:
17+
go install github.com/campoy/[email protected]
18+
19+
.PHONY: example
20+
example: build
21+
$(MYGOBIN)/app-fn pkg/exampleapp/testdata/success/basic/config.yaml
22+
23+
24+
test: generate
25+
go test -v -timeout 45m -cover ./...
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
## Kyaml Functions Framework Example: Application Custom Resource
2+
3+
This is a moderate-complexity example of a KRM function built using the [KRM Functions Framework package](https://pkg.go.dev/sigs.k8s.io/kustomize/kyaml/fn/framework). It demonstrates how to write a function that implements a custom resource (CR) representing an abstract application.
4+
5+
[embedmd]:# (pkg/exampleapp/v1alpha1/testdata/success/basic/config.yaml)
6+
```yaml
7+
apiVersion: platform.example.com/v1alpha1
8+
kind: ExampleApp
9+
metadata:
10+
name: simple-app-sample
11+
env: production
12+
workloads:
13+
webWorkers:
14+
- name: web-worker
15+
domains:
16+
- example.com
17+
jobWorkers:
18+
- name: job-worker
19+
replicas: 10
20+
resources: medium
21+
queues:
22+
- high
23+
- medium
24+
- low
25+
- name: job-worker-2
26+
replicas: 5
27+
queues:
28+
- bg2
29+
datastores:
30+
postgresInstance: simple-app-sample-postgres
31+
```
32+
33+
It also demonstrates the pattern of having the CR accept patches, allowing the user to customize the final result beyond the fields the CR exposes.
34+
35+
[embedmd]:# (pkg/exampleapp/v1alpha1/testdata/success/overrides/config.yaml)
36+
```yaml
37+
apiVersion: platform.example.com/v1alpha1
38+
kind: ExampleApp
39+
metadata:
40+
name: simple-app-sample
41+
env: production
42+
workloads:
43+
webWorkers:
44+
- name: web-worker
45+
domains:
46+
- first.example.com
47+
- name: web-worker-no-sidecar
48+
domains:
49+
- second.example.com
50+
51+
overrides:
52+
additionalResources:
53+
- custom-configmap.yaml
54+
resourcePatches:
55+
- web-worker-sidecar.yaml
56+
containerPatches:
57+
- custom-app-env.yaml
58+
```
59+
60+
## Implementation walkthrough
61+
62+
The entrypoint for the function is [cmd/main.go](cmd/main.go), which invokes a ["dispatcher"](pkg/dispatcher/dispatcher.go) that determines which `Filter` implements the resource passed in. The dispatcher pattern allows a single function binary to handle multiple CRs, and is also useful for evolving a single CR over time (e.g. handle `ExampleApp` API versions `example.com/v1beta1` and `example.com/v1`).
63+
64+
[embedmd]:# (pkg/dispatcher/dispatcher.go go /.*VersionedAPIProcessor.*/ /}}/)
65+
```go
66+
p := framework.VersionedAPIProcessor{FilterProvider: framework.GVKFilterMap{
67+
"ExampleApp": map[string]kio.Filter{
68+
"platform.example.com/v1alpha1": &v1alpha1.ExampleApp{},
69+
},
70+
}}
71+
```
72+
73+
74+
The ExampleApp type is defined in [pkg/exampleapp/v1alpha1/types.go](pkg/exampleapp/v1alpha1/types.go). It is responsible for implementing the logic of the CR, most of which is done by implementing the `kyaml.Filter` interface in [pkg/exampleapp/v1alpha1/processing.go](pkg/exampleapp/v1alpha1/processing.go). Internally, the filter function mostly builds up and executes a `framework.TemplateProcessor`.
75+
76+
The ExampleApp type is annotated with [kubebuilder markers](https://book.kubebuilder.io/reference/markers/crd-validation.html), and a Go generator uses those to create the CRD YAML in [pkg/exampleapp/v1alpha1/platform.example.com_exampleapps.yaml](pkg/exampleapp/v1alpha1/platform.example.com_exampleapps.yaml). The CR then implements `framework.ValidationSchemaProvider`, which causes the CRD to be used for validation. It also implements `framework.Validator` to add custom validations and `framework.Defaulter` to add defaulting.
77+
78+
[embedmd]:# (pkg/exampleapp/v1alpha1/types.go go /.*type ExampleApp.*/ /}/)
79+
```go
80+
type ExampleApp struct {
81+
// Embedding these structs is required to use controller-gen to produce the CRD
82+
metav1.TypeMeta `json:",inline"`
83+
metav1.ObjectMeta `json:"metadata"`
84+
85+
// +kubebuilder:validation:Enum=production;staging;development
86+
Env string `json:"env" yaml:"env"`
87+
88+
// +optional
89+
AppImage string `json:"appImage" yaml:"appImage"`
90+
91+
Workloads Workloads `json:"workloads" yaml:"workloads"`
92+
93+
// +optional
94+
Datastores Datastores `json:"datastores,omitempty" yaml:"datastores,omitempty"`
95+
96+
// +optional
97+
Overrides Overrides `json:"overrides,omitempty" yaml:"overrides,omitempty"`
98+
}
99+
```
100+
101+
[embedmd]:# (pkg/exampleapp/v1alpha1/processing.go go /.*Filter.*/ /error\) {/)
102+
```go
103+
func (a ExampleApp) Filter(items []*yaml.RNode) ([]*yaml.RNode, error) {
104+
```
105+
106+
[embedmd]:# (pkg/exampleapp/v1alpha1/processing.go go /.*Schema.*/ /error\) {/)
107+
```go
108+
func (a *ExampleApp) Schema() (*spec.Schema, error) {
109+
```
110+
111+
[embedmd]:# (pkg/exampleapp/v1alpha1/processing.go go /.*Validate.*/ /error {/)
112+
```go
113+
func (a *ExampleApp) Validate() error {
114+
```
115+
116+
[embedmd]:# (pkg/exampleapp/v1alpha1/processing.go go /.*Default.*/ /error {/)
117+
```go
118+
func (a *ExampleApp) Default() error {
119+
```
120+
121+
122+
## Running the Example
123+
124+
There are three ways to try this out:
125+
126+
A. Run `make example` in the root of the example to run the function with the test data in [pkg/exampleapp/v1alpha1/testdata/success/basic](pkg/exampleapp/v1alpha1/testdata/success/basic).
127+
128+
B. Run `go run cmd/main.go [FILE]` in the root of the example. Try it with the test input from one of the cases in [pkg/exampleapp/v1alpha1/testdata/success](pkg/exampleapp/v1alpha1/testdata/success). For example: `go run cmd/main.go pkg/exampleapp/v1alpha1/testdata/success/basic/config.yaml`.
129+
130+
C. Build the binary with `make build`, then run it with `app-fn [FILE]`. Try it with the test input from one of the cases in [pkg/exampleapp/v1alpha1/testdata/success](pkg/exampleapp/v1alpha1/testdata/success). For example: `app-fn pkg/exampleapp/v1alpha1/testdata/success/basic/config.yaml`.
131+
132+

0 commit comments

Comments
 (0)