mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-30 09:51:23 +00:00
Add labels field to kustomization
This commit is contained in:
@@ -46,6 +46,9 @@ type Kustomization struct {
|
||||
// CommonLabels to add to all objects and selectors.
|
||||
CommonLabels map[string]string `json:"commonLabels,omitempty" yaml:"commonLabels,omitempty"`
|
||||
|
||||
// Labels to add to all objects but not selectors.
|
||||
Labels []Label `json:"labels,omitempty" yaml:"labels,omitempty"`
|
||||
|
||||
// CommonAnnotations to add to all objects.
|
||||
CommonAnnotations map[string]string `json:"commonAnnotations,omitempty" yaml:"commonAnnotations,omitempty"`
|
||||
|
||||
@@ -190,6 +193,11 @@ func (k *Kustomization) FixKustomizationPreMarshalling() {
|
||||
// PatchesJson6902 should be under the Patches field.
|
||||
k.Patches = append(k.Patches, k.PatchesJson6902...)
|
||||
k.PatchesJson6902 = nil
|
||||
|
||||
if l := labelFromCommonLabels(k.CommonLabels); l != nil {
|
||||
k.Labels = append(k.Labels, *l)
|
||||
k.CommonLabels = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (k *Kustomization) EnforceFields() []string {
|
||||
|
||||
@@ -22,6 +22,9 @@ func TestFixKustomizationPostUnmarshalling(t *testing.T) {
|
||||
EnvSource: "c",
|
||||
},
|
||||
}}}
|
||||
k.CommonLabels = map[string]string{
|
||||
"foo": "bar",
|
||||
}
|
||||
k.FixKustomizationPostUnmarshalling()
|
||||
|
||||
expected := Kustomization{
|
||||
@@ -35,6 +38,9 @@ func TestFixKustomizationPostUnmarshalling(t *testing.T) {
|
||||
EnvSources: []string{"a", "b", "c"},
|
||||
},
|
||||
}}},
|
||||
CommonLabels: map[string]string{
|
||||
"foo": "bar",
|
||||
},
|
||||
}
|
||||
if !reflect.DeepEqual(k, expected) {
|
||||
t.Fatalf("unexpected output: %v", k)
|
||||
|
||||
25
api/types/labels.go
Normal file
25
api/types/labels.go
Normal file
@@ -0,0 +1,25 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package types
|
||||
|
||||
type Label struct {
|
||||
// Pairs contains the key-value pairs for labels to add
|
||||
Pairs map[string]string `json:"pairs,omitempty" yaml:"pairs,omitempty"`
|
||||
// IncludeSelectors inidicates should transformer include the
|
||||
// fieldSpecs for selectors. Custom fieldSpecs specified by
|
||||
// FieldSpecs will be merged with builtin fieldSpecs if this
|
||||
// is true.
|
||||
IncludeSelectors bool `json:"includeSelectors,omitempty" yaml:"includeSelectors,omitempty"`
|
||||
FieldSpecs []FieldSpec `json:"fields,omitempty" yaml:"fields,omitempty"`
|
||||
}
|
||||
|
||||
func labelFromCommonLabels(commonLabels map[string]string) *Label {
|
||||
if len(commonLabels) == 0 {
|
||||
return nil
|
||||
}
|
||||
return &Label{
|
||||
Pairs: commonLabels,
|
||||
IncludeSelectors: true,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user