Add labels field to kustomization

This commit is contained in:
Donny Xia
2021-03-22 13:24:24 -07:00
parent 7b38ce4ef2
commit 5ed2067be9
8 changed files with 201 additions and 0 deletions

View File

@@ -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 {

View File

@@ -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
View 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,
}
}