mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-30 01:46:23 +00:00
Reduce indirection.
This commit is contained in:
@@ -57,8 +57,7 @@ func NewMapTransformer(pc []PathConfig, m map[string]string) (Transformer, error
|
||||
// fields specified in mapTransformer.
|
||||
func (o *mapTransformer) Transform(m resmap.ResMap) error {
|
||||
for id := range m {
|
||||
obj := m[id].Unstruct()
|
||||
objMap := obj.UnstructuredContent()
|
||||
objMap := m[id].UnstructuredContent()
|
||||
for _, path := range o.pathConfigs {
|
||||
if !selectByGVK(id.Gvk(), path.GroupVersionKind) {
|
||||
continue
|
||||
|
||||
@@ -22,8 +22,8 @@ import (
|
||||
|
||||
"github.com/kubernetes-sigs/kustomize/pkg/hash"
|
||||
"github.com/kubernetes-sigs/kustomize/pkg/resmap"
|
||||
"github.com/kubernetes-sigs/kustomize/pkg/resource"
|
||||
"k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
)
|
||||
|
||||
@@ -40,20 +40,20 @@ func NewNameHashTransformer() Transformer {
|
||||
|
||||
// Transform appends hash to configmaps and secrets.
|
||||
func (o *nameHashTransformer) Transform(m resmap.ResMap) error {
|
||||
for id, obj := range m {
|
||||
for id, res := range m {
|
||||
switch {
|
||||
case selectByGVK(id.Gvk(), &schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}):
|
||||
appendHashForConfigMap(obj.Unstruct())
|
||||
appendHashForConfigMap(res)
|
||||
|
||||
case selectByGVK(id.Gvk(), &schema.GroupVersionKind{Version: "v1", Kind: "Secret"}):
|
||||
appendHashForSecret(obj.Unstruct())
|
||||
appendHashForSecret(res)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func appendHashForConfigMap(obj *unstructured.Unstructured) error {
|
||||
cm, err := unstructuredToConfigmap(obj)
|
||||
func appendHashForConfigMap(res *resource.Resource) error {
|
||||
cm, err := unstructuredToConfigmap(res)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -61,14 +61,14 @@ func appendHashForConfigMap(obj *unstructured.Unstructured) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nameWithHash := fmt.Sprintf("%s-%s", obj.GetName(), h)
|
||||
obj.SetName(nameWithHash)
|
||||
nameWithHash := fmt.Sprintf("%s-%s", res.GetName(), h)
|
||||
res.SetName(nameWithHash)
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO: Remove this function after we support hash unstructured objects
|
||||
func unstructuredToConfigmap(in *unstructured.Unstructured) (*v1.ConfigMap, error) {
|
||||
marshaled, err := json.Marshal(in)
|
||||
func unstructuredToConfigmap(res *resource.Resource) (*v1.ConfigMap, error) {
|
||||
marshaled, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -77,8 +77,8 @@ func unstructuredToConfigmap(in *unstructured.Unstructured) (*v1.ConfigMap, erro
|
||||
return &out, err
|
||||
}
|
||||
|
||||
func appendHashForSecret(obj *unstructured.Unstructured) error {
|
||||
secret, err := unstructuredToSecret(obj)
|
||||
func appendHashForSecret(res *resource.Resource) error {
|
||||
secret, err := unstructuredToSecret(res)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -86,14 +86,14 @@ func appendHashForSecret(obj *unstructured.Unstructured) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nameWithHash := fmt.Sprintf("%s-%s", obj.GetName(), h)
|
||||
obj.SetName(nameWithHash)
|
||||
nameWithHash := fmt.Sprintf("%s-%s", res.GetName(), h)
|
||||
res.SetName(nameWithHash)
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO: Remove this function after we support hash unstructured objects
|
||||
func unstructuredToSecret(in *unstructured.Unstructured) (*v1.Secret, error) {
|
||||
marshaled, err := json.Marshal(in)
|
||||
func unstructuredToSecret(res *resource.Resource) (*v1.Secret, error) {
|
||||
marshaled, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -49,11 +49,9 @@ func NewNameReferenceTransformer(pc []referencePathConfig) (Transformer, error)
|
||||
// The old name is in the key in the map and the new name is in the object
|
||||
// associated with the key. e.g. if <k, v> is one of the key-value pair in the map,
|
||||
// then the old name is k.Name and the new name is v.GetName()
|
||||
func (o *nameReferenceTransformer) Transform(
|
||||
m resmap.ResMap) error {
|
||||
func (o *nameReferenceTransformer) Transform(m resmap.ResMap) error {
|
||||
for id := range m {
|
||||
obj := m[id].Unstruct()
|
||||
objMap := obj.UnstructuredContent()
|
||||
objMap := m[id].UnstructuredContent()
|
||||
for _, referencePathConfig := range o.pathConfigs {
|
||||
for _, path := range referencePathConfig.pathConfigs {
|
||||
if !selectByGVK(id.Gvk(), path.GroupVersionKind) {
|
||||
@@ -71,21 +69,19 @@ func (o *nameReferenceTransformer) Transform(
|
||||
}
|
||||
|
||||
func (o *nameReferenceTransformer) updateNameReference(
|
||||
GVK schema.GroupVersionKind,
|
||||
m resmap.ResMap,
|
||||
) func(in interface{}) (interface{}, error) {
|
||||
GVK schema.GroupVersionKind, m resmap.ResMap) func(in interface{}) (interface{}, error) {
|
||||
return func(in interface{}) (interface{}, error) {
|
||||
s, ok := in.(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%#v is expectd to be %T", in, s)
|
||||
}
|
||||
|
||||
for id, obj := range m {
|
||||
for id, res := range m {
|
||||
if !selectByGVK(id.Gvk(), &GVK) {
|
||||
continue
|
||||
}
|
||||
if id.Name() == s {
|
||||
return obj.Unstruct().GetName(), nil
|
||||
return res.GetName(), nil
|
||||
}
|
||||
}
|
||||
return in, nil
|
||||
|
||||
@@ -36,8 +36,8 @@ func NewNamespaceTransformer(ns string) Transformer {
|
||||
|
||||
// Transform adds the namespace.
|
||||
func (o *namespaceTransformer) Transform(m resmap.ResMap) error {
|
||||
for _, obj := range m {
|
||||
obj.Unstruct().SetNamespace(o.namespace)
|
||||
for _, res := range m {
|
||||
res.SetNamespace(o.namespace)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -24,7 +24,6 @@ import (
|
||||
|
||||
"github.com/kubernetes-sigs/kustomize/pkg/resmap"
|
||||
"github.com/kubernetes-sigs/kustomize/pkg/resource"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/util/strategicpatch"
|
||||
"k8s.io/client-go/kubernetes/scheme"
|
||||
@@ -63,15 +62,15 @@ func (o *overlayTransformer) Transform(baseResourceMap resmap.ResMap) error {
|
||||
}
|
||||
merged := map[string]interface{}{}
|
||||
versionedObj, err := scheme.Scheme.New(id.Gvk())
|
||||
baseName := base.Unstruct().GetName()
|
||||
baseName := base.GetName()
|
||||
switch {
|
||||
case runtime.IsNotRegisteredError(err):
|
||||
// Use JSON merge patch to handle types w/o schema
|
||||
baseBytes, err := json.Marshal(base.Unstruct())
|
||||
baseBytes, err := json.Marshal(base)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
patchBytes, err := json.Marshal(overlay.Unstruct())
|
||||
patchBytes, err := json.Marshal(overlay)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -95,15 +94,15 @@ func (o *overlayTransformer) Transform(baseResourceMap resmap.ResMap) error {
|
||||
return err
|
||||
}
|
||||
merged, err = strategicpatch.StrategicMergeMapPatchUsingLookupPatchMeta(
|
||||
base.Unstruct().Object,
|
||||
overlay.Unstruct().Object,
|
||||
base.Object,
|
||||
overlay.Object,
|
||||
lookupPatchMeta)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
base.Unstruct().SetName(baseName)
|
||||
baseResourceMap[id].Unstruct().Object = merged
|
||||
base.SetName(baseName)
|
||||
baseResourceMap[id].Object = merged
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -112,7 +111,6 @@ func (o *overlayTransformer) Transform(baseResourceMap resmap.ResMap) error {
|
||||
// It errors out if there is conflict between patches.
|
||||
func (o *overlayTransformer) mergePatches() (resmap.ResMap, error) {
|
||||
rc := resmap.ResMap{}
|
||||
patches := resourcesToObjects(o.overlay)
|
||||
for ix, patch := range o.overlay {
|
||||
id := patch.Id()
|
||||
existing, found := rc[id]
|
||||
@@ -135,31 +133,22 @@ func (o *overlayTransformer) mergePatches() (resmap.ResMap, error) {
|
||||
}
|
||||
}
|
||||
|
||||
conflict, err := cd.hasConflict(existing.Unstruct(), patch.Unstruct())
|
||||
conflict, err := cd.hasConflict(existing, patch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if conflict {
|
||||
conflictingPatch, err := cd.findConflict(ix, patches)
|
||||
conflictingPatch, err := cd.findConflict(ix, o.overlay)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, fmt.Errorf("there is conflict between %#v and %#v", conflictingPatch.Object, patch.Unstruct().Object)
|
||||
} else {
|
||||
merged, err := cd.mergePatches(existing.Unstruct(), patch.Unstruct())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
existing.SetUnstruct(merged)
|
||||
return nil, fmt.Errorf("there is conflict between %#v and %#v", conflictingPatch.Object, patch.Object)
|
||||
}
|
||||
merged, err := cd.mergePatches(existing, patch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rc[id] = merged
|
||||
}
|
||||
return rc, nil
|
||||
}
|
||||
|
||||
func resourcesToObjects(rs []*resource.Resource) []*unstructured.Unstructured {
|
||||
objectList := make([]*unstructured.Unstructured, len(rs))
|
||||
for i := range rs {
|
||||
objectList[i] = rs[i].Unstruct()
|
||||
}
|
||||
return objectList
|
||||
}
|
||||
|
||||
@@ -19,18 +19,18 @@ package transformers
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
jsonpatch "github.com/evanphx/json-patch"
|
||||
"github.com/evanphx/json-patch"
|
||||
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"github.com/kubernetes-sigs/kustomize/pkg/resource"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/util/mergepatch"
|
||||
"k8s.io/apimachinery/pkg/util/strategicpatch"
|
||||
)
|
||||
|
||||
type conflictDetector interface {
|
||||
hasConflict(patch1, patch2 *unstructured.Unstructured) (bool, error)
|
||||
findConflict(conflictingPatchIdx int, patches []*unstructured.Unstructured) (*unstructured.Unstructured, error)
|
||||
mergePatches(patch1, patch2 *unstructured.Unstructured) (*unstructured.Unstructured, error)
|
||||
hasConflict(patch1, patch2 *resource.Resource) (bool, error)
|
||||
findConflict(conflictingPatchIdx int, patches []*resource.Resource) (*resource.Resource, error)
|
||||
mergePatches(patch1, patch2 *resource.Resource) (*resource.Resource, error)
|
||||
}
|
||||
|
||||
type jsonMergePatch struct{}
|
||||
@@ -41,11 +41,11 @@ func newJMPConflictDetector() conflictDetector {
|
||||
return &jsonMergePatch{}
|
||||
}
|
||||
|
||||
func (jmp *jsonMergePatch) hasConflict(patch1, patch2 *unstructured.Unstructured) (bool, error) {
|
||||
func (jmp *jsonMergePatch) hasConflict(patch1, patch2 *resource.Resource) (bool, error) {
|
||||
return mergepatch.HasConflicts(patch1.Object, patch2.Object)
|
||||
}
|
||||
|
||||
func (jmp *jsonMergePatch) findConflict(conflictingPatchIdx int, patches []*unstructured.Unstructured) (*unstructured.Unstructured, error) {
|
||||
func (jmp *jsonMergePatch) findConflict(conflictingPatchIdx int, patches []*resource.Resource) (*resource.Resource, error) {
|
||||
for i, patch := range patches {
|
||||
if i == conflictingPatchIdx {
|
||||
continue
|
||||
@@ -65,8 +65,8 @@ func (jmp *jsonMergePatch) findConflict(conflictingPatchIdx int, patches []*unst
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (jmp *jsonMergePatch) mergePatches(patch1, patch2 *unstructured.Unstructured) (*unstructured.Unstructured, error) {
|
||||
var merged unstructured.Unstructured
|
||||
func (jmp *jsonMergePatch) mergePatches(patch1, patch2 *resource.Resource) (*resource.Resource, error) {
|
||||
var merged resource.Resource
|
||||
var mergedMap map[string]interface{}
|
||||
baseBytes, err := json.Marshal(patch1.Object)
|
||||
if err != nil {
|
||||
@@ -96,11 +96,11 @@ func newSMPConflictDetector(versionedObj runtime.Object) (conflictDetector, erro
|
||||
return &strategicMergePatch{lookupPatchMeta: lookupPatchMeta}, err
|
||||
}
|
||||
|
||||
func (smp *strategicMergePatch) hasConflict(patch1, patch2 *unstructured.Unstructured) (bool, error) {
|
||||
func (smp *strategicMergePatch) hasConflict(patch1, patch2 *resource.Resource) (bool, error) {
|
||||
return strategicpatch.MergingMapsHaveConflicts(patch1.Object, patch2.Object, smp.lookupPatchMeta)
|
||||
}
|
||||
|
||||
func (smp *strategicMergePatch) findConflict(conflictingPatchIdx int, patches []*unstructured.Unstructured) (*unstructured.Unstructured, error) {
|
||||
func (smp *strategicMergePatch) findConflict(conflictingPatchIdx int, patches []*resource.Resource) (*resource.Resource, error) {
|
||||
for i, patch := range patches {
|
||||
if i == conflictingPatchIdx {
|
||||
continue
|
||||
@@ -121,8 +121,8 @@ func (smp *strategicMergePatch) findConflict(conflictingPatchIdx int, patches []
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (smp *strategicMergePatch) mergePatches(patch1, patch2 *unstructured.Unstructured) (*unstructured.Unstructured, error) {
|
||||
merged := unstructured.Unstructured{}
|
||||
func (smp *strategicMergePatch) mergePatches(patch1, patch2 *resource.Resource) (*resource.Resource, error) {
|
||||
merged := resource.Resource{}
|
||||
mergeJsonMap, err := strategicpatch.MergeStrategicMergeMapPatchUsingLookupPatchMeta(
|
||||
smp.lookupPatchMeta, patch1.Object, patch2.Object)
|
||||
merged.SetUnstructuredContent(mergeJsonMap)
|
||||
|
||||
@@ -58,8 +58,7 @@ func NewNamePrefixTransformer(pc []PathConfig, np string) (Transformer, error) {
|
||||
// Transform prepends the name prefix.
|
||||
func (o *namePrefixTransformer) Transform(m resmap.ResMap) error {
|
||||
for id := range m {
|
||||
obj := m[id].Unstruct()
|
||||
objMap := obj.UnstructuredContent()
|
||||
objMap := m[id].UnstructuredContent()
|
||||
for _, path := range o.pathConfigs {
|
||||
if !selectByGVK(id.Gvk(), path.GroupVersionKind) {
|
||||
continue
|
||||
|
||||
@@ -45,8 +45,7 @@ func NewRefVarTransformer(vars map[string]string) (Transformer, error) {
|
||||
// 3. Add remaining service environment vars
|
||||
func (rv *refvarTransformer) Transform(resources resmap.ResMap) error {
|
||||
for GVKn := range resources {
|
||||
obj := resources[GVKn].Unstruct()
|
||||
objMap := obj.UnstructuredContent()
|
||||
objMap := resources[GVKn].UnstructuredContent()
|
||||
for _, pc := range rv.pathConfigs {
|
||||
if !selectByGVK(GVKn.Gvk(), pc.GroupVersionKind) {
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user