mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-13 01:50:55 +00:00
* add labels in template/metadata by default * update comment * fix kustomization labels test * Add spec/template/metadata/labels when includeTemplate is true * remove unnecessary test changes * add error wrap * Revert "add error wrap" This reverts commit 0a203df83edb90a400b35d5521487b984619e919. * add error wrap at template fieldSpec merge
This commit is contained in:
committed by
GitHub
parent
496a962a53
commit
5948f6aa63
@@ -7,6 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
"sigs.k8s.io/kustomize/api/internal/plugins/builtinconfig"
|
"sigs.k8s.io/kustomize/api/internal/plugins/builtinconfig"
|
||||||
"sigs.k8s.io/kustomize/api/internal/plugins/builtinhelpers"
|
"sigs.k8s.io/kustomize/api/internal/plugins/builtinhelpers"
|
||||||
"sigs.k8s.io/kustomize/api/resmap"
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
@@ -285,6 +286,13 @@ var transformerConfigurators = map[builtinhelpers.BuiltinPluginType]func(
|
|||||||
if label.IncludeSelectors {
|
if label.IncludeSelectors {
|
||||||
fss, err = fss.MergeAll(tc.CommonLabels)
|
fss, err = fss.MergeAll(tc.CommonLabels)
|
||||||
} else {
|
} else {
|
||||||
|
// merge spec/template/metadata fieldSpec if includeTemplate flag is true
|
||||||
|
if label.IncludeTemplates {
|
||||||
|
fss, err = fss.MergeOne(types.FieldSpec{Path: "spec/template/metadata/labels", CreateIfNotPresent: false})
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "failed to merge template fieldSpec")
|
||||||
|
}
|
||||||
|
}
|
||||||
// only add to metadata by default
|
// only add to metadata by default
|
||||||
fss, err = fss.MergeOne(types.FieldSpec{Path: "metadata/labels", CreateIfNotPresent: true})
|
fss, err = fss.MergeOne(types.FieldSpec{Path: "metadata/labels", CreateIfNotPresent: true})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -91,3 +91,72 @@ spec:
|
|||||||
c: d
|
c: d
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestKustomizationLabelsInTemplate(t *testing.T) {
|
||||||
|
th := kusttest_test.MakeHarness(t)
|
||||||
|
th.WriteF("app/deployment.yaml", `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app.kubernetes.io/component: a
|
||||||
|
app.kubernetes.io/instance: b
|
||||||
|
app.kubernetes.io/name: c
|
||||||
|
app.kubernetes.io/part-of: d
|
||||||
|
name: deployment
|
||||||
|
spec:
|
||||||
|
replicas: 1
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app.kubernetes.io/component: a
|
||||||
|
app.kubernetes.io/instance: b
|
||||||
|
app.kubernetes.io/name: c
|
||||||
|
app.kubernetes.io/part-of: d
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app.kubernetes.io/component: a
|
||||||
|
app.kubernetes.io/instance: b
|
||||||
|
app.kubernetes.io/name: c
|
||||||
|
app.kubernetes.io/part-of: d
|
||||||
|
`)
|
||||||
|
th.WriteK("/app", `
|
||||||
|
resources:
|
||||||
|
- deployment.yaml
|
||||||
|
|
||||||
|
labels:
|
||||||
|
- pairs:
|
||||||
|
foo: bar
|
||||||
|
includeSelectors: false
|
||||||
|
includeTemplates: true
|
||||||
|
`)
|
||||||
|
m := th.Run("/app", th.MakeDefaultOptions())
|
||||||
|
th.AssertActualEqualsExpected(m, `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app.kubernetes.io/component: a
|
||||||
|
app.kubernetes.io/instance: b
|
||||||
|
app.kubernetes.io/name: c
|
||||||
|
app.kubernetes.io/part-of: d
|
||||||
|
foo: bar
|
||||||
|
name: deployment
|
||||||
|
spec:
|
||||||
|
replicas: 1
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app.kubernetes.io/component: a
|
||||||
|
app.kubernetes.io/instance: b
|
||||||
|
app.kubernetes.io/name: c
|
||||||
|
app.kubernetes.io/part-of: d
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app.kubernetes.io/component: a
|
||||||
|
app.kubernetes.io/instance: b
|
||||||
|
app.kubernetes.io/name: c
|
||||||
|
app.kubernetes.io/part-of: d
|
||||||
|
foo: bar
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,7 +10,12 @@ type Label struct {
|
|||||||
// fieldSpecs for selectors. Custom fieldSpecs specified by
|
// fieldSpecs for selectors. Custom fieldSpecs specified by
|
||||||
// FieldSpecs will be merged with builtin fieldSpecs if this
|
// FieldSpecs will be merged with builtin fieldSpecs if this
|
||||||
// is true.
|
// is true.
|
||||||
IncludeSelectors bool `json:"includeSelectors,omitempty" yaml:"includeSelectors,omitempty"`
|
IncludeSelectors bool `json:"includeSelectors,omitempty" yaml:"includeSelectors,omitempty"`
|
||||||
|
// IncludeTemplates inidicates should transformer include the
|
||||||
|
// spec/template/metadata fieldSpec. Custom fieldSpecs specified by
|
||||||
|
// FieldSpecs will be merged with spec/template/metadata fieldSpec if this
|
||||||
|
// is true. If IncludeSelectors is true, IncludeTemplates is not needed.
|
||||||
|
IncludeTemplates bool `json:"includeTemplates,omitempty" yaml:"includeTemplates,omitempty"`
|
||||||
FieldSpecs []FieldSpec `json:"fields,omitempty" yaml:"fields,omitempty"`
|
FieldSpecs []FieldSpec `json:"fields,omitempty" yaml:"fields,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user