diff --git a/pkg/resmap/resmap.go b/pkg/resmap/resmap.go index 9c9e194fc..58e552137 100644 --- a/pkg/resmap/resmap.go +++ b/pkg/resmap/resmap.go @@ -39,8 +39,23 @@ type ResMap interface { // as appended. Resources() []*resource.Resource - // Append adds a Resource. - // Error on OrgId collision. + // Append adds a Resource. Error on CurId collision. + // + // A class invariant of ResMap is that all of its + // resources must differ in their value of + // CurId(), aka current Id. The Id is the tuple + // of {namespace, group, version, kind, name} + // (see ResId). + // + // This invariant reflects the invariant of a + // kubernetes cluster, where if one tries to add + // a resource to the cluster whose Id matches + // that of a resource already in the cluster, + // only two outcomes are allowed. Either the + // incoming resource is _merged_ into the existing + // one, or the incoming resource is rejected. + // One cannot end up with two resources + // in the cluster with the same Id. Append(*resource.Resource) error // AppendAll appends another ResMap to self, diff --git a/pkg/resmap/resmap_test.go b/pkg/resmap/resmap_test.go index babe8cb20..cb8742599 100644 --- a/pkg/resmap/resmap_test.go +++ b/pkg/resmap/resmap_test.go @@ -6,6 +6,7 @@ package resmap_test import ( "fmt" "reflect" + "strings" "testing" "sigs.k8s.io/kustomize/v3/k8sdeps/kunstruct" @@ -45,6 +46,24 @@ func makeCm(i int) *resource.Resource { }) } +// Maintain the class invariant that no two +// resources can have the same CurId(). +func TestAppendRejectsDuplicateResId(t *testing.T) { + w := New() + if err := w.Append(makeCm(1)); err != nil { + t.Fatalf("append error: %v", err) + } + err := w.Append(makeCm(1)) + if err == nil { + t.Fatalf("expected append error") + } + if !strings.Contains( + err.Error(), + "may not add resource with an already registered id") { + t.Fatalf("unexpected error: %v", err) + } +} + func TestAppendRemove(t *testing.T) { w1 := New() doAppend(t, w1, makeCm(1)) diff --git a/plugin/builtin/namespacetransformer/NamespaceTransformer.go b/plugin/builtin/namespacetransformer/NamespaceTransformer.go index 5bc887e5d..eb0e6732a 100644 --- a/plugin/builtin/namespacetransformer/NamespaceTransformer.go +++ b/plugin/builtin/namespacetransformer/NamespaceTransformer.go @@ -11,15 +11,15 @@ import ( "sigs.k8s.io/kustomize/v3/pkg/resmap" "sigs.k8s.io/kustomize/v3/pkg/resource" "sigs.k8s.io/kustomize/v3/pkg/transformers" - "sigs.k8s.io/kustomize/v3/pkg/types" "sigs.k8s.io/kustomize/v3/pkg/transformers/config" + "sigs.k8s.io/kustomize/v3/pkg/types" "sigs.k8s.io/yaml" ) // Change or set the namespace of non-cluster level resources. type plugin struct { types.ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"` - FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"` + FieldSpecs []config.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"` } //noinspection GoUnusedGlobalVariable diff --git a/plugin/builtin/patchjson6902transformer/PatchJson6902Transformer.go b/plugin/builtin/patchjson6902transformer/PatchJson6902Transformer.go index ba74db05a..e21e88d11 100644 --- a/plugin/builtin/patchjson6902transformer/PatchJson6902Transformer.go +++ b/plugin/builtin/patchjson6902transformer/PatchJson6902Transformer.go @@ -17,11 +17,11 @@ import ( ) type plugin struct { - ldr ifc.Loader + ldr ifc.Loader decodedPatch jsonpatch.Patch - Target types.PatchTarget `json:"target,omitempty" yaml:"target,omitempty"` - Path string `json:"path,omitempty" yaml:"path,omitempty"` - JsonOp string `json:"jsonOp,omitempty" yaml:"jsonOp,omitempty"` + Target types.PatchTarget `json:"target,omitempty" yaml:"target,omitempty"` + Path string `json:"path,omitempty" yaml:"path,omitempty"` + JsonOp string `json:"jsonOp,omitempty" yaml:"jsonOp,omitempty"` } //noinspection GoUnusedGlobalVariable