mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-14 02:20:53 +00:00
Add originalName field to resource.
This commit is contained in:
@@ -43,30 +43,39 @@ func (rf *Factory) Hasher() ifc.KunstructuredHasher {
|
||||
|
||||
// FromMap returns a new instance of Resource.
|
||||
func (rf *Factory) FromMap(m map[string]interface{}) *Resource {
|
||||
return &Resource{
|
||||
Kunstructured: rf.kf.FromMap(m),
|
||||
options: types.NewGenArgs(nil, nil),
|
||||
}
|
||||
return rf.makeOne(rf.kf.FromMap(m), nil)
|
||||
}
|
||||
|
||||
// FromMapWithName returns a new instance with the given "original" name.
|
||||
func (rf *Factory) FromMapWithName(n string, m map[string]interface{}) *Resource {
|
||||
return rf.makeOne(rf.kf.FromMap(m), nil).setOriginalName(n)
|
||||
}
|
||||
|
||||
// FromMapAndOption returns a new instance of Resource with given options.
|
||||
func (rf *Factory) FromMapAndOption(m map[string]interface{}, args *types.GeneratorArgs, option *types.GeneratorOptions) *Resource {
|
||||
return &Resource{
|
||||
Kunstructured: rf.kf.FromMap(m),
|
||||
options: types.NewGenArgs(args, option),
|
||||
}
|
||||
func (rf *Factory) FromMapAndOption(
|
||||
m map[string]interface{}, args *types.GeneratorArgs, option *types.GeneratorOptions) *Resource {
|
||||
return rf.makeOne(rf.kf.FromMap(m), types.NewGenArgs(args, option))
|
||||
}
|
||||
|
||||
// FromKunstructured returns a new instance of Resource.
|
||||
func (rf *Factory) FromKunstructured(
|
||||
u ifc.Kunstructured) *Resource {
|
||||
func (rf *Factory) FromKunstructured(u ifc.Kunstructured) *Resource {
|
||||
return rf.makeOne(u, nil)
|
||||
}
|
||||
|
||||
// makeOne returns a new instance of Resource.
|
||||
func (rf *Factory) makeOne(
|
||||
u ifc.Kunstructured, o *types.GenArgs) *Resource {
|
||||
if u == nil {
|
||||
log.Fatal("unstruct ifc must not be null")
|
||||
}
|
||||
return &Resource{
|
||||
Kunstructured: u,
|
||||
options: types.NewGenArgs(nil, nil),
|
||||
if o == nil {
|
||||
o = types.NewGenArgs(nil, nil)
|
||||
}
|
||||
r := &Resource{
|
||||
Kunstructured: u,
|
||||
options: o,
|
||||
}
|
||||
return r.setOriginalName(r.GetName())
|
||||
}
|
||||
|
||||
// SliceFromPatches returns a slice of resources given a patch path
|
||||
@@ -88,7 +97,7 @@ func (rf *Factory) SliceFromPatches(
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// FromBytes unmarshalls bytes into one Resource.
|
||||
// FromBytes unmarshals bytes into one Resource.
|
||||
func (rf *Factory) FromBytes(in []byte) (*Resource, error) {
|
||||
result, err := rf.SliceFromBytes(in)
|
||||
if err != nil {
|
||||
@@ -101,7 +110,7 @@ func (rf *Factory) FromBytes(in []byte) (*Resource, error) {
|
||||
return result[0], nil
|
||||
}
|
||||
|
||||
// SliceFromBytes unmarshalls bytes into a Resource slice.
|
||||
// SliceFromBytes unmarshals bytes into a Resource slice.
|
||||
func (rf *Factory) SliceFromBytes(in []byte) ([]*Resource, error) {
|
||||
kunStructs, err := rf.kf.SliceFromBytes(in)
|
||||
if err != nil {
|
||||
@@ -149,12 +158,11 @@ func (rf *Factory) MakeConfigMap(
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Resource{
|
||||
Kunstructured: u,
|
||||
options: types.NewGenArgs(
|
||||
return rf.makeOne(
|
||||
u,
|
||||
types.NewGenArgs(
|
||||
&types.GeneratorArgs{Behavior: args.Behavior},
|
||||
options),
|
||||
}, nil
|
||||
options)), nil
|
||||
}
|
||||
|
||||
// MakeSecret makes an instance of Resource for Secret
|
||||
@@ -166,10 +174,9 @@ func (rf *Factory) MakeSecret(
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Resource{
|
||||
Kunstructured: u,
|
||||
options: types.NewGenArgs(
|
||||
return rf.makeOne(
|
||||
u,
|
||||
types.NewGenArgs(
|
||||
&types.GeneratorArgs{Behavior: args.Behavior},
|
||||
options),
|
||||
}, nil
|
||||
options)), nil
|
||||
}
|
||||
|
||||
@@ -31,15 +31,69 @@ import (
|
||||
// paired with a GenerationBehavior.
|
||||
type Resource struct {
|
||||
ifc.Kunstructured
|
||||
options *types.GenArgs
|
||||
refBy []resid.ResId
|
||||
refVarNames []string
|
||||
originalName string
|
||||
options *types.GenArgs
|
||||
refBy []resid.ResId
|
||||
refVarNames []string
|
||||
}
|
||||
|
||||
// DeepCopy returns a new copy of resource
|
||||
func (r *Resource) DeepCopy() *Resource {
|
||||
rc := &Resource{
|
||||
Kunstructured: r.Kunstructured.Copy(),
|
||||
}
|
||||
rc.copyOtherFields(r)
|
||||
return rc
|
||||
}
|
||||
|
||||
// Replace performs replace with other resource.
|
||||
func (r *Resource) Replace(other *Resource) {
|
||||
r.SetLabels(mergeStringMaps(other.GetLabels(), r.GetLabels()))
|
||||
r.SetAnnotations(
|
||||
mergeStringMaps(other.GetAnnotations(), r.GetAnnotations()))
|
||||
r.SetName(other.GetName())
|
||||
r.copyOtherFields(other)
|
||||
}
|
||||
|
||||
func (r *Resource) copyOtherFields(other *Resource) {
|
||||
r.originalName = other.originalName
|
||||
r.options = other.options
|
||||
r.refBy = other.copyRefBy()
|
||||
r.refVarNames = other.copyRefVarNames()
|
||||
}
|
||||
|
||||
// Merge performs merge with other resource.
|
||||
func (r *Resource) Merge(other *Resource) {
|
||||
r.Replace(other)
|
||||
mergeConfigmap(r.Map(), other.Map(), r.Map())
|
||||
}
|
||||
|
||||
func (r *Resource) copyRefBy() []resid.ResId {
|
||||
s := make([]resid.ResId, len(r.refBy))
|
||||
copy(s, r.refBy)
|
||||
return s
|
||||
}
|
||||
|
||||
func (r *Resource) copyRefVarNames() []string {
|
||||
s := make([]string, len(r.refVarNames))
|
||||
copy(s, r.refVarNames)
|
||||
return s
|
||||
}
|
||||
|
||||
func (r *Resource) KunstructEqual(o *Resource) bool {
|
||||
return reflect.DeepEqual(r.Kunstructured, o.Kunstructured)
|
||||
}
|
||||
|
||||
func (r *Resource) GetOriginalName() string {
|
||||
return r.originalName
|
||||
}
|
||||
|
||||
// Making this public would be bad.
|
||||
func (r *Resource) setOriginalName(n string) *Resource {
|
||||
r.originalName = n
|
||||
return r
|
||||
}
|
||||
|
||||
// String returns resource as JSON.
|
||||
func (r *Resource) String() string {
|
||||
bs, err := r.MarshalJSON()
|
||||
@@ -49,25 +103,6 @@ func (r *Resource) String() string {
|
||||
return strings.TrimSpace(string(bs)) + r.options.String()
|
||||
}
|
||||
|
||||
// DeepCopy returns a new copy of resource
|
||||
func (r *Resource) DeepCopy() *Resource {
|
||||
rc := &Resource{
|
||||
Kunstructured: r.Kunstructured.Copy(),
|
||||
options: r.options,
|
||||
}
|
||||
if len(r.refBy) > 0 {
|
||||
refby := make([]resid.ResId, len(r.refBy))
|
||||
copy(refby, r.refBy)
|
||||
rc.refBy = refby
|
||||
}
|
||||
if len(r.refVarNames) > 0 {
|
||||
refVarNames := make([]string, len(r.refVarNames))
|
||||
copy(refVarNames, r.refVarNames)
|
||||
rc.refVarNames = refVarNames
|
||||
}
|
||||
return rc
|
||||
}
|
||||
|
||||
// AsYAML returns the resource in Yaml form.
|
||||
// Easier to read than JSON.
|
||||
func (r *Resource) AsYAML() ([]byte, error) {
|
||||
@@ -88,10 +123,17 @@ func (r *Resource) NeedHashSuffix() bool {
|
||||
return r.options != nil && r.options.NeedsHashSuffix()
|
||||
}
|
||||
|
||||
// GetNamespace returns the namespace the resource thinks it's in.
|
||||
func (r *Resource) GetNamespace() string {
|
||||
namespace, _ := r.GetFieldValue("metadata.namespace")
|
||||
// if err, namespace is empty, so no need to check.
|
||||
return namespace
|
||||
}
|
||||
|
||||
// Id returns the ResId for the resource.
|
||||
func (r *Resource) Id() resid.ResId {
|
||||
namespace, _ := r.GetFieldValue("metadata.namespace")
|
||||
return resid.NewResIdWithPrefixNamespace(r.GetGvk(), r.GetName(), "", namespace)
|
||||
return resid.NewResIdWithPrefixNamespace(
|
||||
r.GetGvk(), r.GetOriginalName(), "", r.GetNamespace())
|
||||
}
|
||||
|
||||
// GetRefBy returns the ResIds that referred to current resource
|
||||
@@ -114,21 +156,6 @@ func (r *Resource) AppendRefVarName(variable types.Var) {
|
||||
r.refVarNames = append(r.refVarNames, variable.Name)
|
||||
}
|
||||
|
||||
// Merge performs merge with other resource.
|
||||
func (r *Resource) Merge(other *Resource) {
|
||||
r.Replace(other)
|
||||
mergeConfigmap(r.Map(), other.Map(), r.Map())
|
||||
}
|
||||
|
||||
// Replace performs replace with other resource.
|
||||
func (r *Resource) Replace(other *Resource) {
|
||||
r.SetLabels(mergeStringMaps(other.GetLabels(), r.GetLabels()))
|
||||
r.SetAnnotations(
|
||||
mergeStringMaps(other.GetAnnotations(), r.GetAnnotations()))
|
||||
r.SetName(other.GetName())
|
||||
r.options = other.options
|
||||
}
|
||||
|
||||
// TODO: Add BinaryData once we sync to new k8s.io/api
|
||||
func mergeConfigmap(
|
||||
mergedTo map[string]interface{},
|
||||
|
||||
@@ -98,7 +98,8 @@ func TestResourceId(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
in: testConfigMap,
|
||||
id: resid.NewResIdWithPrefixNamespace(gvk.Gvk{Version: "v1", Kind: "ConfigMap"}, "winnie", "", "hundred-acre-wood"),
|
||||
id: resid.NewResIdWithNamespace(
|
||||
gvk.Gvk{Version: "v1", Kind: "ConfigMap"}, "winnie", "hundred-acre-wood"),
|
||||
},
|
||||
{
|
||||
in: testDeployment,
|
||||
|
||||
Reference in New Issue
Block a user