Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[address #4717] Re-do create template/metadata when using includeTemplates if not present #4805

Merged
merged 10 commits into from
Oct 11, 2022
25 changes: 16 additions & 9 deletions api/internal/plugins/builtinconfig/transformerconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"sort"

"github.com/pkg/errors"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"github.com/pkg/errors" looks like an already unmaintained and archived.

Could you think of using fmt.Errorf() instead of errors.Wrap() if you don't have any reason?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"github.com/pkg/errors" looks like an already unmaintained and archived.

Could you think of using fmt.Errorf() instead of errors.Wrap() if you don't have any reason?

done! thanks for making me aware of that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI the other errors package we often use in Kustomize is our own "sigs.k8s.io/kustomize/kyaml/errors", which provides errors.WrapPrefixf(err, msg, args...) for situations like these. We have plenty of fmt.Errorf usage as well though, so I don't feel strongly about this.

"sigs.k8s.io/kustomize/api/ifc"
"sigs.k8s.io/kustomize/api/konfig/builtinpluginconsts"
"sigs.k8s.io/kustomize/api/types"
Expand All @@ -18,6 +19,7 @@ type TransformerConfig struct {
NameSuffix types.FsSlice `json:"nameSuffix,omitempty" yaml:"nameSuffix,omitempty"`
NameSpace types.FsSlice `json:"namespace,omitempty" yaml:"namespace,omitempty"`
CommonLabels types.FsSlice `json:"commonLabels,omitempty" yaml:"commonLabels,omitempty"`
TemplateLabels types.FsSlice `json:"templateLabels,omitempty" yaml:"templateLabels,omitempty"`
CommonAnnotations types.FsSlice `json:"commonAnnotations,omitempty" yaml:"commonAnnotations,omitempty"`
NameReference nbrSlice `json:"nameReference,omitempty" yaml:"nameReference,omitempty"`
VarReference types.FsSlice `json:"varReference,omitempty" yaml:"varReference,omitempty"`
Expand Down Expand Up @@ -60,6 +62,7 @@ func (t *TransformerConfig) sortFields() {
sort.Sort(t.NamePrefix)
sort.Sort(t.NameSpace)
sort.Sort(t.CommonLabels)
sort.Sort(t.TemplateLabels)
sort.Sort(t.CommonAnnotations)
sort.Sort(t.NameReference)
sort.Sort(t.VarReference)
Expand Down Expand Up @@ -108,40 +111,44 @@ func (t *TransformerConfig) Merge(input *TransformerConfig) (
merged = &TransformerConfig{}
merged.NamePrefix, err = t.NamePrefix.MergeAll(input.NamePrefix)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to merge NamePrefix fieldSpec")
}
merged.NameSuffix, err = t.NameSuffix.MergeAll(input.NameSuffix)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to merge NameSuffix fieldSpec")
}
merged.NameSpace, err = t.NameSpace.MergeAll(input.NameSpace)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to merge NameSpace fieldSpec")
}
merged.CommonAnnotations, err = t.CommonAnnotations.MergeAll(
input.CommonAnnotations)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to merge CommonAnnotations fieldSpec")
}
merged.CommonLabels, err = t.CommonLabels.MergeAll(input.CommonLabels)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to merge CommonLabels fieldSpec")
}
merged.TemplateLabels, err = t.TemplateLabels.MergeAll(input.TemplateLabels)
if err != nil {
return nil, errors.Wrap(err, "failed to merge TemplateLabels fieldSpec")
}
merged.VarReference, err = t.VarReference.MergeAll(input.VarReference)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to merge VarReference fieldSpec")
}
merged.NameReference, err = t.NameReference.mergeAll(input.NameReference)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to merge NameReference fieldSpec")
}
merged.Images, err = t.Images.MergeAll(input.Images)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to merge Images fieldSpec")
}
merged.Replicas, err = t.Replicas.MergeAll(input.Replicas)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to merge Replicas fieldSpec")
}
merged.sortFields()
return merged, nil
Expand Down
4 changes: 2 additions & 2 deletions api/internal/target/kusttarget_configplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,9 @@ var transformerConfigurators = map[builtinhelpers.BuiltinPluginType]func(
if label.IncludeSelectors {
fss, err = fss.MergeAll(tc.CommonLabels)
} else {
// merge spec/template/metadata fieldSpec if includeTemplate flag is true
// merge spec/template/metadata fieldSpecs if includeTemplate flag is true
if label.IncludeTemplates {
fss, err = fss.MergeOne(types.FieldSpec{Path: "spec/template/metadata/labels", CreateIfNotPresent: false})
fss, err = fss.MergeAll(tc.TemplateLabels)
if err != nil {
return nil, errors.Wrap(err, "failed to merge template fieldSpec")
}
Expand Down
48 changes: 1 addition & 47 deletions api/konfig/builtinpluginconsts/commonlabels.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ package builtinpluginconsts

const commonLabelFieldSpecs = `
commonLabels:
- path: metadata/labels
create: true

- path: spec/selector
create: true
version: v1
Expand All @@ -17,20 +14,10 @@ commonLabels:
create: true
version: v1
kind: ReplicationController

- path: spec/template/metadata/labels
create: true
version: v1
kind: ReplicationController

- path: spec/selector/matchLabels
create: true
kind: Deployment

- path: spec/template/metadata/labels
create: true
kind: Deployment

- path: spec/template/spec/affinity/podAffinity/preferredDuringSchedulingIgnoredDuringExecution/podAffinityTerm/labelSelector/matchLabels
create: false
group: apps
Expand Down Expand Up @@ -60,28 +47,15 @@ commonLabels:
create: true
kind: ReplicaSet

- path: spec/template/metadata/labels
create: true
kind: ReplicaSet

- path: spec/selector/matchLabels
create: true
kind: DaemonSet

- path: spec/template/metadata/labels
create: true
kind: DaemonSet

- path: spec/selector/matchLabels
create: true
group: apps
kind: StatefulSet

- path: spec/template/metadata/labels
create: true
group: apps
kind: StatefulSet

- path: spec/template/spec/affinity/podAffinity/preferredDuringSchedulingIgnoredDuringExecution/podAffinityTerm/labelSelector/matchLabels
create: false
group: apps
Expand All @@ -107,36 +81,16 @@ commonLabels:
group: apps
kind: StatefulSet

- path: spec/volumeClaimTemplates[]/metadata/labels
create: true
group: apps
kind: StatefulSet

- path: spec/selector/matchLabels
create: false
group: batch
kind: Job

- path: spec/template/metadata/labels
create: true
group: batch
kind: Job

- path: spec/jobTemplate/spec/selector/matchLabels
create: false
group: batch
kind: CronJob

- path: spec/jobTemplate/metadata/labels
create: true
group: batch
kind: CronJob

- path: spec/jobTemplate/spec/template/metadata/labels
create: true
group: batch
kind: CronJob

- path: spec/selector/matchLabels
create: false
group: policy
Expand All @@ -156,4 +110,4 @@ commonLabels:
create: false
group: networking.k8s.io
kind: NetworkPolicy
`
` + metadataLabelsFieldSpecs
2 changes: 2 additions & 0 deletions api/konfig/builtinpluginconsts/defaultconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func GetDefaultFieldSpecs() []byte {
[]byte(namePrefixFieldSpecs),
[]byte(nameSuffixFieldSpecs),
[]byte(commonLabelFieldSpecs),
[]byte(templateLabelFieldSpecs),
[]byte(commonAnnotationFieldSpecs),
[]byte(namespaceFieldSpecs),
[]byte(varReferenceFieldSpecs),
Expand All @@ -30,6 +31,7 @@ func GetDefaultFieldSpecsAsMap() map[string]string {
result["nameprefix"] = namePrefixFieldSpecs
result["namesuffix"] = nameSuffixFieldSpecs
result["commonlabels"] = commonLabelFieldSpecs
result["templatelabels"] = templateLabelFieldSpecs
result["commonannotations"] = commonAnnotationFieldSpecs
result["namespace"] = namespaceFieldSpecs
result["varreference"] = varReferenceFieldSpecs
Expand Down
51 changes: 51 additions & 0 deletions api/konfig/builtinpluginconsts/metadatalabels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package builtinpluginconsts

const metadataLabelsFieldSpecs = `
- path: metadata/labels
create: true

- path: spec/template/metadata/labels
create: true
version: v1
kind: ReplicationController

- path: spec/template/metadata/labels
create: true
kind: Deployment

- path: spec/template/metadata/labels
create: true
kind: ReplicaSet

- path: spec/template/metadata/labels
create: true
kind: DaemonSet

- path: spec/template/metadata/labels
create: true
group: apps
kind: StatefulSet

- path: spec/volumeClaimTemplates[]/metadata/labels
create: true
group: apps
kind: StatefulSet

- path: spec/template/metadata/labels
create: true
group: batch
kind: Job

- path: spec/jobTemplate/metadata/labels
create: true
group: batch
kind: CronJob

- path: spec/jobTemplate/spec/template/metadata/labels
create: true
group: batch
kind: CronJob
`
8 changes: 8 additions & 0 deletions api/konfig/builtinpluginconsts/templatelabels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package builtinpluginconsts

const templateLabelFieldSpecs = `
templateLabels:
` + metadataLabelsFieldSpecs
Loading