diff --git a/k8sdeps/transformer/factory.go b/k8sdeps/transformer/factory.go index 61dff0ab5..5a6a0b8d3 100644 --- a/k8sdeps/transformer/factory.go +++ b/k8sdeps/transformer/factory.go @@ -26,12 +26,12 @@ func NewFactoryImpl() *FactoryImpl { func (p *FactoryImpl) MakePatchTransformer( slice []*resource.Resource, rf *resource.Factory) (transformers.Transformer, error) { - return patch.NewPatchTransformer(slice, rf) + return patch.NewTransformer(slice, rf) } // MakeHashTransformer makes a new name hash transformer func (p *FactoryImpl) MakeHashTransformer() transformers.Transformer { - return hash.NewNameHashTransformer() + return hash.NewTransformer() } func (p *FactoryImpl) MakeInventoryTransformer( @@ -39,5 +39,5 @@ func (p *FactoryImpl) MakeInventoryTransformer( ldr ifc.Loader, namespace string, gp types.GarbagePolicy) transformers.Transformer { - return inventory.NewInventoryTransformer(arg, ldr, namespace, gp) + return inventory.NewTransformer(arg, ldr, namespace, gp) } diff --git a/k8sdeps/transformer/hash/hash.go b/k8sdeps/transformer/hash/kusthash.go similarity index 78% rename from k8sdeps/transformer/hash/hash.go rename to k8sdeps/transformer/hash/kusthash.go index cdab7244b..26c647d83 100644 --- a/k8sdeps/transformer/hash/hash.go +++ b/k8sdeps/transformer/hash/kusthash.go @@ -1,18 +1,5 @@ -/* -Copyright 2017 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. -*/ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 package hash @@ -26,16 +13,16 @@ import ( "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" ) -// KustHash compute hash for unstructured objects -type KustHash struct{} +// kustHash computes a hash of an unstructured object. +type kustHash struct{} -// NewKustHash returns a KustHash object -func NewKustHash() *KustHash { - return &KustHash{} +// NewKustHash returns a kustHash object +func NewKustHash() *kustHash { + return &kustHash{} } // Hash returns a hash of either a ConfigMap or a Secret -func (h *KustHash) Hash(m map[string]interface{}) (string, error) { +func (h *kustHash) Hash(m map[string]interface{}) (string, error) { u := unstructured.Unstructured{ Object: m, } @@ -46,22 +33,23 @@ func (h *KustHash) Hash(m map[string]interface{}) (string, error) { if err != nil { return "", err } - return ConfigMapHash(cm) + return configMapHash(cm) case "Secret": sec, err := unstructuredToSecret(u) if err != nil { return "", err } - return SecretHash(sec) + return secretHash(sec) default: - return "", fmt.Errorf("type %s is supported for hashing in %v", kind, m) + return "", fmt.Errorf( + "type %s is not supported for hashing in %v", kind, m) } } -// ConfigMapHash returns a hash of the ConfigMap. +// configMapHash returns a hash of the ConfigMap. // The Data, Kind, and Name are taken into account. -func ConfigMapHash(cm *v1.ConfigMap) (string, error) { +func configMapHash(cm *v1.ConfigMap) (string, error) { encoded, err := encodeConfigMap(cm) if err != nil { return "", err @@ -75,7 +63,7 @@ func ConfigMapHash(cm *v1.ConfigMap) (string, error) { // SecretHash returns a hash of the Secret. // The Data, Kind, Name, and Type are taken into account. -func SecretHash(sec *v1.Secret) (string, error) { +func secretHash(sec *v1.Secret) (string, error) { encoded, err := encodeSecret(sec) if err != nil { return "", err diff --git a/k8sdeps/transformer/hash/hash_test.go b/k8sdeps/transformer/hash/kusthash_test.go similarity index 99% rename from k8sdeps/transformer/hash/hash_test.go rename to k8sdeps/transformer/hash/kusthash_test.go index 4a3c9365c..47d783498 100644 --- a/k8sdeps/transformer/hash/hash_test.go +++ b/k8sdeps/transformer/hash/kusthash_test.go @@ -54,7 +54,7 @@ func TestConfigMapHash(t *testing.T) { } for _, c := range cases { - h, err := ConfigMapHash(c.cm) + h, err := configMapHash(c.cm) if SkipRest(t, c.desc, err, c.err) { continue } @@ -80,7 +80,7 @@ func TestSecretHash(t *testing.T) { } for _, c := range cases { - h, err := SecretHash(c.secret) + h, err := secretHash(c.secret) if SkipRest(t, c.desc, err, c.err) { continue } diff --git a/k8sdeps/transformer/hash/namehash.go b/k8sdeps/transformer/hash/namehash.go deleted file mode 100644 index a52072e8a..000000000 --- a/k8sdeps/transformer/hash/namehash.go +++ /dev/null @@ -1,47 +0,0 @@ -/* -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 hash - -import ( - "fmt" - - "sigs.k8s.io/kustomize/pkg/resmap" - "sigs.k8s.io/kustomize/pkg/transformers" -) - -type nameHashTransformer struct{} - -var _ transformers.Transformer = &nameHashTransformer{} - -// NewNameHashTransformer construct a nameHashTransformer. -func NewNameHashTransformer() transformers.Transformer { - return &nameHashTransformer{} -} - -// Transform appends hash to generated resources. -func (o *nameHashTransformer) Transform(m resmap.ResMap) error { - for _, res := range m { - if res.NeedHashSuffix() { - h, err := NewKustHash().Hash(res.Map()) - if err != nil { - return err - } - res.SetName(fmt.Sprintf("%s-%s", res.GetName(), h)) - } - } - return nil -} diff --git a/k8sdeps/transformer/hash/transformer.go b/k8sdeps/transformer/hash/transformer.go new file mode 100644 index 000000000..3e0e3123a --- /dev/null +++ b/k8sdeps/transformer/hash/transformer.go @@ -0,0 +1,34 @@ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 + +package hash + +import ( + "fmt" + + "sigs.k8s.io/kustomize/pkg/resmap" + "sigs.k8s.io/kustomize/pkg/transformers" +) + +type transformer struct{} + +var _ transformers.Transformer = &transformer{} + +// NewTransformer make a hash transformer. +func NewTransformer() transformers.Transformer { + return &transformer{} +} + +// Transform appends hash to generated resources. +func (tf *transformer) Transform(m resmap.ResMap) error { + for _, res := range m { + if res.NeedHashSuffix() { + h, err := NewKustHash().Hash(res.Map()) + if err != nil { + return err + } + res.SetName(fmt.Sprintf("%s-%s", res.GetName(), h)) + } + } + return nil +} diff --git a/k8sdeps/transformer/hash/namehash_test.go b/k8sdeps/transformer/hash/transformer_test.go similarity index 85% rename from k8sdeps/transformer/hash/namehash_test.go rename to k8sdeps/transformer/hash/transformer_test.go index 7ca724511..cf182671e 100644 --- a/k8sdeps/transformer/hash/namehash_test.go +++ b/k8sdeps/transformer/hash/transformer_test.go @@ -1,18 +1,5 @@ -/* -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. -*/ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 package hash @@ -152,7 +139,7 @@ func TestNameHashTransformer(t *testing.T) { }, &types.GeneratorArgs{Behavior: "create"}, &types.GeneratorOptions{DisableNameSuffixHash: false}), } - tran := NewNameHashTransformer() + tran := NewTransformer() tran.Transform(objs) if !reflect.DeepEqual(objs, expected) { diff --git a/k8sdeps/transformer/inventory/inventory.go b/k8sdeps/transformer/inventory/transformer.go similarity index 83% rename from k8sdeps/transformer/inventory/inventory.go rename to k8sdeps/transformer/inventory/transformer.go index 61fe389a6..431c8c280 100644 --- a/k8sdeps/transformer/inventory/inventory.go +++ b/k8sdeps/transformer/inventory/transformer.go @@ -17,18 +17,18 @@ import ( "sigs.k8s.io/kustomize/pkg/types" ) -// inventoryTransformer compute the inventory object used in prune -type inventoryTransformer struct { +// transformer compute the inventory object used in prune +type transformer struct { garbagePolicy types.GarbagePolicy ldr ifc.Loader cmName string cmNamespace string } -var _ transformers.Transformer = &inventoryTransformer{} +var _ transformers.Transformer = &transformer{} -// NewInventoryTransformer makes a inventoryTransformer. -func NewInventoryTransformer( +// NewTransformer makes a new inventory transformer. +func NewTransformer( p *types.Inventory, ldr ifc.Loader, namespace string, @@ -36,7 +36,7 @@ func NewInventoryTransformer( if p == nil || p.Type != "ConfigMap" || p.ConfigMap.Namespace != namespace { return transformers.NewNoOpTransformer() } - return &inventoryTransformer{ + return &transformer{ garbagePolicy: gp, ldr: ldr, cmName: p.ConfigMap.Name, @@ -52,7 +52,7 @@ func NewInventoryTransformer( // The inventory data is written to annotation since // 1. The key in data field is constrained and couldn't include arbitrary letters // 2. The annotation can be put into any kind of objects -func (o *inventoryTransformer) Transform(m resmap.ResMap) error { +func (tf *transformer) Transform(m resmap.ResMap) error { invty := inventory.NewInventory() var keys []string for _, r := range m { @@ -74,8 +74,8 @@ func (o *inventoryTransformer) Transform(m resmap.ResMap) error { } args := &types.ConfigMapArgs{} - args.Name = o.cmName - args.Namespace = o.cmNamespace + args.Name = tf.cmName + args.Namespace = tf.cmNamespace opts := &types.GeneratorOptions{ Annotations: make(map[string]string), } @@ -86,12 +86,12 @@ func (o *inventoryTransformer) Transform(m resmap.ResMap) error { } kf := kunstruct.NewKunstructuredFactoryImpl() - k, err := kf.MakeConfigMap(o.ldr, opts, args) + k, err := kf.MakeConfigMap(tf.ldr, opts, args) if err != nil { return err } - if o.garbagePolicy == types.GarbageCollect { + if tf.garbagePolicy == types.GarbageCollect { for k := range m { delete(m, k) } @@ -102,8 +102,8 @@ func (o *inventoryTransformer) Transform(m resmap.ResMap) error { Version: "v1", Kind: "ConfigMap", }, - o.cmName, - "", o.cmNamespace) + tf.cmName, + "", tf.cmNamespace) if _, ok := m[id]; ok { return fmt.Errorf("id %v is already used, please use a different name in the prune field", id) } diff --git a/k8sdeps/transformer/inventory/inventory_test.go b/k8sdeps/transformer/inventory/transformer_test.go similarity index 97% rename from k8sdeps/transformer/inventory/inventory_test.go rename to k8sdeps/transformer/inventory/transformer_test.go index b2fa210c4..0c503d396 100644 --- a/k8sdeps/transformer/inventory/inventory_test.go +++ b/k8sdeps/transformer/inventory/transformer_test.go @@ -139,7 +139,7 @@ func TestInventoryTransformer(t *testing.T) { objs := makeResMap() // include the original resmap; only return the ConfigMap for pruning - tran := NewInventoryTransformer(p, ldr, "default", types.GarbageCollect) + tran := NewTransformer(p, ldr, "default", types.GarbageCollect) tran.Transform(objs) if !reflect.DeepEqual(objs, expected) { @@ -151,7 +151,7 @@ func TestInventoryTransformer(t *testing.T) { expected = objs.DeepCopy(rf) expected[resid.NewResIdWithPrefixNamespace(cmap, "pruneCM", "", "default")] = pruneMap // append the ConfigMap for pruning to the original resmap - tran = NewInventoryTransformer(p, ldr, "default", types.GarbageIgnore) + tran = NewTransformer(p, ldr, "default", types.GarbageIgnore) tran.Transform(objs) if !reflect.DeepEqual(objs, expected) { diff --git a/k8sdeps/transformer/patch/patchconflictdetector.go b/k8sdeps/transformer/patch/conflictdetector.go similarity index 85% rename from k8sdeps/transformer/patch/patchconflictdetector.go rename to k8sdeps/transformer/patch/conflictdetector.go index 10353c77f..3d8851505 100644 --- a/k8sdeps/transformer/patch/patchconflictdetector.go +++ b/k8sdeps/transformer/patch/conflictdetector.go @@ -1,18 +1,5 @@ -/* -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. -*/ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 package patch diff --git a/k8sdeps/transformer/patch/patch.go b/k8sdeps/transformer/patch/transformer.go similarity index 75% rename from k8sdeps/transformer/patch/patch.go rename to k8sdeps/transformer/patch/transformer.go index 357f3daba..a0dcff6b2 100644 --- a/k8sdeps/transformer/patch/patch.go +++ b/k8sdeps/transformer/patch/transformer.go @@ -1,18 +1,5 @@ -/* -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. -*/ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 package patch @@ -31,27 +18,27 @@ import ( "sigs.k8s.io/kustomize/pkg/transformers" ) -// patchTransformer applies patches. -type patchTransformer struct { +// transformer applies strategic merge patches. +type transformer struct { patches []*resource.Resource rf *resource.Factory } -var _ transformers.Transformer = &patchTransformer{} +var _ transformers.Transformer = &transformer{} -// NewPatchTransformer constructs a patchTransformer. -func NewPatchTransformer( +// NewTransformer constructs a strategic merge patch transformer. +func NewTransformer( slice []*resource.Resource, rf *resource.Factory) (transformers.Transformer, error) { if len(slice) == 0 { return transformers.NewNoOpTransformer(), nil } - return &patchTransformer{patches: slice, rf: rf}, nil + return &transformer{patches: slice, rf: rf}, nil } // Transform apply the patches on top of the base resources. -func (pt *patchTransformer) Transform(baseResourceMap resmap.ResMap) error { +func (tf *transformer) Transform(baseResourceMap resmap.ResMap) error { // Merge and then index the patches by Id. - patches, err := pt.mergePatches() + patches, err := tf.mergePatches() if err != nil { return err } @@ -118,9 +105,9 @@ func (pt *patchTransformer) Transform(baseResourceMap resmap.ResMap) error { // mergePatches merge and index patches by Id. // It errors out if there is conflict between patches. -func (pt *patchTransformer) mergePatches() (resmap.ResMap, error) { +func (tf *transformer) mergePatches() (resmap.ResMap, error) { rc := resmap.ResMap{} - for ix, patch := range pt.patches { + for ix, patch := range tf.patches { id := patch.Id() existing, found := rc[id] if !found { @@ -134,9 +121,9 @@ func (pt *patchTransformer) mergePatches() (resmap.ResMap, error) { } var cd conflictDetector if err != nil { - cd = newJMPConflictDetector(pt.rf) + cd = newJMPConflictDetector(tf.rf) } else { - cd, err = newSMPConflictDetector(versionedObj, pt.rf) + cd, err = newSMPConflictDetector(versionedObj, tf.rf) if err != nil { return nil, err } @@ -147,7 +134,7 @@ func (pt *patchTransformer) mergePatches() (resmap.ResMap, error) { return nil, err } if conflict { - conflictingPatch, err := cd.findConflict(ix, pt.patches) + conflictingPatch, err := cd.findConflict(ix, tf.patches) if err != nil { return nil, err } diff --git a/k8sdeps/transformer/patch/patch_test.go b/k8sdeps/transformer/patch/transformer_test.go similarity index 93% rename from k8sdeps/transformer/patch/patch_test.go rename to k8sdeps/transformer/patch/transformer_test.go index d6955a6ee..a47d2d25d 100644 --- a/k8sdeps/transformer/patch/patch_test.go +++ b/k8sdeps/transformer/patch/transformer_test.go @@ -1,18 +1,5 @@ -/* -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. -*/ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 package patch @@ -128,7 +115,7 @@ func TestOverlayRun(t *testing.T) { }, }), } - lt, err := NewPatchTransformer(patch, rf) + lt, err := NewTransformer(patch, rf) if err != nil { t.Fatalf("unexpected error: %v", err) } @@ -258,7 +245,7 @@ func TestMultiplePatches(t *testing.T) { }, }), } - lt, err := NewPatchTransformer(patch, rf) + lt, err := NewTransformer(patch, rf) if err != nil { t.Fatalf("unexpected error: %v", err) } @@ -344,7 +331,7 @@ func TestMultiplePatchesWithConflict(t *testing.T) { ), } - lt, err := NewPatchTransformer(patch, rf) + lt, err := NewTransformer(patch, rf) if err != nil { t.Fatalf("unexpected error: %v", err) } @@ -407,7 +394,7 @@ func TestNoSchemaOverlayRun(t *testing.T) { }), } - lt, err := NewPatchTransformer(patch, rf) + lt, err := NewTransformer(patch, rf) if err != nil { t.Fatalf("unexpected error: %v", err) } @@ -491,7 +478,7 @@ func TestNoSchemaMultiplePatches(t *testing.T) { }), } - lt, err := NewPatchTransformer(patch, rf) + lt, err := NewTransformer(patch, rf) if err != nil { t.Fatalf("unexpected error: %v", err) } @@ -549,7 +536,7 @@ func TestNoSchemaMultiplePatchesWithConflict(t *testing.T) { }), } - lt, err := NewPatchTransformer(patch, rf) + lt, err := NewTransformer(patch, rf) if err != nil { t.Fatalf("unexpected error: %v", err) }