Add the rmBuilder test helper.

This commit is contained in:
Jeffrey Regan
2019-06-13 15:32:17 -07:00
parent 16a9975e84
commit 8d9897d5a5
9 changed files with 258 additions and 270 deletions

View File

@@ -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 resource
@@ -51,6 +38,11 @@ func (rf *Factory) FromMapWithName(n string, m map[string]interface{}) *Resource
return rf.makeOne(rf.kf.FromMap(m), nil).setOriginalName(n)
}
// FromMapWithNamespace returns a new instance with the given "original" namespace.
func (rf *Factory) FromMapWithNamespace(n string, m map[string]interface{}) *Resource {
return rf.makeOne(rf.kf.FromMap(m), nil).setOriginalNs(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 {
@@ -75,7 +67,7 @@ func (rf *Factory) makeOne(
Kunstructured: u,
options: o,
}
return r.setOriginalName(r.GetName())
return r.setOriginalName(r.GetName()).setOriginalNs(r.GetNamespace())
}
// SliceFromPatches returns a slice of resources given a patch path

View File

@@ -32,6 +32,7 @@ import (
type Resource struct {
ifc.Kunstructured
originalName string
originalNs string
options *types.GenArgs
refBy []resid.ResId
refVarNames []string
@@ -57,6 +58,7 @@ func (r *Resource) Replace(other *Resource) {
func (r *Resource) copyOtherFields(other *Resource) {
r.originalName = other.originalName
r.originalNs = other.originalNs
r.options = other.options
r.refBy = other.copyRefBy()
r.refVarNames = other.copyRefVarNames()
@@ -98,6 +100,16 @@ func (r *Resource) setOriginalName(n string) *Resource {
return r
}
func (r *Resource) GetOriginalNs() string {
return r.originalNs
}
// Making this public would be bad.
func (r *Resource) setOriginalNs(n string) *Resource {
r.originalNs = n
return r
}
// String returns resource as JSON.
func (r *Resource) String() string {
bs, err := r.MarshalJSON()
@@ -134,10 +146,16 @@ func (r *Resource) GetNamespace() string {
return namespace
}
// Id returns the ResId for the resource.
// Id returns the immutable ResId for the resource.
func (r *Resource) Id() resid.ResId {
return resid.NewResIdWithPrefixNamespace(
r.GetGvk(), r.GetOriginalName(), "", r.GetNamespace())
return resid.NewResIdWithNamespace(
r.GetGvk(), r.GetOriginalName(), r.GetOriginalNs())
}
// FinalId returns a ResId for the resource using the mutable bits.
func (r *Resource) FinalId() resid.ResId {
return resid.NewResIdWithNamespace(
r.GetGvk(), r.GetName(), r.GetNamespace())
}
// GetRefBy returns the ResIds that referred to current resource