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:
103
vendor/sigs.k8s.io/kustomize/api/resource/factory.go
generated
vendored
103
vendor/sigs.k8s.io/kustomize/api/resource/factory.go
generated
vendored
@@ -13,15 +13,21 @@ import (
|
||||
"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/resid"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
// Factory makes instances of Resource.
|
||||
type Factory struct {
|
||||
hasher ifc.KustHasher
|
||||
|
||||
// When set to true, IncludeLocalConfigs indicates
|
||||
// that Factory should include resources with the
|
||||
// annotation 'config.kubernetes.io/local-config'.
|
||||
// By default these resources are ignored.
|
||||
IncludeLocalConfigs bool
|
||||
}
|
||||
|
||||
// NewFactory makes an instance of Factory.
|
||||
@@ -58,18 +64,23 @@ func (rf *Factory) FromMapAndOption(
|
||||
// TODO: return err instead of log.
|
||||
log.Fatal(err)
|
||||
}
|
||||
return rf.makeOne(n, types.NewGenArgs(args))
|
||||
return rf.makeOne(n, args)
|
||||
}
|
||||
|
||||
// makeOne returns a new instance of Resource.
|
||||
func (rf *Factory) makeOne(rn *yaml.RNode, o *types.GenArgs) *Resource {
|
||||
func (rf *Factory) makeOne(rn *yaml.RNode, o *types.GeneratorArgs) *Resource {
|
||||
if rn == nil {
|
||||
log.Fatal("RNode must not be null")
|
||||
}
|
||||
if o == nil {
|
||||
o = types.NewGenArgs(nil)
|
||||
resource := &Resource{RNode: *rn}
|
||||
if o != nil {
|
||||
if o.Options == nil || !o.Options.DisableNameSuffixHash {
|
||||
resource.EnableHashSuffix()
|
||||
}
|
||||
resource.SetBehavior(types.NewGenerationBehavior(o.Behavior))
|
||||
}
|
||||
return &Resource{node: rn, options: o}
|
||||
|
||||
return resource
|
||||
}
|
||||
|
||||
// SliceFromPatches returns a slice of resources given a patch path
|
||||
@@ -113,14 +124,34 @@ func (rf *Factory) SliceFromBytes(in []byte) ([]*Resource, error) {
|
||||
return rf.resourcesFromRNodes(nodes), nil
|
||||
}
|
||||
|
||||
// DropLocalNodes removes the local nodes by default. Local nodes are detected via the annotation `config.kubernetes.io/local-config: "true"`
|
||||
func (rf *Factory) DropLocalNodes(nodes []*yaml.RNode) ([]*Resource, error) {
|
||||
var result []*yaml.RNode
|
||||
for _, node := range nodes {
|
||||
if node.IsNilOrEmpty() {
|
||||
continue
|
||||
}
|
||||
md, err := node.GetValidatedMetadata()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if rf.IncludeLocalConfigs {
|
||||
result = append(result, node)
|
||||
continue
|
||||
}
|
||||
localConfig, exist := md.ObjectMeta.Annotations[konfig.IgnoredByKustomizeAnnotation]
|
||||
if !exist || localConfig == "false" {
|
||||
result = append(result, node)
|
||||
}
|
||||
}
|
||||
return rf.resourcesFromRNodes(result), 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
|
||||
return rf.DropLocalNodes(nodes)
|
||||
}
|
||||
|
||||
// resourcesFromRNode assumes all nodes are good.
|
||||
@@ -132,7 +163,7 @@ func (rf *Factory) resourcesFromRNodes(
|
||||
return
|
||||
}
|
||||
|
||||
func (rf *Factory) RNodesFromBytes(b []byte) (result []*yaml.RNode, err error) {
|
||||
func (rf *Factory) RNodesFromBytes(b []byte) ([]*yaml.RNode, error) {
|
||||
nodes, err := kio.FromBytes(b)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -141,9 +172,17 @@ func (rf *Factory) RNodesFromBytes(b []byte) (result []*yaml.RNode, err error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rf.inlineAnyEmbeddedLists(nodes)
|
||||
}
|
||||
|
||||
// inlineAnyEmbeddedLists scans the RNode slice for nodes named FooList.
|
||||
// Such nodes are expected to be lists of resources, each of type Foo.
|
||||
// These lists are replaced in the result by their inlined resources.
|
||||
func (rf *Factory) inlineAnyEmbeddedLists(
|
||||
nodes []*yaml.RNode) (result []*yaml.RNode, err error) {
|
||||
var n0 *yaml.RNode
|
||||
for len(nodes) > 0 {
|
||||
n0 := nodes[0]
|
||||
nodes = nodes[1:]
|
||||
n0, nodes = nodes[0], nodes[1:]
|
||||
kind := n0.GetKind()
|
||||
if !strings.HasSuffix(kind, "List") {
|
||||
result = append(result, n0)
|
||||
@@ -153,7 +192,7 @@ func (rf *Factory) RNodesFromBytes(b []byte) (result []*yaml.RNode, err error) {
|
||||
var m map[string]interface{}
|
||||
m, err = n0.Map()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("trouble expanding list of %s; %w", kind, err)
|
||||
}
|
||||
items, ok := m["items"]
|
||||
if !ok {
|
||||
@@ -205,36 +244,20 @@ func (rf *Factory) convertObjectSliceToNodeSlice(
|
||||
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 {
|
||||
if n.IsNilOrEmpty() {
|
||||
continue
|
||||
}
|
||||
if _, err := n.GetValidatedMetadata(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !ignore {
|
||||
result = append(result, n)
|
||||
if foundNil, path := n.HasNilEntryInList(); foundNil {
|
||||
return nil, fmt.Errorf("empty item at %v in object %v", path, n)
|
||||
}
|
||||
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) {
|
||||
@@ -257,7 +280,7 @@ func (rf *Factory) MakeConfigMap(kvLdr ifc.KvLoader, args *types.ConfigMapArgs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rf.makeOne(rn, types.NewGenArgs(&args.GeneratorArgs)), nil
|
||||
return rf.makeOne(rn, &args.GeneratorArgs), nil
|
||||
}
|
||||
|
||||
// MakeSecret makes an instance of Resource for Secret
|
||||
@@ -266,5 +289,5 @@ func (rf *Factory) MakeSecret(kvLdr ifc.KvLoader, args *types.SecretArgs) (*Reso
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rf.makeOne(rn, types.NewGenArgs(&args.GeneratorArgs)), nil
|
||||
return rf.makeOne(rn, &args.GeneratorArgs), nil
|
||||
}
|
||||
|
||||
2
vendor/sigs.k8s.io/kustomize/api/resource/idset.go
generated
vendored
2
vendor/sigs.k8s.io/kustomize/api/resource/idset.go
generated
vendored
@@ -3,7 +3,7 @@
|
||||
|
||||
package resource
|
||||
|
||||
import "sigs.k8s.io/kustomize/api/resid"
|
||||
import "sigs.k8s.io/kustomize/kyaml/resid"
|
||||
|
||||
type IdSet struct {
|
||||
ids map[resid.ResId]bool
|
||||
|
||||
106
vendor/sigs.k8s.io/kustomize/api/resource/origin.go
generated
vendored
Normal file
106
vendor/sigs.k8s.io/kustomize/api/resource/origin.go
generated
vendored
Normal file
@@ -0,0 +1,106 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package resource
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/internal/git"
|
||||
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
// Origin retains information about the origin of resources and transformer configs
|
||||
// that contributed to the output of `kustomize build`
|
||||
type Origin struct {
|
||||
// Path is the path to the resource. If a local resource, this path is
|
||||
// rooted from the directory upon which `kustomize build` was invoked. If a
|
||||
// remote resource, this path is rooted from the root of the remote repo.
|
||||
Path string `json:"path,omitempty" yaml:"path,omitempty"`
|
||||
|
||||
// Repo is the remote repository that the resource or transformer originated from if it is
|
||||
// not from a local file
|
||||
Repo string `json:"repo,omitempty" yaml:"repo,omitempty"`
|
||||
|
||||
// Ref is the ref of the remote repository that the resource or transformer originated from
|
||||
// if it is not from a local file
|
||||
Ref string `json:"ref,omitempty" yaml:"ref,omitempty"`
|
||||
|
||||
// The following fields only apply to resources that have been
|
||||
// generated by fields other than the `resources` field, or to transformer
|
||||
// configs.
|
||||
|
||||
// ConfiguredIn is the file path to the generator or transformer config that created the
|
||||
// resource
|
||||
ConfiguredIn string `json:"configuredIn,omitempty" yaml:"configuredIn,omitempty"`
|
||||
|
||||
// ConfiguredBy is the ObjectReference of the generator or transformer config
|
||||
ConfiguredBy kyaml.ResourceIdentifier `json:"configuredBy,omitempty" yaml:"configuredBy,omitempty"`
|
||||
}
|
||||
|
||||
// Copy returns a copy of origin
|
||||
func (origin *Origin) Copy() Origin {
|
||||
if origin == nil {
|
||||
return Origin{}
|
||||
}
|
||||
return *origin
|
||||
}
|
||||
|
||||
// Append returns a copy of origin with a path appended to it
|
||||
func (origin *Origin) Append(path string) *Origin {
|
||||
originCopy := origin.Copy()
|
||||
repoSpec, err := git.NewRepoSpecFromURL(path)
|
||||
if err == nil {
|
||||
originCopy.Repo = repoSpec.Host + repoSpec.OrgRepo
|
||||
absPath := repoSpec.AbsPath()
|
||||
path = absPath[strings.Index(absPath[1:], "/")+1:][1:]
|
||||
originCopy.Path = ""
|
||||
originCopy.Ref = repoSpec.Ref
|
||||
}
|
||||
originCopy.Path = filepath.Join(originCopy.Path, path)
|
||||
return &originCopy
|
||||
}
|
||||
|
||||
// String returns a string version of origin
|
||||
func (origin *Origin) String() (string, error) {
|
||||
anno, err := kyaml.Marshal(origin)
|
||||
return string(anno), err
|
||||
}
|
||||
|
||||
// Transformations is a list of Origin
|
||||
type Transformations []*Origin
|
||||
|
||||
// String returns a string version of Transformations
|
||||
func (transformations *Transformations) String() (string, error) {
|
||||
anno, err := kyaml.Marshal(transformations)
|
||||
return string(anno), err
|
||||
}
|
||||
|
||||
// OriginFromCustomPlugin takes a custom plugin defined as a resource
|
||||
// and returns an origin object to describe it
|
||||
func OriginFromCustomPlugin(res *Resource) (*Origin, error) {
|
||||
origin, err := res.GetOrigin()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result *Origin
|
||||
if origin != nil {
|
||||
result = &Origin{
|
||||
Repo: origin.Repo,
|
||||
Ref: origin.Ref,
|
||||
ConfiguredIn: origin.Path,
|
||||
ConfiguredBy: kyaml.ResourceIdentifier{
|
||||
TypeMeta: kyaml.TypeMeta{
|
||||
APIVersion: res.GetApiVersion(),
|
||||
Kind: res.GetKind(),
|
||||
},
|
||||
NameMeta: kyaml.NameMeta{
|
||||
Name: res.GetName(),
|
||||
Namespace: res.GetNamespace(),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
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