5
vendor/sigs.k8s.io/kustomize/api/resource/doc.go
generated
vendored
Normal file
5
vendor/sigs.k8s.io/kustomize/api/resource/doc.go
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package resource implements representations of k8s API resources.
|
||||
package resource
|
||||
270
vendor/sigs.k8s.io/kustomize/api/resource/factory.go
generated
vendored
Normal file
270
vendor/sigs.k8s.io/kustomize/api/resource/factory.go
generated
vendored
Normal file
@@ -0,0 +1,270 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package resource
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/ifc"
|
||||
"sigs.k8s.io/kustomize/api/internal/generators"
|
||||
"sigs.k8s.io/kustomize/api/internal/kusterr"
|
||||
"sigs.k8s.io/kustomize/api/konfig"
|
||||
"sigs.k8s.io/kustomize/api/resid"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
// Factory makes instances of Resource.
|
||||
type Factory struct {
|
||||
hasher ifc.KustHasher
|
||||
}
|
||||
|
||||
// NewFactory makes an instance of Factory.
|
||||
func NewFactory(h ifc.KustHasher) *Factory {
|
||||
return &Factory{hasher: h}
|
||||
}
|
||||
|
||||
// Hasher returns an ifc.KustHasher
|
||||
func (rf *Factory) Hasher() ifc.KustHasher {
|
||||
return rf.hasher
|
||||
}
|
||||
|
||||
// FromMap returns a new instance of Resource.
|
||||
func (rf *Factory) FromMap(m map[string]interface{}) *Resource {
|
||||
return rf.FromMapAndOption(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.FromMapWithNamespaceAndName(resid.DefaultNamespace, n, m)
|
||||
}
|
||||
|
||||
// FromMapWithNamespaceAndName returns a new instance with the given "original" namespace.
|
||||
func (rf *Factory) FromMapWithNamespaceAndName(ns string, n string, m map[string]interface{}) *Resource {
|
||||
r := rf.FromMapAndOption(m, nil)
|
||||
return r.setPreviousId(ns, n, r.GetKind())
|
||||
}
|
||||
|
||||
// FromMapAndOption returns a new instance of Resource with given options.
|
||||
func (rf *Factory) FromMapAndOption(
|
||||
m map[string]interface{}, args *types.GeneratorArgs) *Resource {
|
||||
n, err := yaml.FromMap(m)
|
||||
if err != nil {
|
||||
// TODO: return err instead of log.
|
||||
log.Fatal(err)
|
||||
}
|
||||
return rf.makeOne(n, types.NewGenArgs(args))
|
||||
}
|
||||
|
||||
// makeOne returns a new instance of Resource.
|
||||
func (rf *Factory) makeOne(rn *yaml.RNode, o *types.GenArgs) *Resource {
|
||||
if rn == nil {
|
||||
log.Fatal("RNode must not be null")
|
||||
}
|
||||
if o == nil {
|
||||
o = types.NewGenArgs(nil)
|
||||
}
|
||||
return &Resource{node: rn, options: o}
|
||||
}
|
||||
|
||||
// SliceFromPatches returns a slice of resources given a patch path
|
||||
// slice from a kustomization file.
|
||||
func (rf *Factory) SliceFromPatches(
|
||||
ldr ifc.Loader, paths []types.PatchStrategicMerge) ([]*Resource, error) {
|
||||
var result []*Resource
|
||||
for _, path := range paths {
|
||||
content, err := ldr.Load(string(path))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res, err := rf.SliceFromBytes(content)
|
||||
if err != nil {
|
||||
return nil, kusterr.Handler(err, string(path))
|
||||
}
|
||||
result = append(result, res...)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// FromBytes unmarshalls bytes into one Resource.
|
||||
func (rf *Factory) FromBytes(in []byte) (*Resource, error) {
|
||||
result, err := rf.SliceFromBytes(in)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(result) != 1 {
|
||||
return nil, fmt.Errorf(
|
||||
"expected 1 resource, found %d in %v", len(result), in)
|
||||
}
|
||||
return result[0], nil
|
||||
}
|
||||
|
||||
// SliceFromBytes unmarshals bytes into a Resource slice.
|
||||
func (rf *Factory) SliceFromBytes(in []byte) ([]*Resource, error) {
|
||||
nodes, err := rf.RNodesFromBytes(in)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rf.resourcesFromRNodes(nodes), nil
|
||||
}
|
||||
|
||||
// ResourcesFromRNodes converts RNodes to Resources.
|
||||
func (rf *Factory) ResourcesFromRNodes(
|
||||
nodes []*yaml.RNode) (result []*Resource, err error) {
|
||||
nodes, err = rf.dropBadNodes(nodes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rf.resourcesFromRNodes(nodes), nil
|
||||
}
|
||||
|
||||
// resourcesFromRNode assumes all nodes are good.
|
||||
func (rf *Factory) resourcesFromRNodes(
|
||||
nodes []*yaml.RNode) (result []*Resource) {
|
||||
for _, n := range nodes {
|
||||
result = append(result, rf.makeOne(n, nil))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (rf *Factory) RNodesFromBytes(b []byte) (result []*yaml.RNode, err error) {
|
||||
nodes, err := kio.FromBytes(b)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nodes, err = rf.dropBadNodes(nodes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for len(nodes) > 0 {
|
||||
n0 := nodes[0]
|
||||
nodes = nodes[1:]
|
||||
kind := n0.GetKind()
|
||||
if !strings.HasSuffix(kind, "List") {
|
||||
result = append(result, n0)
|
||||
continue
|
||||
}
|
||||
// Convert a FooList into a slice of Foo.
|
||||
var m map[string]interface{}
|
||||
m, err = n0.Map()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items, ok := m["items"]
|
||||
if !ok {
|
||||
// treat as an empty list
|
||||
continue
|
||||
}
|
||||
slice, ok := items.([]interface{})
|
||||
if !ok {
|
||||
if items == nil {
|
||||
// an empty list
|
||||
continue
|
||||
}
|
||||
return nil, fmt.Errorf(
|
||||
"expected array in %s/items, but found %T", kind, items)
|
||||
}
|
||||
innerNodes, err := rf.convertObjectSliceToNodeSlice(slice)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nodes = append(nodes, innerNodes...)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// convertObjectSlice converts a list of objects to a list of RNode.
|
||||
func (rf *Factory) convertObjectSliceToNodeSlice(
|
||||
objects []interface{}) (result []*yaml.RNode, err error) {
|
||||
var bytes []byte
|
||||
var nodes []*yaml.RNode
|
||||
for _, obj := range objects {
|
||||
bytes, err = json.Marshal(obj)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
nodes, err = kio.FromBytes(bytes)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
nodes, err = rf.dropBadNodes(nodes)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
result = append(result, nodes...)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// dropBadNodes may drop some nodes from its input argument.
|
||||
func (rf *Factory) dropBadNodes(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||
var result []*yaml.RNode
|
||||
for _, n := range nodes {
|
||||
ignore, err := rf.shouldIgnore(n)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !ignore {
|
||||
result = append(result, n)
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// shouldIgnore returns true if there's some reason to ignore the node.
|
||||
func (rf *Factory) shouldIgnore(n *yaml.RNode) (bool, error) {
|
||||
if n.IsNilOrEmpty() {
|
||||
return true, nil
|
||||
}
|
||||
md, err := n.GetValidatedMetadata()
|
||||
if err != nil {
|
||||
return true, err
|
||||
}
|
||||
_, ignore := md.ObjectMeta.Annotations[konfig.IgnoredByKustomizeAnnotation]
|
||||
if ignore {
|
||||
return true, nil
|
||||
}
|
||||
if foundNil, path := n.HasNilEntryInList(); foundNil {
|
||||
return true, fmt.Errorf("empty item at %v in object %v", path, n)
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// SliceFromBytesWithNames unmarshals bytes into a Resource slice with specified original
|
||||
// name.
|
||||
func (rf *Factory) SliceFromBytesWithNames(names []string, in []byte) ([]*Resource, error) {
|
||||
result, err := rf.SliceFromBytes(in)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(names) != len(result) {
|
||||
return nil, fmt.Errorf("number of names doesn't match number of resources")
|
||||
}
|
||||
for i, res := range result {
|
||||
res.setPreviousId(resid.DefaultNamespace, names[i], res.GetKind())
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// MakeConfigMap makes an instance of Resource for ConfigMap
|
||||
func (rf *Factory) MakeConfigMap(kvLdr ifc.KvLoader, args *types.ConfigMapArgs) (*Resource, error) {
|
||||
rn, err := generators.MakeConfigMap(kvLdr, args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rf.makeOne(rn, types.NewGenArgs(&args.GeneratorArgs)), nil
|
||||
}
|
||||
|
||||
// MakeSecret makes an instance of Resource for Secret
|
||||
func (rf *Factory) MakeSecret(kvLdr ifc.KvLoader, args *types.SecretArgs) (*Resource, error) {
|
||||
rn, err := generators.MakeSecret(kvLdr, args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rf.makeOne(rn, types.NewGenArgs(&args.GeneratorArgs)), nil
|
||||
}
|
||||
30
vendor/sigs.k8s.io/kustomize/api/resource/idset.go
generated
vendored
Normal file
30
vendor/sigs.k8s.io/kustomize/api/resource/idset.go
generated
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package resource
|
||||
|
||||
import "sigs.k8s.io/kustomize/api/resid"
|
||||
|
||||
type IdSet struct {
|
||||
ids map[resid.ResId]bool
|
||||
}
|
||||
|
||||
func MakeIdSet(slice []*Resource) *IdSet {
|
||||
set := make(map[resid.ResId]bool)
|
||||
for _, r := range slice {
|
||||
id := r.CurId()
|
||||
if _, ok := set[id]; !ok {
|
||||
set[id] = true
|
||||
}
|
||||
}
|
||||
return &IdSet{ids: set}
|
||||
}
|
||||
|
||||
func (s IdSet) Contains(id resid.ResId) bool {
|
||||
_, ok := s.ids[id]
|
||||
return ok
|
||||
}
|
||||
|
||||
func (s IdSet) Size() int {
|
||||
return len(s.ids)
|
||||
}
|
||||
587
vendor/sigs.k8s.io/kustomize/api/resource/resource.go
generated
vendored
Normal file
587
vendor/sigs.k8s.io/kustomize/api/resource/resource.go
generated
vendored
Normal file
@@ -0,0 +1,587 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
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/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
// 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
|
||||
refVarNames []string
|
||||
}
|
||||
|
||||
const (
|
||||
buildAnnotationPreviousKinds = konfig.ConfigAnnoDomain + "/previousKinds"
|
||||
buildAnnotationPreviousNames = konfig.ConfigAnnoDomain + "/previousNames"
|
||||
buildAnnotationPrefixes = konfig.ConfigAnnoDomain + "/prefixes"
|
||||
buildAnnotationSuffixes = konfig.ConfigAnnoDomain + "/suffixes"
|
||||
buildAnnotationPreviousNamespaces = konfig.ConfigAnnoDomain + "/previousNamespaces"
|
||||
|
||||
// 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"
|
||||
)
|
||||
|
||||
var buildAnnotations = []string{
|
||||
buildAnnotationPreviousKinds,
|
||||
buildAnnotationPreviousNames,
|
||||
buildAnnotationPrefixes,
|
||||
buildAnnotationSuffixes,
|
||||
buildAnnotationPreviousNamespaces,
|
||||
buildAnnotationAllowNameChange,
|
||||
buildAnnotationAllowKindChange,
|
||||
}
|
||||
|
||||
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) 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}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func (r *Resource) SetLabels(m map[string]string) {
|
||||
if len(m) == 0 {
|
||||
// Force field erasure.
|
||||
r.node.SetLabels(nil)
|
||||
return
|
||||
}
|
||||
r.node.SetLabels(m)
|
||||
}
|
||||
|
||||
func (r *Resource) SetName(n string) {
|
||||
r.node.SetName(n)
|
||||
}
|
||||
|
||||
func (r *Resource) SetNamespace(n string) {
|
||||
r.node.SetNamespace(n)
|
||||
}
|
||||
|
||||
func (r *Resource) SetKind(k string) {
|
||||
gvk := r.GetGvk()
|
||||
gvk.Kind = k
|
||||
r.SetGvk(gvk)
|
||||
}
|
||||
|
||||
func (r *Resource) UnmarshalJSON(s []byte) error {
|
||||
return r.node.UnmarshalJSON(s)
|
||||
}
|
||||
|
||||
// ResCtx is an interface describing the contextual added
|
||||
// kept kustomize in the context of each Resource object.
|
||||
// Currently mainly the name prefix and name suffix are added.
|
||||
type ResCtx interface {
|
||||
AddNamePrefix(p string)
|
||||
AddNameSuffix(s string)
|
||||
GetNamePrefixes() []string
|
||||
GetNameSuffixes() []string
|
||||
}
|
||||
|
||||
// ResCtxMatcher returns true if two Resources are being
|
||||
// modified in the same kustomize context.
|
||||
type ResCtxMatcher func(ResCtx) bool
|
||||
|
||||
// DeepCopy returns a new copy of resource
|
||||
func (r *Resource) DeepCopy() *Resource {
|
||||
rc := &Resource{
|
||||
node: r.node.Copy(),
|
||||
}
|
||||
rc.copyOtherFields(r)
|
||||
return rc
|
||||
}
|
||||
|
||||
// CopyMergeMetaDataFields 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)
|
||||
}
|
||||
|
||||
func (r *Resource) copyOtherFields(other *Resource) {
|
||||
r.options = other.options
|
||||
r.refBy = other.copyRefBy()
|
||||
r.refVarNames = copyStringSlice(other.refVarNames)
|
||||
}
|
||||
|
||||
func (r *Resource) MergeDataMapFrom(o *Resource) {
|
||||
r.SetDataMap(mergeStringMaps(o.GetDataMap(), r.GetDataMap()))
|
||||
}
|
||||
|
||||
func (r *Resource) MergeBinaryDataMapFrom(o *Resource) {
|
||||
r.SetBinaryDataMap(mergeStringMaps(o.GetBinaryDataMap(), r.GetBinaryDataMap()))
|
||||
}
|
||||
|
||||
func (r *Resource) ErrIfNotEquals(o *Resource) error {
|
||||
meYaml, err := r.AsYAML()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
otherYaml, err := o.AsYAML()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !r.ReferencesEqual(o) {
|
||||
return fmt.Errorf(
|
||||
`unequal references - self:
|
||||
%sreferenced by: %s
|
||||
--- other:
|
||||
%sreferenced by: %s
|
||||
`, meYaml, r.GetRefBy(), otherYaml, o.GetRefBy())
|
||||
}
|
||||
if string(meYaml) != string(otherYaml) {
|
||||
return fmt.Errorf(`--- self:
|
||||
%s
|
||||
--- other:
|
||||
%s
|
||||
`, meYaml, otherYaml)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Resource) ReferencesEqual(other *Resource) bool {
|
||||
setSelf := make(map[resid.ResId]bool)
|
||||
setOther := make(map[resid.ResId]bool)
|
||||
for _, ref := range other.refBy {
|
||||
setOther[ref] = true
|
||||
}
|
||||
for _, ref := range r.refBy {
|
||||
if _, ok := setOther[ref]; !ok {
|
||||
return false
|
||||
}
|
||||
setSelf[ref] = true
|
||||
}
|
||||
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
|
||||
}
|
||||
c := make([]string, len(s))
|
||||
copy(c, s)
|
||||
return c
|
||||
}
|
||||
|
||||
// Implements ResCtx AddNamePrefix
|
||||
func (r *Resource) AddNamePrefix(p string) {
|
||||
r.appendCsvAnnotation(buildAnnotationPrefixes, p)
|
||||
}
|
||||
|
||||
// Implements ResCtx AddNameSuffix
|
||||
func (r *Resource) AddNameSuffix(s string) {
|
||||
r.appendCsvAnnotation(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
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
// Implements ResCtx GetNameSuffixes
|
||||
func (r *Resource) GetNameSuffixes() []string {
|
||||
return r.getCsvAnnotation(buildAnnotationSuffixes)
|
||||
}
|
||||
|
||||
func (r *Resource) getCsvAnnotation(name string) []string {
|
||||
annotations := r.GetAnnotations()
|
||||
if _, ok := annotations[name]; !ok {
|
||||
return nil
|
||||
}
|
||||
return strings.Split(annotations[name], ",")
|
||||
}
|
||||
|
||||
// PrefixesSuffixesEquals is conceptually doing the same task
|
||||
// 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())
|
||||
}
|
||||
|
||||
// RemoveBuildAnnotations removes annotations created by the build process.
|
||||
// These are internal-only to kustomize, added to the data pipeline to
|
||||
// track name changes so name references can be fixed.
|
||||
func (r *Resource) RemoveBuildAnnotations() {
|
||||
annotations := r.GetAnnotations()
|
||||
if len(annotations) == 0 {
|
||||
return
|
||||
}
|
||||
for _, a := range buildAnnotations {
|
||||
delete(annotations, a)
|
||||
}
|
||||
r.SetAnnotations(annotations)
|
||||
}
|
||||
|
||||
func (r *Resource) setPreviousId(ns string, n string, k string) *Resource {
|
||||
r.appendCsvAnnotation(buildAnnotationPreviousNames, n)
|
||||
r.appendCsvAnnotation(buildAnnotationPreviousNamespaces, ns)
|
||||
r.appendCsvAnnotation(buildAnnotationPreviousKinds, k)
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Resource) SetAllowNameChange(value string) {
|
||||
annotations := r.GetAnnotations()
|
||||
annotations[buildAnnotationAllowNameChange] = value
|
||||
r.SetAnnotations(annotations)
|
||||
}
|
||||
|
||||
func (r *Resource) NameChangeAllowed() bool {
|
||||
annotations := r.GetAnnotations()
|
||||
if allowed, set := annotations[buildAnnotationAllowNameChange]; set && allowed == "true" {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (r *Resource) SetAllowKindChange(value string) {
|
||||
annotations := r.GetAnnotations()
|
||||
annotations[buildAnnotationAllowKindChange] = value
|
||||
r.SetAnnotations(annotations)
|
||||
}
|
||||
|
||||
func (r *Resource) KindChangeAllowed() bool {
|
||||
annotations := r.GetAnnotations()
|
||||
if allowed, set := annotations[buildAnnotationAllowKindChange]; set && allowed == "true" {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// String returns resource as JSON.
|
||||
func (r *Resource) String() string {
|
||||
bs, err := r.MarshalJSON()
|
||||
if err != nil {
|
||||
return "<" + err.Error() + ">"
|
||||
}
|
||||
return strings.TrimSpace(string(bs)) + r.options.String()
|
||||
}
|
||||
|
||||
// AsYAML returns the resource in Yaml form.
|
||||
// Easier to read than JSON.
|
||||
func (r *Resource) AsYAML() ([]byte, error) {
|
||||
json, err := r.MarshalJSON()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return yaml.JSONToYAML(json)
|
||||
}
|
||||
|
||||
// MustYaml returns YAML or panics.
|
||||
func (r *Resource) MustYaml() string {
|
||||
yml, err := r.AsYAML()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
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()
|
||||
}
|
||||
|
||||
// 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()
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// OrgId returns the original, immutable ResId for the resource.
|
||||
// This doesn't have to be unique in a ResMap.
|
||||
func (r *Resource) OrgId() resid.ResId {
|
||||
ids := r.PrevIds()
|
||||
if len(ids) > 0 {
|
||||
return ids[0]
|
||||
}
|
||||
return r.CurId()
|
||||
}
|
||||
|
||||
// PrevIds returns a list of ResIds that includes every
|
||||
// previous ResId the resource has had through all of its
|
||||
// GVKN transformations, in the order that it had that ID.
|
||||
// I.e. the oldest ID is first.
|
||||
// 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"))
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
// StorePreviousId stores the resource's current ID via build annotations.
|
||||
func (r *Resource) StorePreviousId() {
|
||||
id := r.CurId()
|
||||
r.setPreviousId(id.EffectiveNamespace(), id.Name, id.Kind)
|
||||
}
|
||||
|
||||
// CurId returns a ResId for the resource using the
|
||||
// mutable parts of the resource.
|
||||
// This should be unique in any ResMap.
|
||||
func (r *Resource) CurId() resid.ResId {
|
||||
return resid.NewResIdWithNamespace(
|
||||
r.GetGvk(), r.GetName(), r.GetNamespace())
|
||||
}
|
||||
|
||||
// GetRefBy returns the ResIds that referred to current resource
|
||||
func (r *Resource) GetRefBy() []resid.ResId {
|
||||
return r.refBy
|
||||
}
|
||||
|
||||
// AppendRefBy appends a ResId into the refBy list
|
||||
func (r *Resource) AppendRefBy(id resid.ResId) {
|
||||
r.refBy = append(r.refBy, id)
|
||||
}
|
||||
|
||||
// GetRefVarNames returns vars that refer to current resource
|
||||
func (r *Resource) GetRefVarNames() []string {
|
||||
return r.refVarNames
|
||||
}
|
||||
|
||||
// AppendRefVarName appends a name of a var into the refVar list
|
||||
func (r *Resource) AppendRefVarName(variable types.Var) {
|
||||
r.refVarNames = append(r.refVarNames, variable.Name)
|
||||
}
|
||||
|
||||
// ApplySmPatch applies the provided strategic merge patch.
|
||||
func (r *Resource) ApplySmPatch(patch *Resource) error {
|
||||
n, ns, k := r.GetName(), r.GetNamespace(), r.GetKind()
|
||||
if patch.NameChangeAllowed() || patch.KindChangeAllowed() {
|
||||
r.StorePreviousId()
|
||||
}
|
||||
if err := r.ApplyFilter(patchstrategicmerge.Filter{
|
||||
Patch: patch.node,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if r.IsEmpty() {
|
||||
return nil
|
||||
}
|
||||
if !patch.KindChangeAllowed() {
|
||||
r.SetKind(k)
|
||||
}
|
||||
if !patch.NameChangeAllowed() {
|
||||
r.SetName(n)
|
||||
}
|
||||
r.SetNamespace(ns)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Resource) ApplyFilter(f kio.Filter) error {
|
||||
l, err := f.Filter([]*kyaml.RNode{r.node})
|
||||
if len(l) == 0 {
|
||||
// The node was deleted. The following makes r.IsEmpty() true.
|
||||
r.node = nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func mergeStringMaps(maps ...map[string]string) map[string]string {
|
||||
result := map[string]string{}
|
||||
for _, m := range maps {
|
||||
for key, value := range m {
|
||||
result[key] = value
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user