Maintain resources in order loaded.

This commit is contained in:
Jeffrey Regan
2019-06-03 11:22:53 -07:00
parent af57fc3ece
commit 4162dbc2d8
39 changed files with 1074 additions and 617 deletions

View File

@@ -1,27 +1,13 @@
/*
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 resmap
import (
"fmt"
"github.com/pkg/errors"
"sigs.k8s.io/kustomize/internal/kusterr"
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resource"
"sigs.k8s.io/kustomize/pkg/types"
)
@@ -41,11 +27,17 @@ func (rmF *Factory) RF() *resource.Factory {
return rmF.resF
}
func New() ResMap {
return newOne()
}
// FromResource returns a ResMap with one entry.
func (rmF *Factory) FromResource(res *resource.Resource) ResMap {
result := ResMap{}
result[res.Id()] = res
return result
m, err := newResMapFromResourceSlice([]*resource.Resource{res})
if err != nil {
panic(err)
}
return m
}
// FromFile returns a ResMap given a resource path.
@@ -55,26 +47,41 @@ func (rmF *Factory) FromFile(
if err != nil {
return nil, err
}
res, err := rmF.NewResMapFromBytes(content)
m, err := rmF.NewResMapFromBytes(content)
if err != nil {
return nil, kusterr.Handler(err, path)
}
return res, nil
return m, nil
}
// newResMapFromBytes decodes a list of objects in byte array format.
// NewResMapFromBytes decodes a list of objects in byte array format.
func (rmF *Factory) NewResMapFromBytes(b []byte) (ResMap, error) {
resources, err := rmF.resF.SliceFromBytes(b)
if err != nil {
return nil, err
}
result := ResMap{}
for _, res := range resources {
id := res.Id()
if _, found := result[id]; found {
return result, fmt.Errorf("GroupVersionKindName: %#v already exists in the map", id)
return newResMapFromResourceSlice(resources)
}
// Deprecated.
// FromMap returns a ResMap with arbitrary internal ordering,
// panicing on error. For tests only.
// See also ErrorIfNotEqual.
func FromMap(arg map[resid.ResId]*resource.Resource) ResMap {
result, err := fromMap(arg)
if err != nil {
panic(err)
}
return result
}
func fromMap(arg map[resid.ResId]*resource.Resource) (ResMap, error) {
result := New()
for id, r := range arg {
err := result.AppendWithId(id, r)
if err != nil {
return nil, err
}
result[id] = res
}
return result, nil
}
@@ -136,13 +143,12 @@ func (rmF *Factory) FromSecretArgs(
}
func newResMapFromResourceSlice(resources []*resource.Resource) (ResMap, error) {
result := ResMap{}
result := New()
for _, res := range resources {
id := res.Id()
if _, found := result[id]; found {
return nil, fmt.Errorf("duplicated %#v is not allowed", id)
err := result.Append(res)
if err != nil {
return nil, err
}
result[id] = res
}
return result, nil
}