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:
108
vendor/sigs.k8s.io/kustomize/kyaml/yaml/fns.go
generated
vendored
108
vendor/sigs.k8s.io/kustomize/kyaml/yaml/fns.go
generated
vendored
@@ -10,8 +10,8 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"gopkg.in/yaml.v3"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/internal/forked/github.com/go-yaml/yaml"
|
||||
)
|
||||
|
||||
// Append creates an ElementAppender
|
||||
@@ -424,12 +424,46 @@ func Lookup(path ...string) PathGetter {
|
||||
return PathGetter{Path: path}
|
||||
}
|
||||
|
||||
// Lookup returns a PathGetter to lookup a field by its path and create it if it doesn't already
|
||||
// LookupCreate returns a PathGetter to lookup a field by its path and create it if it doesn't already
|
||||
// exist.
|
||||
func LookupCreate(kind yaml.Kind, path ...string) PathGetter {
|
||||
return PathGetter{Path: path, Create: kind}
|
||||
}
|
||||
|
||||
// ConventionalContainerPaths is a list of paths at which containers typically appear in workload APIs.
|
||||
// It is intended for use with LookupFirstMatch.
|
||||
var ConventionalContainerPaths = [][]string{
|
||||
// e.g. Deployment, ReplicaSet, DaemonSet, Job, StatefulSet
|
||||
{"spec", "template", "spec", "containers"},
|
||||
// e.g. CronJob
|
||||
{"spec", "jobTemplate", "spec", "template", "spec", "containers"},
|
||||
// e.g. Pod
|
||||
{"spec", "containers"},
|
||||
// e.g. PodTemplate
|
||||
{"template", "spec", "containers"},
|
||||
}
|
||||
|
||||
// LookupFirstMatch returns a Filter for locating a value that may exist at one of several possible paths.
|
||||
// For example, it can be used with ConventionalContainerPaths to find the containers field in a standard workload resource.
|
||||
// If more than one of the paths exists in the resource, the first will be returned. If none exist,
|
||||
// nil will be returned. If an error is encountered during lookup, it will be returned.
|
||||
func LookupFirstMatch(paths [][]string) Filter {
|
||||
return FilterFunc(func(object *RNode) (*RNode, error) {
|
||||
var result *RNode
|
||||
var err error
|
||||
for _, path := range paths {
|
||||
result, err = object.Pipe(PathGetter{Path: path})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err)
|
||||
}
|
||||
if result != nil {
|
||||
return result, nil
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
})
|
||||
}
|
||||
|
||||
// PathGetter returns the RNode under Path.
|
||||
type PathGetter struct {
|
||||
Kind string `yaml:"kind,omitempty"`
|
||||
@@ -507,6 +541,9 @@ func (l PathGetter) getFilter(part, nextPart string, fieldPath *[]string) (Filte
|
||||
case part == "-":
|
||||
// part is a hyphen
|
||||
return GetElementByIndex(-1), nil
|
||||
case part == "*":
|
||||
// PathGetter is not support for wildcard matching
|
||||
return nil, errors.Errorf("wildcard is not supported in PathGetter")
|
||||
case IsListIndex(part):
|
||||
// part is surrounded by brackets
|
||||
return l.elemFilter(part)
|
||||
@@ -518,7 +555,6 @@ func (l PathGetter) getFilter(part, nextPart string, fieldPath *[]string) (Filte
|
||||
}
|
||||
|
||||
func (l PathGetter) elemFilter(part string) (Filter, error) {
|
||||
var match *RNode
|
||||
name, value, err := SplitIndexNameValue(part)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err)
|
||||
@@ -533,10 +569,9 @@ func (l PathGetter) elemFilter(part string) (Filter, error) {
|
||||
// append a ScalarNode
|
||||
elem = NewScalarRNode(value)
|
||||
elem.YNode().Style = l.Style
|
||||
match = elem
|
||||
} else {
|
||||
// append a MappingNode
|
||||
match = NewRNode(&yaml.Node{Kind: yaml.ScalarNode, Value: value, Style: l.Style})
|
||||
match := NewRNode(&yaml.Node{Kind: yaml.ScalarNode, Value: value, Style: l.Style})
|
||||
elem = NewRNode(&yaml.Node{
|
||||
Kind: yaml.MappingNode,
|
||||
Content: []*yaml.Node{{Kind: yaml.ScalarNode, Value: name}, match.YNode()},
|
||||
@@ -578,6 +613,50 @@ func Set(value *RNode) FieldSetter {
|
||||
return FieldSetter{Value: value}
|
||||
}
|
||||
|
||||
// MapEntrySetter sets a map entry to a value. If it finds a key with the same
|
||||
// value, it will override both Key and Value RNodes, including style and any
|
||||
// other metadata. If it doesn't find the key, it will insert a new map entry.
|
||||
// It will set the field, even if it's empty or nil, unlike the FieldSetter.
|
||||
// This is useful for rebuilding some pre-existing RNode structure.
|
||||
type MapEntrySetter struct {
|
||||
// Name is the name of the field or key to lookup in a MappingNode.
|
||||
// If Name is unspecified, it will use the Key's Value
|
||||
Name string `yaml:"name,omitempty"`
|
||||
|
||||
// Value is the value to set.
|
||||
Value *RNode `yaml:"value,omitempty"`
|
||||
|
||||
// Key is the map key to set.
|
||||
Key *RNode `yaml:"key,omitempty"`
|
||||
}
|
||||
|
||||
func (s MapEntrySetter) Filter(rn *RNode) (*RNode, error) {
|
||||
if rn == nil {
|
||||
return nil, errors.Errorf("Can't set map entry on a nil RNode")
|
||||
}
|
||||
if err := ErrorIfInvalid(rn, yaml.MappingNode); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if s.Name == "" {
|
||||
s.Name = GetValue(s.Key)
|
||||
}
|
||||
for i := 0; i < len(rn.Content()); i = IncrementFieldIndex(i) {
|
||||
isMatchingField := rn.Content()[i].Value == s.Name
|
||||
if isMatchingField {
|
||||
rn.Content()[i] = s.Key.YNode()
|
||||
rn.Content()[i+1] = s.Value.YNode()
|
||||
return rn, nil
|
||||
}
|
||||
}
|
||||
|
||||
// create the field
|
||||
rn.YNode().Content = append(
|
||||
rn.YNode().Content,
|
||||
s.Key.YNode(),
|
||||
s.Value.YNode())
|
||||
return rn, nil
|
||||
}
|
||||
|
||||
// FieldSetter sets a field or map entry to a value.
|
||||
type FieldSetter struct {
|
||||
Kind string `yaml:"kind,omitempty"`
|
||||
@@ -607,6 +686,12 @@ func (s FieldSetter) Filter(rn *RNode) (*RNode, error) {
|
||||
s.Value = NewScalarRNode(s.StringValue)
|
||||
}
|
||||
|
||||
// need to set style for strings not recognized by yaml 1.1 to quoted if not previously set
|
||||
// TODO: fix in upstream yaml library so this can be handled with yaml SetString
|
||||
if s.Value.IsStringValue() && !s.OverrideStyle && s.Value.YNode().Style == 0 && IsYaml1_1NonString(s.Value.YNode()) {
|
||||
s.Value.YNode().Style = yaml.DoubleQuotedStyle
|
||||
}
|
||||
|
||||
if s.Name == "" {
|
||||
if err := ErrorIfInvalid(rn, yaml.ScalarNode); err != nil {
|
||||
return rn, err
|
||||
@@ -748,6 +833,19 @@ func IsListIndex(p string) bool {
|
||||
return strings.HasPrefix(p, "[") && strings.HasSuffix(p, "]")
|
||||
}
|
||||
|
||||
// IsIdxNumber returns true if p is an index number.
|
||||
// e.g. 1
|
||||
func IsIdxNumber(p string) bool {
|
||||
idx, err := strconv.Atoi(p)
|
||||
return err == nil && idx >= 0
|
||||
}
|
||||
|
||||
// IsWildcard returns true if p is matching every elements.
|
||||
// e.g. "*"
|
||||
func IsWildcard(p string) bool {
|
||||
return p == "*"
|
||||
}
|
||||
|
||||
// SplitIndexNameValue splits a lookup part Val index into the field name
|
||||
// and field value to match.
|
||||
// e.g. splits [name=nginx] into (name, nginx)
|
||||
|
||||
Reference in New Issue
Block a user