Upgrade k8s package verison (#5358)
* upgrade k8s package version Signed-off-by: hongzhouzi <hongzhouzi@kubesphere.io> * Script upgrade and code formatting. Signed-off-by: hongzhouzi <hongzhouzi@kubesphere.io> Signed-off-by: hongzhouzi <hongzhouzi@kubesphere.io>
This commit is contained in:
464
vendor/sigs.k8s.io/kustomize/api/resource/resource.go
generated
vendored
464
vendor/sigs.k8s.io/kustomize/api/resource/resource.go
generated
vendored
@@ -4,18 +4,17 @@
|
||||
package resource
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/filters/patchstrategicmerge"
|
||||
"sigs.k8s.io/kustomize/api/ifc"
|
||||
"sigs.k8s.io/kustomize/api/konfig"
|
||||
"sigs.k8s.io/kustomize/api/resid"
|
||||
"sigs.k8s.io/kustomize/api/internal/utils"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
|
||||
"sigs.k8s.io/kustomize/kyaml/resid"
|
||||
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
@@ -23,173 +22,112 @@ import (
|
||||
// Resource is an RNode, representing a Kubernetes Resource Model object,
|
||||
// paired with metadata used by kustomize.
|
||||
type Resource struct {
|
||||
// TODO: Inline RNode, dropping complexity. Resource is just a decorator.
|
||||
node *kyaml.RNode
|
||||
options *types.GenArgs
|
||||
refBy []resid.ResId
|
||||
kyaml.RNode
|
||||
refVarNames []string
|
||||
}
|
||||
|
||||
const (
|
||||
buildAnnotationPreviousKinds = konfig.ConfigAnnoDomain + "/previousKinds"
|
||||
buildAnnotationPreviousNames = konfig.ConfigAnnoDomain + "/previousNames"
|
||||
buildAnnotationPrefixes = konfig.ConfigAnnoDomain + "/prefixes"
|
||||
buildAnnotationSuffixes = konfig.ConfigAnnoDomain + "/suffixes"
|
||||
buildAnnotationPreviousNamespaces = konfig.ConfigAnnoDomain + "/previousNamespaces"
|
||||
var BuildAnnotations = []string{
|
||||
utils.BuildAnnotationPreviousKinds,
|
||||
utils.BuildAnnotationPreviousNames,
|
||||
utils.BuildAnnotationPrefixes,
|
||||
utils.BuildAnnotationSuffixes,
|
||||
utils.BuildAnnotationPreviousNamespaces,
|
||||
utils.BuildAnnotationAllowNameChange,
|
||||
utils.BuildAnnotationAllowKindChange,
|
||||
utils.BuildAnnotationsRefBy,
|
||||
utils.BuildAnnotationsGenBehavior,
|
||||
utils.BuildAnnotationsGenAddHashSuffix,
|
||||
|
||||
// the following are only for patches, to specify whether they can change names
|
||||
// and kinds of their targets
|
||||
buildAnnotationAllowNameChange = konfig.ConfigAnnoDomain + "/allowNameChange"
|
||||
buildAnnotationAllowKindChange = konfig.ConfigAnnoDomain + "/allowKindChange"
|
||||
)
|
||||
kioutil.PathAnnotation,
|
||||
kioutil.IndexAnnotation,
|
||||
kioutil.SeqIndentAnnotation,
|
||||
kioutil.IdAnnotation,
|
||||
kioutil.InternalAnnotationsMigrationResourceIDAnnotation,
|
||||
|
||||
var buildAnnotations = []string{
|
||||
buildAnnotationPreviousKinds,
|
||||
buildAnnotationPreviousNames,
|
||||
buildAnnotationPrefixes,
|
||||
buildAnnotationSuffixes,
|
||||
buildAnnotationPreviousNamespaces,
|
||||
buildAnnotationAllowNameChange,
|
||||
buildAnnotationAllowKindChange,
|
||||
kioutil.LegacyPathAnnotation,
|
||||
kioutil.LegacyIndexAnnotation,
|
||||
kioutil.LegacyIdAnnotation,
|
||||
}
|
||||
|
||||
func (r *Resource) AsRNode() *kyaml.RNode {
|
||||
return r.node.Copy()
|
||||
}
|
||||
|
||||
func (r *Resource) ResetPrimaryData(incoming *Resource) {
|
||||
r.node = incoming.node.Copy()
|
||||
}
|
||||
|
||||
func (r *Resource) GetAnnotations() map[string]string {
|
||||
annotations, err := r.node.GetAnnotations()
|
||||
if err != nil || annotations == nil {
|
||||
return make(map[string]string)
|
||||
}
|
||||
return annotations
|
||||
}
|
||||
|
||||
func (r *Resource) GetFieldValue(f string) (interface{}, error) {
|
||||
//nolint:staticcheck
|
||||
return r.node.GetFieldValue(f)
|
||||
}
|
||||
|
||||
func (r *Resource) GetDataMap() map[string]string {
|
||||
return r.node.GetDataMap()
|
||||
}
|
||||
|
||||
func (r *Resource) GetBinaryDataMap() map[string]string {
|
||||
return r.node.GetBinaryDataMap()
|
||||
func (r *Resource) ResetRNode(incoming *Resource) {
|
||||
r.RNode = *incoming.Copy()
|
||||
}
|
||||
|
||||
func (r *Resource) GetGvk() resid.Gvk {
|
||||
meta, err := r.node.GetMeta()
|
||||
if err != nil {
|
||||
return resid.GvkFromString("")
|
||||
}
|
||||
g, v := resid.ParseGroupVersion(meta.APIVersion)
|
||||
return resid.Gvk{Group: g, Version: v, Kind: meta.Kind}
|
||||
return resid.GvkFromNode(&r.RNode)
|
||||
}
|
||||
|
||||
func (r *Resource) Hash(h ifc.KustHasher) (string, error) {
|
||||
return h.Hash(r.node)
|
||||
}
|
||||
|
||||
func (r *Resource) GetKind() string {
|
||||
return r.node.GetKind()
|
||||
}
|
||||
|
||||
func (r *Resource) GetLabels() map[string]string {
|
||||
l, err := r.node.GetLabels()
|
||||
if err != nil {
|
||||
return map[string]string{}
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
func (r *Resource) GetName() string {
|
||||
return r.node.GetName()
|
||||
}
|
||||
|
||||
func (r *Resource) GetSlice(p string) ([]interface{}, error) {
|
||||
//nolint:staticcheck
|
||||
return r.node.GetSlice(p)
|
||||
}
|
||||
|
||||
func (r *Resource) GetString(p string) (string, error) {
|
||||
//nolint:staticcheck
|
||||
return r.node.GetString(p)
|
||||
}
|
||||
|
||||
func (r *Resource) IsEmpty() bool {
|
||||
return r.node.IsNilOrEmpty()
|
||||
}
|
||||
|
||||
func (r *Resource) Map() (map[string]interface{}, error) {
|
||||
return r.node.Map()
|
||||
}
|
||||
|
||||
func (r *Resource) MarshalJSON() ([]byte, error) {
|
||||
return r.node.MarshalJSON()
|
||||
}
|
||||
|
||||
func (r *Resource) MatchesLabelSelector(selector string) (bool, error) {
|
||||
return r.node.MatchesLabelSelector(selector)
|
||||
}
|
||||
|
||||
func (r *Resource) MatchesAnnotationSelector(selector string) (bool, error) {
|
||||
return r.node.MatchesAnnotationSelector(selector)
|
||||
}
|
||||
|
||||
func (r *Resource) SetAnnotations(m map[string]string) {
|
||||
if len(m) == 0 {
|
||||
// Force field erasure.
|
||||
r.node.SetAnnotations(nil)
|
||||
return
|
||||
}
|
||||
r.node.SetAnnotations(m)
|
||||
}
|
||||
|
||||
func (r *Resource) SetDataMap(m map[string]string) {
|
||||
r.node.SetDataMap(m)
|
||||
}
|
||||
|
||||
func (r *Resource) SetBinaryDataMap(m map[string]string) {
|
||||
r.node.SetBinaryDataMap(m)
|
||||
return h.Hash(&r.RNode)
|
||||
}
|
||||
|
||||
func (r *Resource) SetGvk(gvk resid.Gvk) {
|
||||
r.node.SetMapField(
|
||||
kyaml.NewScalarRNode(gvk.Kind), kyaml.KindField)
|
||||
r.node.SetMapField(
|
||||
kyaml.NewScalarRNode(gvk.ApiVersion()), kyaml.APIVersionField)
|
||||
r.SetKind(gvk.Kind)
|
||||
r.SetApiVersion(gvk.ApiVersion())
|
||||
}
|
||||
|
||||
func (r *Resource) SetLabels(m map[string]string) {
|
||||
if len(m) == 0 {
|
||||
// Force field erasure.
|
||||
r.node.SetLabels(nil)
|
||||
return
|
||||
func (r *Resource) GetOrigin() (*Origin, error) {
|
||||
annotations := r.GetAnnotations()
|
||||
originAnnotations, ok := annotations[utils.OriginAnnotationKey]
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
r.node.SetLabels(m)
|
||||
var origin Origin
|
||||
if err := yaml.Unmarshal([]byte(originAnnotations), &origin); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &origin, nil
|
||||
}
|
||||
|
||||
func (r *Resource) SetName(n string) {
|
||||
r.node.SetName(n)
|
||||
func (r *Resource) SetOrigin(origin *Origin) error {
|
||||
annotations := r.GetAnnotations()
|
||||
if origin == nil {
|
||||
delete(annotations, utils.OriginAnnotationKey)
|
||||
} else {
|
||||
originStr, err := origin.String()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
annotations[utils.OriginAnnotationKey] = originStr
|
||||
}
|
||||
return r.SetAnnotations(annotations)
|
||||
}
|
||||
|
||||
func (r *Resource) SetNamespace(n string) {
|
||||
r.node.SetNamespace(n)
|
||||
func (r *Resource) GetTransformations() (Transformations, error) {
|
||||
annotations := r.GetAnnotations()
|
||||
transformerAnnotations, ok := annotations[utils.TransformerAnnotationKey]
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
var transformations Transformations
|
||||
if err := yaml.Unmarshal([]byte(transformerAnnotations), &transformations); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return transformations, nil
|
||||
}
|
||||
|
||||
func (r *Resource) SetKind(k string) {
|
||||
gvk := r.GetGvk()
|
||||
gvk.Kind = k
|
||||
r.SetGvk(gvk)
|
||||
func (r *Resource) AddTransformation(origin *Origin) error {
|
||||
annotations := r.GetAnnotations()
|
||||
transformations, err := r.GetTransformations()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if transformations == nil {
|
||||
transformations = Transformations{}
|
||||
}
|
||||
transformations = append(transformations, origin)
|
||||
transformationStr, err := transformations.String()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
annotations[utils.TransformerAnnotationKey] = transformationStr
|
||||
return r.SetAnnotations(annotations)
|
||||
}
|
||||
|
||||
func (r *Resource) UnmarshalJSON(s []byte) error {
|
||||
return r.node.UnmarshalJSON(s)
|
||||
func (r *Resource) ClearTransformations() error {
|
||||
annotations := r.GetAnnotations()
|
||||
delete(annotations, utils.TransformerAnnotationKey)
|
||||
return r.SetAnnotations(annotations)
|
||||
}
|
||||
|
||||
// ResCtx is an interface describing the contextual added
|
||||
@@ -209,26 +147,38 @@ type ResCtxMatcher func(ResCtx) bool
|
||||
// DeepCopy returns a new copy of resource
|
||||
func (r *Resource) DeepCopy() *Resource {
|
||||
rc := &Resource{
|
||||
node: r.node.Copy(),
|
||||
RNode: *r.Copy(),
|
||||
}
|
||||
rc.copyOtherFields(r)
|
||||
rc.copyKustomizeSpecificFields(r)
|
||||
return rc
|
||||
}
|
||||
|
||||
// CopyMergeMetaDataFields copies everything but the non-metadata in
|
||||
// CopyMergeMetaDataFieldsFrom copies everything but the non-metadata in
|
||||
// the resource.
|
||||
func (r *Resource) CopyMergeMetaDataFieldsFrom(other *Resource) {
|
||||
r.SetLabels(mergeStringMaps(other.GetLabels(), r.GetLabels()))
|
||||
r.SetAnnotations(
|
||||
mergeStringMaps(other.GetAnnotations(), r.GetAnnotations()))
|
||||
r.SetName(other.GetName())
|
||||
r.SetNamespace(other.GetNamespace())
|
||||
r.copyOtherFields(other)
|
||||
// TODO: move to RNode, use GetMeta to improve performance.
|
||||
// TODO: make a version of mergeStringMaps that is build-annotation aware
|
||||
// to avoid repeatedly setting refby and genargs annotations
|
||||
// Must remove the kustomize bit at the end.
|
||||
func (r *Resource) CopyMergeMetaDataFieldsFrom(other *Resource) error {
|
||||
if err := r.SetLabels(
|
||||
mergeStringMaps(other.GetLabels(), r.GetLabels())); err != nil {
|
||||
return fmt.Errorf("copyMerge cannot set labels - %w", err)
|
||||
}
|
||||
if err := r.SetAnnotations(
|
||||
mergeStringMapsWithBuildAnnotations(other.GetAnnotations(), r.GetAnnotations())); err != nil {
|
||||
return fmt.Errorf("copyMerge cannot set annotations - %w", err)
|
||||
}
|
||||
if err := r.SetName(other.GetName()); err != nil {
|
||||
return fmt.Errorf("copyMerge cannot set name - %w", err)
|
||||
}
|
||||
if err := r.SetNamespace(other.GetNamespace()); err != nil {
|
||||
return fmt.Errorf("copyMerge cannot set namespace - %w", err)
|
||||
}
|
||||
r.copyKustomizeSpecificFields(other)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Resource) copyOtherFields(other *Resource) {
|
||||
r.options = other.options
|
||||
r.refBy = other.copyRefBy()
|
||||
func (r *Resource) copyKustomizeSpecificFields(other *Resource) {
|
||||
r.refVarNames = copyStringSlice(other.refVarNames)
|
||||
}
|
||||
|
||||
@@ -270,10 +220,10 @@ func (r *Resource) ErrIfNotEquals(o *Resource) error {
|
||||
func (r *Resource) ReferencesEqual(other *Resource) bool {
|
||||
setSelf := make(map[resid.ResId]bool)
|
||||
setOther := make(map[resid.ResId]bool)
|
||||
for _, ref := range other.refBy {
|
||||
for _, ref := range other.GetRefBy() {
|
||||
setOther[ref] = true
|
||||
}
|
||||
for _, ref := range r.refBy {
|
||||
for _, ref := range r.GetRefBy() {
|
||||
if _, ok := setOther[ref]; !ok {
|
||||
return false
|
||||
}
|
||||
@@ -282,21 +232,6 @@ func (r *Resource) ReferencesEqual(other *Resource) bool {
|
||||
return len(setSelf) == len(setOther)
|
||||
}
|
||||
|
||||
// NodeEqual returns true if the resource's nodes are
|
||||
// equal, ignoring ancillary information like genargs, refby, etc.
|
||||
func (r *Resource) NodeEqual(o *Resource) bool {
|
||||
return reflect.DeepEqual(r.node, o.node)
|
||||
}
|
||||
|
||||
func (r *Resource) copyRefBy() []resid.ResId {
|
||||
if r.refBy == nil {
|
||||
return nil
|
||||
}
|
||||
s := make([]resid.ResId, len(r.refBy))
|
||||
copy(s, r.refBy)
|
||||
return s
|
||||
}
|
||||
|
||||
func copyStringSlice(s []string) []string {
|
||||
if s == nil {
|
||||
return nil
|
||||
@@ -308,51 +243,33 @@ func copyStringSlice(s []string) []string {
|
||||
|
||||
// Implements ResCtx AddNamePrefix
|
||||
func (r *Resource) AddNamePrefix(p string) {
|
||||
r.appendCsvAnnotation(buildAnnotationPrefixes, p)
|
||||
r.appendCsvAnnotation(utils.BuildAnnotationPrefixes, p)
|
||||
}
|
||||
|
||||
// Implements ResCtx AddNameSuffix
|
||||
func (r *Resource) AddNameSuffix(s string) {
|
||||
r.appendCsvAnnotation(buildAnnotationSuffixes, s)
|
||||
r.appendCsvAnnotation(utils.BuildAnnotationSuffixes, s)
|
||||
}
|
||||
|
||||
func (r *Resource) appendCsvAnnotation(name, value string) {
|
||||
if value == "" {
|
||||
return
|
||||
}
|
||||
annotations := r.GetAnnotations()
|
||||
if existing, ok := annotations[name]; ok {
|
||||
annotations[name] = existing + "," + value
|
||||
} else {
|
||||
annotations[name] = value
|
||||
currentValue := r.getCsvAnnotation(name)
|
||||
newValue := strings.Join(append(currentValue, value), ",")
|
||||
if err := r.RNode.PipeE(kyaml.SetAnnotation(name, newValue)); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
r.SetAnnotations(annotations)
|
||||
}
|
||||
|
||||
func SameEndingSubarray(shortest, longest []string) bool {
|
||||
if len(shortest) > len(longest) {
|
||||
longest, shortest = shortest, longest
|
||||
}
|
||||
diff := len(longest) - len(shortest)
|
||||
if len(shortest) == 0 {
|
||||
return diff == 0
|
||||
}
|
||||
for i := len(shortest) - 1; i >= 0; i-- {
|
||||
if longest[i+diff] != shortest[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Implements ResCtx GetNamePrefixes
|
||||
func (r *Resource) GetNamePrefixes() []string {
|
||||
return r.getCsvAnnotation(buildAnnotationPrefixes)
|
||||
return r.getCsvAnnotation(utils.BuildAnnotationPrefixes)
|
||||
}
|
||||
|
||||
// Implements ResCtx GetNameSuffixes
|
||||
func (r *Resource) GetNameSuffixes() []string {
|
||||
return r.getCsvAnnotation(buildAnnotationSuffixes)
|
||||
return r.getCsvAnnotation(utils.BuildAnnotationSuffixes)
|
||||
}
|
||||
|
||||
func (r *Resource) getCsvAnnotation(name string) []string {
|
||||
@@ -367,7 +284,8 @@ func (r *Resource) getCsvAnnotation(name string) []string {
|
||||
// as OutermostPrefixSuffix but performs a deeper comparison
|
||||
// of the suffix and prefix slices.
|
||||
func (r *Resource) PrefixesSuffixesEquals(o ResCtx) bool {
|
||||
return SameEndingSubarray(r.GetNamePrefixes(), o.GetNamePrefixes()) && SameEndingSubarray(r.GetNameSuffixes(), o.GetNameSuffixes())
|
||||
return utils.SameEndingSubSlice(r.GetNamePrefixes(), o.GetNamePrefixes()) &&
|
||||
utils.SameEndingSubSlice(r.GetNameSuffixes(), o.GetNameSuffixes())
|
||||
}
|
||||
|
||||
// RemoveBuildAnnotations removes annotations created by the build process.
|
||||
@@ -378,45 +296,53 @@ func (r *Resource) RemoveBuildAnnotations() {
|
||||
if len(annotations) == 0 {
|
||||
return
|
||||
}
|
||||
for _, a := range buildAnnotations {
|
||||
for _, a := range BuildAnnotations {
|
||||
delete(annotations, a)
|
||||
}
|
||||
r.SetAnnotations(annotations)
|
||||
if err := r.SetAnnotations(annotations); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Resource) setPreviousId(ns string, n string, k string) *Resource {
|
||||
r.appendCsvAnnotation(buildAnnotationPreviousNames, n)
|
||||
r.appendCsvAnnotation(buildAnnotationPreviousNamespaces, ns)
|
||||
r.appendCsvAnnotation(buildAnnotationPreviousKinds, k)
|
||||
r.appendCsvAnnotation(utils.BuildAnnotationPreviousNames, n)
|
||||
r.appendCsvAnnotation(utils.BuildAnnotationPreviousNamespaces, ns)
|
||||
r.appendCsvAnnotation(utils.BuildAnnotationPreviousKinds, k)
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Resource) SetAllowNameChange(value string) {
|
||||
annotations := r.GetAnnotations()
|
||||
annotations[buildAnnotationAllowNameChange] = value
|
||||
r.SetAnnotations(annotations)
|
||||
// AllowNameChange allows name changes to the resource.
|
||||
func (r *Resource) AllowNameChange() {
|
||||
r.enable(utils.BuildAnnotationAllowNameChange)
|
||||
}
|
||||
|
||||
// NameChangeAllowed checks if a patch resource is allowed to change another resource's name.
|
||||
func (r *Resource) NameChangeAllowed() bool {
|
||||
annotations := r.GetAnnotations()
|
||||
if allowed, set := annotations[buildAnnotationAllowNameChange]; set && allowed == "true" {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
return r.isEnabled(utils.BuildAnnotationAllowNameChange)
|
||||
}
|
||||
|
||||
func (r *Resource) SetAllowKindChange(value string) {
|
||||
annotations := r.GetAnnotations()
|
||||
annotations[buildAnnotationAllowKindChange] = value
|
||||
r.SetAnnotations(annotations)
|
||||
// AllowKindChange allows kind changes to the resource.
|
||||
func (r *Resource) AllowKindChange() {
|
||||
r.enable(utils.BuildAnnotationAllowKindChange)
|
||||
}
|
||||
|
||||
// KindChangeAllowed checks if a patch resource is allowed to change another resource's kind.
|
||||
func (r *Resource) KindChangeAllowed() bool {
|
||||
return r.isEnabled(utils.BuildAnnotationAllowKindChange)
|
||||
}
|
||||
|
||||
func (r *Resource) isEnabled(annoKey string) bool {
|
||||
annotations := r.GetAnnotations()
|
||||
if allowed, set := annotations[buildAnnotationAllowKindChange]; set && allowed == "true" {
|
||||
return true
|
||||
v, ok := annotations[annoKey]
|
||||
return ok && v == utils.Enabled
|
||||
}
|
||||
|
||||
func (r *Resource) enable(annoKey string) {
|
||||
annotations := r.GetAnnotations()
|
||||
annotations[annoKey] = utils.Enabled
|
||||
if err := r.SetAnnotations(annotations); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// String returns resource as JSON.
|
||||
@@ -425,7 +351,7 @@ func (r *Resource) String() string {
|
||||
if err != nil {
|
||||
return "<" + err.Error() + ">"
|
||||
}
|
||||
return strings.TrimSpace(string(bs)) + r.options.String()
|
||||
return strings.TrimSpace(string(bs))
|
||||
}
|
||||
|
||||
// AsYAML returns the resource in Yaml form.
|
||||
@@ -447,27 +373,34 @@ func (r *Resource) MustYaml() string {
|
||||
return string(yml)
|
||||
}
|
||||
|
||||
// SetOptions updates the generator options for the resource.
|
||||
func (r *Resource) SetOptions(o *types.GenArgs) {
|
||||
r.options = o
|
||||
}
|
||||
|
||||
// Behavior returns the behavior for the resource.
|
||||
func (r *Resource) Behavior() types.GenerationBehavior {
|
||||
return r.options.Behavior()
|
||||
annotations := r.GetAnnotations()
|
||||
if v, ok := annotations[utils.BuildAnnotationsGenBehavior]; ok {
|
||||
return types.NewGenerationBehavior(v)
|
||||
}
|
||||
return types.NewGenerationBehavior("")
|
||||
}
|
||||
|
||||
// SetBehavior sets the behavior for the resource.
|
||||
func (r *Resource) SetBehavior(behavior types.GenerationBehavior) {
|
||||
annotations := r.GetAnnotations()
|
||||
annotations[utils.BuildAnnotationsGenBehavior] = behavior.String()
|
||||
if err := r.SetAnnotations(annotations); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// NeedHashSuffix returns true if a resource content
|
||||
// hash should be appended to the name of the resource.
|
||||
func (r *Resource) NeedHashSuffix() bool {
|
||||
return r.options != nil && r.options.ShouldAddHashSuffixToName()
|
||||
return r.isEnabled(utils.BuildAnnotationsGenAddHashSuffix)
|
||||
}
|
||||
|
||||
// GetNamespace returns the namespace the resource thinks it's in.
|
||||
func (r *Resource) GetNamespace() string {
|
||||
namespace, _ := r.GetString("metadata.namespace")
|
||||
// if err, namespace is empty, so no need to check.
|
||||
return namespace
|
||||
// EnableHashSuffix marks the resource as needing a content
|
||||
// hash to be appended to the name of the resource.
|
||||
func (r *Resource) EnableHashSuffix() {
|
||||
r.enable(utils.BuildAnnotationsGenAddHashSuffix)
|
||||
}
|
||||
|
||||
// OrgId returns the original, immutable ResId for the resource.
|
||||
@@ -487,26 +420,12 @@ func (r *Resource) OrgId() resid.ResId {
|
||||
// The returned array does not include the resource's current
|
||||
// ID. If there are no previous IDs, this will return nil.
|
||||
func (r *Resource) PrevIds() []resid.ResId {
|
||||
var ids []resid.ResId
|
||||
// TODO: merge previous names and namespaces into one list of
|
||||
// pairs on one annotation so there is no chance of error
|
||||
names := r.getCsvAnnotation(buildAnnotationPreviousNames)
|
||||
ns := r.getCsvAnnotation(buildAnnotationPreviousNamespaces)
|
||||
kinds := r.getCsvAnnotation(buildAnnotationPreviousKinds)
|
||||
if len(names) != len(ns) || len(names) != len(kinds) {
|
||||
panic(errors.New(
|
||||
"number of previous names, " +
|
||||
"number of previous namespaces, " +
|
||||
"number of previous kinds not equal"))
|
||||
prevIds, err := utils.PrevIds(&r.RNode)
|
||||
if err != nil {
|
||||
// this should never happen
|
||||
panic(err)
|
||||
}
|
||||
for i := range names {
|
||||
k := kinds[i]
|
||||
gvk := r.GetGvk()
|
||||
gvk.Kind = k
|
||||
ids = append(ids, resid.NewResIdWithNamespace(
|
||||
gvk, names[i], ns[i]))
|
||||
}
|
||||
return ids
|
||||
return prevIds
|
||||
}
|
||||
|
||||
// StorePreviousId stores the resource's current ID via build annotations.
|
||||
@@ -525,12 +444,18 @@ func (r *Resource) CurId() resid.ResId {
|
||||
|
||||
// GetRefBy returns the ResIds that referred to current resource
|
||||
func (r *Resource) GetRefBy() []resid.ResId {
|
||||
return r.refBy
|
||||
var resIds []resid.ResId
|
||||
asStrings := r.getCsvAnnotation(utils.BuildAnnotationsRefBy)
|
||||
for _, s := range asStrings {
|
||||
resIds = append(resIds, resid.FromString(s))
|
||||
}
|
||||
return resIds
|
||||
}
|
||||
|
||||
// AppendRefBy appends a ResId into the refBy list
|
||||
func (r *Resource) AppendRefBy(id resid.ResId) {
|
||||
r.refBy = append(r.refBy, id)
|
||||
// Using any type except fmt.Stringer here results in a compilation error
|
||||
func (r *Resource) AppendRefBy(id fmt.Stringer) {
|
||||
r.appendCsvAnnotation(utils.BuildAnnotationsRefBy, id.String())
|
||||
}
|
||||
|
||||
// GetRefVarNames returns vars that refer to current resource
|
||||
@@ -550,11 +475,11 @@ func (r *Resource) ApplySmPatch(patch *Resource) error {
|
||||
r.StorePreviousId()
|
||||
}
|
||||
if err := r.ApplyFilter(patchstrategicmerge.Filter{
|
||||
Patch: patch.node,
|
||||
Patch: &patch.RNode,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if r.IsEmpty() {
|
||||
if r.IsNilOrEmpty() {
|
||||
return nil
|
||||
}
|
||||
if !patch.KindChangeAllowed() {
|
||||
@@ -568,10 +493,11 @@ func (r *Resource) ApplySmPatch(patch *Resource) error {
|
||||
}
|
||||
|
||||
func (r *Resource) ApplyFilter(f kio.Filter) error {
|
||||
l, err := f.Filter([]*kyaml.RNode{r.node})
|
||||
l, err := f.Filter([]*kyaml.RNode{&r.RNode})
|
||||
if len(l) == 0 {
|
||||
// The node was deleted. The following makes r.IsEmpty() true.
|
||||
r.node = nil
|
||||
// The node was deleted, which means the entire resource
|
||||
// must be deleted. Signal that via the following:
|
||||
r.SetYNode(nil)
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -585,3 +511,17 @@ func mergeStringMaps(maps ...map[string]string) map[string]string {
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func mergeStringMapsWithBuildAnnotations(maps ...map[string]string) map[string]string {
|
||||
result := mergeStringMaps(maps...)
|
||||
for i := range BuildAnnotations {
|
||||
if len(maps) > 0 {
|
||||
if v, ok := maps[0][BuildAnnotations[i]]; ok {
|
||||
result[BuildAnnotations[i]] = v
|
||||
continue
|
||||
}
|
||||
}
|
||||
delete(result, BuildAnnotations[i])
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user