mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-17 18:25:26 +00:00
* feat: support labels key in transformer configuration Allow the usage of a separate transformer configuration for the labels key, similar to what is currently available for commonLabels and commonAnnotations. This aims to provide the same functionality that commonLabels currently provide for labels, since commonLabels is deprecated and slated for removal in a future release. * chore(transformerconfig): add nolint hint Add a nolint hint to the new method so the returns can stay consistent with one another. * fix: changes from code review * Rename methods `AddCommonLabelFieldSpec` and `AddLabelFieldSpec` to `AddCommonLabelsFieldSpec` and `AddLabelsFieldSpec`. * Add extra test to verify scenarios applying labels to Custom Resource Definitions.
73 lines
1.7 KiB
Go
73 lines
1.7 KiB
Go
// Copyright 2019 The Kubernetes Authors.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package builtinconfig
|
|
|
|
import (
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"sigs.k8s.io/kustomize/api/internal/loader"
|
|
"sigs.k8s.io/kustomize/api/types"
|
|
"sigs.k8s.io/kustomize/kyaml/filesys"
|
|
"sigs.k8s.io/kustomize/kyaml/resid"
|
|
)
|
|
|
|
func TestLoadDefaultConfigsFromFiles(t *testing.T) {
|
|
fSys := filesys.MakeFsInMemory()
|
|
err := fSys.WriteFile("config.yaml", []byte(`
|
|
namePrefix:
|
|
- path: nameprefix/path
|
|
kind: SomeKind
|
|
`))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ldr, err := loader.NewLoader(
|
|
loader.RestrictionRootOnly, filesys.Separator, fSys)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tCfg, err := loadDefaultConfig(ldr, []string{"config.yaml"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
expected := &TransformerConfig{
|
|
NamePrefix: []types.FieldSpec{
|
|
{
|
|
Gvk: resid.Gvk{Kind: "SomeKind"},
|
|
Path: "nameprefix/path",
|
|
},
|
|
},
|
|
}
|
|
if !reflect.DeepEqual(tCfg, expected) {
|
|
t.Fatalf("expected %v\n but go6t %v\n", expected, tCfg)
|
|
}
|
|
}
|
|
|
|
func TestLoadDefaultConfigsFromFilesWithMissingFields(t *testing.T) {
|
|
fSys := filesys.MakeFsInMemory()
|
|
filePathContainsTypo := "config_contains_typo.yaml"
|
|
if err := fSys.WriteFile(filePathContainsTypo, []byte(`
|
|
namoPrefix:
|
|
- path: nameprefix/path
|
|
kind: SomeKind
|
|
`)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ldr, err := loader.NewLoader(
|
|
loader.RestrictionRootOnly, filesys.Separator, fSys)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
errMsg := "error unmarshaling JSON: while decoding JSON: json: unknown field"
|
|
_, err = loadDefaultConfig(ldr, []string{filePathContainsTypo})
|
|
if err == nil {
|
|
t.Fatalf("expected to fail unmarshal yaml, but got nil %s", filePathContainsTypo)
|
|
}
|
|
if !strings.Contains(err.Error(), errMsg) {
|
|
t.Fatalf("expected error %s, but got %s", errMsg, err)
|
|
}
|
|
}
|