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:
hongzhouzi
2022-11-15 14:56:38 +08:00
committed by GitHub
parent 5f91c1663a
commit 44167aa47a
3106 changed files with 321340 additions and 172080 deletions

View File

@@ -12,8 +12,10 @@ import (
"strconv"
"strings"
"gopkg.in/yaml.v3"
"sigs.k8s.io/kustomize/kyaml/errors"
"sigs.k8s.io/kustomize/kyaml/internal/forked/github.com/go-yaml/yaml"
"sigs.k8s.io/kustomize/kyaml/sliceutil"
"sigs.k8s.io/kustomize/kyaml/utils"
"sigs.k8s.io/kustomize/kyaml/yaml/internal/k8sgen/pkg/labels"
)
@@ -146,6 +148,50 @@ func NewMapRNode(values *map[string]string) *RNode {
return m
}
// SyncMapNodesOrder sorts the map node keys in 'to' node to match the order of
// map node keys in 'from' node, additional keys are moved to the end
func SyncMapNodesOrder(from, to *RNode) {
to.Copy()
res := &RNode{value: &yaml.Node{
Kind: to.YNode().Kind,
Style: to.YNode().Style,
Tag: to.YNode().Tag,
Anchor: to.YNode().Anchor,
Alias: to.YNode().Alias,
HeadComment: to.YNode().HeadComment,
LineComment: to.YNode().LineComment,
FootComment: to.YNode().FootComment,
Line: to.YNode().Line,
Column: to.YNode().Column,
}}
fromFieldNames, err := from.Fields()
if err != nil {
return
}
toFieldNames, err := to.Fields()
if err != nil {
return
}
for _, fieldName := range fromFieldNames {
if !sliceutil.Contains(toFieldNames, fieldName) {
continue
}
// append the common nodes in the order defined in 'from' node
res.value.Content = append(res.value.Content, to.Field(fieldName).Key.YNode(), to.Field(fieldName).Value.YNode())
toFieldNames = sliceutil.Remove(toFieldNames, fieldName)
}
for _, fieldName := range toFieldNames {
// append the residual nodes which are not present in 'from' node
res.value.Content = append(res.value.Content, to.Field(fieldName).Key.YNode(), to.Field(fieldName).Value.YNode())
}
to.SetYNode(res.YNode())
}
// NewRNode returns a new RNode pointer containing the provided Node.
func NewRNode(value *yaml.Node) *RNode {
return &RNode{value: value}
@@ -203,6 +249,11 @@ func (rn *RNode) IsNilOrEmpty() bool {
IsYNodeZero(rn.YNode())
}
// IsStringValue is true if the RNode is not nil and is scalar string node
func (rn *RNode) IsStringValue() bool {
return !rn.IsNil() && IsYNodeString(rn.YNode())
}
// GetMeta returns the ResourceMeta for an RNode
func (rn *RNode) GetMeta() (ResourceMeta, error) {
if IsMissingOrNull(rn) {
@@ -337,26 +388,83 @@ func (rn *RNode) SetYNode(node *yaml.Node) {
*rn.value = *node
}
// GetKind returns the kind.
// GetKind returns the kind, if it exists, else empty string.
func (rn *RNode) GetKind() string {
node, err := rn.Pipe(FieldMatcher{Name: KindField})
if err != nil {
return ""
if node := rn.getMapFieldValue(KindField); node != nil {
return node.Value
}
return GetValue(node)
return ""
}
// GetName returns the name.
// SetKind sets the kind.
func (rn *RNode) SetKind(k string) {
rn.SetMapField(NewScalarRNode(k), KindField)
}
// GetApiVersion returns the apiversion, if it exists, else empty string.
func (rn *RNode) GetApiVersion() string {
if node := rn.getMapFieldValue(APIVersionField); node != nil {
return node.Value
}
return ""
}
// SetApiVersion sets the apiVersion.
func (rn *RNode) SetApiVersion(av string) {
rn.SetMapField(NewScalarRNode(av), APIVersionField)
}
// getMapFieldValue returns the value (*yaml.Node) of a mapping field.
// The value might be nil. Also, the function returns nil, not an error,
// if this node is not a mapping node, or if this node does not have the
// given field, so this function cannot be used to make distinctions
// between these cases.
func (rn *RNode) getMapFieldValue(field string) *yaml.Node {
for i := 0; i < len(rn.Content()); i = IncrementFieldIndex(i) {
if rn.Content()[i].Value == field {
return rn.Content()[i+1]
}
}
return nil
}
// GetName returns the name, or empty string if
// field not found. The setter is more restrictive.
func (rn *RNode) GetName() string {
f := rn.Field(MetadataField)
return rn.getMetaStringField(NameField)
}
// getMetaStringField returns the value of a string field in metadata.
func (rn *RNode) getMetaStringField(fName string) string {
md := rn.getMetaData()
if md == nil {
return ""
}
f := md.Field(fName)
if f.IsNilOrEmpty() {
return ""
}
f = f.Value.Field(NameField)
if f.IsNilOrEmpty() {
return ""
return GetValue(f.Value)
}
// getMetaData returns the RNode holding the value of the metadata field.
// Return nil if field not found (no error).
func (rn *RNode) getMetaData() *RNode {
if IsMissingOrNull(rn) {
return nil
}
return f.Value.YNode().Value
var n *RNode
if rn.YNode().Kind == DocumentNode {
// get the content if this is the document node
n = NewRNode(rn.Content()[0])
} else {
n = rn
}
mf := n.Field(MetadataField)
if mf.IsNilOrEmpty() {
return nil
}
return mf.Value
}
// SetName sets the metadata name field.
@@ -364,16 +472,14 @@ func (rn *RNode) SetName(name string) error {
return rn.SetMapField(NewScalarRNode(name), MetadataField, NameField)
}
// GetNamespace gets the metadata namespace field.
func (rn *RNode) GetNamespace() (string, error) {
meta, err := rn.GetMeta()
if err != nil {
return "", err
}
return meta.Namespace, nil
// GetNamespace gets the metadata namespace field, or empty string if
// field not found. The setter is more restrictive.
func (rn *RNode) GetNamespace() string {
return rn.getMetaStringField(NamespaceField)
}
// SetNamespace tries to set the metadata namespace field.
// SetNamespace tries to set the metadata namespace field. If the argument
// is empty, the field is dropped.
func (rn *RNode) SetNamespace(ns string) error {
meta, err := rn.Pipe(Lookup(MetadataField))
if err != nil {
@@ -390,12 +496,14 @@ func (rn *RNode) SetNamespace(ns string) error {
}
// GetAnnotations gets the metadata annotations field.
func (rn *RNode) GetAnnotations() (map[string]string, error) {
meta, err := rn.GetMeta()
if err != nil {
return nil, err
// If the field is missing, returns an empty map.
// Use another method to check for missing metadata.
func (rn *RNode) GetAnnotations() map[string]string {
meta := rn.getMetaData()
if meta == nil {
return make(map[string]string)
}
return meta.Annotations, nil
return rn.getMapFromMeta(meta, AnnotationsField)
}
// SetAnnotations tries to set the metadata annotations field.
@@ -404,12 +512,26 @@ func (rn *RNode) SetAnnotations(m map[string]string) error {
}
// GetLabels gets the metadata labels field.
func (rn *RNode) GetLabels() (map[string]string, error) {
meta, err := rn.GetMeta()
if err != nil {
return nil, err
// If the field is missing, returns an empty map.
// Use another method to check for missing metadata.
func (rn *RNode) GetLabels() map[string]string {
meta := rn.getMetaData()
if meta == nil {
return make(map[string]string)
}
return meta.Labels, nil
return rn.getMapFromMeta(meta, LabelsField)
}
// getMapFromMeta returns map, sometimes empty, from metadata.
func (rn *RNode) getMapFromMeta(meta *RNode, fName string) map[string]string {
result := make(map[string]string)
if f := meta.Field(fName); !f.IsNilOrEmpty() {
_ = f.Value.VisitFields(func(node *MapNode) error {
result[GetValue(node.Key)] = GetValue(node.Value)
return nil
})
}
return result
}
// SetLabels sets the metadata labels field.
@@ -419,7 +541,7 @@ func (rn *RNode) SetLabels(m map[string]string) error {
// This established proper quoting on string values, and sorts by key.
func (rn *RNode) setMapInMetadata(m map[string]string, field string) error {
meta, err := rn.Pipe(Lookup(MetadataField))
meta, err := rn.Pipe(LookupCreate(MappingNode, MetadataField))
if err != nil {
return err
}
@@ -475,6 +597,32 @@ func (rn *RNode) GetBinaryDataMap() map[string]string {
return result
}
// GetValidatedDataMap retrieves the data map and returns an error if the data
// map contains entries which are not included in the expectedKeys set.
func (rn *RNode) GetValidatedDataMap(expectedKeys []string) (map[string]string, error) {
dataMap := rn.GetDataMap()
err := rn.validateDataMap(dataMap, expectedKeys)
return dataMap, err
}
func (rn *RNode) validateDataMap(dataMap map[string]string, expectedKeys []string) error {
if dataMap == nil {
return fmt.Errorf("The datamap is unassigned")
}
for key := range dataMap {
found := false
for _, expected := range expectedKeys {
if expected == key {
found = true
}
}
if !found {
return fmt.Errorf("an unexpected key (%v) was found", key)
}
}
return nil
}
func (rn *RNode) SetDataMap(m map[string]string) {
if rn == nil {
log.Fatal("cannot set data map on nil Rnode")
@@ -761,6 +909,155 @@ func (rn *RNode) UnmarshalJSON(b []byte) error {
return nil
}
// DeAnchor inflates all YAML aliases with their anchor values.
// All YAML anchor data is permanently removed (feel free to call Copy first).
func (rn *RNode) DeAnchor() (err error) {
rn.value, err = deAnchor(rn.value)
return
}
// deAnchor removes all AliasNodes from the yaml.Node's tree, replacing
// them with what they point to. All Anchor fields (these are used to mark
// anchor definitions) are cleared.
func deAnchor(yn *yaml.Node) (res *yaml.Node, err error) {
if yn == nil {
return nil, nil
}
if yn.Anchor != "" {
// This node defines an anchor. Clear the field so that it
// doesn't show up when marshalling.
if yn.Kind == yaml.AliasNode {
// Maybe this is OK, but for now treating it as a bug.
return nil, fmt.Errorf(
"anchor %q defined using alias %v", yn.Anchor, yn.Alias)
}
yn.Anchor = ""
}
switch yn.Kind {
case yaml.ScalarNode:
return yn, nil
case yaml.AliasNode:
return deAnchor(yn.Alias)
case yaml.MappingNode:
toMerge, err := removeMergeTags(yn)
if err != nil {
return nil, err
}
err = mergeAll(yn, toMerge)
if err != nil {
return nil, err
}
fallthrough
case yaml.DocumentNode, yaml.SequenceNode:
for i := range yn.Content {
yn.Content[i], err = deAnchor(yn.Content[i])
if err != nil {
return nil, err
}
}
return yn, nil
default:
return nil, fmt.Errorf("cannot deAnchor kind %q", yn.Kind)
}
}
// isMerge returns if the node is tagged with !!merge
func isMerge(yn *yaml.Node) bool {
return yn.Tag == MergeTag
}
// findMergeValues receives either a MappingNode, a AliasNode or a potentially
// mixed list of MappingNodes and AliasNodes. It returns a list of MappingNodes.
func findMergeValues(yn *yaml.Node) ([]*yaml.Node, error) {
if yn == nil {
return []*yaml.Node{}, nil
}
switch yn.Kind {
case MappingNode:
return []*yaml.Node{yn}, nil
case AliasNode:
if yn.Alias != nil && yn.Alias.Kind != MappingNode {
return nil, errors.Errorf("invalid map merge: received alias for a non-map value")
}
return []*yaml.Node{yn.Alias}, nil
case SequenceNode:
mergeValues := []*yaml.Node{}
for i := 0; i < len(yn.Content); i++ {
if yn.Content[i].Kind == SequenceNode {
return nil, errors.Errorf("invalid map merge: received a nested sequence")
}
newMergeValues, err := findMergeValues(yn.Content[i])
if err != nil {
return nil, err
}
mergeValues = append(newMergeValues, mergeValues...)
}
return mergeValues, nil
default:
return nil, errors.Errorf("map merge requires map or sequence of maps as the value")
}
}
// getMergeTagValue receives a MappingNode yaml node, and it searches for
// merge tagged keys and return its value yaml node. If the key is duplicated,
// it fails.
func getMergeTagValue(yn *yaml.Node) (*yaml.Node, error) {
var result *yaml.Node
for i := 0; i < len(yn.Content); i += 2 {
key := yn.Content[i]
value := yn.Content[i+1]
if isMerge(key) {
if result != nil {
return nil, fmt.Errorf("duplicate merge key")
}
result = value
}
}
return result, nil
}
// removeMergeTags removes all merge tags and returns a ordered list of yaml
// nodes to merge and a error
func removeMergeTags(yn *yaml.Node) ([]*yaml.Node, error) {
if yn == nil || yn.Content == nil {
return nil, nil
}
if yn.Kind != yaml.MappingNode {
return nil, nil
}
value, err := getMergeTagValue(yn)
if err != nil {
return nil, err
}
toMerge, err := findMergeValues(value)
if err != nil {
return nil, err
}
err = NewRNode(yn).PipeE(Clear("<<"))
if err != nil {
return nil, err
}
return toMerge, nil
}
func mergeAll(yn *yaml.Node, toMerge []*yaml.Node) error {
// We only need to start with a copy of the existing node because we need to
// maintain duplicated keys and style
rn := NewRNode(yn).Copy()
toMerge = append(toMerge, yn)
for i := range toMerge {
rnToMerge := NewRNode(toMerge[i]).Copy()
err := rnToMerge.VisitFields(func(node *MapNode) error {
return rn.PipeE(MapEntrySetter{Key: node.Key, Value: node.Value})
})
if err != nil {
return err
}
}
*yn = *rn.value
return nil
}
// GetValidatedMetadata returns metadata after subjecting it to some tests.
func (rn *RNode) GetValidatedMetadata() (ResourceMeta, error) {
m, err := rn.GetMeta()
@@ -786,11 +1083,7 @@ func (rn *RNode) MatchesAnnotationSelector(selector string) (bool, error) {
if err != nil {
return false, err
}
slice, err := rn.GetAnnotations()
if err != nil {
return false, err
}
return s.Matches(labels.Set(slice)), nil
return s.Matches(labels.Set(rn.GetAnnotations())), nil
}
// MatchesLabelSelector returns true on a selector match to labels.
@@ -799,11 +1092,7 @@ func (rn *RNode) MatchesLabelSelector(selector string) (bool, error) {
if err != nil {
return false, err
}
slice, err := rn.GetLabels()
if err != nil {
return false, err
}
return s.Matches(labels.Set(slice)), nil
return s.Matches(labels.Set(rn.GetLabels())), nil
}
// HasNilEntryInList returns true if the RNode contains a list which has
@@ -887,7 +1176,6 @@ func checkKey(key string, elems []*Node) bool {
return count == len(elems)
}
// Deprecated: use pipes instead.
// GetSlice returns the contents of the slice field at the given path.
func (rn *RNode) GetSlice(path string) ([]interface{}, error) {
value, err := rn.GetFieldValue(path)
@@ -900,7 +1188,6 @@ func (rn *RNode) GetSlice(path string) ([]interface{}, error) {
return nil, fmt.Errorf("node %s is not a slice", path)
}
// Deprecated: use pipes instead.
// GetString returns the contents of the string field at the given path.
func (rn *RNode) GetString(path string) (string, error) {
value, err := rn.GetFieldValue(path)
@@ -913,7 +1200,6 @@ func (rn *RNode) GetString(path string) (string, error) {
return "", fmt.Errorf("node %s is not a string: %v", path, value)
}
// Deprecated: use slash paths instead.
// GetFieldValue finds period delimited fields.
// TODO: When doing kustomize var replacement, which is likely a
// a primary use of this function and the reason it returns interface{}
@@ -922,7 +1208,7 @@ func (rn *RNode) GetString(path string) (string, error) {
// assuming we keep the var feature in kustomize.
// The other end of this is: refvar.go:updateNodeValue.
func (rn *RNode) GetFieldValue(path string) (interface{}, error) {
fields := convertSliceIndex(strings.Split(path, "."))
fields := convertSliceIndex(utils.SmarterPathSplitter(path, "."))
rn, err := rn.Pipe(Lookup(fields...))
if err != nil {
return nil, err