mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-21 22:41:42 +00:00
- In ResMap, drop concept of internal Id to Resource map. The ResMap is now (just) a list, allowing only very particular edits. - Resources should now be maintained in the order loaded. A later PR can adjust tests to remove the internal legacy sorting, and confirm order-out is predictable from order-in. The PR would suppress the sort in tests, and reorder the output to make all tests pass again, and confirm that the new order matched depth-first input traversal. The FromMap fixture function was removed from all test inputs to establish a predictable input order. - Resources now have two 'Ids', OriginalId and CurrentId. The former is fixed as GVK-name-namespace at load time, the latter changes during transformations. The latter can be used to narrow name references when the former maps to multiple resources. We allow bases to be loaded more than once in a build (a diamond pattern), so the OriginalId is not unique across the resources set. The CurrentId is (and must be) unique, but is constantly mutating. Failing to make this distinction clear, and attempting to maintain a mapping from a single mutating Id to a resource was making the code too complex. - Drop prefix/suffix from ResId - the ResId is now immutable. A later PR can remove the distinction with ItemId. - This PR increases coverage of ResMap is since this is a large refactor. Higher level tests didn't need much change outside reordering of results at the resource level.
90 lines
2.3 KiB
Go
90 lines
2.3 KiB
Go
/*
|
|
Copyright 2018 The Kubernetes Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package transformers
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"sigs.k8s.io/kustomize/pkg/resmap"
|
|
"sigs.k8s.io/kustomize/pkg/transformers/config"
|
|
)
|
|
|
|
// mapTransformer applies a string->string map to fieldSpecs.
|
|
type mapTransformer struct {
|
|
m map[string]string
|
|
fieldSpecs []config.FieldSpec
|
|
}
|
|
|
|
var _ Transformer = &mapTransformer{}
|
|
|
|
// NewLabelsMapTransformer constructs a mapTransformer.
|
|
func NewLabelsMapTransformer(
|
|
m map[string]string, fs []config.FieldSpec) (Transformer, error) {
|
|
return NewMapTransformer(fs, m)
|
|
}
|
|
|
|
// NewAnnotationsMapTransformer construct a mapTransformer.
|
|
func NewAnnotationsMapTransformer(
|
|
m map[string]string, fs []config.FieldSpec) (Transformer, error) {
|
|
return NewMapTransformer(fs, m)
|
|
}
|
|
|
|
// NewMapTransformer construct a mapTransformer.
|
|
func NewMapTransformer(
|
|
pc []config.FieldSpec, m map[string]string) (Transformer, error) {
|
|
if m == nil {
|
|
return NewNoOpTransformer(), nil
|
|
}
|
|
if pc == nil {
|
|
return nil, errors.New("fieldSpecs is not expected to be nil")
|
|
}
|
|
return &mapTransformer{fieldSpecs: pc, m: m}, nil
|
|
}
|
|
|
|
// Transform apply each <key, value> pair in the mapTransformer to the
|
|
// fields specified in mapTransformer.
|
|
func (o *mapTransformer) Transform(m resmap.ResMap) error {
|
|
for _, r := range m.Resources() {
|
|
for _, path := range o.fieldSpecs {
|
|
if !r.OrgId().IsSelected(&path.Gvk) {
|
|
continue
|
|
}
|
|
err := MutateField(
|
|
r.Map(), path.PathSlice(),
|
|
path.CreateIfNotPresent, o.addMap)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (o *mapTransformer) addMap(in interface{}) (interface{}, error) {
|
|
m, ok := in.(map[string]interface{})
|
|
if in == nil {
|
|
m = map[string]interface{}{}
|
|
} else if !ok {
|
|
return nil, fmt.Errorf("%#v is expected to be %T", in, m)
|
|
}
|
|
for k, v := range o.m {
|
|
m[k] = v
|
|
}
|
|
return m, nil
|
|
}
|